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