Merge von 732 aus unstable: Bugfix Splitbuchungen
[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">$tax</select></td>|;
572
573     print qq|
574         <tr>
575           <td width=50%><select name="AR_amount_$i">$selectAR_amount</select></td>
576           <td><input name="amount_$i" onChange="setTaxkey(this, $i)" size=10 value=$form->{"amount_$i"}></td>
577           <td><input name="tax_$i" size=10 value=$form->{"tax_$i"}></td>
578           <td><input type="checkbox" name="korrektur_$i" value="1"></td>
579           $tax
580           <td><input name="projectnumber_$i" size=20 value="$form->{"projectnumber_$i"}">
581               <input type=hidden name="project_id_$i" value=$form->{"project_id_$i"}>
582               <input type=hidden name="oldprojectnumber_$i" value="$form->{"oldprojectnumber_$i"}"></td>
583         </tr>
584 |;
585     $amount  = "";
586     $project = "";
587   }
588
589   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
590
591   print qq|
592         <tr>
593           <td colspan=6>
594             <hr noshade>
595           </td>
596         </tr>
597         <tr>
598           <td><select name=ARselected>$form->{selectAR}</select></td>
599           <input type=hidden name=AR value="$form->{AR}">
600           <th align=left>$form->{invtotal}</th>
601
602           <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
603           <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
604
605           <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
606
607           <td colspan=4></td>
608
609
610         </tr>
611         </table>
612         </td>
613     </tr>
614     <tr>
615       <td>
616         <table width=100%>
617         <tr>
618           <th align=left width=1%>| . $locale->text('Notes') . qq|</th>
619           <td align=left>$notes</td>
620         </tr>
621       </table>
622     </td>
623   </tr>
624   <tr>
625     <td>
626       <table width=100%>
627         <tr class=listheading>
628           <th colspan=6 class=listheading>|
629     . $locale->text('Incoming Payments') . qq|</th>
630         </tr>
631 |;
632
633   if ($form->{currency} eq $form->{defaultcurrency}) {
634     @column_index = qw(datepaid source memo paid AR_paid);
635   } else {
636     @column_index = qw(datepaid source memo paid exchangerate AR_paid);
637   }
638
639   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
640   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
641   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
642   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
643   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
644   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
645
646   print "
647         <tr>
648 ";
649   map { print "$column_data{$_}\n" } @column_index;
650   print "
651         </tr>
652 ";
653
654   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
655   for $i (1 .. $form->{paidaccounts}) {
656     print "
657         <tr>
658 ";
659
660     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
661     $form->{"selectAR_paid_$i"} =~
662       s/option value=\"$form->{"AR_paid_$i"}\">/option value=\"$form->{"AR_paid_$i"}\" selected>/;
663
664     # format amounts
665     $form->{"paid_$i"} =
666       $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
667     $form->{"exchangerate_$i"} =
668       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
669
670     $exchangerate = qq|&nbsp;|;
671     if ($form->{currency} ne $form->{defaultcurrency}) {
672       if ($form->{"forex_$i"}) {
673         $exchangerate =
674           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
675       } else {
676         $exchangerate =
677           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
678       }
679     }
680
681     $exchangerate .= qq|
682 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
683 |;
684
685     $column_data{paid} =
686       qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
687     $column_data{AR_paid} =
688       qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
689     $column_data{exchangerate} = qq|<td align=center>$exchangerate</td>|;
690     $column_data{datepaid}     =
691       qq|<td align=center><input name="datepaid_$i" size=11 value=$form->{"datepaid_$i"}></td>|;
692     $column_data{source} =
693       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
694     $column_data{memo} =
695       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
696
697     map { print qq|$column_data{$_}\n| } @column_index;
698
699     print "
700         </tr>
701 ";
702   }
703   map { $form->{$_} =~ s/\"/&quot;/g } qw(selectAR_paid);
704   print qq|
705 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
706 <input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
707
708       </table>
709     </td>
710   </tr>
711   <tr>
712     <td><hr size=3 noshade></td>
713   </tr>
714 </table>
715 |;
716
717   $lxdebug->leave_sub();
718 }
719
720 sub form_footer {
721   $lxdebug->enter_sub();
722
723   print qq|
724
725 <input name=callback type=hidden value="$form->{callback}">
726
727 <input type=hidden name=path value=$form->{path}>
728 <input type=hidden name=login value=$form->{login}>
729 <input type=hidden name=password value=$form->{password}>
730
731 <br>
732 |;
733
734   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
735   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
736
737   if ($form->{id} && $form->{radier}) {
738
739     print qq|<input class=submit type=submit name=action value="|
740       . $locale->text('Update') . qq|">
741 |;
742
743     if (!$form->{revtrans}) {
744       if (!$form->{locked}) {
745         print qq|
746         <input class=submit type=submit name=action value="|
747           . $locale->text('Post') . qq|">
748         <input class=submit type=submit name=action value="|
749           . $locale->text('Delete') . qq|">
750 |;
751       }
752     }
753
754     if ($transdate > $closedto) {
755       print qq|
756 <input class=submit type=submit name=action value="|
757         . $locale->text('Post as new') . qq|">
758 |;
759     }
760
761   } else {
762     if ($transdate > $closedto) {
763       print qq|<input class=submit type=submit name=action value="|
764         . $locale->text('Update') . qq|">
765       <input class=submit type=submit name=action value="|
766         . $locale->text('Post') . qq|">|;
767     }
768   }
769
770   if ($form->{menubar}) {
771     require "$form->{path}/menu.pl";
772     &menubar;
773   }
774
775   print "
776 </form>
777
778 </body>
779 </html>
780 ";
781
782   $lxdebug->leave_sub();
783 }
784
785 sub update {
786   $lxdebug->enter_sub();
787
788   my $display = shift;
789
790   #   if ($display) {
791   #     goto TAXCALC;
792   #   }
793
794   $form->{invtotal} = 0;
795
796   #   $form->{selectAR_amount} = $form->{AR_amount};
797   #   $form->{selectAR_amount} =~
798   #     s/value=\"$form->{AR_amountselected}\"/value=\"$form->{AR_amountselected}\" selected/;
799
800   $form->{selectAR} = $form->{AR};
801
802   $form->{selectAR} =~
803     s/value=\"$form->{ARselected}\"/value=\"$form->{ARselected}\" selected/;
804
805   ($AR_amountaccno, $AR_amounttaxkey) =
806     split(/--/, $form->{AR_amountselected});
807   $form->{selecttaxchart} = $form->{taxchart};
808   $form->{selecttaxchart} =~
809     s/value=\"$AR_amounttaxkey--([^\"]*)\"/value=\"$AR_amounttaxkey--$1\" selected/;
810
811   $form->{rate} = $1;
812
813   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
814     qw(exchangerate creditlimit creditremaining);
815
816   @flds  = qw(amount AR_amount projectnumber oldprojectnumber project_id);
817   $count = 0;
818   @a     = ();
819
820   for $i (1 .. $form->{rowcount}) {
821     $form->{"amount_$i"} =
822       $form->parse_amount(\%myconfig, $form->{"amount_$i"});
823     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
824     if ($form->{"amount_$i"}) {
825       push @a, {};
826       $j = $#a;
827       if (!$form->{"korrektur_$i"}) {
828         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
829         if ($taxkey > 1) {
830           if ($form->{taxincluded}) {
831             $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
832           } else {
833             $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
834           }
835         } else {
836           $form->{"tax_$i"} = 0;
837         }
838       }
839       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
840
841       $totaltax += $form->{"tax_$i"};
842       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
843       $count++;
844     }
845   }
846
847   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
848   $form->{rowcount} = $count + 1;
849   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
850
851   $form->{exchangerate} = $exchangerate
852     if (
853         $form->{forex} = (
854                      $exchangerate =
855                        $form->check_exchangerate(
856                        \%myconfig, $form->{currency}, $form->{transdate}, 'buy'
857                        )));
858
859   $form->{invdate} = $form->{transdate};
860   $save_AR = $form->{AR};
861   &check_name(customer);
862   $form->{AR} = $save_AR;
863
864   &check_project;
865
866   $form->{invtotal} =
867     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
868
869   for $i (1 .. $form->{paidaccounts}) {
870     if ($form->{"paid_$i"}) {
871       map {
872         $form->{"${_}_$i"} =
873           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
874       } qw(paid exchangerate);
875
876       $totalpaid += $form->{"paid_$i"};
877
878       $form->{"exchangerate_$i"} = $exchangerate
879         if (
880             $form->{"forex_$i"} = (
881                  $exchangerate =
882                    $form->check_exchangerate(
883                    \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy'
884                    )));
885     }
886   }
887
888   $form->{creditremaining} -=
889     ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
890      $form->{oldinvtotal});
891   $form->{oldinvtotal}  = $form->{invtotal};
892   $form->{oldtotalpaid} = $totalpaid;
893
894   &display_form;
895
896   $lxdebug->leave_sub();
897 }
898
899 sub post {
900   $lxdebug->enter_sub();
901
902   # check if there is an invoice number, invoice and due date
903   $form->isblank("transdate", $locale->text('Invoice Date missing!'));
904   $form->isblank("duedate",   $locale->text('Due Date missing!'));
905   $form->isblank("customer",  $locale->text('Customer missing!'));
906
907   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
908   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
909
910   $form->error($locale->text('Cannot post transaction for a closed period!'))
911     if ($transdate <= $closedto);
912
913   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
914     if ($form->{currency} ne $form->{defaultcurrency});
915
916   delete($form->{AR});
917
918   for $i (1 .. $form->{paidaccounts}) {
919     if ($form->{"paid_$i"}) {
920       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
921
922       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
923
924       $form->error($locale->text('Cannot post payment for a closed period!'))
925         if ($datepaid <= $closedto);
926
927       if ($form->{currency} ne $form->{defaultcurrency}) {
928         $form->{"exchangerate_$i"} = $form->{exchangerate}
929           if ($transdate == $datepaid);
930         $form->isblank("exchangerate_$i",
931                        $locale->text('Exchangerate for payment missing!'));
932       }
933     }
934   }
935
936   # if oldcustomer ne customer redo form
937   ($customer) = split /--/, $form->{customer};
938   if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
939     &update;
940     exit;
941   }
942
943   ($creditaccno, $credittaxkey) = split /--/, $form->{AR_amountselected};
944   ($taxkey,      $NULL)         = split /--/, $form->{taxchartselected};
945   ($receivablesaccno, $payablestaxkey) = split /--/, $form->{ARselected};
946   $form->{AR}{amount_1}    = $creditaccno;
947   $form->{AR}{receivables} = $receivablesaccno;
948   $form->{taxkey}          = $taxkey;
949
950   $form->{invnumber} = $form->update_defaults(\%myconfig, "invnumber")
951     unless $form->{invnumber};
952
953   $form->{id} = 0 if $form->{postasnew};
954
955   $form->redirect($locale->text('Transaction posted!'))
956     if (AR->post_transaction(\%myconfig, \%$form));
957   $form->error($locale->text('Cannot post transaction!'));
958
959   $lxdebug->leave_sub();
960 }
961
962 sub post_as_new {
963   $lxdebug->enter_sub();
964
965   $form->{postasnew} = 1;
966   &post;
967
968   $lxdebug->leave_sub();
969 }
970
971 sub delete {
972   $lxdebug->enter_sub();
973
974   $form->{title} = $locale->text('Confirm!');
975
976   $form->header;
977
978   delete $form->{header};
979
980   print qq|
981 <body>
982
983 <form method=post action=$form->{script}>
984 |;
985
986   foreach $key (keys %$form) {
987     $form->{$key} =~ s/\"/&quot;/g;
988     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
989   }
990
991   print qq|
992 <h2 class=confirm>$form->{title}</h2>
993
994 <h4>|
995     . $locale->text('Are you sure you want to delete Transaction')
996     . qq| $form->{invnumber}</h4>
997
998 <input name=action class=submit type=submit value="|
999     . $locale->text('Yes') . qq|">
1000 </form>
1001
1002 </body>
1003 </html>
1004 |;
1005
1006   $lxdebug->leave_sub();
1007 }
1008
1009 sub yes {
1010   $lxdebug->enter_sub();
1011
1012   $form->redirect($locale->text('Transaction deleted!'))
1013     if (AR->delete_transaction(\%myconfig, \%$form, $spool));
1014   $form->error($locale->text('Cannot delete transaction!'));
1015
1016   $lxdebug->leave_sub();
1017 }
1018
1019 sub search {
1020   $lxdebug->enter_sub();
1021
1022   # setup customer selection
1023   $form->all_vc(\%myconfig, "customer", "AR");
1024
1025   if (@{ $form->{all_customer} }) {
1026     map { $customer .= "<option>$_->{name}--$_->{id}\n" }
1027       @{ $form->{all_customer} };
1028     $customer = qq|<select name=customer><option>\n$customer</select>|;
1029   } else {
1030     $customer = qq|<input name=customer size=35>|;
1031   }
1032
1033   # departments
1034   if (@{ $form->{all_departments} }) {
1035     $form->{selectdepartment} = "<option>\n";
1036
1037     map {
1038       $form->{selectdepartment} .=
1039         "<option>$_->{description}--$_->{id}\n"
1040     } (@{ $form->{all_departments} });
1041   }
1042
1043   $department = qq|
1044         <tr>
1045           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1046           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1047         </tr>
1048 | if $form->{selectdepartment};
1049
1050   $form->{title} = $locale->text('AR Transactions');
1051
1052   # use JavaScript Calendar or not
1053   $form->{jsscript} = $jscalendar;
1054   $jsscript = "";
1055   if ($form->{jsscript}) {
1056
1057     # with JavaScript Calendar
1058     $button1 = qq|
1059        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
1060        <input type=button name=transdatefrom id="trigger1" value=|
1061       . $locale->text('button') . qq|></td>
1062       |;
1063     $button2 = qq|
1064        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
1065        <input type=button name=transdateto name=transdateto id="trigger2" value=|
1066       . $locale->text('button') . qq|></td>
1067      |;
1068
1069     #write Trigger
1070     $jsscript =
1071       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
1072                           "transdateto", "BL", "trigger2");
1073   } else {
1074
1075     # without JavaScript Calendar
1076     $button1 = qq|
1077                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
1078     $button2 = qq|
1079                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
1080   }
1081
1082   $form->{fokus} = "search.customer";
1083   $form->header;
1084
1085   print qq|
1086 <body onLoad="fokus()">
1087
1088 <form method=post name="search" action=$form->{script}>
1089
1090 <table width=100%>
1091   <tr><th class=listtop>$form->{title}</th></tr>
1092   <tr height="5"></tr>
1093   <tr>
1094     <td>
1095       <table>
1096         <tr>
1097           <th align=right>| . $locale->text('Customer') . qq|</th>
1098           <td colspan=3>$customer</td>
1099         </tr>
1100         $department
1101         <tr>
1102           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
1103           <td colspan=3><input name=invnumber size=20></td>
1104         </tr>
1105         <tr>
1106           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
1107           <td colspan=3><input name=ordnumber size=20></td>
1108         </tr>
1109         <tr>
1110           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
1111           <td colspan=3><input name=notes size=40></td>
1112         </tr>
1113         <tr>
1114           <th align=right nowrap>| . $locale->text('From') . qq|</th>
1115           $button1
1116           <th align=right>| . $locale->text('Bis') . qq|</th>
1117           $button2
1118         </tr>
1119         <input type=hidden name=sort value=transdate>
1120       </table>
1121     </td>
1122   </tr>
1123   <tr>
1124     <td>
1125       <table>
1126         <tr>
1127           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
1128           <td>
1129             <table width=100%>
1130               <tr>
1131                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1132                 <td nowrap>| . $locale->text('Open') . qq|</td>
1133                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1134                 <td nowrap>| . $locale->text('Closed') . qq|</td>
1135               </tr>
1136               <tr>
1137                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1138                 <td nowrap>| . $locale->text('ID') . qq|</td>
1139                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1140                 <td nowrap>| . $locale->text('Invoice Number') . qq|</td>
1141                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1142                 <td nowrap>| . $locale->text('Order Number') . qq|</td>
1143                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1144                 <td nowrap>| . $locale->text('Invoice Date') . qq|</td>
1145               </tr>
1146               <tr>
1147                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1148                 <td nowrap>| . $locale->text('Customer') . qq|</td>
1149                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1150                 <td nowrap>| . $locale->text('Amount') . qq|</td>
1151                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1152                 <td nowrap>| . $locale->text('Tax') . qq|</td>
1153                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1154                 <td nowrap>| . $locale->text('Total') . qq|</td>
1155               </tr>
1156               <tr>
1157                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1158                 <td nowrap>| . $locale->text('Date Paid') . qq|</td>
1159                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1160                 <td nowrap>| . $locale->text('Paid') . qq|</td>
1161                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1162                 <td nowrap>| . $locale->text('Due Date') . qq|</td>
1163                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1164                 <td nowrap>| . $locale->text('Amount Due') . qq|</td>
1165               </tr>
1166               <tr>
1167                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1168                 <td nowrap>| . $locale->text('Notes') . qq|</td>
1169                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1170                 <td nowrap>| . $locale->text('Salesperson') . qq|</td>
1171                 <td align=right><input name="l_shippingpoint" class=checkbox type=checkbox value=Y></td>
1172                 <td nowrap>| . $locale->text('Shipping Point') . qq|</td>
1173                 <td align=right><input name="l_shipvia" class=checkbox type=checkbox value=Y></td>
1174                 <td nowrap>| . $locale->text('Ship via') . qq|</td>
1175               </tr>
1176               <tr>
1177                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1178                 <td nowrap>| . $locale->text('Subtotal') . qq|</td>
1179               </tr>
1180             </table>
1181           </td>
1182         </tr>
1183       </table>
1184     </td>
1185   </tr>
1186   <tr>
1187     <td><hr size=3 noshade></td>
1188   </tr>
1189 </table>
1190
1191 <input type=hidden name=nextsub value=$form->{nextsub}>
1192
1193 <input type=hidden name=path value=$form->{path}>
1194 <input type=hidden name=login value=$form->{login}>
1195 <input type=hidden name=password value=$form->{password}>
1196
1197 <br>
1198 <input class=submit type=submit name=action value="|
1199     . $locale->text('Continue') . qq|">
1200
1201 </form>
1202
1203 </body>
1204
1205 $jsscript
1206
1207 </html>
1208 |;
1209
1210   $lxdebug->leave_sub();
1211 }
1212
1213 sub ar_transactions {
1214   $lxdebug->enter_sub();
1215
1216   $form->{customer} = $form->unescape($form->{customer});
1217   ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
1218
1219   AR->ar_transactions(\%myconfig, \%$form);
1220
1221   $callback =
1222     "$form->{script}?action=ar_transactions&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1223   $href = $callback;
1224
1225   if ($form->{customer}) {
1226     $callback .= "&customer=" . $form->escape($form->{customer}, 1);
1227     $href .= "&customer=" . $form->escape($form->{customer});
1228     $option = $locale->text('Customer') . " : $form->{customer}";
1229   }
1230   if ($form->{department}) {
1231     $callback .= "&department=" . $form->escape($form->{department}, 1);
1232     $href .= "&department=" . $form->escape($form->{department});
1233     ($department) = split /--/, $form->{department};
1234     $option .= "\n<br>" if ($option);
1235     $option .= $locale->text('Department') . " : $department";
1236   }
1237   if ($form->{invnumber}) {
1238     $callback .= "&invnumber=" . $form->escape($form->{invnumber}, 1);
1239     $href .= "&invnumber=" . $form->escape($form->{invnumber});
1240     $option .= "\n<br>" if ($option);
1241     $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
1242   }
1243   if ($form->{ordnumber}) {
1244     $callback .= "&ordnumber=" . $form->escape($form->{ordnumber}, 1);
1245     $href .= "&ordnumber=" . $form->escape($form->{ordnumber});
1246     $option .= "\n<br>" if ($option);
1247     $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
1248   }
1249   if ($form->{notes}) {
1250     $callback .= "&notes=" . $form->escape($form->{notes}, 1);
1251     $href .= "&notes=" . $form->escape($form->{notes});
1252     $option .= "\n<br>" if $option;
1253     $option .= $locale->text('Notes') . " : $form->{notes}";
1254   }
1255
1256   if ($form->{transdatefrom}) {
1257     $callback .= "&transdatefrom=$form->{transdatefrom}";
1258     $href     .= "&transdatefrom=$form->{transdatefrom}";
1259     $option   .= "\n<br>" if ($option);
1260     $option   .=
1261         $locale->text('From') . "&nbsp;"
1262       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1263   }
1264   if ($form->{transdateto}) {
1265     $callback .= "&transdateto=$form->{transdateto}";
1266     $href     .= "&transdateto=$form->{transdateto}";
1267     $option   .= "\n<br>" if ($option);
1268     $option   .=
1269         $locale->text('Bis') . "&nbsp;"
1270       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1271   }
1272   if ($form->{open}) {
1273     $callback .= "&open=$form->{open}";
1274     $href     .= "&open=$form->{open}";
1275     $option   .= "\n<br>" if ($option);
1276     $option   .= $locale->text('Open');
1277   }
1278   if ($form->{closed}) {
1279     $callback .= "&closed=$form->{closed}";
1280     $href     .= "&closed=$form->{closed}";
1281     $option   .= "\n<br>" if ($option);
1282     $option   .= $locale->text('Closed');
1283   }
1284
1285   @columns = $form->sort_columns(
1286     qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee shippingpoint shipvia)
1287   );
1288
1289   foreach $item (@columns) {
1290     if ($form->{"l_$item"} eq "Y") {
1291       push @column_index, $item;
1292
1293       # add column to href and callback
1294       $callback .= "&l_$item=Y";
1295       $href     .= "&l_$item=Y";
1296     }
1297   }
1298
1299   if ($form->{l_subtotal} eq 'Y') {
1300     $callback .= "&l_subtotal=Y";
1301     $href     .= "&l_subtotal=Y";
1302   }
1303
1304   $column_header{id} =
1305       "<th><a class=listheading href=$href&sort=id>"
1306     . $locale->text('ID')
1307     . "</a></th>";
1308   $column_header{transdate} =
1309       "<th><a class=listheading href=$href&sort=transdate>"
1310     . $locale->text('Date')
1311     . "</a></th>";
1312   $column_header{duedate} =
1313       "<th><a class=listheading href=$href&sort=duedate>"
1314     . $locale->text('Due Date')
1315     . "</a></th>";
1316   $column_header{invnumber} =
1317       "<th><a class=listheading href=$href&sort=invnumber>"
1318     . $locale->text('Invoice')
1319     . "</a></th>";
1320   $column_header{ordnumber} =
1321       "<th><a class=listheading href=$href&sort=ordnumber>"
1322     . $locale->text('Order')
1323     . "</a></th>";
1324   $column_header{name} =
1325       "<th><a class=listheading href=$href&sort=name>"
1326     . $locale->text('Customer')
1327     . "</a></th>";
1328   $column_header{netamount} =
1329     "<th class=listheading>" . $locale->text('Amount') . "</th>";
1330   $column_header{tax} =
1331     "<th class=listheading>" . $locale->text('Tax') . "</th>";
1332   $column_header{amount} =
1333     "<th class=listheading>" . $locale->text('Total') . "</th>";
1334   $column_header{paid} =
1335     "<th class=listheading>" . $locale->text('Paid') . "</th>";
1336   $column_header{datepaid} =
1337       "<th><a class=listheading href=$href&sort=datepaid>"
1338     . $locale->text('Date Paid')
1339     . "</a></th>";
1340   $column_header{due} =
1341     "<th class=listheading>" . $locale->text('Amount Due') . "</th>";
1342   $column_header{notes} =
1343     "<th class=listheading>" . $locale->text('Notes') . "</th>";
1344   $column_header{employee} =
1345     "<th><a class=listheading href=$href&sort=employee>"
1346     . $locale->text('Salesperson') . "</th>";
1347
1348   $column_header{shippingpoint} =
1349       "<th><a class=listheading href=$href&sort=shippingpoint>"
1350     . $locale->text('Shipping Point')
1351     . "</a></th>";
1352   $column_header{shipvia} =
1353       "<th><a class=listheading href=$href&sort=shipvia>"
1354     . $locale->text('Ship via')
1355     . "</a></th>";
1356
1357   $form->{title} = $locale->text('AR Transactions');
1358
1359   $form->header;
1360
1361   print qq|
1362 <body>
1363
1364 <table width=100%>
1365   <tr>
1366     <th class=listtop>$form->{title}</th>
1367   </tr>
1368   <tr height="5"></tr>
1369   <tr>
1370     <td>$option</td>
1371   </tr>
1372   <tr>
1373     <td>
1374       <table width=100%>
1375         <tr class=listheading>
1376 |;
1377
1378   map { print "\n$column_header{$_}" } @column_index;
1379
1380   print qq|
1381         </tr>
1382 |;
1383
1384   # add sort and escape callback, this one we use for the add sub
1385   $form->{callback} = $callback .= "&sort=$form->{sort}";
1386
1387   # escape callback for href
1388   $callback = $form->escape($callback);
1389
1390   if (@{ $form->{AR} }) {
1391     $sameitem = $form->{AR}->[0]->{ $form->{sort} };
1392   }
1393
1394   # sums and tax on reports by Antonio Gallardo
1395   #
1396   foreach $ar (@{ $form->{AR} }) {
1397
1398     if ($form->{l_subtotal} eq 'Y') {
1399       if ($sameitem ne $ar->{ $form->{sort} }) {
1400         &ar_subtotal;
1401       }
1402     }
1403
1404     $column_data{netamount} =
1405         "<td align=right>"
1406       . $form->format_amount(\%myconfig, $ar->{netamount}, 2, "&nbsp;")
1407       . "</td>";
1408     $column_data{tax} = "<td align=right>"
1409       . $form->format_amount(\%myconfig, $ar->{amount} - $ar->{netamount},
1410                              2, "&nbsp;")
1411       . "</td>";
1412     $column_data{amount} =
1413       "<td align=right>"
1414       . $form->format_amount(\%myconfig, $ar->{amount}, 2, "&nbsp;") . "</td>";
1415     $column_data{paid} =
1416       "<td align=right>"
1417       . $form->format_amount(\%myconfig, $ar->{paid}, 2, "&nbsp;") . "</td>";
1418     $column_data{due} = "<td align=right>"
1419       . $form->format_amount(\%myconfig, $ar->{amount} - $ar->{paid},
1420                              2, "&nbsp;")
1421       . "</td>";
1422
1423     $subtotalnetamount += $ar->{netamount};
1424     $subtotalamount    += $ar->{amount};
1425     $subtotalpaid      += $ar->{paid};
1426     $subtotaldue       += $ar->{amount} - $ar->{paid};
1427
1428     $totalnetamount += $ar->{netamount};
1429     $totalamount    += $ar->{amount};
1430     $totalpaid      += $ar->{paid};
1431     $totaldue       += ($ar->{amount} - $ar->{paid});
1432
1433     $column_data{transdate} = "<td>$ar->{transdate}&nbsp;</td>";
1434     $column_data{id}        = "<td>$ar->{id}</td>";
1435     $column_data{datepaid}  = "<td>$ar->{datepaid}&nbsp;</td>";
1436     $column_data{duedate}   = "<td>$ar->{duedate}&nbsp;</td>";
1437
1438     $module = ($ar->{invoice}) ? "is.pl" : $form->{script};
1439
1440     $column_data{invnumber} =
1441       "<td><a href=$module?action=edit&id=$ar->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ar->{invnumber}</a></td>";
1442     $column_data{ordnumber} = "<td>$ar->{ordnumber}&nbsp;</td>";
1443     $column_data{name}      = "<td>$ar->{name}</td>";
1444     $ar->{notes} =~ s/\r\n/<br>/g;
1445     $column_data{notes}         = "<td>$ar->{notes}&nbsp;</td>";
1446     $column_data{shippingpoint} = "<td>$ar->{shippingpoint}&nbsp;</td>";
1447     $column_data{shipvia}       = "<td>$ar->{shipvia}&nbsp;</td>";
1448     $column_data{employee}      = "<td>$ar->{employee}&nbsp;</td>";
1449
1450     $i++;
1451     $i %= 2;
1452     print "
1453         <tr class=listrow$i>
1454 ";
1455
1456     map { print "\n$column_data{$_}" } @column_index;
1457
1458     print qq|
1459         </tr>
1460 |;
1461
1462   }
1463
1464   if ($form->{l_subtotal} eq 'Y') {
1465     &ar_subtotal;
1466   }
1467
1468   # print totals
1469   print qq|
1470         <tr class=listtotal>
1471 |;
1472
1473   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1474
1475   $column_data{netamount} =
1476     "<th class=listtotal align=right>"
1477     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1478   $column_data{tax} = "<th class=listtotal align=right>"
1479     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1480                            2, "&nbsp;")
1481     . "</th>";
1482   $column_data{amount} =
1483     "<th class=listtotal align=right>"
1484     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1485   $column_data{paid} =
1486     "<th class=listtotal align=right>"
1487     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;") . "</th>";
1488   $column_data{due} =
1489     "<th class=listtotal align=right>"
1490     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;") . "</th>";
1491
1492   map { print "\n$column_data{$_}" } @column_index;
1493
1494   print qq|
1495         </tr>
1496       </table>
1497     </td>
1498   </tr>
1499   <tr>
1500     <td><hr size=3 noshade></td>
1501   </tr>
1502 </table>
1503
1504 <br>
1505 <form method=post action=$form->{script}>
1506
1507 <input name=callback type=hidden value="$form->{callback}">
1508
1509 <input type=hidden name=path value=$form->{path}>
1510 <input type=hidden name=login value=$form->{login}>
1511 <input type=hidden name=password value=$form->{password}>
1512
1513 <input class=submit type=submit name=action value="|
1514     . $locale->text('AR Transaction') . qq|">
1515 <input class=submit type=submit name=action value="|
1516     . $locale->text('Sales Invoice') . qq|">|;
1517
1518   if ($form->{menubar}) {
1519     require "$form->{path}/menu.pl";
1520     &menubar;
1521   }
1522
1523   print qq|
1524 </form>
1525
1526 </body>
1527 </html>
1528 |;
1529
1530   $lxdebug->leave_sub();
1531 }
1532
1533 sub ar_subtotal {
1534   $lxdebug->enter_sub();
1535
1536   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1537
1538   $column_data{tax} = "<th class=listsubtotal align=right>"
1539     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1540                            2, "&nbsp;")
1541     . "</th>";
1542   $column_data{amount} =
1543     "<th class=listsubtotal align=right>"
1544     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1545   $column_data{paid} =
1546     "<th class=listsubtotal align=right>"
1547     . $form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;") . "</th>";
1548   $column_data{due} =
1549     "<th class=listsubtotal align=right>"
1550     . $form->format_amount(\%myconfig, $subtotaldue, 2, "&nbsp;") . "</th>";
1551
1552   $subtotalnetamount = 0;
1553   $subtotalamount    = 0;
1554   $subtotalpaid      = 0;
1555   $subtotaldue       = 0;
1556
1557   $sameitem = $ar->{ $form->{sort} };
1558
1559   print "<tr class=listsubtotal>";
1560
1561   map { print "\n$column_data{$_}" } @column_index;
1562
1563   print "
1564 </tr>
1565 ";
1566
1567   $lxdebug->leave_sub();
1568 }