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