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