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