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