unstable-Zweig als Kopie des "alten" trunks erstellt.
[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
35 use SL::AP;
36 use SL::IR;
37 use SL::PE;
38
39 require "$form->{path}/arap.pl";
40
41 1;
42 # end of main
43
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
74 sub add {
75   $lxdebug->enter_sub();
76
77
78   $form->{title} = "Add";
79   
80   $form->{callback} = "$form->{script}?action=add&path=$form->{path}&login=$form->{login}&password=$form->{password}" unless $form->{callback};
81
82   &create_links;
83   &display_form;
84   
85   $lxdebug->leave_sub();
86 }
87
88
89 sub edit {
90   $lxdebug->enter_sub();
91
92   
93   $form->{title} = "Edit";
94
95   &create_links;
96   &display_form;
97
98   $lxdebug->leave_sub();
99 }
100
101
102 sub display_form {
103   $lxdebug->enter_sub();
104
105   
106   &form_header;
107   &form_footer;
108
109   $lxdebug->leave_sub();
110 }
111
112
113 sub create_links {
114   $lxdebug->enter_sub();
115
116
117   $form->create_links("AP", \%myconfig, "vendor");
118   $taxincluded = $form->{taxincluded};
119   $duedate = $form->{duedate};
120
121   IR->get_vendor(\%myconfig, \%$form);
122   
123   $form->{duedate} = $duedate if $duedate;
124   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
125   
126   # build the popup menus
127   $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
128   
129   map { $tax .= qq|<option value=\"$_->{taxkey}--$_->{rate}\">$_->{taxdescription}  |.($_->{rate} * 100).qq| %|} @{ $form->{TAX} };
130   $form->{taxchart} = $tax;
131   $form->{selecttaxchart} = $tax;
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" } (@{ $form->{all_vendor} });
144   }
145   
146   # departments
147   if (@{ $form->{all_departments} }) {
148     $form->{selectdepartment} = "<option>\n";
149     $form->{department} = "$form->{department}--$form->{department_id}";
150
151     map { $form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } (@{ $form->{all_departments} });
152   }
153
154   $form->{employee} = "$form->{employee}--$form->{employee_id}"; 
155   
156   # forex
157   $form->{forex} = $form->{exchangerate};
158   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
159   
160   foreach $key (keys %{ $form->{AP_links} }) {
161     foreach $ref (@{ $form->{AP_links}{$key} }) {
162       if ($key eq "AP_paid") {
163         $form->{"select$key"} .= "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
164       } else {
165         $form->{"select$key"} .= "<option value=\"$ref->{accno}--$ref->{taxkey}\">$ref->{accno}--$ref->{description}</option>\n";
166       }
167     }
168
169     $form->{$key} = $form->{"select$key"};
170     # if there is a value we have an old entry
171     $j = 0;
172     for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
173
174       if ($key eq "AP_paid") {
175         $j++;
176         $form->{"AP_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
177         $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{amount};
178         $form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
179         $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i-1]->{source};
180         $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i-1]->{memo};
181         
182         $form->{"forex_$j"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
183         $form->{"AP_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}";    
184         $form->{paidaccounts}++;
185       } else {
186
187         $akey = $key;
188         $akey =~ s/AP_//;
189
190         if ($key eq "AP_tax") {
191           $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
192           $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i-1]->{amount} / $exchangerate * -1, 2);
193           if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
194             $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
195             $taxrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
196           } else {
197             $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
198             $withholdingrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
199           }
200           $formtax = $form->round_amount($form->{acc_trans}{$key}->[$i-1]->{amount} / $exchangerate, 2) *-1;
201         } else {
202           $form->{"${akey}_$i"} = $form->round_amount($form->{acc_trans}{$key}->[$i-1]->{amount} / $exchangerate, 2);
203           if ($akey eq 'amount') {
204             $form->{"${akey}_$i"} *= -1;
205             $totalamount += $form->{"${akey}_$i"};
206             $form->{taxrate} = $form->{acc_trans}{$key}->[$i-1]->{rate};
207             $form->{rowcount}++;            
208             $form->{"oldprojectnumber_$i"} = $form->{"projectnumber_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
209             $form->{"project_id_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
210           }
211           $form->{"${key}_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
212            $form->{"select${key}"} =~ /(<option value=\"$form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*\">$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}<\/option>\n)/;
213            $test = $1;
214            $form->{"select${key}"} = $1;
215            if ($akey eq 'amount') {
216              $form->{selecttaxchart} =~ /(<option value=\"$form->{acc_trans}{$key}->[$i-1]->{taxkey}--[^<]*)/;
217              $form->{selecttaxchart} = $1;
218            }
219         }
220       }
221     }
222   }
223
224   $form->{taxincluded} = $taxincluded if ($form->{id});
225   $form->{paidaccounts} = 1 if not defined $form->{paidaccounts};
226
227   if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
228   # add tax to amounts and invtotal
229     for $i (1 .. 1) {
230       $taxamount = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
231       $tax = $form->round_amount($taxamount, 2);
232       $diff += ($taxamount - $tax);
233       $form->{"amount_$i"} += $tax;
234     }
235     $form->{amount_1} += $form->round_amount($diff, 2);
236   }
237
238   # check if calculated is equal to stored
239   if ($form->{taxincluded} && $form->{taxrate} && $witholdingrate) {
240       if ($form->{"taxrate"} > 0) {
241         $taxamount = $form->round_amount(($form->{amount_1} - ($form->{amount_1} / ($form->{taxrate} + 1))), 2);
242       } else {
243         $taxamount = $form->round_amount(($totalamount + $totaltax + $totalwithholding) * $withholdingrate / (1 - $withholdingrate), 2) * $form->{"taxrate"} / $withholdingrate;
244       }
245   } else {
246     $taxamount = $totalamount * $form->{"taxrate"};
247     }
248   $taxamount = $form->round_amount($taxamount, 2);
249   $form->{tax} = $taxamount;
250   
251   $form->{tax} = $formtax;
252   
253   $form->{invtotal} = $totalamount + $totaltax + $totalwithholding;
254   $form->{rowcount}++ if $form->{id};
255   
256   
257   $form->{locked} = ($form->datetonum($form->{transdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig));
258
259   $lxdebug->leave_sub();
260 }
261
262
263 sub form_header {
264   $lxdebug->enter_sub();
265
266
267   $title = $form->{title};
268   $form->{title} = $locale->text("$title Accounts Payables Transaction");
269
270   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
271
272 # type=submit $locale->text('Add Accounts Payables Transaction')
273 # type=submit $locale->text('Edit Accounts Payables Transaction')
274
275   # set option selected
276   foreach $item (qw(vendor currency department)) {
277     $form->{"select$item"} =~ s/ selected//;
278     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
279   }
280   $readonly = ($form->{id}) ?  "readonly" : "";
281   
282   $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
283   $readonly = ($form->{radier}) ? "" : $readonly;
284   
285   map { $form->{$_} =~ s/\"/&quot;/g } qw(AP_amount AP taxchart);  
286   # format amounts
287   $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
288
289   $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
290   $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
291   
292   $exchangerate = qq|
293 <input type=hidden name=forex value=$form->{forex}>
294 |;
295   if ($form->{currency} ne $form->{defaultcurrency}) {
296     if ($form->{forex}) {
297       $exchangerate .= qq|
298             <tr>
299               <th align=right>|.$locale->text('Exchangerate').qq|</th>
300               <td><input type=hidden name=exchangerate value=$form->{exchangerate}>$form->{exchangerate}</td>
301            </tr>
302 |;
303     } else {
304       $exchangerate .= qq|
305              <th align=right>|.$locale->text('Exchangerate').qq|</th>
306              <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
307 |;
308     }
309   }
310   
311   $taxincluded = "";
312
313   $taxincluded = qq|
314             <tr>
315               <td align=right><input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td>
316               <th align=left nowrap>|.$locale->text('Tax Included').qq|</th>
317             </tr>
318 |;
319
320
321
322   if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
323     $rows = 2;
324   }
325   $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft $readonly>$form->{notes}</textarea>|;
326   
327   $department = qq|
328               <tr>
329                 <th align="right" nowrap>|.$locale->text('Department').qq|</th>
330                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
331                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
332                 </td>
333               </tr>
334 | if $form->{selectdepartment};
335
336   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
337   
338   $vendor = ($form->{selectvendor}) ? qq|<select name=vendor>$form->{selectvendor}</select>| : qq|<input name=vendor value="$form->{vendor}" size=35>|; 
339
340   # use JavaScript Calendar or not
341   $form->{jsscript} = $jscalendar;
342   $jsscript = "";
343   if ($form->{jsscript}) 
344   {
345     # with JavaScript Calendar
346     $button1 = qq|
347        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}> $readonly</td>
348        <td><input type=button name=transdate id="trigger1" value=|.$locale->text('button').qq|></td>  
349        |;
350      $button2 = qq|
351        <td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}> $readonly</td>
352        <td><input type=button name=duedate id="trigger2" value=|.$locale->text('button').qq|></td></td>
353      |;
354     #write Trigger
355     $jsscript = Form->write_trigger(\%myconfig,"2","transdate","BL","trigger1","duedate","BL","trigger2");
356    }
357    else
358    {
359       # without JavaScript Calendar
360       $button1 = qq|<td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}> $readonly</td>|;
361       $button2 = qq|<td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}> $readonly</td>|;
362     }
363     
364   $form->header;
365  
366   print qq|
367 <body>
368
369 <form method=post action=$form->{script}>
370
371 <input type=hidden name=id value=$form->{id}>
372 <input type=hidden name=sort value=$form->{sort}>
373 <input type=hidden name=closedto value=$form->{closedto}>
374 <input type=hidden name=locked value=$form->{locked}>
375 <input type=hidden name=title value="$title">
376
377 <table width=100%>
378   <tr class=listtop>
379     <th class=listtop>$form->{title}</th>
380   </tr>
381   <tr height="5"></tr>
382   <tr valign=top>
383     <td>
384       <table width=100%>
385         <tr valign=top>
386           <td>
387             <table>
388               <tr>
389                 <th align=right nowrap>|.$locale->text('Vendor').qq|</th>
390                 <td colspan=3>$vendor</td>
391                 <input type=hidden name=selectvendor value="$form->{selectvendor}">
392                 <input type=hidden name=oldvendor value="$form->{oldvendor}">
393                 <input type=hidden name=vendor_id value="$form->{vendor_id}">
394                 <input type=hidden name=terms value=$form->{terms}>
395               </tr>
396               <tr>
397                 <td></td>
398                 <td colspan=3>
399                   <table width=100%>
400                     <tr>
401                       <th align=left nowrap>|.$locale->text('Credit Limit').qq|</th>
402                       <td>$form->{creditlimit}</td>
403                       <th align=left nowrap>|.$locale->text('Remaining').qq|</th>
404                       <td class="plus$n">$form->{creditremaining}</td>
405                       <input type=hidden name=creditlimit value=$form->{creditlimit}>
406                       <input type=hidden name=creditremaining value=$form->{creditremaining}>
407                     </tr>
408                   </table>
409                 </td>
410               <tr>
411                 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
412                 <td><select name=currency>$form->{selectcurrency}</select></td>
413                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
414                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
415                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
416                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
417                 $exchangerate
418               </tr>
419               $department
420               $taxincluded
421             </table>
422           </td>
423           <td align=right>
424             <table>
425               <tr>
426                 <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
427                 <td><input name=invnumber size=11 value="$form->{invnumber}" $readonly></td>
428               </tr>
429               <tr>
430                 <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
431                 <td><input name=ordnumber size=11 value="$form->{ordnumber}" $readonly></td>
432               </tr>
433               <tr>
434                 <th align=right nowrap>|.$locale->text('Invoice Date').qq|</th>
435                 $button1
436               </tr>
437               <tr>
438                 <th align=right nowrap>|.$locale->text('Due Date').qq|</th>
439                 $button2
440               </tr>
441             </table>
442           </td>
443         </tr>
444       </table>
445     </td>
446   </tr>
447   <input type=hidden name=AP_amount value="$form->{AP_amount}">
448   <input type=hidden name=taxchart value="$form->{taxchart}">
449   <input type=hidden name=rowcount value=$form->{rowcount}>
450   <tr>
451     <td>
452       <table width=100%>
453
454 $jsscript  
455 |;
456
457   $amount = $locale->text('Amount');
458   $project = $locale->text('Project');
459
460   for $i (1 .. 1) {
461
462 #     $form->{"selectAP_amount"} =~ s/ selected//;
463 #     $form->{"selectAP_amount"} =~ s/option>\Q$form->{"AP_amount_$i"}\E/option selected>$form->{"AP_amount_$i"}/;
464
465     # format amounts
466     $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
467
468     print qq|
469         <tr>
470           <th align=right nowrap>$amount</th>
471           <td><input name="amount_$i" size=10 value=$form->{"amount_$i"} $readonly></td>
472           <th>$project</th>
473           <td><input name="projectnumber_$i" size=20 value="$form->{"projectnumber_$i"}">
474               <input type=hidden name="project_id_$i" value=$form->{"project_id_$i"}>
475               <input type=hidden name="oldprojectnumber_$i" value="$form->{"oldprojectnumber_$i"}"></td>
476           <td width=50%><select  name="AP_amountselected">$form->{selectAP_amount}</select></td>
477         </tr>
478 |;
479     $amount = "";
480     $project = "";
481   }
482
483   $taxlabel = ($form->{taxincluded}) ? $locale->text('Tax Included') : $locale->text('Tax');
484   # format and reverse tax
485   $form->{"tax"} = $form->format_amount(\%myconfig, $form->{"tax"}, 2);  
486    
487    print qq|
488         <tr>
489           <th align=right nowrap>${taxlabel}</th>
490           <td><input name="tax" size=10 value=$form->{"tax"} $readonly></td>
491           <td align=right><input name=taxcheck class=checkbox type=checkbox value=1></td>
492           <td align=left nowrap>|.$locale->text('Correct Tax').qq|</td>
493           <td><select name=taxchartselected>$form->{"selecttaxchart"}</select></td>
494         </tr>
495 |;
496   
497     
498  
499
500
501    
502   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
503
504   print qq|
505         <tr>
506           <th align=right nowrap>|.$locale->text('Total').qq|</th>
507           <td>$form->{invtotal}</td>
508
509           <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
510           <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
511           
512           <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
513           
514           <td colspan=2></td>
515           <td><select name=APselected>$form->{selectAP}</select></td>
516           <input type=hidden name=AP value="$form->{AP}">
517           
518         </tr>
519         <tr>
520           <th align=right nowrap>|.$locale->text('Notes').qq|</th>
521           <td colspan=5>$notes</td>
522         </tr>
523       </table>
524     </td>
525   </tr>
526   <tr>
527     <td>
528       <table width=100%>
529         <tr class=listheading>
530           <th class=listheading colspan=6>|.$locale->text('Payments').qq|</th>
531         </tr>
532 |;
533
534
535   if ($form->{currency} eq $form->{defaultcurrency}) {
536     @column_index = qw(datepaid source memo paid AP_paid);
537   } else {
538     @column_index = qw(datepaid source memo paid exchangerate AP_paid);
539   }
540
541   $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
542   $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
543   $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
544   $column_data{AP_paid} = "<th>".$locale->text('Account')."</th>";
545   $column_data{source} = "<th>".$locale->text('Source')."</th>";
546   $column_data{memo} = "<th>".$locale->text('Memo')."</th>";
547
548   print "
549         <tr>
550 ";
551   map { print "$column_data{$_}\n" } @column_index;
552   print "
553         </tr>
554 ";
555
556   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
557   for $i (1 .. $form->{paidaccounts}) {
558     print "
559         <tr>
560 ";
561
562     $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
563     $form->{"selectAP_paid_$i"} =~ s/option value=\"$form->{"AP_paid_$i"}\">/option value=\"$form->{"AP_paid_$i"}\" selected>/;
564
565     
566     # format amounts
567     $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
568     $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
569     
570     $exchangerate = qq|&nbsp;|;
571     if ($form->{currency} ne $form->{defaultcurrency}) {
572       if ($form->{"forex_$i"}) {
573         $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
574       } else {
575         $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
576       }
577     }
578
579     $exchangerate .= qq|
580 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
581 |;
582
583     $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
584     $column_data{"AP_paid_$i"} = qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
585     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
586     $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 title="($myconfig{'dateformat'})" value=$form->{"datepaid_$i"}></td>|;
587     $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
588     $column_data{"memo_$i"} = qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
589     
590     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
591
592     print "
593         </tr>
594 ";
595   }
596   map { $form->{$_} =~ s/\"/&quot;/g } qw(selectAP_paid);
597   print qq|
598     <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
599     <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
600     
601       </table>
602     </td>
603   </tr>
604   <tr>
605     <td><hr size=3 noshade></td>
606   </tr>
607 </table>
608 |;
609
610   $lxdebug->leave_sub();
611 }
612
613
614 sub form_footer {
615   $lxdebug->enter_sub();
616
617
618   print qq|
619
620 <input name=callback type=hidden value="$form->{callback}">
621
622 <input type=hidden name=path value=$form->{path}>
623 <input type=hidden name=login value=$form->{login}>
624 <input type=hidden name=password value=$form->{password}>
625
626 <br>
627 |;
628
629   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
630   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
631   
632   if ($form->{id} && $form->{radier}) {
633 #     print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
634 # |;
635
636     if (!$form->{revtrans}) {
637       if (!$form->{locked}) {
638         print qq|
639         <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">
640         <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
641 |;
642       }
643     }
644
645     if ($transdate > $closedto) {
646       print qq|
647 <input class=submit type=submit name=action value="|.$locale->text('Post as new').qq|">
648 |;
649     }
650
651   } else {
652     if (($transdate > $closedto) && !$form->{id}) {
653       print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
654       <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
655     }
656   }
657
658   if ($form->{menubar}) {
659     require "$form->{path}/menu.pl";
660     &menubar;
661   }
662
663   print "
664 </form>
665
666 </body>
667 </html>
668 ";
669
670   $lxdebug->leave_sub();
671 }
672
673
674 sub update {
675   $lxdebug->enter_sub();
676
677   my $display = shift;
678
679 #   if ($display) {
680 #     goto TAXCALC;
681 #   }
682
683   $form->{invtotal} = 0;
684
685
686
687   $form->{selectAP_amount} = $form->{AP_amount};
688   $form->{selectAP_amount}  =~ s/value=\"$form->{AP_amountselected}\"/value=\"$form->{AP_amountselected}\" selected/;
689
690   
691   $form->{selectAP} = $form->{AP};
692   $form->{selectAP}  =~ s/value=\"$form->{APselected}\"/value=\"$form->{APselected}\" selected/;
693
694   ($AP_amountaccno, $AP_amounttaxkey) = split(/--/, $form->{AP_amountselected});  
695   $form->{selecttaxchart} = $form->{taxchart};
696   $form->{selecttaxchart}  =~ s/value=\"$AP_amounttaxkey--([^\"]*)\"/value=\"$AP_amounttaxkey--$1\" selected/; 
697   
698   $form->{rate} = $1;
699
700   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
701   
702   @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id);
703   $count = 0;
704   for $i (1 .. 1) {
705     $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
706     if ($form->{"amount_$i"}) {
707       push @a, {};
708       my $j = $#a;
709       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
710       $count++;
711     }
712   }
713   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
714   
715   $form->{invtotal} += $form->{"amount_1"};
716   
717   $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'sell')));
718
719   $form->{invdate} = $form->{transdate};
720   $save_AP = $form->{AP};
721   &check_name(vendor);
722   $form->{AP} = $save_AP;
723
724   &check_project;
725
726
727 TAXCALC:
728   # recalculate taxes
729
730   @taxaccounts = split / /, $form->{taxaccounts};
731
732   $form->{"tax"} = $form->parse_amount(\%myconfig, $form->{"tax"});
733   
734   if ($form->{taxincluded} && $form->{"rate"}) {
735     $taxrate = $form->{"rate"};
736     $withholdingrate = 0;
737
738    $amount = $form->round_amount(($form->{invtotal} - ($form->{invtotal} / ($taxrate + 1))), 2);
739    $form->{"tax"} = $form->round_amount($amount, 2);
740    $taxdiff += ($amount - $form->{"tax"});
741
742         
743    if (abs $taxdiff >= 0.005) {
744         $form->{"tax"} += $form->round_amount($taxdiff, 2);
745         $taxdiff = 0;
746    }
747    $totaltax += $form->{"tax"};
748  
749   } else {
750    $form->{"tax"} = $form->round_amount($form->{invtotal} * $form->{"rate"}, 2);
751    $totaltax += $form->{"tax"};
752     
753   }
754
755   $form->{invtotal} = ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
756   
757   for $i (1 .. $form->{paidaccounts}) {
758     if ($form->{"paid_$i"}) {
759       map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
760
761       $totalpaid += $form->{"paid_$i"};
762       
763       $form->{"exchangerate_$i"} = $exchangerate if ($form->{"forex_$i"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell')));
764     }
765   }
766
767   $form->{creditremaining} -= ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} - $form->{oldinvtotal});
768   $form->{oldinvtotal} = $form->{invtotal};
769   $form->{oldtotalpaid} = $totalpaid;
770   
771   &display_form;
772
773   $lxdebug->leave_sub();
774 }
775  
776  
777 sub post {
778   $lxdebug->enter_sub();
779
780
781   # check if there is a vendor, invoice and due date
782   $form->isblank("transdate", $locale->text("Invoice Date missing!"));
783   $form->isblank("duedate", $locale->text("Due Date missing!"));
784   $form->isblank("vendor", $locale->text('Vendor missing!'));
785   
786   
787   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
788   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
789
790   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $closedto);
791
792   $form->isblank("exchangerate", $locale->text('Exchangerate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
793   delete($form->{AP});
794
795   for $i (1 .. $form->{paidaccounts}) {
796     if ($form->{"paid_$i"}) {
797       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
798
799       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
800
801       $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
802
803       if ($form->{currency} ne $form->{defaultcurrency}) {
804         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
805         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
806       }
807       
808     }
809   }
810       
811
812   # if old vendor ne vendor redo form
813   ($vendor) = split /--/, $form->{vendor};
814   if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
815     &update;
816     exit;
817   }
818   ($debitaccno, $debittaxkey) = split /--/, $form->{AP_amountselected};
819   ($taxkey, $NULL) = split /--/, $form->{taxchartselected};
820   ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};  
821   $form->{AP}{amount_1} = $debitaccno;
822   $form->{AP}{payables} = $payablesaccno;
823   $form->{taxkey} = $taxkey;
824     
825   $form->{id} = 0 if $form->{postasnew};
826
827   $form->redirect($locale->text('Transaction posted!')) if (AP->post_transaction(\%myconfig, \%$form));
828   $form->error($locale->text('Cannot post transaction!'));
829   
830   $lxdebug->leave_sub();
831 }
832
833
834 sub post_as_new {
835   $lxdebug->enter_sub();
836
837
838   $form->{postasnew} = 1;
839   &post;
840
841   $lxdebug->leave_sub();
842 }
843
844
845 sub delete {
846   $lxdebug->enter_sub();
847
848
849   $form->{title} = $locale->text('Confirm!');
850   
851   $form->header;
852
853   delete $form->{header};
854   
855   print qq|
856 <body>
857
858 <form method=post action=$form->{script}>
859 |;
860
861   foreach $key (keys %$form) {
862     $form->{$key} =~ s/\"/&quot;/g;  
863     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
864   }
865
866   print qq|
867 <h2 class=confirm>$form->{title}</h2>
868
869 <h4>|.$locale->text('Are you sure you want to delete Transaction').qq| $form->{invnumber}</h4>
870
871 <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
872 </form>
873
874 </body>
875 </html>
876 |;
877
878   $lxdebug->leave_sub();
879 }
880
881
882
883 sub yes {
884   $lxdebug->enter_sub();
885
886
887   $form->redirect($locale->text('Transaction deleted!')) if (AP->delete_transaction(\%myconfig, \%$form, $spool));
888   $form->error($locale->text('Cannot delete transaction!'));
889
890   $lxdebug->leave_sub();
891 }
892
893
894 sub search {
895   $lxdebug->enter_sub();
896
897   
898   # setup vendor selection
899   $form->all_vc(\%myconfig, "vendor", "AP");
900
901   if (@{ $form->{all_vendor} }) {
902     map { $vendor .= "<option>$_->{name}--$_->{id}\n" } @{ $form->{all_vendor} };
903     $vendor = qq|<select name=vendor><option>\n$vendor\n</select>|;
904   } else {
905     $vendor = qq|<input name=vendor size=35>|;
906   }
907
908   # departments
909   if (@{ $form->{all_departments} }) { 
910     $form->{selectdepartment} = "<option>\n";
911
912     map { $form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } (@{ $form->{all_departments} });
913   }
914
915   $department = qq|
916         <tr>
917           <th align=right nowrap>|.$locale->text('Department').qq|</th>
918           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
919         </tr>
920 | if $form->{selectdepartment};
921
922   $form->{title} = $locale->text('AP Transactions');
923
924   # use JavaScript Calendar or not
925   $form->{jsscript} = $jscalendar;
926   $jsscript = "";    
927   if ($form->{jsscript}) 
928   {
929     # with JavaScript Calendar
930     $button1 = qq|
931        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
932        <input type=button name=transdatefrom id="trigger1" value=|.$locale->text('button').qq|></td>
933       |;
934      $button2 = qq|
935        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
936        <input type=button name=transdateto name=transdateto id="trigger2" value=|.$locale->text('button').qq|></td>
937      |;
938     #write Trigger
939     $jsscript = Form->write_trigger(\%myconfig,"2","transdatefrom","BR","trigger1","transdateto","BL","trigger2");
940    }
941    else
942    {
943       # without JavaScript Calendar
944       $button1 = qq|
945                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
946       $button2 = qq|
947                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
948     }
949   
950   $form->header;
951
952   print qq|
953 <body>
954
955 <form method=post action=$form->{script}>
956
957 <table width=100%>
958   <tr>
959     <th class=listtop>$form->{title}</th>
960   </tr>
961   <tr height="5"></tr>
962   <tr>
963     <td>
964       <table>
965         <tr>
966           <th align=right>|.$locale->text('Vendor').qq|</th>
967           <td colspan=3>$vendor</td>
968         </tr>
969         $department
970         <tr>
971           <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
972           <td colspan=3><input name=invnumber size=20></td>
973         </tr>
974         <tr>
975           <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
976           <td colspan=3><input name=ordnumber size=20></td>
977         </tr>
978         <tr>
979           <th align=right nowrap>|.$locale->text('Notes').qq|</th>
980           <td colspan=3><input name=notes size=40></td>
981         </tr>
982         <tr>
983           <th align=right nowrap>|.$locale->text('From').qq|</th>
984           $button1
985           <th align=right>|.$locale->text('Bis').qq|</th>
986           $button2
987         </tr>
988         <input type=hidden name=sort value=transdate>
989       </table>
990     </td>
991   </tr>
992   <tr>
993     <td>
994       <table>
995         <tr>
996           <th align=right nowrap>|.$locale->text('Include in Report').qq|</th>
997           <td>
998             <table width=100%>
999               <tr>
1000                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1001                 <td nowrap>|.$locale->text('Open').qq|</td>
1002                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1003                 <td nowrap>|.$locale->text('Closed').qq|</td>
1004               </tr>
1005               <tr>
1006                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1007                 <td nowrap>|.$locale->text('ID').qq|</td>
1008                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1009                 <td nowrap>|.$locale->text('Invoice Number').qq|</td>
1010                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1011                 <td nowrap>|.$locale->text('Order Number').qq|</td>
1012               </tr>
1013               <tr>
1014                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1015                 <td nowrap>|.$locale->text('Vendor').qq|</td>
1016                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1017                 <td nowrap>|.$locale->text('Invoice Date').qq|</td>
1018                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1019                 <td nowrap>|.$locale->text('Amount').qq|</td>
1020               </tr>
1021               <tr>
1022                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1023                 <td nowrap>|.$locale->text('Tax').qq|</td>
1024                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1025                 <td nowrap>|.$locale->text('Total').qq|</td>
1026                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1027                 <td nowrap>|.$locale->text('Date Paid').qq|</td>
1028               </tr>
1029               <tr>
1030                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1031                 <td nowrap>|.$locale->text('Paid').qq|</td>
1032                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1033                 <td nowrap>|.$locale->text('Due Date').qq|</td>
1034                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1035                 <td nowrap>|.$locale->text('Amount Due').qq|</td>
1036               </tr>
1037               <tr>
1038                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1039                 <td nowrap>|.$locale->text('Notes').qq|</td>
1040                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1041                 <td nowrap>|.$locale->text('Employee').qq|</td>
1042               </tr>
1043               <tr>
1044                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1045                 <td nowrap>|.$locale->text('Subtotal').qq|</td>
1046               </tr>
1047             </table>
1048           </td>
1049         </tr>
1050       </table>
1051     </td>
1052   </tr>
1053   <tr>
1054     <td><hr size=3 noshade></td>
1055   </tr>
1056 </table>
1057
1058 $jsscript
1059
1060 <br>
1061 <input type=hidden name=nextsub value=$form->{nextsub}>
1062 <input type=hidden name=path value=$form->{path}>
1063 <input type=hidden name=login value=$form->{login}>
1064 <input type=hidden name=password value=$form->{password}>
1065
1066 <input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
1067 </form>
1068
1069 </body>
1070 </html>
1071 |;
1072
1073   $lxdebug->leave_sub();
1074 }
1075
1076
1077 sub ap_transactions {
1078   $lxdebug->enter_sub();
1079
1080
1081   $form->{vendor} = $form->unescape($form->{vendor});
1082   ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
1083
1084   AP->ap_transactions(\%myconfig, \%$form);
1085
1086   $callback = "$form->{script}?action=ap_transactions&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1087   $href = $callback;
1088     
1089
1090   if ($form->{vendor}) {
1091     $callback .= "&vendor=".$form->escape($form->{vendor},1);
1092     $href .= "&vendor=".$form->escape($form->{vendor});
1093     $option .= $locale->text('Vendor')." : $form->{vendor}";
1094   }
1095   if ($form->{department}) {
1096     $callback .= "&department=".$form->escape($form->{department},1);
1097     $href .= "&department=".$form->escape($form->{department});
1098     ($department) = split /--/, $form->{department};
1099     $option .= "\n<br>" if ($option);
1100     $option .= $locale->text('Department')." : $department";
1101   }
1102   if ($form->{invnumber}) {
1103     $callback .= "&invnumber=".$form->escape($form->{invnumber},1);
1104     $href .= "&invnumber=".$form->escape($form->{invnumber});
1105     $option .= "\n<br>" if ($option);
1106     $option .= $locale->text('Invoice Number')." : $form->{invnumber}";
1107   }
1108   if ($form->{ordnumber}) {
1109     $callback .= "&ordnumber=".$form->escape($form->{ordnumber},1);
1110     $href .= "&ordnumber=".$form->escape($form->{ordnumber});
1111     $option .= "\n<br>" if ($option);
1112     $option .= $locale->text('Order Number')." : $form->{ordnumber}";
1113   }
1114   if ($form->{notes}) {
1115     $callback .= "&notes=".$form->escape($form->{notes},1);
1116     $href .= "&notes=".$form->escape($form->{notes});
1117     $option .= "\n<br>" if $option;
1118     $option .= $locale->text('Notes')." : $form->{notes}";
1119   }
1120   
1121   if ($form->{transdatefrom}) {
1122     $callback .= "&transdatefrom=$form->{transdatefrom}";
1123     $href .= "&transdatefrom=$form->{transdatefrom}";
1124     $option .= "\n<br>" if ($option);
1125     $option .= $locale->text('From')." ".$locale->date(\%myconfig, $form->{transdatefrom}, 1);
1126   }
1127   if ($form->{transdateto}) {
1128     $callback .= "&transdateto=$form->{transdateto}";
1129     $href .= "&transdateto=$form->{transdateto}";
1130     $option .= "\n<br>" if ($option);
1131     $option .= $locale->text('Bis')." ".$locale->date(\%myconfig, $form->{transdateto}, 1);
1132   }
1133   if ($form->{open}) {
1134     $callback .= "&open=$form->{open}";
1135     $href .= "&open=$form->{open}";
1136     $option .= "\n<br>" if ($option);
1137     $option .= $locale->text('Open');
1138   }
1139   if ($form->{closed}) {
1140     $callback .= "&closed=$form->{closed}";
1141     $href .= "&closed=$form->{closed}";
1142     $option .= "\n<br>" if ($option);
1143     $option .= $locale->text('Closed');
1144   }
1145
1146   @columns = $form->sort_columns(qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee));
1147
1148   foreach $item (@columns) {
1149     if ($form->{"l_$item"} eq "Y") {
1150       push @column_index, $item;
1151       
1152       # add column to href and callback
1153       $callback .= "&l_$item=Y";
1154       $href .= "&l_$item=Y";
1155     }
1156   }
1157   
1158   if ($form->{l_subtotal} eq 'Y') {
1159     $callback .= "&l_subtotal=Y";
1160     $href .= "&l_subtotal=Y";
1161   }
1162   
1163     
1164   $column_header{id} = qq|<th><a class=listheading href=$href&sort=id>|.$locale->text('ID').qq|</a></th>|;
1165   $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>|.$locale->text('Date').qq|</a></th>|;
1166   $column_header{duedate} = qq|<th><a class=listheading href=$href&sort=duedate>|.$locale->text('Due Date').qq|</a></th>|;
1167   $column_header{due} = qq|<th class=listheading>|.$locale->text('Amount Due').qq|</th>|;
1168   $column_header{invnumber} = qq|<th><a class=listheading href=$href&sort=invnumber>|.$locale->text('Invoice').qq|</a></th>|;
1169   $column_header{ordnumber} = qq|<th><a class=listheading href=$href&sort=ordnumber>|.$locale->text('Order').qq|</a></th>|;
1170   $column_header{name} = qq|<th><a class=listheading href=$href&sort=name>|.$locale->text('Vendor').qq|</a></th>|;
1171   $column_header{netamount} = qq|<th class=listheading>|.$locale->text('Amount').qq|</th>|;
1172   $column_header{tax} = qq|<th class=listheading>|.$locale->text('Tax').qq|</th>|;
1173   $column_header{amount} = qq|<th class=listheading>|.$locale->text('Total').qq|</th>|;
1174   $column_header{paid} = qq|<th class=listheading>|.$locale->text('Paid').qq|</th>|;
1175   $column_header{datepaid} = qq|<th><a class=listheading href=$href&sort=datepaid>|.$locale->text('Date Paid').qq|</a></th>|;
1176   $column_header{notes} = qq|<th class=listheading>|.$locale->text('Notes').qq|</th>|;
1177   $column_header{employee} = "<th><a class=listheading href=$href&sort=employee>".$locale->text('Employee')."</th>";
1178
1179   
1180   $form->{title} = $locale->text('AP Transactions');
1181
1182   $form->header;
1183   
1184   print qq|
1185 <body>
1186
1187 <table width=100%>
1188   <tr>
1189     <th class=listtop>$form->{title}</th>
1190   </tr>
1191   <tr height="5"></tr>
1192   <tr>
1193     <td>$option</td>
1194   </tr>
1195   <tr>
1196     <td>
1197       <table width=100%>
1198         <tr class=listheading>
1199 |;
1200
1201   map { print "\n$column_header{$_}" } @column_index;
1202
1203   print qq|
1204         </tr>
1205 |;
1206
1207   # add sort and escape callback
1208   $form->{callback} = "$callback&sort=$form->{sort}";
1209   $callback = $form->escape($form->{callback});
1210
1211   if (@{ $form->{AP} }) {
1212     $sameitem = $form->{AP}->[0]->{$form->{sort}};
1213   }
1214   
1215   # sums and tax on reports by Antonio Gallardo
1216   #
1217   foreach $ap (@{ $form->{AP} }) {
1218
1219     if ($form->{l_subtotal} eq 'Y') {
1220       if ($sameitem ne $ap->{$form->{sort}}) {
1221         &ap_subtotal;
1222         $sameitem = $ap->{$form->{sort}};
1223       }
1224     }
1225     
1226     $column_data{netamount} = "<td align=right>".$form->format_amount(\%myconfig, $ap->{netamount}, 2, "&nbsp;")."</td>";
1227     $column_data{tax} = "<td align=right>".$form->format_amount(\%myconfig, $ap->{amount} - $ap->{netamount}, 2, "&nbsp;") . "</td>";
1228     $column_data{amount} = "<td align=right>".$form->format_amount(\%myconfig, $ap->{amount}, 2, "&nbsp;") . "</td>";
1229     $column_data{paid} = "<td align=right>".$form->format_amount(\%myconfig, $ap->{paid}, 2, "&nbsp;")."</td>";
1230     $column_data{due} = "<td align=right>".$form->format_amount(\%myconfig, $ap->{amount} - $ap->{paid}, 2, "&nbsp;")."</td>";
1231
1232     $totalnetamount += $ap->{netamount};
1233     $totalamount += $ap->{amount};
1234     $totalpaid += $ap->{paid};
1235     $totaldue += ($ap->{amount} - $ap->{paid});
1236
1237     $subtotalnetamount += $ap->{netamount};
1238     $subtotalamount += $ap->{amount};
1239     $subtotalpaid += $ap->{paid};
1240     $subtotaldue += ($ap->{amount} - $ap->{paid});
1241
1242     $column_data{transdate} = "<td>$ap->{transdate}&nbsp;</td>";
1243     $column_data{duedate} = "<td>$ap->{duedate}&nbsp;</td>";
1244     $column_data{datepaid} = "<td>$ap->{datepaid}&nbsp;</td>";
1245
1246     $module = ($ap->{invoice}) ? "ir.pl" : $form->{script};
1247
1248     $column_data{invnumber} = 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>|;
1249     $column_data{id} = "<td>$ap->{id}</td>";
1250     $column_data{ordnumber} = "<td>$ap->{ordnumber}&nbsp;</td>";
1251     $column_data{name} = "<td>$ap->{name}</td>";
1252     $ap->{notes} =~ s/\r\n/<br>/g;
1253     $column_data{notes} = "<td>$ap->{notes}&nbsp;</td>";
1254     $column_data{employee} = "<td>$ap->{employee}&nbsp;</td>";
1255     
1256     $i++;
1257     $i %= 2;
1258     print "
1259         <tr class=listrow$i >
1260 ";
1261     
1262     map { print "\n$column_data{$_}" } @column_index;
1263
1264     print qq|
1265         </tr>
1266 |;
1267
1268   }
1269   
1270   if ($form->{l_subtotal} eq 'Y') {
1271     &ap_subtotal;
1272   }
1273   
1274   # print totals
1275   print qq|
1276         <tr class=listtotal>
1277 |;
1278   
1279   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1280   
1281   $column_data{netamount} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;")."</th>";
1282   $column_data{tax} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount - $totalnetamount, 2, "&nbsp;")."</th>";
1283   $column_data{amount} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;")."</th>";
1284   $column_data{paid} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;")."</th>";
1285   $column_data{due} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;")."</th>";
1286
1287   map { print "$column_data{$_}\n" } @column_index;
1288
1289   print qq|
1290         </tr>
1291       </table>
1292     </td>
1293   </tr>
1294   <tr>
1295     <td><hr size=3 noshade></td>
1296   </tr>  
1297 </table>
1298
1299 <br>
1300 <form method=post action=$form->{script}>
1301  
1302 <input name=callback type=hidden value="$form->{callback}">
1303   
1304 <input type=hidden name=path value=$form->{path}>
1305 <input type=hidden name=login value=$form->{login}>
1306 <input type=hidden name=password value=$form->{password}>
1307    
1308 <input class=submit type=submit name=action value="|.$locale->text('AP Transaction').qq|">
1309
1310 <input class=submit type=submit name=action value="|.$locale->text('Vendor Invoice').qq|">|;
1311
1312   if ($form->{menubar}) {
1313     require "$form->{path}/menu.pl";
1314     &menubar;
1315   }
1316
1317   print qq|
1318   </form>
1319
1320 </body>
1321 </html>
1322 |;
1323
1324   $lxdebug->leave_sub();
1325 }
1326
1327
1328 sub ap_subtotal {
1329   $lxdebug->enter_sub();
1330
1331
1332   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1333   
1334   $column_data{netamount} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")."</th>";
1335   $column_data{tax} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount, 2, "&nbsp;")."</th>";
1336   $column_data{amount} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;")."</th>";
1337   $column_data{paid} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;")."</th>";
1338   $column_data{due} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotaldue, 2, "&nbsp;")."</th>";
1339
1340   $subtotalnetamount = 0;
1341   $subtotalamount = 0;
1342   $subtotalpaid = 0;
1343   $subtotaldue = 0;
1344
1345   print "<tr class=listsubtotal>";
1346   
1347   map { print "\n$column_data{$_}" } @column_index;
1348
1349   print qq|
1350   </tr>
1351 |;
1352
1353   $lxdebug->leave_sub();
1354 }
1355
1356
1357