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