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