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