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