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