Debitorenrechnungen: Drop-Down-Box für Steuerschlüssel bei jedem Formularaufruf aus...
[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   # forex
175   $form->{forex} = $form->{exchangerate};
176   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
177   foreach $key (keys %{ $form->{AR_links} }) {
178
179     foreach $ref (@{ $form->{AR_links}{$key} }) {
180       if ($key eq "AR_paid") {
181         $form->{"select$key"} .=
182           "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
183       } else {
184         $form->{"select$key"} .=
185           "<option value=\"$ref->{accno}--$ref->{tax_id}\">$ref->{accno}--$ref->{description}</option>\n";
186       }
187     }
188
189     $form->{$key} = $form->{"select$key"};
190
191     # if there is a value we have an old entry
192     my $j = 0;
193     my $k = 0;
194
195     for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
196       if ($key eq "AR_paid") {
197         $j++;
198         $form->{"AR_paid_$j"} =
199           "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
200
201         # reverse paid
202         $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
203         $form->{"datepaid_$j"} =
204           $form->{acc_trans}{$key}->[$i - 1]->{transdate};
205         $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
206         $form->{"memo_$j"}   = $form->{acc_trans}{$key}->[$i - 1]->{memo};
207
208         $form->{"forex_$j"} = $form->{"exchangerate_$i"} =
209           $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
210         $form->{"AR_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}";
211         $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
212         $form->{paidaccounts}++;
213       } else {
214
215         $akey = $key;
216         $akey =~ s/AR_//;
217
218         if ($key eq "AR_tax" || $key eq "AP_tax") {
219           $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} =
220             "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
221           $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} =
222             $form->round_amount(
223                   $form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate,
224                   2);
225
226           if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
227             $totaltax +=
228               $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
229             $taxrate +=
230               $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
231           } else {
232             $totalwithholding +=
233               $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
234             $withholdingrate +=
235               $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
236           }
237           $index = $form->{acc_trans}{$key}->[$i - 1]->{index};
238           $form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
239           $totaltax += $form->{"tax_$index"};
240
241         } else {
242           $k++;
243           $form->{"${akey}_$k"} =
244             $form->round_amount(
245                   $form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate,
246                   2);
247           if ($akey eq 'amount') {
248             $form->{rowcount}++;
249             $totalamount += $form->{"${akey}_$i"};
250
251             $form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"} =
252               "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
253             $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
254             $form->{"project_id_$k"} =
255               "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
256           }
257           $form->{"${key}_$k"} =
258             "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
259           $form->{"${key}_$i"} =
260             "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
261           my $q_description = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description});
262           $form->{"select${key}"} =~
263             /<option value=\"($form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*)\">$form->{acc_trans}{$key}->[$i-1]->{accno}--${q_description}<\/option>\n/;
264           $form->{"${key}_$k"} = $1;
265           if ($akey eq 'amount') {
266             $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} .
267               "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
268           }
269         }
270       }
271     }
272   }
273
274   $form->{taxincluded}  = $taxincluded if ($form->{id});
275   $form->{paidaccounts} = 1            if not defined $form->{paidaccounts};
276
277   if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
278
279     # add tax to amounts and invtotal
280     for $i (1 .. $form->{rowcount}) {
281       $taxamount =
282         ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
283       $tax = $form->round_amount($taxamount, 2);
284       $diff                += ($taxamount - $tax);
285       $form->{"amount_$i"} += $form->{"tax_$i"};
286     }
287     $form->{amount_1} += $form->round_amount($diff, 2);
288   }
289
290   $taxamount = $form->round_amount($taxamount, 2);
291   $form->{tax} = $taxamount;
292
293   $form->{invtotal} = $totalamount + $totaltax;
294
295   $form->{locked} =
296     ($form->datetonum($form->{transdate}, \%myconfig) <=
297      $form->datetonum($form->{closedto}, \%myconfig));
298
299   $form->{"ARselected"} = $form->{"AR_1"};
300
301   $lxdebug->leave_sub();
302 }
303
304 sub form_header {
305   $lxdebug->enter_sub();
306
307   $title = $form->{title};
308   $form->{title} = $locale->text("$title Accounts Receivables Transaction");
309
310   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
311
312   # $locale->text('Add Accounts Receivables Transaction')
313   # $locale->text('Edit Accounts Receivables Transaction')
314   $form->{javascript} = qq|<script type="text/javascript">
315   <!--
316   function setTaxkey(accno, row) {
317     var taxkey = accno.options[accno.selectedIndex].value;
318     var reg = /--([0-9]*)/;
319     var found = reg.exec(taxkey);
320     var index = found[1];
321     index = parseInt(index);
322     var tax = 'taxchart_' + row;
323     for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
324       var reg2 = new RegExp("^"+ index, "");
325       if (reg2.exec(document.getElementById(tax).options[i].value)) {
326         document.getElementById(tax).options[i].selected = true;
327         break;
328       }
329     }
330   };
331   //-->
332   </script>|;
333   # show history button js
334   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
335   #/show history button js
336
337   $readonly = ($form->{id}) ? "readonly" : "";
338
339   $form->{radier} =
340     ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
341   $readonly = ($form->{radier}) ? "" : $readonly;
342
343   my $ARselected_quoted = quotemeta($form->{"ARselected"});
344   $form->{selectAR} = $form->{AR};
345   $form->{selectAR} =~
346     s/value=\"${ARselected_quoted}\"/value=\"$form->{ARselected}\" selected/;
347
348   # set option selected
349   foreach $item (qw(customer currency department employee)) {
350     $form->{"select$item"} =~ s/ selected//;
351     $form->{"select$item"} =~
352       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
353   }
354   $selectAR_amount_unquoted = $form->{selectAR_amount};
355   map { $form->{$_} =~ s/\"/&quot;/g }
356     qw(AR_amount selectAR_amount AR);
357
358   # format amounts
359   $form->{exchangerate} =
360     $form->format_amount(\%myconfig, $form->{exchangerate});
361
362   $form->{creditlimit} =
363     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
364   $form->{creditremaining} =
365     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
366
367   $exchangerate = qq|
368 <input type=hidden name=forex value=$form->{forex}>
369 |;
370   if ($form->{currency} ne $form->{defaultcurrency}) {
371     if ($form->{forex}) {
372       $exchangerate .= qq|
373         <th align=right>| . $locale->text('Exchangerate') . qq|</th>
374         <td><input type=hidden name=exchangerate value=$form->{exchangerate}>$form->{exchangerate}</td>
375 |;
376     } else {
377       $exchangerate .= qq|
378         <th align=right>| . $locale->text('Exchangerate') . qq|</th>
379         <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
380 |;
381     }
382   }
383
384   $taxincluded = "";
385
386   $taxincluded = qq|
387               <tr>
388                 <td align=right><input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td>
389                 <th align=left nowrap>| . $locale->text('Tax Included') . qq|</th>
390               </tr>
391 |;
392
393   if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
394     $rows = 2;
395   }
396   $notes =
397     qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
398
399   $department = qq|
400               <tr>
401                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
402                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
403                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
404                 </td>
405               </tr>
406 | if $form->{selectdepartment};
407
408   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
409
410   $customer =
411     ($form->{selectcustomer})
412     ? qq|<select name=customer>$form->{selectcustomer}</select>|
413     : qq|<input name=customer value="$form->{customer}" size=35>|;
414
415   $employee = qq|
416                 <input type=hidden name=employee value="$form->{employee}">
417 |;
418
419   if ($form->{selectemployee}) {
420     $employee = qq|
421               <tr>
422                 <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
423                 <td  colspan=2><select name=employee>$form->{selectemployee}</select></td>
424                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
425               </tr>
426 |;
427   }
428
429   my @old_project_ids = ();
430   map({ push(@old_project_ids, $form->{"project_id_$_"})
431           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
432
433   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
434                                    "all" => 0,
435                                    "old_id" => \@old_project_ids },
436                    "taxcharts" => "ALL_TAXCHARTS");
437
438   my %project_labels = ();
439   my @project_values = ("");
440   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
441     push(@project_values, $item->{"id"});
442     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
443   }
444
445   my %taxchart_labels = ();
446   my @taxchart_values = ();
447   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
448     my $key = Q($item->{id}) . "--" . Q($item->{rate});
449     $taxchart_init = $key if ($taxchart_init eq $item->{taxkey});
450     push(@taxchart_values, $key);
451     $taxchart_labels{$key} = H($item->{taxdescription}) . " " .
452       H($item->{rate} * 100) . ' %';
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=rowcount value=$form->{rowcount}>
579   <tr>
580       <td>
581           <table width=100%>
582            <tr class=listheading>
583           <th class=listheading style="width:15%">|
584     . $locale->text('Account') . qq|</th>
585           <th class=listheading style="width:10%">|
586     . $locale->text('Amount') . qq|</th>
587           <th class=listheading style="width:10%">|
588     . $locale->text('Tax') . qq|</th>
589           <th class=listheading style="width:5%">|
590     . $locale->text('Korrektur') . qq|</th>
591           <th class=listheading style="width:10%">|
592     . $locale->text('Taxkey') . qq|</th>
593           <th class=listheading style="width:10%">|
594     . $locale->text('Project') . qq|</th>
595         </tr>
596 |;
597
598   $amount  = $locale->text('Amount');
599   $project = $locale->text('Project');
600
601   for $i (1 .. $form->{rowcount}) {
602
603     # format amounts
604     $form->{"amount_$i"} =
605       $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
606     $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
607     $selectAR_amount = $selectAR_amount_unquoted;
608     $selectAR_amount =~
609       s/option value=\"$form->{"AR_amount_$i"}\"/option value=\"$form->{"AR_amount_$i"}\" selected/;
610
611     $tax = qq|<td>| .
612       $cgi->popup_menu('-name' => "taxchart_$i",
613                        '-id' => "taxchart_$i",
614                        '-style' => 'width:200px',
615                        '-tabindex' => ($i + 10 + (($i - 1) * 8)),
616                        '-values' => \@taxchart_values,
617                        '-labels' => \%taxchart_labels,
618                        '-default' => $form->{"taxchart_$i"})
619       . qq|</td>|;
620
621     $korrektur_checked = ($form->{"korrektur_$i"} ? 'checked' : '');
622
623     my $projectnumber =
624       NTI($cgi->popup_menu('-name' => "project_id_$i",
625                            '-values' => \@project_values,
626                            '-labels' => \%project_labels,
627                            '-default' => $form->{"project_id_$i"} ));
628
629     print qq|
630         <tr>
631           <td width=50%><select name="AR_amount_$i" onChange="setTaxkey(this, $i)" style="width:100%">$selectAR_amount</select></td>
632           <td><input name="amount_$i" size=10 value=$form->{"amount_$i"}></td>
633           <td><input name="tax_$i" size=10 value=$form->{"tax_$i"}></td>
634           <td><input type="checkbox" name="korrektur_$i" value="1" $korrektur_checked></td>
635           $tax
636           <td>$projectnumber</td>
637         </tr>
638 |;
639     $amount  = "";
640     $project = "";
641   }
642
643   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
644
645   print qq|
646         <tr>
647           <td colspan=6>
648             <hr noshade>
649           </td>
650         </tr>
651         <tr>
652           <td><select name=ARselected>$form->{selectAR}</select></td>
653           <input type=hidden name=AR value="$form->{AR}">
654           <th align=left>$form->{invtotal}</th>
655
656           <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
657           <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
658
659           <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
660
661           <td colspan=4></td>
662
663
664         </tr>
665         </table>
666         </td>
667     </tr>
668     <tr>
669       <td>
670         <table width=100%>
671         <tr>
672           <th align=left width=1%>| . $locale->text('Notes') . qq|</th>
673           <td align=left>$notes</td>
674         </tr>
675       </table>
676     </td>
677   </tr>
678   <tr>
679     <td>
680       <table width=100%>
681         <tr class=listheading>
682           <th colspan=7 class=listheading>|
683     . $locale->text('Incoming Payments') . qq|</th>
684         </tr>
685 |;
686
687   if ($form->{currency} eq $form->{defaultcurrency}) {
688     @column_index = qw(datepaid source memo paid AR_paid paid_project_id);
689   } else {
690     @column_index = qw(datepaid source memo paid exchangerate AR_paid paid_project_id);
691   }
692
693   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
694   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
695   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
696   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
697   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
698   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>"; 
699   $column_data{paid_project_id} = "<th>" . $locale->text('Project Number') . "</th>"; 
700
701   print "
702         <tr>
703 ";
704   map { print "$column_data{$_}\n" } @column_index;
705   print "
706         </tr>
707 ";
708
709   my @triggers = ();
710   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
711   for $i (1 .. $form->{paidaccounts}) {
712     print "
713         <tr>
714 ";
715
716     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
717     $form->{"selectAR_paid_$i"} =~
718       s/option value=\"$form->{"AR_paid_$i"}\">/option value=\"$form->{"AR_paid_$i"}\" selected>/;
719
720     # format amounts
721     if ($form->{"paid_$i"}) {
722       $form->{"paid_$i"} =
723         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
724     }
725     $form->{"exchangerate_$i"} =
726       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
727
728     $exchangerate = qq|&nbsp;|;
729     if ($form->{currency} ne $form->{defaultcurrency}) {
730       if ($form->{"forex_$i"}) {
731         $exchangerate =
732           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
733       } else {
734         $exchangerate =
735           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
736       }
737     }
738
739     $exchangerate .= qq|
740 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
741 |;
742
743     $column_data{paid} =
744       qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
745     $column_data{AR_paid} =
746       qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
747     $column_data{exchangerate} = qq|<td align=center>$exchangerate</td>|;
748     $column_data{datepaid}     =
749       qq|<td align=center><input name="datepaid_$i" id="datepaid_$i" size=11 value=$form->{"datepaid_$i"}>
750          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
751     $column_data{source} =
752       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
753     $column_data{memo} =
754       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
755
756     $column_data{paid_project_id} =
757       qq|<td>|
758       . NTI($cgi->popup_menu('-name' => "paid_project_id_$i",
759                              '-values' => \@project_values,
760                              '-labels' => \%project_labels,
761                              '-default' => $form->{"paid_project_id_$i"} ))
762       . qq|</td>|;
763
764     map { print qq|$column_data{$_}\n| } @column_index;
765
766     print "
767         </tr>
768 ";
769     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
770   }
771   map { $form->{$_} =~ s/\"/&quot;/g } qw(selectAR_paid);
772
773   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
774     qq|
775 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
776 <input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
777
778       </table>
779     </td>
780   </tr>
781   <tr>
782     <td><hr size=3 noshade></td>
783   </tr>
784 </table>
785 |;
786
787   $lxdebug->leave_sub();
788 }
789
790 sub form_footer {
791   $lxdebug->enter_sub();
792
793   print qq|
794
795 <input name=gldate type=hidden value="| . Q($form->{gldate}) . qq|">
796
797 <input name=callback type=hidden value="$form->{callback}">
798
799 <input type=hidden name=path value=$form->{path}>
800 <input type=hidden name=login value=$form->{login}>
801 <input type=hidden name=password value=$form->{password}>
802 |
803 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
804 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
805 . qq|
806
807 <br>
808 |;
809
810   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
811   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
812
813   print qq|<input class=submit type=submit name=action value="|
814     . $locale->text('Update') . qq|">
815 |;
816   if ($form->{id}) {
817     if ($form->{radier}) {
818       print qq|
819           <input class=submit type=submit name=action value="|
820             . $locale->text('Post') . qq|">
821           <input class=submit type=submit name=action value="|
822             . $locale->text('Delete') . qq|">
823   |;
824     }
825     if ($transdate > $closedto) {
826       print qq|
827   <input class=submit type=submit name=action value="|
828           . $locale->text('Use As Template') . qq|">
829   |;
830     }
831     print qq|
832   <input class=submit type=submit name=action value="|
833     . $locale->text('Post Payment') . qq|">
834   |;
835
836   } else {
837     if ($transdate > $closedto) {
838       print qq|<input class=submit type=submit name=action value="|
839         . $locale->text('Post') . qq|"> | .
840         NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
841                          '-class' => 'submit'));
842     }
843   }
844
845   if ($form->{menubar}) {
846     require "$form->{path}/menu.pl";
847     &menubar;
848   }
849   # button for saving history
850   if($form->{id} ne "") {
851     print qq|
852           <input type=button class=submit onclick=set_history_window(|
853           . $form->{id} 
854           . qq|); name=history id=history value=|
855           . $locale->text('history') 
856           . qq|>|;
857   }
858   # /button for saving history
859   print "
860 </form>
861
862 </body>
863 </html>
864 ";
865
866   $lxdebug->leave_sub();
867 }
868
869 sub update {
870   $lxdebug->enter_sub();
871
872   my $display = shift;
873
874   $form->{invtotal} = 0;
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 ($receivablesaccno, $payablestaxkey) = split /--/, $form->{ARselected};
1036   $form->{AR}{amount_1}    = $creditaccno;
1037   $form->{AR}{receivables} = $receivablesaccno;
1038
1039   $form->{invnumber} = $form->update_defaults(\%myconfig, "invnumber")
1040     unless $form->{invnumber};
1041
1042   $form->{id} = 0 if $form->{postasnew};
1043   if (AR->post_transaction(\%myconfig, \%$form)) {
1044     # saving the history
1045     if(!exists $form->{addition} && $form->{id} ne "") {
1046           $form->{addition} = "POSTED";
1047           $form->save_history($form->dbconnect(\%myconfig));
1048     }
1049     # /saving the history 
1050     remove_draft() if $form->{remove_draft};
1051     $form->redirect($locale->text('Transaction posted!'));
1052   }
1053   $form->error($locale->text('Cannot post transaction!'));
1054
1055   $lxdebug->leave_sub();
1056 }
1057
1058 sub post_as_new {
1059   $lxdebug->enter_sub();
1060
1061   $form->{postasnew} = 1;
1062   # saving the history
1063   if(!exists $form->{addition} && $form->{id} ne "") {
1064         $form->{addition} = "POSTED AS NEW";
1065         $form->save_history($form->dbconnect(\%myconfig));
1066   }
1067   # /saving the history 
1068   &post;
1069
1070   $lxdebug->leave_sub();
1071 }
1072
1073 sub use_as_template {
1074   $lxdebug->enter_sub();
1075
1076   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);
1077   $form->{paidaccounts} = 1;
1078   $form->{rowcount}--;
1079   $form->{invdate} = $form->current_date(\%myconfig);
1080   &update;
1081
1082   $lxdebug->leave_sub();
1083 }
1084
1085 sub delete {
1086   $lxdebug->enter_sub();
1087
1088   $form->{title} = $locale->text('Confirm!');
1089
1090   $form->header;
1091
1092   delete $form->{header};
1093
1094   print qq|
1095 <body>
1096
1097 <form method=post action=$form->{script}>
1098 |;
1099
1100   foreach $key (keys %$form) {
1101     $form->{$key} =~ s/\"/&quot;/g;
1102     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1103   }
1104
1105   print qq|
1106 <h2 class=confirm>$form->{title}</h2>
1107
1108 <h4>|
1109     . $locale->text('Are you sure you want to delete Transaction')
1110     . qq| $form->{invnumber}</h4>
1111
1112 <input name=action class=submit type=submit value="|
1113     . $locale->text('Yes') . qq|">
1114 </form>
1115
1116 </body>
1117 </html>
1118 |;
1119
1120   $lxdebug->leave_sub();
1121 }
1122
1123 sub yes {
1124   $lxdebug->enter_sub();
1125   if (AR->delete_transaction(\%myconfig, \%$form, $spool)) {
1126     # saving the history
1127     if(!exists $form->{addition}) {
1128           $form->{addition} = "DELETED";
1129           $form->save_history($form->dbconnect(\%myconfig));
1130     }
1131     # /saving the history 
1132     $form->redirect($locale->text('Transaction deleted!'));
1133   }
1134   $form->error($locale->text('Cannot delete transaction!'));
1135
1136   $lxdebug->leave_sub();
1137 }
1138
1139 sub search {
1140   $lxdebug->enter_sub();
1141
1142   # setup customer selection
1143   $form->all_vc(\%myconfig, "customer", "AR");
1144
1145   if (@{ $form->{all_customer} }) {
1146     map { $customer .= "<option>$_->{name}--$_->{id}\n" }
1147       @{ $form->{all_customer} };
1148     $customer = qq|<select name=customer><option>\n$customer</select>|;
1149   } else {
1150     $customer = qq|<input name=customer size=35>|;
1151   }
1152
1153   # departments
1154   if (@{ $form->{all_departments} }) {
1155     $form->{selectdepartment} = "<option>\n";
1156
1157     map {
1158       $form->{selectdepartment} .=
1159         "<option>$_->{description}--$_->{id}\n"
1160     } (@{ $form->{all_departments} });
1161   }
1162
1163   $department = qq|
1164         <tr>
1165           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1166           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1167         </tr>
1168 | if $form->{selectdepartment};
1169
1170   $form->{title} = $locale->text('AR Transactions');
1171
1172   # use JavaScript Calendar or not
1173   $form->{jsscript} = $jscalendar;
1174   $jsscript = "";
1175   if ($form->{jsscript}) {
1176
1177     # with JavaScript Calendar
1178     $button1 = qq|
1179        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
1180        <input type=button name=transdatefrom id="trigger1" value=|
1181       . $locale->text('button') . qq|></td>
1182       |;
1183     $button2 = qq|
1184        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
1185        <input type=button name=transdateto name=transdateto id="trigger2" value=|
1186       . $locale->text('button') . qq|></td>
1187      |;
1188
1189     #write Trigger
1190     $jsscript =
1191       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
1192                           "transdateto", "BL", "trigger2");
1193   } else {
1194
1195     # without JavaScript Calendar
1196     $button1 = qq|
1197                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
1198     $button2 = qq|
1199                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
1200   }
1201
1202   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1203                                    "all" => 1 });
1204
1205   my %labels = ();
1206   my @values = ("");
1207   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
1208     push(@values, $item->{"id"});
1209     $labels{$item->{"id"}} = $item->{"projectnumber"};
1210   }
1211   my $projectnumber =
1212     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
1213                          '-labels' => \%labels));
1214
1215   $form->{fokus} = "search.customer";
1216   $form->header;
1217
1218   print qq|
1219 <body onLoad="fokus()">
1220
1221 <form method=post name="search" action=$form->{script}>
1222
1223 <table width=100%>
1224   <tr><th class=listtop>$form->{title}</th></tr>
1225   <tr height="5"></tr>
1226   <tr>
1227     <td>
1228       <table>
1229         <tr>
1230           <th align=right>| . $locale->text('Customer') . qq|</th>
1231           <td colspan=3>$customer</td>
1232         </tr>
1233         $department
1234         <tr>
1235           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
1236           <td colspan=3><input name=invnumber size=20></td>
1237         </tr>
1238         <tr>
1239           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
1240           <td colspan=3><input name=ordnumber size=20></td>
1241         </tr>
1242         <tr>
1243           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
1244           <td colspan=3><input name=notes size=40></td>
1245         </tr>
1246         <tr>
1247           <th align="right">| . $locale->text("Project Number") . qq|</th>
1248           <td colspan="3">$projectnumber</td>
1249         </tr>
1250         <tr>
1251           <th align=right nowrap>| . $locale->text('From') . qq|</th>
1252           $button1
1253           <th align=right>| . $locale->text('Bis') . qq|</th>
1254           $button2
1255         </tr>
1256         <input type=hidden name=sort value=transdate>
1257       </table>
1258     </td>
1259   </tr>
1260   <tr>
1261     <td>
1262       <table>
1263         <tr>
1264           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
1265           <td>
1266             <table width=100%>
1267               <tr>
1268                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
1269                 <td nowrap>| . $locale->text('Open') . qq|</td>
1270                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
1271                 <td nowrap>| . $locale->text('Closed') . qq|</td>
1272               </tr>
1273               <tr>
1274                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
1275                 <td nowrap>| . $locale->text('ID') . qq|</td>
1276                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
1277                 <td nowrap>| . $locale->text('Invoice Number') . qq|</td>
1278                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
1279                 <td nowrap>| . $locale->text('Order Number') . qq|</td>
1280                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
1281                 <td nowrap>| . $locale->text('Invoice Date') . qq|</td>
1282               </tr>
1283               <tr>
1284                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
1285                 <td nowrap>| . $locale->text('Customer') . qq|</td>
1286                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
1287                 <td nowrap>| . $locale->text('Amount') . qq|</td>
1288                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
1289                 <td nowrap>| . $locale->text('Tax') . qq|</td>
1290                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
1291                 <td nowrap>| . $locale->text('Total') . qq|</td>
1292               </tr>
1293               <tr>
1294                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
1295                 <td nowrap>| . $locale->text('Date Paid') . qq|</td>
1296                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
1297                 <td nowrap>| . $locale->text('Paid') . qq|</td>
1298                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
1299                 <td nowrap>| . $locale->text('Due Date') . qq|</td>
1300                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
1301                 <td nowrap>| . $locale->text('Amount Due') . qq|</td>
1302               </tr>
1303               <tr>
1304                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
1305                 <td nowrap>| . $locale->text('Notes') . qq|</td>
1306                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
1307                 <td nowrap>| . $locale->text('Salesperson') . qq|</td>
1308                 <td align=right><input name="l_shippingpoint" class=checkbox type=checkbox value=Y></td>
1309                 <td nowrap>| . $locale->text('Shipping Point') . qq|</td>
1310                 <td align=right><input name="l_shipvia" class=checkbox type=checkbox value=Y></td>
1311                 <td nowrap>| . $locale->text('Ship via') . qq|</td>
1312               </tr>
1313               <tr>
1314                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
1315                 <td nowrap>| . $locale->text('Subtotal') . qq|</td>
1316                 <td align=right><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y></td>
1317                 <td nowrap>| . $locale->text('Project Number') . qq|</td>
1318               </tr>
1319             </table>
1320           </td>
1321         </tr>
1322       </table>
1323     </td>
1324   </tr>
1325   <tr>
1326     <td><hr size=3 noshade></td>
1327   </tr>
1328 </table>
1329
1330 <input type=hidden name=nextsub value=$form->{nextsub}>
1331
1332 <input type=hidden name=path value=$form->{path}>
1333 <input type=hidden name=login value=$form->{login}>
1334 <input type=hidden name=password value=$form->{password}>
1335
1336 <br>
1337 <input class=submit type=submit name=action value="|
1338     . $locale->text('Continue') . qq|">
1339
1340 </form>
1341
1342 </body>
1343
1344 $jsscript
1345
1346 </html>
1347 |;
1348
1349   $lxdebug->leave_sub();
1350 }
1351
1352 sub ar_transactions {
1353   $lxdebug->enter_sub();
1354
1355   $form->{customer} = $form->unescape($form->{customer});
1356   ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
1357
1358   AR->ar_transactions(\%myconfig, \%$form);
1359
1360   $callback =
1361     "$form->{script}?action=ar_transactions&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1362   $href = $callback;
1363
1364   if ($form->{customer}) {
1365     $callback .= "&customer=" . $form->escape($form->{customer}, 1);
1366     $href .= "&customer=" . $form->escape($form->{customer});
1367     $option = $locale->text('Customer') . " : $form->{customer}";
1368   }
1369   if ($form->{department}) {
1370     $callback .= "&department=" . $form->escape($form->{department}, 1);
1371     $href .= "&department=" . $form->escape($form->{department});
1372     ($department) = split /--/, $form->{department};
1373     $option .= "\n<br>" if ($option);
1374     $option .= $locale->text('Department') . " : $department";
1375   }
1376   if ($form->{invnumber}) {
1377     $callback .= "&invnumber=" . $form->escape($form->{invnumber}, 1);
1378     $href .= "&invnumber=" . $form->escape($form->{invnumber});
1379     $option .= "\n<br>" if ($option);
1380     $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
1381   }
1382   if ($form->{ordnumber}) {
1383     $callback .= "&ordnumber=" . $form->escape($form->{ordnumber}, 1);
1384     $href .= "&ordnumber=" . $form->escape($form->{ordnumber});
1385     $option .= "\n<br>" if ($option);
1386     $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
1387   }
1388   if ($form->{notes}) {
1389     $callback .= "&notes=" . $form->escape($form->{notes}, 1);
1390     $href .= "&notes=" . $form->escape($form->{notes});
1391     $option .= "\n<br>" if $option;
1392     $option .= $locale->text('Notes') . " : $form->{notes}";
1393   }
1394
1395   if ($form->{transdatefrom}) {
1396     $callback .= "&transdatefrom=$form->{transdatefrom}";
1397     $href     .= "&transdatefrom=$form->{transdatefrom}";
1398     $option   .= "\n<br>" if ($option);
1399     $option   .=
1400         $locale->text('From') . "&nbsp;"
1401       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1402   }
1403   if ($form->{transdateto}) {
1404     $callback .= "&transdateto=$form->{transdateto}";
1405     $href     .= "&transdateto=$form->{transdateto}";
1406     $option   .= "\n<br>" if ($option);
1407     $option   .=
1408         $locale->text('Bis') . "&nbsp;"
1409       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1410   }
1411   if ($form->{open}) {
1412     $callback .= "&open=$form->{open}";
1413     $href     .= "&open=$form->{open}";
1414     $option   .= "\n<br>" if ($option);
1415     $option   .= $locale->text('Open');
1416   }
1417   if ($form->{closed}) {
1418     $callback .= "&closed=$form->{closed}";
1419     $href     .= "&closed=$form->{closed}";
1420     $option   .= "\n<br>" if ($option);
1421     $option   .= $locale->text('Closed');
1422   }
1423   if ($form->{globalproject_id}) {
1424     $callback .= "&globalproject_id=" . E($form->{globalproject_id});
1425     $href     .= "&globalproject_id=" . E($form->{globalproject_id});
1426   }
1427
1428   @columns =
1429     qw(transdate id type invnumber ordnumber name netamount tax amount paid
1430        datepaid due duedate notes employee shippingpoint shipvia
1431        globalprojectnumber);
1432
1433   $form->{"l_type"} = "Y";
1434
1435   foreach $item (@columns) {
1436     if ($form->{"l_$item"} eq "Y") {
1437       push @column_index, $item;
1438
1439       # add column to href and callback
1440       $callback .= "&l_$item=Y";
1441       $href     .= "&l_$item=Y";
1442     }
1443   }
1444
1445   if ($form->{l_subtotal} eq 'Y') {
1446     $callback .= "&l_subtotal=Y";
1447     $href     .= "&l_subtotal=Y";
1448   }
1449
1450   $column_header{id} =
1451       "<th><a class=listheading href=$href&sort=id>"
1452     . $locale->text('ID')
1453     . "</a></th>";
1454   $column_header{transdate} =
1455       "<th><a class=listheading href=$href&sort=transdate>"
1456     . $locale->text('Date')
1457     . "</a></th>";
1458   $column_header{duedate} =
1459       "<th><a class=listheading href=$href&sort=duedate>"
1460     . $locale->text('Due Date')
1461     . "</a></th>";
1462   $column_header{type} =
1463       "<th class=\"listheading\">" . $locale->text('Type') . "</th>";
1464   $column_header{invnumber} =
1465       "<th><a class=listheading href=$href&sort=invnumber>"
1466     . $locale->text('Invoice')
1467     . "</a></th>";
1468   $column_header{ordnumber} =
1469       "<th><a class=listheading href=$href&sort=ordnumber>"
1470     . $locale->text('Order')
1471     . "</a></th>";
1472   $column_header{name} =
1473       "<th><a class=listheading href=$href&sort=name>"
1474     . $locale->text('Customer')
1475     . "</a></th>";
1476   $column_header{netamount} =
1477     "<th class=listheading>" . $locale->text('Amount') . "</th>";
1478   $column_header{tax} =
1479     "<th class=listheading>" . $locale->text('Tax') . "</th>";
1480   $column_header{amount} =
1481     "<th class=listheading>" . $locale->text('Total') . "</th>";
1482   $column_header{paid} =
1483     "<th class=listheading>" . $locale->text('Paid') . "</th>";
1484   $column_header{datepaid} =
1485       "<th><a class=listheading href=$href&sort=datepaid>"
1486     . $locale->text('Date Paid')
1487     . "</a></th>";
1488   $column_header{due} =
1489     "<th class=listheading>" . $locale->text('Amount Due') . "</th>";
1490   $column_header{notes} =
1491     "<th class=listheading>" . $locale->text('Notes') . "</th>";
1492   $column_header{employee} =
1493     "<th><a class=listheading href=$href&sort=employee>"
1494     . $locale->text('Salesperson') . "</th>";
1495
1496   $column_header{shippingpoint} =
1497       "<th><a class=listheading href=$href&sort=shippingpoint>"
1498     . $locale->text('Shipping Point')
1499     . "</a></th>";
1500   $column_header{shipvia} =
1501       "<th><a class=listheading href=$href&sort=shipvia>"
1502     . $locale->text('Ship via')
1503     . "</a></th>";
1504   $column_header{globalprojectnumber} =
1505     qq|<th class="listheading">| . $locale->text('Project Number') . qq|</th>|;
1506
1507   $form->{title} = $locale->text('AR Transactions');
1508
1509   $form->header;
1510
1511   print qq|
1512 <body>
1513
1514 <table width=100%>
1515   <tr>
1516     <th class=listtop>$form->{title}</th>
1517   </tr>
1518   <tr height="5"></tr>
1519   <tr>
1520     <td>$option</td>
1521   </tr>
1522   <tr>
1523     <td>
1524       <table width=100%>
1525         <tr class=listheading>
1526 |;
1527
1528   map { print "\n$column_header{$_}" } @column_index;
1529
1530   print qq|
1531         </tr>
1532 |;
1533
1534   # add sort and escape callback, this one we use for the add sub
1535   $form->{callback} = $callback .= "&sort=$form->{sort}";
1536
1537   # escape callback for href
1538   $callback = $form->escape($callback);
1539
1540   if (@{ $form->{AR} }) {
1541     $sameitem = $form->{AR}->[0]->{ $form->{sort} };
1542   }
1543
1544   # sums and tax on reports by Antonio Gallardo
1545   #
1546   foreach $ar (@{ $form->{AR} }) {
1547
1548     if ($form->{l_subtotal} eq 'Y') {
1549       if ($sameitem ne $ar->{ $form->{sort} }) {
1550         &ar_subtotal;
1551       }
1552     }
1553
1554     $column_data{netamount} =
1555         "<td align=right>"
1556       . $form->format_amount(\%myconfig, $ar->{netamount}, 2, "&nbsp;")
1557       . "</td>";
1558     $column_data{tax} = "<td align=right>"
1559       . $form->format_amount(\%myconfig, $ar->{amount} - $ar->{netamount},
1560                              2, "&nbsp;")
1561       . "</td>";
1562     $column_data{amount} =
1563       "<td align=right>"
1564       . $form->format_amount(\%myconfig, $ar->{amount}, 2, "&nbsp;") . "</td>";
1565     $column_data{paid} =
1566       "<td align=right>"
1567       . $form->format_amount(\%myconfig, $ar->{paid}, 2, "&nbsp;") . "</td>";
1568     $column_data{due} = "<td align=right>"
1569       . $form->format_amount(\%myconfig, $ar->{amount} - $ar->{paid},
1570                              2, "&nbsp;")
1571       . "</td>";
1572
1573     $subtotalnetamount += $ar->{netamount};
1574     $subtotalamount    += $ar->{amount};
1575     $subtotalpaid      += $ar->{paid};
1576     $subtotaldue       += $ar->{amount} - $ar->{paid};
1577
1578     $totalnetamount += $ar->{netamount};
1579     $totalamount    += $ar->{amount};
1580     $totalpaid      += $ar->{paid};
1581     $totaldue       += ($ar->{amount} - $ar->{paid});
1582
1583     $column_data{transdate} = "<td>$ar->{transdate}&nbsp;</td>";
1584     $column_data{id}        = "<td>$ar->{id}</td>";
1585     $column_data{datepaid}  = "<td>$ar->{datepaid}&nbsp;</td>";
1586     $column_data{duedate}   = "<td>$ar->{duedate}&nbsp;</td>";
1587
1588     $module = ($ar->{invoice}) ? "is.pl" : $form->{script};
1589
1590     $column_data{invnumber} =
1591       "<td><a href=$module?action=edit&id=$ar->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ar->{invnumber}</a></td>";
1592     $column_data{type} = "<td>" .
1593       ($ar->{storno} ? $locale->text("Storno (one letter abbreviation)") :
1594        $ar->{amount} < 0 ?
1595        $locale->text("Credit note (one letter abbreviation)") :
1596        $locale->text("Invoice (one letter abbreviation)")) . "</td>";
1597     $column_data{ordnumber} = "<td>$ar->{ordnumber}&nbsp;</td>";
1598     $column_data{name}      = "<td>$ar->{name}</td>";
1599     $ar->{notes} =~ s/\r\n/<br>/g;
1600     $column_data{notes}         = "<td>$ar->{notes}&nbsp;</td>";
1601     $column_data{shippingpoint} = "<td>$ar->{shippingpoint}&nbsp;</td>";
1602     $column_data{shipvia}       = "<td>$ar->{shipvia}&nbsp;</td>";
1603     $column_data{employee}      = "<td>$ar->{employee}&nbsp;</td>";
1604     $column_data{globalprojectnumber}  =
1605       "<td>" . H($ar->{globalprojectnumber}) . "</td>";
1606
1607     $i++;
1608     $i %= 2;
1609     print "
1610         <tr class=listrow$i>
1611 ";
1612
1613     map { print "\n$column_data{$_}" } @column_index;
1614
1615     print qq|
1616         </tr>
1617 |;
1618
1619   }
1620
1621   if ($form->{l_subtotal} eq 'Y') {
1622     &ar_subtotal;
1623   }
1624
1625   # print totals
1626   print qq|
1627         <tr class=listtotal>
1628 |;
1629
1630   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1631
1632   $column_data{netamount} =
1633     "<th class=listtotal align=right>"
1634     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1635   $column_data{tax} = "<th class=listtotal align=right>"
1636     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1637                            2, "&nbsp;")
1638     . "</th>";
1639   $column_data{amount} =
1640     "<th class=listtotal align=right>"
1641     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1642   $column_data{paid} =
1643     "<th class=listtotal align=right>"
1644     . $form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;") . "</th>";
1645   $column_data{due} =
1646     "<th class=listtotal align=right>"
1647     . $form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;") . "</th>";
1648
1649   map { print "\n$column_data{$_}" } @column_index;
1650
1651   print qq|
1652         </tr>
1653       </table>
1654     </td>
1655   </tr>
1656   <tr>
1657     <td><hr size=3 noshade></td>
1658   </tr>
1659 </table>
1660
1661 <br>
1662 <form method=post action=$form->{script}>
1663
1664 <input name=callback type=hidden value="$form->{callback}">
1665
1666 <input type=hidden name=path value=$form->{path}>
1667 <input type=hidden name=login value=$form->{login}>
1668 <input type=hidden name=password value=$form->{password}>
1669
1670 <input class=submit type=submit name=action value="|
1671     . $locale->text('AR Transaction') . qq|">
1672 <input class=submit type=submit name=action value="|
1673     . $locale->text('Sales Invoice') . qq|">
1674
1675 </form>
1676
1677 </body>
1678 </html>
1679 |;
1680
1681   $lxdebug->leave_sub();
1682 }
1683
1684 sub ar_subtotal {
1685   $lxdebug->enter_sub();
1686
1687   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1688
1689   $column_data{tax} = "<th class=listsubtotal align=right>"
1690     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1691                            2, "&nbsp;")
1692     . "</th>";
1693   $column_data{amount} =
1694     "<th class=listsubtotal align=right>"
1695     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1696   $column_data{paid} =
1697     "<th class=listsubtotal align=right>"
1698     . $form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;") . "</th>";
1699   $column_data{due} =
1700     "<th class=listsubtotal align=right>"
1701     . $form->format_amount(\%myconfig, $subtotaldue, 2, "&nbsp;") . "</th>";
1702
1703   $subtotalnetamount = 0;
1704   $subtotalamount    = 0;
1705   $subtotalpaid      = 0;
1706   $subtotaldue       = 0;
1707
1708   $sameitem = $ar->{ $form->{sort} };
1709
1710   print "<tr class=listsubtotal>";
1711
1712   map { print "\n$column_data{$_}" } @column_index;
1713
1714   print "
1715 </tr>
1716 ";
1717
1718   $lxdebug->leave_sub();
1719 }