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