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