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