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