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