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