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