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