Alle Dateien durch Perltidy laufen lassen. Die verwendeten Optionen sind am Ende...
[kivitendo-erp.git] / bin / mozilla / ap.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Accounts Payables
31 #
32 #======================================================================
33
34 use SL::AP;
35 use SL::IR;
36 use SL::PE;
37
38 require "$form->{path}/arap.pl";
39
40 1;
41
42 # end of main
43
44 # this is for our long dates
45 # $locale->text('January')
46 # $locale->text('February')
47 # $locale->text('March')
48 # $locale->text('April')
49 # $locale->text('May ')
50 # $locale->text('June')
51 # $locale->text('July')
52 # $locale->text('August')
53 # $locale->text('September')
54 # $locale->text('October')
55 # $locale->text('November')
56 # $locale->text('December')
57
58 # this is for our short month
59 # $locale->text('Jan')
60 # $locale->text('Feb')
61 # $locale->text('Mar')
62 # $locale->text('Apr')
63 # $locale->text('May')
64 # $locale->text('Jun')
65 # $locale->text('Jul')
66 # $locale->text('Aug')
67 # $locale->text('Sep')
68 # $locale->text('Oct')
69 # $locale->text('Nov')
70 # $locale->text('Dec')
71
72 sub add {
73   $lxdebug->enter_sub();
74
75   $form->{title} = "Add";
76
77   $form->{callback} =
78     "$form->{script}?action=add&path=$form->{path}&login=$form->{login}&password=$form->{password}"
79     unless $form->{callback};
80
81   &create_links;
82   &display_form;
83
84   $lxdebug->leave_sub();
85 }
86
87 sub edit {
88   $lxdebug->enter_sub();
89
90   $form->{title} = "Edit";
91
92   &create_links;
93   &display_form;
94
95   $lxdebug->leave_sub();
96 }
97
98 sub display_form {
99   $lxdebug->enter_sub();
100
101   &form_header;
102   &form_footer;
103
104   $lxdebug->leave_sub();
105 }
106
107 sub create_links {
108   $lxdebug->enter_sub();
109
110   $form->create_links("AP", \%myconfig, "vendor");
111   $taxincluded = $form->{taxincluded};
112   $duedate     = $form->{duedate};
113
114   IR->get_vendor(\%myconfig, \%$form);
115
116   $form->{duedate}   = $duedate if $duedate;
117   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
118
119   # build the popup menus
120   $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
121
122   map {
123     $tax .=
124       qq|<option value=\"$_->{taxkey}--$_->{rate}\">$_->{taxdescription}  |
125       . ($_->{rate} * 100) . qq| %|
126   } @{ $form->{TAX} };
127   $form->{taxchart}       = $tax;
128   $form->{selecttaxchart} = $tax;
129
130   # currencies
131   @curr = split /:/, $form->{currencies};
132   chomp $curr[0];
133   $form->{defaultcurrency} = $curr[0];
134
135   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
136
137   # vendors
138   if (@{ $form->{all_vendor} }) {
139     $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
140     map { $form->{selectvendor} .= "<option>$_->{name}--$_->{id}\n" }
141       (@{ $form->{all_vendor} });
142   }
143
144   # departments
145   if (@{ $form->{all_departments} }) {
146     $form->{selectdepartment} = "<option>\n";
147     $form->{department}       = "$form->{department}--$form->{department_id}";
148
149     map {
150       $form->{selectdepartment} .=
151         "<option>$_->{description}--$_->{id}\n"
152     } (@{ $form->{all_departments} });
153   }
154
155   $form->{employee} = "$form->{employee}--$form->{employee_id}";
156
157   # forex
158   $form->{forex} = $form->{exchangerate};
159   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
160
161   foreach $key (keys %{ $form->{AP_links} }) {
162     foreach $ref (@{ $form->{AP_links}{$key} }) {
163       if ($key eq "AP_paid") {
164         $form->{"select$key"} .=
165           "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
166       } else {
167         $form->{"select$key"} .=
168           "<option value=\"$ref->{accno}--$ref->{taxkey}\">$ref->{accno}--$ref->{description}</option>\n";
169       }
170     }
171
172     $form->{$key} = $form->{"select$key"};
173
174     # if there is a value we have an old entry
175     $j = 0;
176     for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
177
178       if ($key eq "AP_paid") {
179         $j++;
180         $form->{"AP_paid_$j"} =
181           "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
182         $form->{"paid_$j"}     = $form->{acc_trans}{$key}->[$i - 1]->{amount};
183         $form->{"datepaid_$j"} =
184           $form->{acc_trans}{$key}->[$i - 1]->{transdate};
185         $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
186         $form->{"memo_$j"}   = $form->{acc_trans}{$key}->[$i - 1]->{memo};
187
188         $form->{"forex_$j"} = $form->{"exchangerate_$i"} =
189           $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
190         $form->{"AP_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}";
191         $form->{paidaccounts}++;
192       } else {
193
194         $akey = $key;
195         $akey =~ s/AP_//;
196
197         if ($key eq "AP_tax") {
198           $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} =
199             "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
200           $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} =
201             $form->round_amount(
202              $form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate * -1,
203              2);
204           if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
205             $totaltax +=
206               $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
207             $taxrate +=
208               $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
209           } else {
210             $totalwithholding +=
211               $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
212             $withholdingrate +=
213               $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
214           }
215           $formtax =
216             $form->round_amount(
217                   $form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate,
218                   2) * -1;
219         } else {
220           $form->{"${akey}_$i"} =
221             $form->round_amount(
222                   $form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate,
223                   2);
224           if ($akey eq 'amount') {
225             $form->{"${akey}_$i"} *= -1;
226             $totalamount += $form->{"${akey}_$i"};
227             $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
228             $form->{rowcount}++;
229             $form->{"oldprojectnumber_$i"} = $form->{"projectnumber_$i"} =
230               "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
231             $form->{"project_id_$i"} =
232               "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
233           }
234           $form->{"${key}_$i"} =
235             "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
236           $form->{"select${key}"} =~
237             /(<option value=\"$form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*\">$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}<\/option>\n)/;
238           $test = $1;
239           $form->{"select${key}"} = $1;
240           if ($akey eq 'amount') {
241             $form->{selecttaxchart} =~
242               /(<option value=\"$form->{acc_trans}{$key}->[$i-1]->{taxkey}--[^<]*)/;
243             $form->{selecttaxchart} = $1;
244           }
245         }
246       }
247     }
248   }
249
250   $form->{taxincluded}  = $taxincluded if ($form->{id});
251   $form->{paidaccounts} = 1            if not defined $form->{paidaccounts};
252
253   if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
254
255     # add tax to amounts and invtotal
256     for $i (1 .. 1) {
257       $taxamount =
258         ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
259       $tax = $form->round_amount($taxamount, 2);
260       $diff                += ($taxamount - $tax);
261       $form->{"amount_$i"} += $tax;
262     }
263     $form->{amount_1} += $form->round_amount($diff, 2);
264   }
265
266   # check if calculated is equal to stored
267   if ($form->{taxincluded} && $form->{taxrate} && $witholdingrate) {
268     if ($form->{"taxrate"} > 0) {
269       $taxamount =
270         $form->round_amount(
271             ($form->{amount_1} - ($form->{amount_1} / ($form->{taxrate} + 1))),
272             2);
273     } else {
274       $taxamount =
275         $form->round_amount(
276             ($totalamount + $totaltax + $totalwithholding) * $withholdingrate /
277               (1 - $withholdingrate),
278             2
279         ) * $form->{"taxrate"} / $withholdingrate;
280     }
281   } else {
282     $taxamount = $totalamount * $form->{"taxrate"};
283   }
284   $taxamount = $form->round_amount($taxamount, 2);
285   $form->{tax} = $taxamount;
286
287   $form->{tax} = $formtax;
288
289   $form->{invtotal} = $totalamount + $totaltax + $totalwithholding;
290   $form->{rowcount}++ if $form->{id};
291
292   $form->{locked} =
293     ($form->datetonum($form->{transdate}, \%myconfig) <=
294      $form->datetonum($form->{closedto}, \%myconfig));
295
296   $lxdebug->leave_sub();
297 }
298
299 sub form_header {
300   $lxdebug->enter_sub();
301
302   $title = $form->{title};
303   $form->{title} = $locale->text("$title Accounts Payables Transaction");
304
305   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
306
307   # type=submit $locale->text('Add Accounts Payables Transaction')
308   # type=submit $locale->text('Edit Accounts Payables Transaction')
309
310   # set option selected
311   foreach $item (qw(vendor currency department)) {
312     $form->{"select$item"} =~ s/ selected//;
313     $form->{"select$item"} =~
314       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
315   }
316   $readonly = ($form->{id}) ? "readonly" : "";
317
318   $form->{radier} =
319     ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
320   $readonly = ($form->{radier}) ? "" : $readonly;
321
322   map { $form->{$_} =~ s/\"/&quot;/g } qw(AP_amount AP taxchart);
323
324   # format amounts
325   $form->{exchangerate} =
326     $form->format_amount(\%myconfig, $form->{exchangerate});
327
328   $form->{creditlimit} =
329     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
330   $form->{creditremaining} =
331     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
332
333   $exchangerate = qq|
334 <input type=hidden name=forex value=$form->{forex}>
335 |;
336   if ($form->{currency} ne $form->{defaultcurrency}) {
337     if ($form->{forex}) {
338       $exchangerate .= qq|
339             <tr>
340               <th align=right>| . $locale->text('Exchangerate') . qq|</th>
341               <td><input type=hidden name=exchangerate value=$form->{exchangerate}>$form->{exchangerate}</td>
342            </tr>
343 |;
344     } else {
345       $exchangerate .= qq|
346              <th align=right>| . $locale->text('Exchangerate') . qq|</th>
347              <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
348 |;
349     }
350   }
351
352   $taxincluded = "";
353
354   $taxincluded = qq|
355             <tr>
356               <td align=right><input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td>
357               <th align=left nowrap>|
358     . $locale->text('Tax Included')
359     . qq|</th>
360             </tr>
361 |;
362
363   if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
364     $rows = 2;
365   }
366   $notes =
367     qq|<textarea name=notes rows=$rows cols=50 wrap=soft $readonly>$form->{notes}</textarea>|;
368
369   $department = qq|
370               <tr>
371                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
372                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
373                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
374                 </td>
375               </tr>
376 | if $form->{selectdepartment};
377
378   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
379
380   $vendor =
381     ($form->{selectvendor})
382     ? qq|<select name=vendor>$form->{selectvendor}</select>|
383     : qq|<input name=vendor value="$form->{vendor}" size=35>|;
384
385   # use JavaScript Calendar or not
386   $form->{jsscript} = $jscalendar;
387   $jsscript = "";
388   if ($form->{jsscript}) {
389
390     # with JavaScript Calendar
391     $button1 = qq|
392        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}> $readonly</td>
393        <td><input type=button name=transdate id="trigger1" value=|
394       . $locale->text('button')
395       . qq|></td>  
396        |;
397     $button2 = qq|
398        <td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}> $readonly</td>
399        <td><input type=button name=duedate id="trigger2" value=|
400       . $locale->text('button')
401       . qq|></td></td>
402      |;
403
404     #write Trigger
405     $jsscript =
406       Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
407                           "duedate", "BL", "trigger2");
408   } else {
409
410     # without JavaScript Calendar
411     $button1 =
412       qq|<td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}> $readonly</td>|;
413     $button2 =
414       qq|<td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}> $readonly</td>|;
415   }
416
417   $form->header;
418
419   print qq|
420 <body>
421
422 <form method=post action=$form->{script}>
423
424 <input type=hidden name=id value=$form->{id}>
425 <input type=hidden name=sort value=$form->{sort}>
426 <input type=hidden name=closedto value=$form->{closedto}>
427 <input type=hidden name=locked value=$form->{locked}>
428 <input type=hidden name=title value="$title">
429
430 <table width=100%>
431   <tr class=listtop>
432     <th class=listtop>$form->{title}</th>
433   </tr>
434   <tr height="5"></tr>
435   <tr valign=top>
436     <td>
437       <table width=100%>
438         <tr valign=top>
439           <td>
440             <table>
441               <tr>
442                 <th align=right nowrap>| . $locale->text('Vendor') . qq|</th>
443                 <td colspan=3>$vendor</td>
444                 <input type=hidden name=selectvendor value="$form->{selectvendor}">
445                 <input type=hidden name=oldvendor value="$form->{oldvendor}">
446                 <input type=hidden name=vendor_id value="$form->{vendor_id}">
447                 <input type=hidden name=terms value=$form->{terms}>
448               </tr>
449               <tr>
450                 <td></td>
451                 <td colspan=3>
452                   <table width=100%>
453                     <tr>
454                       <th align=left nowrap>| . $locale->text('Credit Limit') . qq|</th>
455                       <td>$form->{creditlimit}</td>
456                       <th align=left nowrap>| . $locale->text('Remaining') . qq|</th>
457                       <td class="plus$n">$form->{creditremaining}</td>
458                       <input type=hidden name=creditlimit value=$form->{creditlimit}>
459                       <input type=hidden name=creditremaining value=$form->{creditremaining}>
460                     </tr>
461                   </table>
462                 </td>
463               <tr>
464                 <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
465                 <td><select name=currency>$form->{selectcurrency}</select></td>
466                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
467                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
468                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
469                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
470                 $exchangerate
471               </tr>
472               $department
473               $taxincluded
474             </table>
475           </td>
476           <td align=right>
477             <table>
478               <tr>
479                 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
480                 <td><input name=invnumber size=11 value="$form->{invnumber}" $readonly></td>
481               </tr>
482               <tr>
483                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
484                 <td><input name=ordnumber size=11 value="$form->{ordnumber}" $readonly></td>
485               </tr>
486               <tr>
487                 <th align=right nowrap>| . $locale->text('Invoice Date') . qq|</th>
488                 $button1
489               </tr>
490               <tr>
491                 <th align=right nowrap>| . $locale->text('Due Date') . qq|</th>
492                 $button2
493               </tr>
494             </table>
495           </td>
496         </tr>
497       </table>
498     </td>
499   </tr>
500   <input type=hidden name=AP_amount value="$form->{AP_amount}">
501   <input type=hidden name=taxchart value="$form->{taxchart}">
502   <input type=hidden name=rowcount value=$form->{rowcount}>
503   <tr>
504     <td>
505       <table width=100%>
506
507 $jsscript  
508 |;
509
510   $amount  = $locale->text('Amount');
511   $project = $locale->text('Project');
512
513   for $i (1 .. 1) {
514
515     #     $form->{"selectAP_amount"} =~ s/ selected//;
516     #     $form->{"selectAP_amount"} =~ s/option>\Q$form->{"AP_amount_$i"}\E/option selected>$form->{"AP_amount_$i"}/;
517
518     # format amounts
519     $form->{"amount_$i"} =
520       $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
521
522     print qq|
523         <tr>
524           <th align=right nowrap>$amount</th>
525           <td><input name="amount_$i" size=10 value=$form->{"amount_$i"} $readonly></td>
526           <th>$project</th>
527           <td><input name="projectnumber_$i" size=20 value="$form->{"projectnumber_$i"}">
528               <input type=hidden name="project_id_$i" value=$form->{"project_id_$i"}>
529               <input type=hidden name="oldprojectnumber_$i" value="$form->{"oldprojectnumber_$i"}"></td>
530           <td width=50%><select  name="AP_amountselected">$form->{selectAP_amount}</select></td>
531         </tr>
532 |;
533     $amount  = "";
534     $project = "";
535   }
536
537   $taxlabel =
538     ($form->{taxincluded})
539     ? $locale->text('Tax Included')
540     : $locale->text('Tax');
541
542   # format and reverse tax
543   $form->{"tax"} = $form->format_amount(\%myconfig, $form->{"tax"}, 2);
544
545   print qq|
546         <tr>
547           <th align=right nowrap>${taxlabel}</th>
548           <td><input name="tax" size=10 value=$form->{"tax"} $readonly></td>
549           <td align=right><input name=taxcheck class=checkbox type=checkbox value=1></td>
550           <td align=left nowrap>| . $locale->text('Correct Tax') . qq|</td>
551           <td><select name=taxchartselected>$form->{"selecttaxchart"}</select></td>
552         </tr>
553 |;
554
555   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
556
557   print qq|
558         <tr>
559           <th align=right nowrap>| . $locale->text('Total') . qq|</th>
560           <td>$form->{invtotal}</td>
561
562           <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
563           <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
564           
565           <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
566           
567           <td colspan=2></td>
568           <td><select name=APselected>$form->{selectAP}</select></td>
569           <input type=hidden name=AP value="$form->{AP}">
570           
571         </tr>
572         <tr>
573           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
574           <td colspan=5>$notes</td>
575         </tr>
576       </table>
577     </td>
578   </tr>
579   <tr>
580     <td>
581       <table width=100%>
582         <tr class=listheading>
583           <th class=listheading colspan=6>| . $locale->text('Payments') . qq|</th>
584         </tr>
585 |;
586
587   if ($form->{currency} eq $form->{defaultcurrency}) {
588     @column_index = qw(datepaid source memo paid AP_paid);
589   } else {
590     @column_index = qw(datepaid source memo paid exchangerate AP_paid);
591   }
592
593   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
594   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
595   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
596   $column_data{AP_paid}      = "<th>" . $locale->text('Account') . "</th>";
597   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
598   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
599
600   print "
601         <tr>
602 ";
603   map { print "$column_data{$_}\n" } @column_index;
604   print "
605         </tr>
606 ";
607
608   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
609   for $i (1 .. $form->{paidaccounts}) {
610     print "
611         <tr>
612 ";
613
614     $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
615     $form->{"selectAP_paid_$i"} =~
616       s/option value=\"$form->{"AP_paid_$i"}\">/option value=\"$form->{"AP_paid_$i"}\" selected>/;
617
618     # format amounts
619     $form->{"paid_$i"} =
620       $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
621     $form->{"exchangerate_$i"} =
622       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
623
624     $exchangerate = qq|&nbsp;|;
625     if ($form->{currency} ne $form->{defaultcurrency}) {
626       if ($form->{"forex_$i"}) {
627         $exchangerate =
628           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
629       } else {
630         $exchangerate =
631           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
632       }
633     }
634
635     $exchangerate .= qq|
636 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
637 |;
638
639     $column_data{"paid_$i"} =
640       qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
641     $column_data{"AP_paid_$i"} =
642       qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
643     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
644     $column_data{"datepaid_$i"}     =
645       qq|<td align=center><input name="datepaid_$i" size=11 title="($myconfig{'dateformat'})" value=$form->{"datepaid_$i"}></td>|;
646     $column_data{"source_$i"} =
647       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
648     $column_data{"memo_$i"} =
649       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
650
651     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
652
653     print "
654         </tr>
655 ";
656   }
657   map { $form->{$_} =~ s/\"/&quot;/g } qw(selectAP_paid);
658   print qq|
659     <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
660     <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
661     
662       </table>
663     </td>
664   </tr>
665   <tr>
666     <td><hr size=3 noshade></td>
667   </tr>
668 </table>
669 |;
670
671   $lxdebug->leave_sub();
672 }
673
674 sub form_footer {
675   $lxdebug->enter_sub();
676
677   print qq|
678
679 <input name=callback type=hidden value="$form->{callback}">
680
681 <input type=hidden name=path value=$form->{path}>
682 <input type=hidden name=login value=$form->{login}>
683 <input type=hidden name=password value=$form->{password}>
684
685 <br>
686 |;
687
688   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
689   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
690
691   if ($form->{id} && $form->{radier}) {
692
693     #     print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
694     # |;
695
696     if (!$form->{revtrans}) {
697       if (!$form->{locked}) {
698         print qq|
699         <input class=submit type=submit name=action value="|
700           . $locale->text('Post') . qq|">
701         <input class=submit type=submit name=action value="|
702           . $locale->text('Delete') . qq|">
703 |;
704       }
705     }
706
707     if ($transdate > $closedto) {
708       print qq|
709 <input class=submit type=submit name=action value="|
710         . $locale->text('Post as new') . qq|">
711 |;
712     }
713
714   } else {
715     if (($transdate > $closedto) && !$form->{id}) {
716       print qq|<input class=submit type=submit name=action value="|
717         . $locale->text('Update') . qq|">
718       <input class=submit type=submit name=action value="|
719         . $locale->text('Post') . qq|">|;
720     }
721   }
722
723   if ($form->{menubar}) {
724     require "$form->{path}/menu.pl";
725     &menubar;
726   }
727
728   print "
729 </form>
730
731 </body>
732 </html>
733 ";
734
735   $lxdebug->leave_sub();
736 }
737
738 sub update {
739   $lxdebug->enter_sub();
740
741   my $display = shift;
742
743   #   if ($display) {
744   #     goto TAXCALC;
745   #   }
746
747   $form->{invtotal} = 0;
748
749   $form->{selectAP_amount} = $form->{AP_amount};
750   $form->{selectAP_amount} =~
751     s/value=\"$form->{AP_amountselected}\"/value=\"$form->{AP_amountselected}\" selected/;
752
753   $form->{selectAP} = $form->{AP};
754   $form->{selectAP} =~
755     s/value=\"$form->{APselected}\"/value=\"$form->{APselected}\" selected/;
756
757   ($AP_amountaccno, $AP_amounttaxkey) =
758     split(/--/, $form->{AP_amountselected});
759   $form->{selecttaxchart} = $form->{taxchart};
760   $form->{selecttaxchart} =~
761     s/value=\"$AP_amounttaxkey--([^\"]*)\"/value=\"$AP_amounttaxkey--$1\" selected/;
762
763   $form->{rate} = $1;
764
765   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
766     qw(exchangerate creditlimit creditremaining);
767
768   @flds  = qw(amount AP_amount projectnumber oldprojectnumber project_id);
769   $count = 0;
770   for $i (1 .. 1) {
771     $form->{"amount_$i"} =
772       $form->parse_amount(\%myconfig, $form->{"amount_$i"});
773     if ($form->{"amount_$i"}) {
774       push @a, {};
775       my $j = $#a;
776       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
777       $count++;
778     }
779   }
780   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
781
782   $form->{invtotal} += $form->{"amount_1"};
783
784   $form->{exchangerate} = $exchangerate
785     if (
786         $form->{forex} = (
787                     $exchangerate =
788                       $form->check_exchangerate(
789                       \%myconfig, $form->{currency}, $form->{transdate}, 'sell'
790                       )));
791
792   $form->{invdate} = $form->{transdate};
793   $save_AP = $form->{AP};
794   &check_name(vendor);
795   $form->{AP} = $save_AP;
796
797   &check_project;
798
799 TAXCALC:
800
801   # recalculate taxes
802
803   @taxaccounts = split / /, $form->{taxaccounts};
804
805   $form->{"tax"} = $form->parse_amount(\%myconfig, $form->{"tax"});
806
807   if ($form->{taxincluded} && $form->{"rate"}) {
808     $taxrate         = $form->{"rate"};
809     $withholdingrate = 0;
810
811     $amount =
812       $form->round_amount(
813                     ($form->{invtotal} - ($form->{invtotal} / ($taxrate + 1))),
814                     2);
815     $form->{"tax"} = $form->round_amount($amount, 2);
816     $taxdiff += ($amount - $form->{"tax"});
817
818     if (abs $taxdiff >= 0.005) {
819       $form->{"tax"} += $form->round_amount($taxdiff, 2);
820       $taxdiff = 0;
821     }
822     $totaltax += $form->{"tax"};
823
824   } else {
825     $form->{"tax"} =
826       $form->round_amount($form->{invtotal} * $form->{"rate"}, 2);
827     $totaltax += $form->{"tax"};
828
829   }
830
831   $form->{invtotal} =
832     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
833
834   for $i (1 .. $form->{paidaccounts}) {
835     if ($form->{"paid_$i"}) {
836       map {
837         $form->{"${_}_$i"} =
838           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
839       } qw(paid exchangerate);
840
841       $totalpaid += $form->{"paid_$i"};
842
843       $form->{"exchangerate_$i"} = $exchangerate
844         if (
845             $form->{"forex_$i"} = (
846                 $exchangerate =
847                   $form->check_exchangerate(
848                   \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'
849                   )));
850     }
851   }
852
853   $form->{creditremaining} -=
854     ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
855      $form->{oldinvtotal});
856   $form->{oldinvtotal}  = $form->{invtotal};
857   $form->{oldtotalpaid} = $totalpaid;
858
859   &display_form;
860
861   $lxdebug->leave_sub();
862 }
863
864 sub post {
865   $lxdebug->enter_sub();
866
867   # check if there is a vendor, invoice and due date
868   $form->isblank("transdate", $locale->text("Invoice Date missing!"));
869   $form->isblank("duedate",   $locale->text("Due Date missing!"));
870   $form->isblank("vendor",    $locale->text('Vendor missing!'));
871
872   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
873   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
874
875   $form->error($locale->text('Cannot post transaction for a closed period!'))
876     if ($transdate <= $closedto);
877
878   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
879     if ($form->{currency} ne $form->{defaultcurrency});
880   delete($form->{AP});
881
882   for $i (1 .. $form->{paidaccounts}) {
883     if ($form->{"paid_$i"}) {
884       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
885
886       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
887
888       $form->error($locale->text('Cannot post payment for a closed period!'))
889         if ($datepaid <= $closedto);
890
891       if ($form->{currency} ne $form->{defaultcurrency}) {
892         $form->{"exchangerate_$i"} = $form->{exchangerate}
893           if ($transdate == $datepaid);
894         $form->isblank("exchangerate_$i",
895                        $locale->text('Exchangerate for payment missing!'));
896       }
897
898     }
899   }
900
901   # if old vendor ne vendor redo form
902   ($vendor) = split /--/, $form->{vendor};
903   if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
904     &update;
905     exit;
906   }
907   ($debitaccno,    $debittaxkey)    = split /--/, $form->{AP_amountselected};
908   ($taxkey,        $NULL)           = split /--/, $form->{taxchartselected};
909   ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
910   $form->{AP}{amount_1} = $debitaccno;
911   $form->{AP}{payables} = $payablesaccno;
912   $form->{taxkey}       = $taxkey;
913
914   $form->{id} = 0 if $form->{postasnew};
915
916   $form->redirect($locale->text('Transaction posted!'))
917     if (AP->post_transaction(\%myconfig, \%$form));
918   $form->error($locale->text('Cannot post transaction!'));
919
920   $lxdebug->leave_sub();
921 }
922
923 sub post_as_new {
924   $lxdebug->enter_sub();
925
926   $form->{postasnew} = 1;
927   &post;
928
929   $lxdebug->leave_sub();
930 }
931
932 sub delete {
933   $lxdebug->enter_sub();
934
935   $form->{title} = $locale->text('Confirm!');
936
937   $form->header;
938
939   delete $form->{header};
940
941   print qq|
942 <body>
943
944 <form method=post action=$form->{script}>
945 |;
946
947   foreach $key (keys %$form) {
948     $form->{$key} =~ s/\"/&quot;/g;
949     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
950   }
951
952   print qq|
953 <h2 class=confirm>$form->{title}</h2>
954
955 <h4>|
956     . $locale->text('Are you sure you want to delete Transaction')
957     . qq| $form->{invnumber}</h4>
958
959 <input name=action class=submit type=submit value="|
960     . $locale->text('Yes') . qq|">
961 </form>
962
963 </body>
964 </html>
965 |;
966
967   $lxdebug->leave_sub();
968 }
969
970 sub yes {
971   $lxdebug->enter_sub();
972
973   $form->redirect($locale->text('Transaction deleted!'))
974     if (AP->delete_transaction(\%myconfig, \%$form, $spool));
975   $form->error($locale->text('Cannot delete transaction!'));
976
977   $lxdebug->leave_sub();
978 }
979
980 sub search {
981   $lxdebug->enter_sub();
982
983   # setup vendor selection
984   $form->all_vc(\%myconfig, "vendor", "AP");
985
986   if (@{ $form->{all_vendor} }) {
987     map { $vendor .= "<option>$_->{name}--$_->{id}\n" }
988       @{ $form->{all_vendor} };
989     $vendor = qq|<select name=vendor><option>\n$vendor\n</select>|;
990   } else {
991     $vendor = qq|<input name=vendor size=35>|;
992   }
993
994   # departments
995   if (@{ $form->{all_departments} }) {
996     $form->{selectdepartment} = "<option>\n";
997
998     map {
999       $form->{selectdepartment} .=
1000         "<option>$_->{description}--$_->{id}\n"
1001     } (@{ $form->{all_departments} });
1002   }
1003
1004   $department = qq|
1005         <tr>
1006           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1007           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1008         </tr>
1009 | if $form->{selectdepartment};
1010
1011   $form->{title} = $locale->text('AP Transactions');
1012
1013   # use JavaScript Calendar or not
1014   $form->{jsscript} = $jscalendar;
1015   $jsscript = "";
1016   if ($form->{jsscript}) {
1017
1018     # with JavaScript Calendar
1019     $button1 = qq|
1020        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
1021        <input type=button name=transdatefrom id="trigger1" value=|
1022       . $locale->text('button')
1023       . qq|></td>
1024       |;
1025     $button2 = qq|
1026        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
1027        <input type=button name=transdateto name=transdateto id="trigger2" value=|
1028       . $locale->text('button')
1029       . qq|></td>
1030      |;
1031
1032     #write Trigger
1033     $jsscript =
1034       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
1035                           "transdateto", "BL", "trigger2");
1036   } else {
1037
1038     # without JavaScript Calendar
1039     $button1 = qq|
1040                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
1041     $button2 = qq|
1042                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
1043   }
1044
1045   $form->header;
1046
1047   print qq|
1048 <body>
1049
1050 <form method=post action=$form->{script}>
1051
1052 <table width=100%>
1053   <tr>
1054     <th class=listtop>$form->{title}</th>
1055   </tr>
1056   <tr height="5"></tr>
1057   <tr>
1058     <td>
1059       <table>
1060         <tr>
1061           <th align=right>| . $locale->text('Vendor') . qq|</th>
1062           <td colspan=3>$vendor</td>
1063         </tr>
1064         $department
1065         <tr>
1066           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
1067           <td colspan=3><input name=invnumber size=20></td>
1068         </tr>
1069         <tr>
1070           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
1071           <td colspan=3><input name=ordnumber size=20></td>
1072         </tr>
1073         <tr>
1074           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
1075           <td colspan=3><input name=notes size=40></td>
1076         </tr>
1077         <tr>
1078           <th align=right nowrap>| . $locale->text('From') . qq|</th>
1079           $button1
1080           <th align=right>| . $locale->text('Bis') . qq|</th>
1081           $button2
1082         </tr>
1083         <input type=hidden name=sort value=transdate>
1084       </table>
1085     </td>
1086   </tr>
1087   <tr>
1088     <td>
1089       <table>
1090         <tr>
1091           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
1092           <td>
1093             <table width=100%>
1094               <tr>
1095                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1096                 <td nowrap>| . $locale->text('Open') . qq|</td>
1097                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1098                 <td nowrap>| . $locale->text('Closed') . qq|</td>
1099               </tr>
1100               <tr>
1101                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1102                 <td nowrap>| . $locale->text('ID') . qq|</td>
1103                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1104                 <td nowrap>| . $locale->text('Invoice Number') . qq|</td>
1105                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1106                 <td nowrap>| . $locale->text('Order Number') . qq|</td>
1107               </tr>
1108               <tr>
1109                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1110                 <td nowrap>| . $locale->text('Vendor') . qq|</td>
1111                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1112                 <td nowrap>| . $locale->text('Invoice Date') . qq|</td>
1113                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1114                 <td nowrap>| . $locale->text('Amount') . qq|</td>
1115               </tr>
1116               <tr>
1117                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1118                 <td nowrap>| . $locale->text('Tax') . qq|</td>
1119                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1120                 <td nowrap>| . $locale->text('Total') . qq|</td>
1121                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1122                 <td nowrap>| . $locale->text('Date Paid') . qq|</td>
1123               </tr>
1124               <tr>
1125                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1126                 <td nowrap>| . $locale->text('Paid') . qq|</td>
1127                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1128                 <td nowrap>| . $locale->text('Due Date') . qq|</td>
1129                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1130                 <td nowrap>| . $locale->text('Amount Due') . qq|</td>
1131               </tr>
1132               <tr>
1133                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1134                 <td nowrap>| . $locale->text('Notes') . qq|</td>
1135                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1136                 <td nowrap>| . $locale->text('Employee') . qq|</td>
1137               </tr>
1138               <tr>
1139                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1140                 <td nowrap>| . $locale->text('Subtotal') . qq|</td>
1141               </tr>
1142             </table>
1143           </td>
1144         </tr>
1145       </table>
1146     </td>
1147   </tr>
1148   <tr>
1149     <td><hr size=3 noshade></td>
1150   </tr>
1151 </table>
1152
1153 $jsscript
1154
1155 <br>
1156 <input type=hidden name=nextsub value=$form->{nextsub}>
1157 <input type=hidden name=path value=$form->{path}>
1158 <input type=hidden name=login value=$form->{login}>
1159 <input type=hidden name=password value=$form->{password}>
1160
1161 <input class=submit type=submit name=action value="|
1162     . $locale->text('Continue') . qq|">
1163 </form>
1164
1165 </body>
1166 </html>
1167 |;
1168
1169   $lxdebug->leave_sub();
1170 }
1171
1172 sub ap_transactions {
1173   $lxdebug->enter_sub();
1174
1175   $form->{vendor} = $form->unescape($form->{vendor});
1176   ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
1177
1178   AP->ap_transactions(\%myconfig, \%$form);
1179
1180   $callback =
1181     "$form->{script}?action=ap_transactions&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1182   $href = $callback;
1183
1184   if ($form->{vendor}) {
1185     $callback .= "&vendor=" . $form->escape($form->{vendor}, 1);
1186     $href .= "&vendor=" . $form->escape($form->{vendor});
1187     $option .= $locale->text('Vendor') . " : $form->{vendor}";
1188   }
1189   if ($form->{department}) {
1190     $callback .= "&department=" . $form->escape($form->{department}, 1);
1191     $href .= "&department=" . $form->escape($form->{department});
1192     ($department) = split /--/, $form->{department};
1193     $option .= "\n<br>" if ($option);
1194     $option .= $locale->text('Department') . " : $department";
1195   }
1196   if ($form->{invnumber}) {
1197     $callback .= "&invnumber=" . $form->escape($form->{invnumber}, 1);
1198     $href .= "&invnumber=" . $form->escape($form->{invnumber});
1199     $option .= "\n<br>" if ($option);
1200     $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
1201   }
1202   if ($form->{ordnumber}) {
1203     $callback .= "&ordnumber=" . $form->escape($form->{ordnumber}, 1);
1204     $href .= "&ordnumber=" . $form->escape($form->{ordnumber});
1205     $option .= "\n<br>" if ($option);
1206     $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
1207   }
1208   if ($form->{notes}) {
1209     $callback .= "&notes=" . $form->escape($form->{notes}, 1);
1210     $href .= "&notes=" . $form->escape($form->{notes});
1211     $option .= "\n<br>" if $option;
1212     $option .= $locale->text('Notes') . " : $form->{notes}";
1213   }
1214
1215   if ($form->{transdatefrom}) {
1216     $callback .= "&transdatefrom=$form->{transdatefrom}";
1217     $href     .= "&transdatefrom=$form->{transdatefrom}";
1218     $option   .= "\n<br>" if ($option);
1219     $option   .=
1220         $locale->text('From') . " "
1221       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1222   }
1223   if ($form->{transdateto}) {
1224     $callback .= "&transdateto=$form->{transdateto}";
1225     $href     .= "&transdateto=$form->{transdateto}";
1226     $option   .= "\n<br>" if ($option);
1227     $option   .=
1228         $locale->text('Bis') . " "
1229       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1230   }
1231   if ($form->{open}) {
1232     $callback .= "&open=$form->{open}";
1233     $href     .= "&open=$form->{open}";
1234     $option   .= "\n<br>" if ($option);
1235     $option   .= $locale->text('Open');
1236   }
1237   if ($form->{closed}) {
1238     $callback .= "&closed=$form->{closed}";
1239     $href     .= "&closed=$form->{closed}";
1240     $option   .= "\n<br>" if ($option);
1241     $option   .= $locale->text('Closed');
1242   }
1243
1244   @columns =
1245     $form->sort_columns(
1246     qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee)
1247     );
1248
1249   foreach $item (@columns) {
1250     if ($form->{"l_$item"} eq "Y") {
1251       push @column_index, $item;
1252
1253       # add column to href and callback
1254       $callback .= "&l_$item=Y";
1255       $href     .= "&l_$item=Y";
1256     }
1257   }
1258
1259   if ($form->{l_subtotal} eq 'Y') {
1260     $callback .= "&l_subtotal=Y";
1261     $href     .= "&l_subtotal=Y";
1262   }
1263
1264   $column_header{id} =
1265       qq|<th><a class=listheading href=$href&sort=id>|
1266     . $locale->text('ID')
1267     . qq|</a></th>|;
1268   $column_header{transdate} =
1269       qq|<th><a class=listheading href=$href&sort=transdate>|
1270     . $locale->text('Date')
1271     . qq|</a></th>|;
1272   $column_header{duedate} =
1273       qq|<th><a class=listheading href=$href&sort=duedate>|
1274     . $locale->text('Due Date')
1275     . qq|</a></th>|;
1276   $column_header{due} =
1277     qq|<th class=listheading>| . $locale->text('Amount Due') . qq|</th>|;
1278   $column_header{invnumber} =
1279       qq|<th><a class=listheading href=$href&sort=invnumber>|
1280     . $locale->text('Invoice')
1281     . qq|</a></th>|;
1282   $column_header{ordnumber} =
1283       qq|<th><a class=listheading href=$href&sort=ordnumber>|
1284     . $locale->text('Order')
1285     . qq|</a></th>|;
1286   $column_header{name} =
1287       qq|<th><a class=listheading href=$href&sort=name>|
1288     . $locale->text('Vendor')
1289     . qq|</a></th>|;
1290   $column_header{netamount} =
1291     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1292   $column_header{tax} =
1293     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1294   $column_header{amount} =
1295     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1296   $column_header{paid} =
1297     qq|<th class=listheading>| . $locale->text('Paid') . qq|</th>|;
1298   $column_header{datepaid} =
1299       qq|<th><a class=listheading href=$href&sort=datepaid>|
1300     . $locale->text('Date Paid')
1301     . qq|</a></th>|;
1302   $column_header{notes} =
1303     qq|<th class=listheading>| . $locale->text('Notes') . qq|</th>|;
1304   $column_header{employee} =
1305     "<th><a class=listheading href=$href&sort=employee>"
1306     . $locale->text('Employee') . "</th>";
1307
1308   $form->{title} = $locale->text('AP Transactions');
1309
1310   $form->header;
1311
1312   print qq|
1313 <body>
1314
1315 <table width=100%>
1316   <tr>
1317     <th class=listtop>$form->{title}</th>
1318   </tr>
1319   <tr height="5"></tr>
1320   <tr>
1321     <td>$option</td>
1322   </tr>
1323   <tr>
1324     <td>
1325       <table width=100%>
1326         <tr class=listheading>
1327 |;
1328
1329   map { print "\n$column_header{$_}" } @column_index;
1330
1331   print qq|
1332         </tr>
1333 |;
1334
1335   # add sort and escape callback
1336   $form->{callback} = "$callback&sort=$form->{sort}";
1337   $callback = $form->escape($form->{callback});
1338
1339   if (@{ $form->{AP} }) {
1340     $sameitem = $form->{AP}->[0]->{ $form->{sort} };
1341   }
1342
1343   # sums and tax on reports by Antonio Gallardo
1344   #
1345   foreach $ap (@{ $form->{AP} }) {
1346
1347     if ($form->{l_subtotal} eq 'Y') {
1348       if ($sameitem ne $ap->{ $form->{sort} }) {
1349         &ap_subtotal;
1350         $sameitem = $ap->{ $form->{sort} };
1351       }
1352     }
1353
1354     $column_data{netamount} =
1355         "<td align=right>"
1356       . $form->format_amount(\%myconfig, $ap->{netamount}, 2, "&nbsp;")
1357       . "</td>";
1358     $column_data{tax} = "<td align=right>"
1359       . $form->format_amount(\%myconfig, $ap->{amount} - $ap->{netamount},
1360                              2, "&nbsp;")
1361       . "</td>";
1362     $column_data{amount} =
1363       "<td align=right>"
1364       . $form->format_amount(\%myconfig, $ap->{amount}, 2, "&nbsp;") . "</td>";
1365     $column_data{paid} =
1366       "<td align=right>"
1367       . $form->format_amount(\%myconfig, $ap->{paid}, 2, "&nbsp;") . "</td>";
1368     $column_data{due} = "<td align=right>"
1369       . $form->format_amount(\%myconfig, $ap->{amount} - $ap->{paid},
1370                              2, "&nbsp;")
1371       . "</td>";
1372
1373     $totalnetamount += $ap->{netamount};
1374     $totalamount    += $ap->{amount};
1375     $totalpaid      += $ap->{paid};
1376     $totaldue       += ($ap->{amount} - $ap->{paid});
1377
1378     $subtotalnetamount += $ap->{netamount};
1379     $subtotalamount    += $ap->{amount};
1380     $subtotalpaid      += $ap->{paid};
1381     $subtotaldue       += ($ap->{amount} - $ap->{paid});
1382
1383     $column_data{transdate} = "<td>$ap->{transdate}&nbsp;</td>";
1384     $column_data{duedate}   = "<td>$ap->{duedate}&nbsp;</td>";
1385     $column_data{datepaid}  = "<td>$ap->{datepaid}&nbsp;</td>";
1386
1387     $module = ($ap->{invoice}) ? "ir.pl" : $form->{script};
1388
1389     $column_data{invnumber} =
1390       qq|<td><a href="$module?action=edit&path=$form->{path}&id=$ap->{id}&login=$form->{login}&password=$form->{password}&callback=$callback">$ap->{invnumber}</a></td>|;
1391     $column_data{id}        = "<td>$ap->{id}</td>";
1392     $column_data{ordnumber} = "<td>$ap->{ordnumber}&nbsp;</td>";
1393     $column_data{name}      = "<td>$ap->{name}</td>";
1394     $ap->{notes} =~ s/\r\n/<br>/g;
1395     $column_data{notes}    = "<td>$ap->{notes}&nbsp;</td>";
1396     $column_data{employee} = "<td>$ap->{employee}&nbsp;</td>";
1397
1398     $i++;
1399     $i %= 2;
1400     print "
1401         <tr class=listrow$i >
1402 ";
1403
1404     map { print "\n$column_data{$_}" } @column_index;
1405
1406     print qq|
1407         </tr>
1408 |;
1409
1410   }
1411
1412   if ($form->{l_subtotal} eq 'Y') {
1413     &ap_subtotal;
1414   }
1415
1416   # print totals
1417   print qq|
1418         <tr class=listtotal>
1419 |;
1420
1421   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1422
1423   $column_data{netamount} =
1424     "<th class=listtotal align=right>"
1425     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1426   $column_data{tax} = "<th class=listtotal align=right>"
1427     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1428                            2, "&nbsp;")
1429     . "</th>";
1430   $column_data{amount} =
1431     "<th class=listtotal align=right>"
1432     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1433   $column_data{paid} =
1434     "<th class=listtotal align=right>"
1435     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;") . "</th>";
1436   $column_data{due} =
1437     "<th class=listtotal align=right>"
1438     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;") . "</th>";
1439
1440   map { print "$column_data{$_}\n" } @column_index;
1441
1442   print qq|
1443         </tr>
1444       </table>
1445     </td>
1446   </tr>
1447   <tr>
1448     <td><hr size=3 noshade></td>
1449   </tr>  
1450 </table>
1451
1452 <br>
1453 <form method=post action=$form->{script}>
1454  
1455 <input name=callback type=hidden value="$form->{callback}">
1456   
1457 <input type=hidden name=path value=$form->{path}>
1458 <input type=hidden name=login value=$form->{login}>
1459 <input type=hidden name=password value=$form->{password}>
1460    
1461 <input class=submit type=submit name=action value="|
1462     . $locale->text('AP Transaction') . qq|">
1463
1464 <input class=submit type=submit name=action value="|
1465     . $locale->text('Vendor Invoice') . qq|">|;
1466
1467   if ($form->{menubar}) {
1468     require "$form->{path}/menu.pl";
1469     &menubar;
1470   }
1471
1472   print qq|
1473   </form>
1474
1475 </body>
1476 </html>
1477 |;
1478
1479   $lxdebug->leave_sub();
1480 }
1481
1482 sub ap_subtotal {
1483   $lxdebug->enter_sub();
1484
1485   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1486
1487   $column_data{netamount} =
1488       "<th class=listsubtotal align=right>"
1489     . $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")
1490     . "</th>";
1491   $column_data{tax} = "<th class=listsubtotal align=right>"
1492     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1493                            2, "&nbsp;")
1494     . "</th>";
1495   $column_data{amount} =
1496     "<th class=listsubtotal align=right>"
1497     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1498   $column_data{paid} =
1499     "<th class=listsubtotal align=right>"
1500     . $form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;") . "</th>";
1501   $column_data{due} =
1502     "<th class=listsubtotal align=right>"
1503     . $form->format_amount(\%myconfig, $subtotaldue, 2, "&nbsp;") . "</th>";
1504
1505   $subtotalnetamount = 0;
1506   $subtotalamount    = 0;
1507   $subtotalpaid      = 0;
1508   $subtotaldue       = 0;
1509
1510   print "<tr class=listsubtotal>";
1511
1512   map { print "\n$column_data{$_}" } @column_index;
1513
1514   print qq|
1515   </tr>
1516 |;
1517
1518   $lxdebug->leave_sub();
1519 }
1520