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