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