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