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