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