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