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