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