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