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