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