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