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