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