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