Globale Projektnummer in Einkaufs- und Verkaufsbelegen.
[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" id="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         print qq|
775   <input class=submit type=submit name=action value="|
776           . $locale->text('Post Payment') . qq|">
777   |;
778     
779   } else {
780     if ($transdate > $closedto) {
781       print qq|<input class=submit type=submit name=action value="|
782         . $locale->text('Update') . qq|">
783       <input class=submit type=submit name=action value="|
784         . $locale->text('Post') . qq|">|;
785     }
786   }
787
788   print "
789 </form>
790
791 </body>
792 </html>
793 ";
794
795   $lxdebug->leave_sub();
796 }
797
798 sub update {
799   $lxdebug->enter_sub();
800
801   my $display = shift;
802
803   #   if ($display) {
804   #     goto TAXCALC;
805   #   }
806
807   $form->{invtotal} = 0;
808
809   #   $form->{selectAR_amount} = $form->{AR_amount};
810   #   $form->{selectAR_amount} =~
811   #     s/value=\"$form->{AR_amountselected}\"/value=\"$form->{AR_amountselected}\" selected/;
812
813   ($AR_amountaccno, $AR_amounttaxkey) =
814     split(/--/, $form->{AR_amountselected});
815   $form->{selecttaxchart} = $form->{taxchart};
816   $form->{selecttaxchart} =~
817     s/value=\"$AR_amounttaxkey--([^\"]*)\"/value=\"$AR_amounttaxkey--$1\" selected/;
818
819   $form->{rate} = $1;
820
821   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
822     qw(exchangerate creditlimit creditremaining);
823
824   @flds  = qw(amount AR_amount projectnumber oldprojectnumber project_id);
825   $count = 0;
826   @a     = ();
827
828   for $i (1 .. $form->{rowcount}) {
829     $form->{"amount_$i"} =
830       $form->parse_amount(\%myconfig, $form->{"amount_$i"});
831     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
832     if ($form->{"amount_$i"}) {
833       push @a, {};
834       $j = $#a;
835       if (!$form->{"korrektur_$i"}) {
836         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
837         if ($taxkey > 1) {
838           if ($form->{taxincluded}) {
839             $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
840           } else {
841             $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
842           }
843         } else {
844           $form->{"tax_$i"} = 0;
845         }
846       }
847       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
848
849       $totaltax += $form->{"tax_$i"};
850       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
851       $count++;
852     }
853   }
854
855   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
856   $form->{rowcount} = $count + 1;
857   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
858
859   $form->{exchangerate} = $exchangerate
860     if (
861         $form->{forex} = (
862                      $exchangerate =
863                        $form->check_exchangerate(
864                        \%myconfig, $form->{currency}, $form->{transdate}, 'buy'
865                        )));
866
867   $form->{invdate} = $form->{transdate};
868   $save_AR = $form->{AR};
869   &check_name(customer);
870   $form->{AR} = $save_AR;
871
872   &check_project;
873
874   $form->{invtotal} =
875     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
876
877   for $i (1 .. $form->{paidaccounts}) {
878     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
879       map {
880         $form->{"${_}_$i"} =
881           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
882       } qw(paid exchangerate);
883
884       $totalpaid += $form->{"paid_$i"};
885
886       $form->{"exchangerate_$i"} = $exchangerate
887         if (
888             $form->{"forex_$i"} = (
889                  $exchangerate =
890                    $form->check_exchangerate(
891                    \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy'
892                    )));
893     }
894   }
895
896   $form->{creditremaining} -=
897     ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
898      $form->{oldinvtotal});
899   $form->{oldinvtotal}  = $form->{invtotal};
900   $form->{oldtotalpaid} = $totalpaid;
901
902   &display_form;
903
904   $lxdebug->leave_sub();
905 }
906
907 sub post_payment {
908   $lxdebug->enter_sub();
909   for $i (1 .. $form->{paidaccounts}) {
910     if ($form->{"paid_$i"}) {
911       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
912
913       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
914
915       $form->error($locale->text('Cannot post payment for a closed period!'))
916         if ($datepaid <= $closedto);
917
918       if ($form->{currency} ne $form->{defaultcurrency}) {
919         $form->{"exchangerate_$i"} = $form->{exchangerate}
920           if ($invdate == $datepaid);
921         $form->isblank("exchangerate_$i",
922                        $locale->text('Exchangerate for payment missing!'));
923       }
924     }
925   }
926
927   ($form->{AR})      = split /--/, $form->{AR};
928   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
929   $form->redirect($locale->text(' Payment posted!'))
930       if (AR->post_payment(\%myconfig, \%$form));
931     $form->error($locale->text('Cannot post payment!'));
932
933
934   $lxdebug->leave_sub();
935 }
936
937 sub post {
938   $lxdebug->enter_sub();
939
940   # check if there is an invoice number, invoice and due date
941   $form->isblank("transdate", $locale->text('Invoice Date missing!'));
942   $form->isblank("duedate",   $locale->text('Due Date missing!'));
943   $form->isblank("customer",  $locale->text('Customer missing!'));
944
945   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
946   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
947
948   $form->error($locale->text('Cannot post transaction for a closed period!'))
949     if ($transdate <= $closedto);
950
951   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
952     if ($form->{currency} ne $form->{defaultcurrency});
953
954   delete($form->{AR});
955
956   for $i (1 .. $form->{paidaccounts}) {
957     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
958       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
959
960       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
961
962       $form->error($locale->text('Cannot post payment for a closed period!'))
963         if ($datepaid <= $closedto);
964
965       if ($form->{currency} ne $form->{defaultcurrency}) {
966         $form->{"exchangerate_$i"} = $form->{exchangerate}
967           if ($transdate == $datepaid);
968         $form->isblank("exchangerate_$i",
969                        $locale->text('Exchangerate for payment missing!'));
970       }
971     }
972   }
973
974   # if oldcustomer ne customer redo form
975   ($customer) = split /--/, $form->{customer};
976   if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
977     &update;
978     exit;
979   }
980
981   ($creditaccno, $credittaxkey) = split /--/, $form->{AR_amountselected};
982   ($taxkey,      $NULL)         = split /--/, $form->{taxchartselected};
983   ($receivablesaccno, $payablestaxkey) = split /--/, $form->{ARselected};
984   $form->{AR}{amount_1}    = $creditaccno;
985   $form->{AR}{receivables} = $receivablesaccno;
986   $form->{taxkey}          = $taxkey;
987
988   $form->{invnumber} = $form->update_defaults(\%myconfig, "invnumber")
989     unless $form->{invnumber};
990
991   $form->{id} = 0 if $form->{postasnew};
992
993   $form->redirect($locale->text('Transaction posted!'))
994     if (AR->post_transaction(\%myconfig, \%$form));
995   $form->error($locale->text('Cannot post transaction!'));
996
997   $lxdebug->leave_sub();
998 }
999
1000 sub post_as_new {
1001   $lxdebug->enter_sub();
1002
1003   $form->{postasnew} = 1;
1004   &post;
1005
1006   $lxdebug->leave_sub();
1007 }
1008
1009 sub use_as_template {
1010   $lxdebug->enter_sub();
1011
1012   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);
1013   $form->{paidaccounts} = 1;
1014   $form->{rowcount}--;
1015   $form->{invdate} = $form->current_date(\%myconfig);
1016   &update;
1017
1018   $lxdebug->leave_sub();
1019 }
1020
1021 sub delete {
1022   $lxdebug->enter_sub();
1023
1024   $form->{title} = $locale->text('Confirm!');
1025
1026   $form->header;
1027
1028   delete $form->{header};
1029
1030   print qq|
1031 <body>
1032
1033 <form method=post action=$form->{script}>
1034 |;
1035
1036   foreach $key (keys %$form) {
1037     $form->{$key} =~ s/\"/&quot;/g;
1038     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1039   }
1040
1041   print qq|
1042 <h2 class=confirm>$form->{title}</h2>
1043
1044 <h4>|
1045     . $locale->text('Are you sure you want to delete Transaction')
1046     . qq| $form->{invnumber}</h4>
1047
1048 <input name=action class=submit type=submit value="|
1049     . $locale->text('Yes') . qq|">
1050 </form>
1051
1052 </body>
1053 </html>
1054 |;
1055
1056   $lxdebug->leave_sub();
1057 }
1058
1059 sub yes {
1060   $lxdebug->enter_sub();
1061
1062   $form->redirect($locale->text('Transaction deleted!'))
1063     if (AR->delete_transaction(\%myconfig, \%$form, $spool));
1064   $form->error($locale->text('Cannot delete transaction!'));
1065
1066   $lxdebug->leave_sub();
1067 }
1068
1069 sub search {
1070   $lxdebug->enter_sub();
1071
1072   # setup customer selection
1073   $form->all_vc(\%myconfig, "customer", "AR");
1074
1075   if (@{ $form->{all_customer} }) {
1076     map { $customer .= "<option>$_->{name}--$_->{id}\n" }
1077       @{ $form->{all_customer} };
1078     $customer = qq|<select name=customer><option>\n$customer</select>|;
1079   } else {
1080     $customer = qq|<input name=customer size=35>|;
1081   }
1082
1083   # departments
1084   if (@{ $form->{all_departments} }) {
1085     $form->{selectdepartment} = "<option>\n";
1086
1087     map {
1088       $form->{selectdepartment} .=
1089         "<option>$_->{description}--$_->{id}\n"
1090     } (@{ $form->{all_departments} });
1091   }
1092
1093   $department = qq|
1094         <tr>
1095           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1096           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1097         </tr>
1098 | if $form->{selectdepartment};
1099
1100   $form->{title} = $locale->text('AR Transactions');
1101
1102   # use JavaScript Calendar or not
1103   $form->{jsscript} = $jscalendar;
1104   $jsscript = "";
1105   if ($form->{jsscript}) {
1106
1107     # with JavaScript Calendar
1108     $button1 = qq|
1109        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
1110        <input type=button name=transdatefrom id="trigger1" value=|
1111       . $locale->text('button') . qq|></td>
1112       |;
1113     $button2 = qq|
1114        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
1115        <input type=button name=transdateto name=transdateto id="trigger2" value=|
1116       . $locale->text('button') . qq|></td>
1117      |;
1118
1119     #write Trigger
1120     $jsscript =
1121       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
1122                           "transdateto", "BL", "trigger2");
1123   } else {
1124
1125     # without JavaScript Calendar
1126     $button1 = qq|
1127                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
1128     $button2 = qq|
1129                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
1130   }
1131
1132   $form->{fokus} = "search.customer";
1133   $form->header;
1134
1135   print qq|
1136 <body onLoad="fokus()">
1137
1138 <form method=post name="search" action=$form->{script}>
1139
1140 <table width=100%>
1141   <tr><th class=listtop>$form->{title}</th></tr>
1142   <tr height="5"></tr>
1143   <tr>
1144     <td>
1145       <table>
1146         <tr>
1147           <th align=right>| . $locale->text('Customer') . qq|</th>
1148           <td colspan=3>$customer</td>
1149         </tr>
1150         $department
1151         <tr>
1152           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
1153           <td colspan=3><input name=invnumber size=20></td>
1154         </tr>
1155         <tr>
1156           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
1157           <td colspan=3><input name=ordnumber size=20></td>
1158         </tr>
1159         <tr>
1160           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
1161           <td colspan=3><input name=notes size=40></td>
1162         </tr>
1163         <tr>
1164           <th align=right nowrap>| . $locale->text('From') . qq|</th>
1165           $button1
1166           <th align=right>| . $locale->text('Bis') . qq|</th>
1167           $button2
1168         </tr>
1169         <input type=hidden name=sort value=transdate>
1170       </table>
1171     </td>
1172   </tr>
1173   <tr>
1174     <td>
1175       <table>
1176         <tr>
1177           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
1178           <td>
1179             <table width=100%>
1180               <tr>
1181                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1182                 <td nowrap>| . $locale->text('Open') . qq|</td>
1183                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1184                 <td nowrap>| . $locale->text('Closed') . qq|</td>
1185               </tr>
1186               <tr>
1187                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1188                 <td nowrap>| . $locale->text('ID') . qq|</td>
1189                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1190                 <td nowrap>| . $locale->text('Invoice Number') . qq|</td>
1191                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1192                 <td nowrap>| . $locale->text('Order Number') . qq|</td>
1193                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1194                 <td nowrap>| . $locale->text('Invoice Date') . qq|</td>
1195               </tr>
1196               <tr>
1197                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1198                 <td nowrap>| . $locale->text('Customer') . qq|</td>
1199                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1200                 <td nowrap>| . $locale->text('Amount') . qq|</td>
1201                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1202                 <td nowrap>| . $locale->text('Tax') . qq|</td>
1203                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1204                 <td nowrap>| . $locale->text('Total') . qq|</td>
1205               </tr>
1206               <tr>
1207                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1208                 <td nowrap>| . $locale->text('Date Paid') . qq|</td>
1209                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1210                 <td nowrap>| . $locale->text('Paid') . qq|</td>
1211                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1212                 <td nowrap>| . $locale->text('Due Date') . qq|</td>
1213                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1214                 <td nowrap>| . $locale->text('Amount Due') . qq|</td>
1215               </tr>
1216               <tr>
1217                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1218                 <td nowrap>| . $locale->text('Notes') . qq|</td>
1219                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1220                 <td nowrap>| . $locale->text('Salesperson') . qq|</td>
1221                 <td align=right><input name="l_shippingpoint" class=checkbox type=checkbox value=Y></td>
1222                 <td nowrap>| . $locale->text('Shipping Point') . qq|</td>
1223                 <td align=right><input name="l_shipvia" class=checkbox type=checkbox value=Y></td>
1224                 <td nowrap>| . $locale->text('Ship via') . qq|</td>
1225               </tr>
1226               <tr>
1227                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1228                 <td nowrap>| . $locale->text('Subtotal') . qq|</td>
1229               </tr>
1230             </table>
1231           </td>
1232         </tr>
1233       </table>
1234     </td>
1235   </tr>
1236   <tr>
1237     <td><hr size=3 noshade></td>
1238   </tr>
1239 </table>
1240
1241 <input type=hidden name=nextsub value=$form->{nextsub}>
1242
1243 <input type=hidden name=path value=$form->{path}>
1244 <input type=hidden name=login value=$form->{login}>
1245 <input type=hidden name=password value=$form->{password}>
1246
1247 <br>
1248 <input class=submit type=submit name=action value="|
1249     . $locale->text('Continue') . qq|">
1250
1251 </form>
1252
1253 </body>
1254
1255 $jsscript
1256
1257 </html>
1258 |;
1259
1260   $lxdebug->leave_sub();
1261 }
1262
1263 sub ar_transactions {
1264   $lxdebug->enter_sub();
1265
1266   $form->{customer} = $form->unescape($form->{customer});
1267   ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
1268
1269   AR->ar_transactions(\%myconfig, \%$form);
1270
1271   $callback =
1272     "$form->{script}?action=ar_transactions&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1273   $href = $callback;
1274
1275   if ($form->{customer}) {
1276     $callback .= "&customer=" . $form->escape($form->{customer}, 1);
1277     $href .= "&customer=" . $form->escape($form->{customer});
1278     $option = $locale->text('Customer') . " : $form->{customer}";
1279   }
1280   if ($form->{department}) {
1281     $callback .= "&department=" . $form->escape($form->{department}, 1);
1282     $href .= "&department=" . $form->escape($form->{department});
1283     ($department) = split /--/, $form->{department};
1284     $option .= "\n<br>" if ($option);
1285     $option .= $locale->text('Department') . " : $department";
1286   }
1287   if ($form->{invnumber}) {
1288     $callback .= "&invnumber=" . $form->escape($form->{invnumber}, 1);
1289     $href .= "&invnumber=" . $form->escape($form->{invnumber});
1290     $option .= "\n<br>" if ($option);
1291     $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
1292   }
1293   if ($form->{ordnumber}) {
1294     $callback .= "&ordnumber=" . $form->escape($form->{ordnumber}, 1);
1295     $href .= "&ordnumber=" . $form->escape($form->{ordnumber});
1296     $option .= "\n<br>" if ($option);
1297     $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
1298   }
1299   if ($form->{notes}) {
1300     $callback .= "&notes=" . $form->escape($form->{notes}, 1);
1301     $href .= "&notes=" . $form->escape($form->{notes});
1302     $option .= "\n<br>" if $option;
1303     $option .= $locale->text('Notes') . " : $form->{notes}";
1304   }
1305
1306   if ($form->{transdatefrom}) {
1307     $callback .= "&transdatefrom=$form->{transdatefrom}";
1308     $href     .= "&transdatefrom=$form->{transdatefrom}";
1309     $option   .= "\n<br>" if ($option);
1310     $option   .=
1311         $locale->text('From') . "&nbsp;"
1312       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1313   }
1314   if ($form->{transdateto}) {
1315     $callback .= "&transdateto=$form->{transdateto}";
1316     $href     .= "&transdateto=$form->{transdateto}";
1317     $option   .= "\n<br>" if ($option);
1318     $option   .=
1319         $locale->text('Bis') . "&nbsp;"
1320       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1321   }
1322   if ($form->{open}) {
1323     $callback .= "&open=$form->{open}";
1324     $href     .= "&open=$form->{open}";
1325     $option   .= "\n<br>" if ($option);
1326     $option   .= $locale->text('Open');
1327   }
1328   if ($form->{closed}) {
1329     $callback .= "&closed=$form->{closed}";
1330     $href     .= "&closed=$form->{closed}";
1331     $option   .= "\n<br>" if ($option);
1332     $option   .= $locale->text('Closed');
1333   }
1334
1335   @columns = $form->sort_columns(
1336     qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee shippingpoint shipvia)
1337   );
1338
1339   $form->{"l_type"} = "Y";
1340
1341   foreach $item (@columns) {
1342     if ($form->{"l_$item"} eq "Y") {
1343       push @column_index, $item;
1344
1345       # add column to href and callback
1346       $callback .= "&l_$item=Y";
1347       $href     .= "&l_$item=Y";
1348     }
1349   }
1350
1351   if ($form->{l_subtotal} eq 'Y') {
1352     $callback .= "&l_subtotal=Y";
1353     $href     .= "&l_subtotal=Y";
1354   }
1355
1356   $column_header{id} =
1357       "<th><a class=listheading href=$href&sort=id>"
1358     . $locale->text('ID')
1359     . "</a></th>";
1360   $column_header{transdate} =
1361       "<th><a class=listheading href=$href&sort=transdate>"
1362     . $locale->text('Date')
1363     . "</a></th>";
1364   $column_header{duedate} =
1365       "<th><a class=listheading href=$href&sort=duedate>"
1366     . $locale->text('Due Date')
1367     . "</a></th>";
1368   $column_header{type} =
1369       "<th class=\"listheading\">" . $locale->text('Type') . "</th>";
1370   $column_header{invnumber} =
1371       "<th><a class=listheading href=$href&sort=invnumber>"
1372     . $locale->text('Invoice')
1373     . "</a></th>";
1374   $column_header{ordnumber} =
1375       "<th><a class=listheading href=$href&sort=ordnumber>"
1376     . $locale->text('Order')
1377     . "</a></th>";
1378   $column_header{name} =
1379       "<th><a class=listheading href=$href&sort=name>"
1380     . $locale->text('Customer')
1381     . "</a></th>";
1382   $column_header{netamount} =
1383     "<th class=listheading>" . $locale->text('Amount') . "</th>";
1384   $column_header{tax} =
1385     "<th class=listheading>" . $locale->text('Tax') . "</th>";
1386   $column_header{amount} =
1387     "<th class=listheading>" . $locale->text('Total') . "</th>";
1388   $column_header{paid} =
1389     "<th class=listheading>" . $locale->text('Paid') . "</th>";
1390   $column_header{datepaid} =
1391       "<th><a class=listheading href=$href&sort=datepaid>"
1392     . $locale->text('Date Paid')
1393     . "</a></th>";
1394   $column_header{due} =
1395     "<th class=listheading>" . $locale->text('Amount Due') . "</th>";
1396   $column_header{notes} =
1397     "<th class=listheading>" . $locale->text('Notes') . "</th>";
1398   $column_header{employee} =
1399     "<th><a class=listheading href=$href&sort=employee>"
1400     . $locale->text('Salesperson') . "</th>";
1401
1402   $column_header{shippingpoint} =
1403       "<th><a class=listheading href=$href&sort=shippingpoint>"
1404     . $locale->text('Shipping Point')
1405     . "</a></th>";
1406   $column_header{shipvia} =
1407       "<th><a class=listheading href=$href&sort=shipvia>"
1408     . $locale->text('Ship via')
1409     . "</a></th>";
1410
1411   $form->{title} = $locale->text('AR Transactions');
1412
1413   $form->header;
1414
1415   print qq|
1416 <body>
1417
1418 <table width=100%>
1419   <tr>
1420     <th class=listtop>$form->{title}</th>
1421   </tr>
1422   <tr height="5"></tr>
1423   <tr>
1424     <td>$option</td>
1425   </tr>
1426   <tr>
1427     <td>
1428       <table width=100%>
1429         <tr class=listheading>
1430 |;
1431
1432   map { print "\n$column_header{$_}" } @column_index;
1433
1434   print qq|
1435         </tr>
1436 |;
1437
1438   # add sort and escape callback, this one we use for the add sub
1439   $form->{callback} = $callback .= "&sort=$form->{sort}";
1440
1441   # escape callback for href
1442   $callback = $form->escape($callback);
1443
1444   if (@{ $form->{AR} }) {
1445     $sameitem = $form->{AR}->[0]->{ $form->{sort} };
1446   }
1447
1448   # sums and tax on reports by Antonio Gallardo
1449   #
1450   foreach $ar (@{ $form->{AR} }) {
1451
1452     if ($form->{l_subtotal} eq 'Y') {
1453       if ($sameitem ne $ar->{ $form->{sort} }) {
1454         &ar_subtotal;
1455       }
1456     }
1457
1458     $column_data{netamount} =
1459         "<td align=right>"
1460       . $form->format_amount(\%myconfig, $ar->{netamount}, 2, "&nbsp;")
1461       . "</td>";
1462     $column_data{tax} = "<td align=right>"
1463       . $form->format_amount(\%myconfig, $ar->{amount} - $ar->{netamount},
1464                              2, "&nbsp;")
1465       . "</td>";
1466     $column_data{amount} =
1467       "<td align=right>"
1468       . $form->format_amount(\%myconfig, $ar->{amount}, 2, "&nbsp;") . "</td>";
1469     $column_data{paid} =
1470       "<td align=right>"
1471       . $form->format_amount(\%myconfig, $ar->{paid}, 2, "&nbsp;") . "</td>";
1472     $column_data{due} = "<td align=right>"
1473       . $form->format_amount(\%myconfig, $ar->{amount} - $ar->{paid},
1474                              2, "&nbsp;")
1475       . "</td>";
1476
1477     $subtotalnetamount += $ar->{netamount};
1478     $subtotalamount    += $ar->{amount};
1479     $subtotalpaid      += $ar->{paid};
1480     $subtotaldue       += $ar->{amount} - $ar->{paid};
1481
1482     $totalnetamount += $ar->{netamount};
1483     $totalamount    += $ar->{amount};
1484     $totalpaid      += $ar->{paid};
1485     $totaldue       += ($ar->{amount} - $ar->{paid});
1486
1487     $column_data{transdate} = "<td>$ar->{transdate}&nbsp;</td>";
1488     $column_data{id}        = "<td>$ar->{id}</td>";
1489     $column_data{datepaid}  = "<td>$ar->{datepaid}&nbsp;</td>";
1490     $column_data{duedate}   = "<td>$ar->{duedate}&nbsp;</td>";
1491
1492     $module = ($ar->{invoice}) ? "is.pl" : $form->{script};
1493
1494     $column_data{invnumber} =
1495       "<td><a href=$module?action=edit&id=$ar->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ar->{invnumber}</a></td>";
1496     $column_data{type} = "<td>" .
1497       ($ar->{storno} ? $locale->text("Storno (one letter abbreviation)") :
1498        $ar->{amount} < 0 ?
1499        $locale->text("Credit note (one letter abbreviation)") :
1500        $locale->text("Invoice (one letter abbreviation)")) . "</td>";
1501     $column_data{ordnumber} = "<td>$ar->{ordnumber}&nbsp;</td>";
1502     $column_data{name}      = "<td>$ar->{name}</td>";
1503     $ar->{notes} =~ s/\r\n/<br>/g;
1504     $column_data{notes}         = "<td>$ar->{notes}&nbsp;</td>";
1505     $column_data{shippingpoint} = "<td>$ar->{shippingpoint}&nbsp;</td>";
1506     $column_data{shipvia}       = "<td>$ar->{shipvia}&nbsp;</td>";
1507     $column_data{employee}      = "<td>$ar->{employee}&nbsp;</td>";
1508
1509     $i++;
1510     $i %= 2;
1511     print "
1512         <tr class=listrow$i>
1513 ";
1514
1515     map { print "\n$column_data{$_}" } @column_index;
1516
1517     print qq|
1518         </tr>
1519 |;
1520
1521   }
1522
1523   if ($form->{l_subtotal} eq 'Y') {
1524     &ar_subtotal;
1525   }
1526
1527   # print totals
1528   print qq|
1529         <tr class=listtotal>
1530 |;
1531
1532   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1533
1534   $column_data{netamount} =
1535     "<th class=listtotal align=right>"
1536     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1537   $column_data{tax} = "<th class=listtotal align=right>"
1538     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1539                            2, "&nbsp;")
1540     . "</th>";
1541   $column_data{amount} =
1542     "<th class=listtotal align=right>"
1543     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1544   $column_data{paid} =
1545     "<th class=listtotal align=right>"
1546     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;") . "</th>";
1547   $column_data{due} =
1548     "<th class=listtotal align=right>"
1549     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;") . "</th>";
1550
1551   map { print "\n$column_data{$_}" } @column_index;
1552
1553   print qq|
1554         </tr>
1555       </table>
1556     </td>
1557   </tr>
1558   <tr>
1559     <td><hr size=3 noshade></td>
1560   </tr>
1561 </table>
1562
1563 <br>
1564 <form method=post action=$form->{script}>
1565
1566 <input name=callback type=hidden value="$form->{callback}">
1567
1568 <input type=hidden name=path value=$form->{path}>
1569 <input type=hidden name=login value=$form->{login}>
1570 <input type=hidden name=password value=$form->{password}>
1571
1572 <input class=submit type=submit name=action value="|
1573     . $locale->text('AR Transaction') . qq|">
1574 <input class=submit type=submit name=action value="|
1575     . $locale->text('Sales Invoice') . qq|">
1576
1577 </form>
1578
1579 </body>
1580 </html>
1581 |;
1582
1583   $lxdebug->leave_sub();
1584 }
1585
1586 sub ar_subtotal {
1587   $lxdebug->enter_sub();
1588
1589   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1590
1591   $column_data{tax} = "<th class=listsubtotal align=right>"
1592     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1593                            2, "&nbsp;")
1594     . "</th>";
1595   $column_data{amount} =
1596     "<th class=listsubtotal align=right>"
1597     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1598   $column_data{paid} =
1599     "<th class=listsubtotal align=right>"
1600     . $form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;") . "</th>";
1601   $column_data{due} =
1602     "<th class=listsubtotal align=right>"
1603     . $form->format_amount(\%myconfig, $subtotaldue, 2, "&nbsp;") . "</th>";
1604
1605   $subtotalnetamount = 0;
1606   $subtotalamount    = 0;
1607   $subtotalpaid      = 0;
1608   $subtotaldue       = 0;
1609
1610   $sameitem = $ar->{ $form->{sort} };
1611
1612   print "<tr class=listsubtotal>";
1613
1614   map { print "\n$column_data{$_}" } @column_index;
1615
1616   print "
1617 </tr>
1618 ";
1619
1620   $lxdebug->leave_sub();
1621 }