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