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