03a2681ac4b57015f220ef36ad1c976a1e85eb0f
[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   my @triggers = ();
639   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
640   for $i (1 .. $form->{paidaccounts}) {
641     print "
642         <tr>
643 ";
644
645     $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
646     $form->{"selectAP_paid_$i"} =~
647       s/option value=\"$form->{"AP_paid_$i"}\">/option value=\"$form->{"AP_paid_$i"}\" selected>/;
648
649     # format amounts
650     if ($form->{"paid_$i"}) {
651       $form->{"paid_$i"} =
652       $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
653     }
654     $form->{"exchangerate_$i"} =
655       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
656
657     $exchangerate = qq|&nbsp;|;
658     if ($form->{currency} ne $form->{defaultcurrency}) {
659       if ($form->{"forex_$i"}) {
660         $exchangerate =
661           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
662       } else {
663         $exchangerate =
664           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
665       }
666     }
667
668     $exchangerate .= qq|
669 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
670 |;
671
672     $column_data{"paid_$i"} =
673       qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
674     $column_data{"AP_paid_$i"} =
675       qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
676     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
677     $column_data{"datepaid_$i"}     =
678       qq|<td align=center><input name="datepaid_$i" size=11 title="($myconfig{'dateformat'})" value=$form->{"datepaid_$i"}>
679          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
680     $column_data{"source_$i"} =
681       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
682     $column_data{"memo_$i"} =
683       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
684
685     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
686
687     print "
688         </tr>
689 ";
690     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
691   }
692   map { $form->{$_} =~ s/\"/&quot;/g } qw(selectAP_paid);
693   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
694     qq|
695     <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
696     <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
697
698       </table>
699     </td>
700   </tr>
701   <tr>
702     <td><hr size=3 noshade></td>
703   </tr>
704 </table>
705 |;
706
707   $lxdebug->leave_sub();
708 }
709
710 sub form_footer {
711   $lxdebug->enter_sub();
712
713   print qq|
714
715 <input name=callback type=hidden value="$form->{callback}">
716
717 <input type=hidden name=path value=$form->{path}>
718 <input type=hidden name=login value=$form->{login}>
719 <input type=hidden name=password value=$form->{password}>
720
721 <br>
722 |;
723
724   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
725   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
726
727   if ($form->{id}) {
728
729     #     print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
730     # |;
731   if ($form->{radier}) {
732         print qq|
733         <input class=submit type=submit name=action value="|
734           . $locale->text('Post') . qq|">
735         <input class=submit type=submit name=action value="|
736           . $locale->text('Delete') . qq|">
737 |;
738   }
739
740       print qq|
741 <input class=submit type=submit name=action value="|
742         . $locale->text('Use As Template') . qq|">
743 |;
744  
745   } else {
746     if (($transdate > $closedto) && !$form->{id}) {
747       print qq|<input class=submit type=submit name=action value="|
748         . $locale->text('Update') . qq|">
749       <input class=submit type=submit name=action value="|
750         . $locale->text('Post') . qq|">|;
751     }
752   }
753
754   if ($form->{menubar}) {
755     require "$form->{path}/menu.pl";
756     &menubar;
757   }
758
759   print "
760 </form>
761
762 </body>
763 </html>
764 ";
765
766   $lxdebug->leave_sub();
767 }
768
769 sub update {
770   $lxdebug->enter_sub();
771
772   my $display = shift;
773
774   #   if ($display) {
775   #     goto TAXCALC;
776   #   }
777
778   $form->{invtotal} = 0;
779
780   #   $form->{selectAP_amount} = $form->{AP_amount};
781   #   $form->{selectAP_amount} =~
782   #     s/value=\"$form->{AP_amountselected}\"/value=\"$form->{AP_amountselected}\" selected/;
783
784   $form->{selectAP} = $form->{AP};
785   $form->{selectAP} =~
786     s/value=\"$form->{APselected}\"/value=\"$form->{APselected}\" selected/;
787
788   ($AP_amountaccno, $AP_amounttaxkey) =
789     split(/--/, $form->{AP_amountselected});
790   $form->{selecttaxchart} = $form->{taxchart};
791   $form->{selecttaxchart} =~
792     s/value=\"$AP_amounttaxkey--([^\"]*)\"/value=\"$AP_amounttaxkey--$1\" selected/;
793
794   $form->{rate} = $1;
795
796   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
797     qw(exchangerate creditlimit creditremaining);
798
799   @flds  = qw(amount AP_amount projectnumber oldprojectnumber project_id);
800   $count = 0;
801   for $i (1 .. $form->{rowcount}) {
802     $form->{"amount_$i"} =
803       $form->parse_amount(\%myconfig, $form->{"amount_$i"});
804     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
805     if ($form->{"amount_$i"}) {
806       push @a, {};
807       $j = $#a;
808       if (!$form->{"korrektur_$i"}) {
809         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
810         if ($taxkey > 1) {
811           if ($form->{taxincluded}) {
812             $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
813           } else {
814             $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
815           }
816         } else {
817           $form->{"tax_$i"} = 0;
818         }
819       }
820       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
821
822       $totaltax += $form->{"tax_$i"};
823       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
824       $count++;
825     }
826   }
827   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
828
829   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
830
831   $form->{exchangerate} = $exchangerate
832     if (
833         $form->{forex} = (
834                     $exchangerate =
835                       $form->check_exchangerate(
836                       \%myconfig, $form->{currency}, $form->{transdate}, 'sell'
837                       )));
838
839   $form->{invdate} = $form->{transdate};
840   $save_AP = $form->{AP};
841   &check_name(vendor);
842   $form->{AP} = $save_AP;
843
844   &check_project;
845   $form->{rowcount} = $count + 1;
846
847   $form->{invtotal} =
848     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
849
850   for $i (1 .. $form->{paidaccounts}) {
851     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
852       map {
853         $form->{"${_}_$i"} =
854           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
855       } qw(paid exchangerate);
856
857       $totalpaid += $form->{"paid_$i"};
858
859       $form->{"exchangerate_$i"} = $exchangerate
860         if (
861             $form->{"forex_$i"} = (
862                 $exchangerate =
863                   $form->check_exchangerate(
864                   \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'
865                   )));
866     }
867   }
868
869   $form->{creditremaining} -=
870     ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
871      $form->{oldinvtotal});
872   $form->{oldinvtotal}  = $form->{invtotal};
873   $form->{oldtotalpaid} = $totalpaid;
874
875   &display_form;
876
877   $lxdebug->leave_sub();
878 }
879
880 sub post {
881   $lxdebug->enter_sub();
882
883   # check if there is a vendor, invoice and due date
884   $form->isblank("transdate", $locale->text("Invoice Date missing!"));
885   $form->isblank("duedate",   $locale->text("Due Date missing!"));
886   $form->isblank("vendor",    $locale->text('Vendor missing!'));
887
888   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
889   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
890
891   $form->error($locale->text('Cannot post transaction for a closed period!'))
892     if ($transdate <= $closedto);
893
894   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
895     if ($form->{currency} ne $form->{defaultcurrency});
896   delete($form->{AP});
897
898   for $i (1 .. $form->{paidaccounts}) {
899     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
900       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
901
902       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
903
904       $form->error($locale->text('Cannot post payment for a closed period!'))
905         if ($datepaid <= $closedto);
906
907       if ($form->{currency} ne $form->{defaultcurrency}) {
908         $form->{"exchangerate_$i"} = $form->{exchangerate}
909           if ($transdate == $datepaid);
910         $form->isblank("exchangerate_$i",
911                        $locale->text('Exchangerate for payment missing!'));
912       }
913
914     }
915   }
916
917   # if old vendor ne vendor redo form
918   ($vendor) = split /--/, $form->{vendor};
919   if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
920     &update;
921     exit;
922   }
923   ($debitaccno,    $debittaxkey)    = split /--/, $form->{AP_amountselected};
924   ($taxkey,        $NULL)           = split /--/, $form->{taxchartselected};
925   ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
926   $form->{AP}{amount_1} = $debitaccno;
927   $form->{AP}{payables} = $payablesaccno;
928   $form->{taxkey}       = $taxkey;
929
930   $form->{id} = 0 if $form->{postasnew};
931
932   $form->redirect($locale->text('Transaction posted!'))
933     if (AP->post_transaction(\%myconfig, \%$form));
934   $form->error($locale->text('Cannot post transaction!'));
935
936   $lxdebug->leave_sub();
937 }
938
939 sub post_as_new {
940   $lxdebug->enter_sub();
941
942   $form->{postasnew} = 1;
943   &post;
944
945   $lxdebug->leave_sub();
946 }
947
948 sub use_as_template {
949   $lxdebug->enter_sub();
950
951   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);
952   $form->{paidaccounts} = 1;
953   $form->{rowcount}--;
954   $form->{invdate} = $form->current_date(\%myconfig);
955   &update;
956
957   $lxdebug->leave_sub();
958 }
959
960 sub delete {
961   $lxdebug->enter_sub();
962
963   $form->{title} = $locale->text('Confirm!');
964
965   $form->header;
966
967   delete $form->{header};
968
969   print qq|
970 <body>
971
972 <form method=post action=$form->{script}>
973 |;
974
975   foreach $key (keys %$form) {
976     $form->{$key} =~ s/\"/&quot;/g;
977     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
978   }
979
980   print qq|
981 <h2 class=confirm>$form->{title}</h2>
982
983 <h4>|
984     . $locale->text('Are you sure you want to delete Transaction')
985     . qq| $form->{invnumber}</h4>
986
987 <input name=action class=submit type=submit value="|
988     . $locale->text('Yes') . qq|">
989 </form>
990
991 </body>
992 </html>
993 |;
994
995   $lxdebug->leave_sub();
996 }
997
998 sub yes {
999   $lxdebug->enter_sub();
1000
1001   $form->redirect($locale->text('Transaction deleted!'))
1002     if (AP->delete_transaction(\%myconfig, \%$form, $spool));
1003   $form->error($locale->text('Cannot delete transaction!'));
1004
1005   $lxdebug->leave_sub();
1006 }
1007
1008 sub search {
1009   $lxdebug->enter_sub();
1010
1011   # setup vendor selection
1012   $form->all_vc(\%myconfig, "vendor", "AP");
1013
1014   if (@{ $form->{all_vendor} }) {
1015     map { $vendor .= "<option>$_->{name}--$_->{id}\n" }
1016       @{ $form->{all_vendor} };
1017     $vendor = qq|<select name=vendor><option>\n$vendor\n</select>|;
1018   } else {
1019     $vendor = qq|<input name=vendor size=35>|;
1020   }
1021
1022   # departments
1023   if (@{ $form->{all_departments} }) {
1024     $form->{selectdepartment} = "<option>\n";
1025
1026     map {
1027       $form->{selectdepartment} .=
1028         "<option>$_->{description}--$_->{id}\n"
1029     } (@{ $form->{all_departments} });
1030   }
1031
1032   $department = qq|
1033         <tr>
1034           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1035           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1036         </tr>
1037 | if $form->{selectdepartment};
1038
1039   $form->{title} = $locale->text('AP Transactions');
1040
1041   # use JavaScript Calendar or not
1042   $form->{jsscript} = $jscalendar;
1043   $jsscript = "";
1044   if ($form->{jsscript}) {
1045
1046     # with JavaScript Calendar
1047     $button1 = qq|
1048        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
1049        <input type=button name=transdatefrom id="trigger1" value=|
1050       . $locale->text('button') . qq|></td>
1051       |;
1052     $button2 = qq|
1053        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
1054        <input type=button name=transdateto name=transdateto id="trigger2" value=|
1055       . $locale->text('button') . qq|></td>
1056      |;
1057
1058     #write Trigger
1059     $jsscript =
1060       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
1061                           "transdateto", "BL", "trigger2");
1062   } else {
1063
1064     # without JavaScript Calendar
1065     $button1 = qq|
1066                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
1067     $button2 = qq|
1068                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
1069   }
1070
1071   $form->header;
1072
1073   print qq|
1074 <body>
1075
1076 <form method=post action=$form->{script}>
1077
1078 <table width=100%>
1079   <tr>
1080     <th class=listtop>$form->{title}</th>
1081   </tr>
1082   <tr height="5"></tr>
1083   <tr>
1084     <td>
1085       <table>
1086         <tr>
1087           <th align=right>| . $locale->text('Vendor') . qq|</th>
1088           <td colspan=3>$vendor</td>
1089         </tr>
1090         $department
1091         <tr>
1092           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
1093           <td colspan=3><input name=invnumber size=20></td>
1094         </tr>
1095         <tr>
1096           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
1097           <td colspan=3><input name=ordnumber size=20></td>
1098         </tr>
1099         <tr>
1100           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
1101           <td colspan=3><input name=notes size=40></td>
1102         </tr>
1103         <tr>
1104           <th align=right nowrap>| . $locale->text('From') . qq|</th>
1105           $button1
1106           <th align=right>| . $locale->text('Bis') . qq|</th>
1107           $button2
1108         </tr>
1109         <input type=hidden name=sort value=transdate>
1110       </table>
1111     </td>
1112   </tr>
1113   <tr>
1114     <td>
1115       <table>
1116         <tr>
1117           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
1118           <td>
1119             <table width=100%>
1120               <tr>
1121                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1122                 <td nowrap>| . $locale->text('Open') . qq|</td>
1123                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1124                 <td nowrap>| . $locale->text('Closed') . qq|</td>
1125               </tr>
1126               <tr>
1127                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1128                 <td nowrap>| . $locale->text('ID') . qq|</td>
1129                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1130                 <td nowrap>| . $locale->text('Invoice Number') . qq|</td>
1131                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1132                 <td nowrap>| . $locale->text('Order Number') . qq|</td>
1133               </tr>
1134               <tr>
1135                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1136                 <td nowrap>| . $locale->text('Vendor') . qq|</td>
1137                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1138                 <td nowrap>| . $locale->text('Invoice Date') . qq|</td>
1139                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1140                 <td nowrap>| . $locale->text('Amount') . qq|</td>
1141               </tr>
1142               <tr>
1143                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1144                 <td nowrap>| . $locale->text('Tax') . qq|</td>
1145                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1146                 <td nowrap>| . $locale->text('Total') . qq|</td>
1147                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1148                 <td nowrap>| . $locale->text('Date Paid') . qq|</td>
1149               </tr>
1150               <tr>
1151                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1152                 <td nowrap>| . $locale->text('Paid') . qq|</td>
1153                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1154                 <td nowrap>| . $locale->text('Due Date') . qq|</td>
1155                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1156                 <td nowrap>| . $locale->text('Amount Due') . qq|</td>
1157               </tr>
1158               <tr>
1159                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1160                 <td nowrap>| . $locale->text('Notes') . qq|</td>
1161                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1162                 <td nowrap>| . $locale->text('Employee') . qq|</td>
1163               </tr>
1164               <tr>
1165                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1166                 <td nowrap>| . $locale->text('Subtotal') . qq|</td>
1167               </tr>
1168             </table>
1169           </td>
1170         </tr>
1171       </table>
1172     </td>
1173   </tr>
1174   <tr>
1175     <td><hr size=3 noshade></td>
1176   </tr>
1177 </table>
1178
1179 $jsscript
1180
1181 <br>
1182 <input type=hidden name=nextsub value=$form->{nextsub}>
1183 <input type=hidden name=path value=$form->{path}>
1184 <input type=hidden name=login value=$form->{login}>
1185 <input type=hidden name=password value=$form->{password}>
1186
1187 <input class=submit type=submit name=action value="|
1188     . $locale->text('Continue') . qq|">
1189 </form>
1190
1191 </body>
1192 </html>
1193 |;
1194
1195   $lxdebug->leave_sub();
1196 }
1197
1198 sub ap_transactions {
1199   $lxdebug->enter_sub();
1200
1201   $form->{vendor} = $form->unescape($form->{vendor});
1202   ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
1203
1204   AP->ap_transactions(\%myconfig, \%$form);
1205
1206   $callback =
1207     "$form->{script}?action=ap_transactions&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1208   $href = $callback;
1209
1210   if ($form->{vendor}) {
1211     $callback .= "&vendor=" . $form->escape($form->{vendor}, 1);
1212     $href .= "&vendor=" . $form->escape($form->{vendor});
1213     $option .= $locale->text('Vendor') . " : $form->{vendor}";
1214   }
1215   if ($form->{department}) {
1216     $callback .= "&department=" . $form->escape($form->{department}, 1);
1217     $href .= "&department=" . $form->escape($form->{department});
1218     ($department) = split /--/, $form->{department};
1219     $option .= "\n<br>" if ($option);
1220     $option .= $locale->text('Department') . " : $department";
1221   }
1222   if ($form->{invnumber}) {
1223     $callback .= "&invnumber=" . $form->escape($form->{invnumber}, 1);
1224     $href .= "&invnumber=" . $form->escape($form->{invnumber});
1225     $option .= "\n<br>" if ($option);
1226     $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
1227   }
1228   if ($form->{ordnumber}) {
1229     $callback .= "&ordnumber=" . $form->escape($form->{ordnumber}, 1);
1230     $href .= "&ordnumber=" . $form->escape($form->{ordnumber});
1231     $option .= "\n<br>" if ($option);
1232     $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
1233   }
1234   if ($form->{notes}) {
1235     $callback .= "&notes=" . $form->escape($form->{notes}, 1);
1236     $href .= "&notes=" . $form->escape($form->{notes});
1237     $option .= "\n<br>" if $option;
1238     $option .= $locale->text('Notes') . " : $form->{notes}";
1239   }
1240
1241   if ($form->{transdatefrom}) {
1242     $callback .= "&transdatefrom=$form->{transdatefrom}";
1243     $href     .= "&transdatefrom=$form->{transdatefrom}";
1244     $option   .= "\n<br>" if ($option);
1245     $option   .=
1246         $locale->text('From') . " "
1247       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1248   }
1249   if ($form->{transdateto}) {
1250     $callback .= "&transdateto=$form->{transdateto}";
1251     $href     .= "&transdateto=$form->{transdateto}";
1252     $option   .= "\n<br>" if ($option);
1253     $option   .=
1254         $locale->text('Bis') . " "
1255       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1256   }
1257   if ($form->{open}) {
1258     $callback .= "&open=$form->{open}";
1259     $href     .= "&open=$form->{open}";
1260     $option   .= "\n<br>" if ($option);
1261     $option   .= $locale->text('Open');
1262   }
1263   if ($form->{closed}) {
1264     $callback .= "&closed=$form->{closed}";
1265     $href     .= "&closed=$form->{closed}";
1266     $option   .= "\n<br>" if ($option);
1267     $option   .= $locale->text('Closed');
1268   }
1269
1270   @columns = $form->sort_columns(
1271     qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee)
1272   );
1273
1274   foreach $item (@columns) {
1275     if ($form->{"l_$item"} eq "Y") {
1276       push @column_index, $item;
1277
1278       # add column to href and callback
1279       $callback .= "&l_$item=Y";
1280       $href     .= "&l_$item=Y";
1281     }
1282   }
1283
1284   if ($form->{l_subtotal} eq 'Y') {
1285     $callback .= "&l_subtotal=Y";
1286     $href     .= "&l_subtotal=Y";
1287   }
1288
1289   $column_header{id} =
1290       qq|<th><a class=listheading href=$href&sort=id>|
1291     . $locale->text('ID')
1292     . qq|</a></th>|;
1293   $column_header{transdate} =
1294       qq|<th><a class=listheading href=$href&sort=transdate>|
1295     . $locale->text('Date')
1296     . qq|</a></th>|;
1297   $column_header{duedate} =
1298       qq|<th><a class=listheading href=$href&sort=duedate>|
1299     . $locale->text('Due Date')
1300     . qq|</a></th>|;
1301   $column_header{due} =
1302     qq|<th class=listheading>| . $locale->text('Amount Due') . qq|</th>|;
1303   $column_header{invnumber} =
1304       qq|<th><a class=listheading href=$href&sort=invnumber>|
1305     . $locale->text('Invoice')
1306     . qq|</a></th>|;
1307   $column_header{ordnumber} =
1308       qq|<th><a class=listheading href=$href&sort=ordnumber>|
1309     . $locale->text('Order')
1310     . qq|</a></th>|;
1311   $column_header{name} =
1312       qq|<th><a class=listheading href=$href&sort=name>|
1313     . $locale->text('Vendor')
1314     . qq|</a></th>|;
1315   $column_header{netamount} =
1316     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1317   $column_header{tax} =
1318     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1319   $column_header{amount} =
1320     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1321   $column_header{paid} =
1322     qq|<th class=listheading>| . $locale->text('Paid') . qq|</th>|;
1323   $column_header{datepaid} =
1324       qq|<th><a class=listheading href=$href&sort=datepaid>|
1325     . $locale->text('Date Paid')
1326     . qq|</a></th>|;
1327   $column_header{notes} =
1328     qq|<th class=listheading>| . $locale->text('Notes') . qq|</th>|;
1329   $column_header{employee} =
1330     "<th><a class=listheading href=$href&sort=employee>"
1331     . $locale->text('Employee') . "</th>";
1332
1333   $form->{title} = $locale->text('AP Transactions');
1334
1335   $form->header;
1336
1337   print qq|
1338 <body>
1339
1340 <table width=100%>
1341   <tr>
1342     <th class=listtop>$form->{title}</th>
1343   </tr>
1344   <tr height="5"></tr>
1345   <tr>
1346     <td>$option</td>
1347   </tr>
1348   <tr>
1349     <td>
1350       <table width=100%>
1351         <tr class=listheading>
1352 |;
1353
1354   map { print "\n$column_header{$_}" } @column_index;
1355
1356   print qq|
1357         </tr>
1358 |;
1359
1360   # add sort and escape callback
1361   $form->{callback} = "$callback&sort=$form->{sort}";
1362   $callback = $form->escape($form->{callback});
1363
1364   if (@{ $form->{AP} }) {
1365     $sameitem = $form->{AP}->[0]->{ $form->{sort} };
1366   }
1367
1368   # sums and tax on reports by Antonio Gallardo
1369   #
1370   foreach $ap (@{ $form->{AP} }) {
1371
1372     if ($form->{l_subtotal} eq 'Y') {
1373       if ($sameitem ne $ap->{ $form->{sort} }) {
1374         &ap_subtotal;
1375         $sameitem = $ap->{ $form->{sort} };
1376       }
1377     }
1378
1379     $column_data{netamount} =
1380         "<td align=right>"
1381       . $form->format_amount(\%myconfig, $ap->{netamount}, 2, "&nbsp;")
1382       . "</td>";
1383     $column_data{tax} = "<td align=right>"
1384       . $form->format_amount(\%myconfig, $ap->{amount} - $ap->{netamount},
1385                              2, "&nbsp;")
1386       . "</td>";
1387     $column_data{amount} =
1388       "<td align=right>"
1389       . $form->format_amount(\%myconfig, $ap->{amount}, 2, "&nbsp;") . "</td>";
1390     $column_data{paid} =
1391       "<td align=right>"
1392       . $form->format_amount(\%myconfig, $ap->{paid}, 2, "&nbsp;") . "</td>";
1393     $column_data{due} = "<td align=right>"
1394       . $form->format_amount(\%myconfig, $ap->{amount} - $ap->{paid},
1395                              2, "&nbsp;")
1396       . "</td>";
1397
1398     $totalnetamount += $ap->{netamount};
1399     $totalamount    += $ap->{amount};
1400     $totalpaid      += $ap->{paid};
1401     $totaldue       += ($ap->{amount} - $ap->{paid});
1402
1403     $subtotalnetamount += $ap->{netamount};
1404     $subtotalamount    += $ap->{amount};
1405     $subtotalpaid      += $ap->{paid};
1406     $subtotaldue       += ($ap->{amount} - $ap->{paid});
1407
1408     $column_data{transdate} = "<td>$ap->{transdate}&nbsp;</td>";
1409     $column_data{duedate}   = "<td>$ap->{duedate}&nbsp;</td>";
1410     $column_data{datepaid}  = "<td>$ap->{datepaid}&nbsp;</td>";
1411
1412     $module = ($ap->{invoice}) ? "ir.pl" : $form->{script};
1413
1414     $column_data{invnumber} =
1415       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>|;
1416     $column_data{id}        = "<td>$ap->{id}</td>";
1417     $column_data{ordnumber} = "<td>$ap->{ordnumber}&nbsp;</td>";
1418     $column_data{name}      = "<td>$ap->{name}</td>";
1419     $ap->{notes} =~ s/\r\n/<br>/g;
1420     $column_data{notes}    = "<td>$ap->{notes}&nbsp;</td>";
1421     $column_data{employee} = "<td>$ap->{employee}&nbsp;</td>";
1422
1423     $i++;
1424     $i %= 2;
1425     print "
1426         <tr class=listrow$i >
1427 ";
1428
1429     map { print "\n$column_data{$_}" } @column_index;
1430
1431     print qq|
1432         </tr>
1433 |;
1434
1435   }
1436
1437   if ($form->{l_subtotal} eq 'Y') {
1438     &ap_subtotal;
1439   }
1440
1441   # print totals
1442   print qq|
1443         <tr class=listtotal>
1444 |;
1445
1446   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1447
1448   $column_data{netamount} =
1449     "<th class=listtotal align=right>"
1450     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1451   $column_data{tax} = "<th class=listtotal align=right>"
1452     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1453                            2, "&nbsp;")
1454     . "</th>";
1455   $column_data{amount} =
1456     "<th class=listtotal align=right>"
1457     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1458   $column_data{paid} =
1459     "<th class=listtotal align=right>"
1460     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;") . "</th>";
1461   $column_data{due} =
1462     "<th class=listtotal align=right>"
1463     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;") . "</th>";
1464
1465   map { print "$column_data{$_}\n" } @column_index;
1466
1467   print qq|
1468         </tr>
1469       </table>
1470     </td>
1471   </tr>
1472   <tr>
1473     <td><hr size=3 noshade></td>
1474   </tr>
1475 </table>
1476
1477 <br>
1478 <form method=post action=$form->{script}>
1479
1480 <input name=callback type=hidden value="$form->{callback}">
1481
1482 <input type=hidden name=path value=$form->{path}>
1483 <input type=hidden name=login value=$form->{login}>
1484 <input type=hidden name=password value=$form->{password}>
1485
1486 <input class=submit type=submit name=action value="|
1487     . $locale->text('AP Transaction') . qq|">
1488
1489 <input class=submit type=submit name=action value="|
1490     . $locale->text('Vendor Invoice') . qq|">|;
1491
1492   if ($form->{menubar}) {
1493     require "$form->{path}/menu.pl";
1494     &menubar;
1495   }
1496
1497   print qq|
1498   </form>
1499
1500 </body>
1501 </html>
1502 |;
1503
1504   $lxdebug->leave_sub();
1505 }
1506
1507 sub ap_subtotal {
1508   $lxdebug->enter_sub();
1509
1510   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1511
1512   $column_data{netamount} =
1513       "<th class=listsubtotal align=right>"
1514     . $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")
1515     . "</th>";
1516   $column_data{tax} = "<th class=listsubtotal align=right>"
1517     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1518                            2, "&nbsp;")
1519     . "</th>";
1520   $column_data{amount} =
1521     "<th class=listsubtotal align=right>"
1522     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1523   $column_data{paid} =
1524     "<th class=listsubtotal align=right>"
1525     . $form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;") . "</th>";
1526   $column_data{due} =
1527     "<th class=listsubtotal align=right>"
1528     . $form->format_amount(\%myconfig, $subtotaldue, 2, "&nbsp;") . "</th>";
1529
1530   $subtotalnetamount = 0;
1531   $subtotalamount    = 0;
1532   $subtotalpaid      = 0;
1533   $subtotaldue       = 0;
1534
1535   print "<tr class=listsubtotal>";
1536
1537   map { print "\n$column_data{$_}" } @column_index;
1538
1539   print qq|
1540   </tr>
1541 |;
1542
1543   $lxdebug->leave_sub();
1544 }