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