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