8c1249334c3d414291c3d3d723ca13c9246849e4
[kivitendo-erp.git] / bin / mozilla / is.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) 1998-2002
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 # Inventory invoicing module
31 #
32 #======================================================================
33
34 use SL::IS;
35 use SL::PE;
36 use Data::Dumper;
37
38 require "$form->{path}/io.pl";
39 require "$form->{path}/arap.pl";
40 require "bin/mozilla/drafts.pl";
41
42 1;
43
44 # end of main
45
46 sub add {
47   $lxdebug->enter_sub();
48
49   return $lxdebug->leave_sub() if (load_draft_maybe());
50
51   if ($form->{type} eq "credit_note") {
52     $form->{title} = $locale->text('Add Credit Note');
53
54     if ($form->{storno}) {
55       $form->{title} = $locale->text('Add Storno Credit Note');
56     }
57   } else {
58     $form->{title} = $locale->text('Add Sales Invoice');
59
60   }
61
62
63   $form->{callback} =
64     "$form->{script}?action=add&type=$form->{type}&login=$form->{login}&path=$form->{path}&password=$form->{password}"
65     unless $form->{callback};
66
67   $form{jsscript} = "date";
68
69   if ($myconfig{acs} =~ "AR--Add Sales Invoice" || $myconfig{acs} =~ "AR--AR")
70   {
71     $form->error("Access Denied");
72   }
73   &invoice_links;
74   &prepare_invoice;
75   &display_form;
76
77   $lxdebug->leave_sub();
78 }
79
80 sub edit {
81   $lxdebug->enter_sub();
82
83   # show history button
84   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
85   #/show hhistory button
86   
87   if ($myconfig{acs} =~ "AR--Add Sales Invoice" || $myconfig{acs} =~ "AR--AR")
88   {
89     $form->error("Access Denied");
90   }
91   $edit = 1;
92   if ($form->{print_and_post}) {
93     $form->{action}   = "print";
94     $form->{resubmit} = 1;
95     $language_id = $form->{language_id};
96     $printer_id = $form->{printer_id};
97   }
98   &invoice_links;
99   &prepare_invoice;
100   if ($form->{print_and_post}) {
101     $form->{language_id} = $language_id;
102     $form->{printer_id} = $printer_id;
103   }
104
105   &display_form;
106
107   $lxdebug->leave_sub();
108 }
109
110 sub invoice_links {
111   $lxdebug->enter_sub();
112
113   $form->{vc} = 'customer';
114
115   # create links
116   $form->{webdav}   = $webdav;
117   $form->{lizenzen} = $lizenzen;
118
119   $form->create_links("AR", \%myconfig, "customer");
120
121   if ($form->{all_customer}) {
122     unless ($form->{customer_id}) {
123       $form->{customer_id} = $form->{all_customer}->[0]->{id};
124       $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
125     }
126   }
127
128   if ($form->{payment_id}) {
129     $payment_id = $form->{payment_id};
130   }
131   if ($form->{language_id}) {
132     $language_id = $form->{language_id};
133   }
134   if ($form->{taxzone_id}) {
135     $taxzone_id = $form->{taxzone_id};
136   }
137   if ($form->{id}) {
138     $id = $form->{id};
139   }
140   if ($form->{shipto_id}) {
141     $shipto_id = $form->{shipto_id};
142   }
143
144   $cp_id = $form->{cp_id};
145   IS->get_customer(\%myconfig, \%$form);
146
147   #quote all_customer Bug 133
148   foreach $ref (@{ $form->{all_customer} }) {
149     $ref->{name} = $form->quote($ref->{name});
150   }
151   if ($id) {
152     $form->{id} = $id;
153   }
154   IS->retrieve_invoice(\%myconfig, \%$form);
155   $form->{cp_id} = $cp_id;
156
157   if ($payment_id) {
158     $form->{payment_id} = $payment_id;
159   }
160   if ($language_id) {
161     $form->{language_id} = $language_id;
162   }
163   if ($taxzone_id) {
164     $form->{taxzone_id} = $taxzone_id;
165   }
166   if ($shipto_id) {
167     $form->{shipto_id} = $shipto_id;
168   }
169
170   # currencies
171   @curr = split(/:/, $form->{currencies});
172   chomp $curr[0];
173   $form->{defaultcurrency} = $curr[0];
174
175   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
176
177   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
178
179   if (@{ $form->{all_customer} }) {
180     $form->{customer} = "$form->{customer}--$form->{customer_id}";
181     map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
182       (@{ $form->{all_customer} });
183   }
184
185   # departments
186   if ($form->{all_departments}) {
187     $form->{selectdepartment} = "<option>\n";
188     $form->{department}       = "$form->{department}--$form->{department_id}";
189
190     map {
191       $form->{selectdepartment} .=
192         "<option>$_->{description}--$_->{id}\n"
193     } (@{ $form->{all_departments} });
194   }
195
196   $form->{employee} = "$form->{employee}--$form->{employee_id}";
197
198   # sales staff
199   if ($form->{all_employees}) {
200     $form->{selectemployee} = "";
201     map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
202       (@{ $form->{all_employees} });
203   }
204
205   # forex
206   $form->{forex} = $form->{exchangerate};
207   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
208
209   foreach $key (keys %{ $form->{AR_links} }) {
210     foreach $ref (@{ $form->{AR_links}{$key} }) {
211       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
212     }
213
214     if ($key eq "AR_paid") {
215       for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
216         $form->{"AR_paid_$i"} =
217           "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
218
219         # reverse paid
220         $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
221         $form->{"datepaid_$i"} =
222           $form->{acc_trans}{$key}->[$i - 1]->{transdate};
223         $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
224           $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
225         $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
226         $form->{"memo_$i"}   = $form->{acc_trans}{$key}->[$i - 1]->{memo};
227
228         $form->{paidaccounts} = $i;
229       }
230     } else {
231       $form->{$key} =
232         "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
233     }
234
235   }
236
237   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
238
239   $form->{AR} = $form->{AR_1} unless $form->{id};
240
241   $form->{locked} =
242     ($form->datetonum($form->{invdate}, \%myconfig) <=
243      $form->datetonum($form->{closedto}, \%myconfig));
244
245   $lxdebug->leave_sub();
246 }
247
248 sub prepare_invoice {
249   $lxdebug->enter_sub();
250
251   if ($form->{type} eq "credit_note") {
252     $form->{type}     = "credit_note";
253     $form->{formname} = "credit_note";
254   } else {
255     $form->{type}     = "invoice";
256     $form->{formname} = "invoice";
257   }
258
259   if ($form->{id}) {
260
261     map { $form->{$_} =~ s/\"/&quot;/g }
262       qw(invnumber ordnumber quonumber shippingpoint shipvia notes intnotes);
263
264     #     # get pricegroups for parts
265     #     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
266
267     my $i = 0;
268
269     foreach $ref (@{ $form->{invoice_details} }) {
270       $i++;
271
272       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
273       $form->{"discount_$i"} =
274         $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
275       ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
276       $dec           = length $dec;
277       $decimalplaces = ($dec > 2) ? $dec : 2;
278
279       $form->{"sellprice_$i"} =
280         $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
281                              $decimalplaces);
282
283       (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
284       $dec_qty = length $dec_qty;
285
286       $form->{"qty_$i"} =
287         $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
288
289       map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
290         qw(partnumber description unit partnotes);
291       $form->{rowcount} = $i;
292
293     }
294   }
295   $lxdebug->leave_sub();
296 }
297
298 sub form_header {
299   $lxdebug->enter_sub();
300
301   if ($edit) {
302
303     if ($form->{type} eq "credit_note") {
304       $form->{title} = $locale->text('Edit Credit Note');
305     
306       if ($form->{storno}) {
307         $form->{title} = $locale->text('Edit Storno Credit Note');
308       }
309     } else {
310       $form->{title} = $locale->text('Edit Sales Invoice');
311     
312       if ($form->{storno}) {
313         $form->{title} = $locale->text('Edit Storno Invoice');
314       }
315     }
316   }
317
318   $form->{radier} =
319     ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
320
321   $payment = qq|<option value=""></option>|;
322   foreach $item (@{ $form->{payment_terms} }) {
323     if ($form->{payment_id} eq $item->{id}) {
324       $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
325     } else {
326       $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
327     }
328   }
329
330   my $set_duedate_url =
331     "$form->{script}?login=$form->{login}&path=$form->{path}&password=$form->{password}&action=set_duedate";
332
333   my $pjx = new CGI::Ajax( 'set_duedate' => $set_duedate_url );
334   push(@ { $form->{AJAX} }, $pjx);
335
336   if (@{ $form->{TAXZONE} }) {
337     $form->{selecttaxzone} = "";
338     foreach $item (@{ $form->{TAXZONE} }) {
339       if ($item->{id} == $form->{taxzone_id}) {
340         $form->{selecttaxzone} .=
341           "<option value=$item->{id} selected>" . H($item->{description}) .
342           "</option>";
343       } else {
344         $form->{selecttaxzone} .=
345           "<option value=$item->{id}>" . H($item->{description}) . "</option>";
346       }
347
348     }
349   } else {
350     $form->{selecttaxzone} =~ s/ selected//g;
351     if ($form->{taxzone_id} ne "") {
352       $form->{selecttaxzone} =~ s/value=\"$form->{taxzone_id}\"/value=\"$form->{taxzone_id}\" selected/;
353     }
354   }
355
356   $taxzone = qq|
357               <tr>
358                 <th align="right">| . $locale->text('Steuersatz') . qq|</th>
359                 <td><select name="taxzone_id">$form->{selecttaxzone}</select></td>
360                 <input type="hidden" name="selecttaxzone" value="$form->{selecttaxzone}">
361               </tr>|;
362
363   my @old_project_ids = ($form->{"globalproject_id"});
364   map({ push(@old_project_ids, $form->{"project_id_$_"})
365           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
366
367   $form->get_lists("contacts" => "ALL_CONTACTS",
368                    "shipto" => "ALL_SHIPTO",
369                    "projects" => { "key" => "ALL_PROJECTS",
370                                    "all" => 0,
371                                    "old_id" => \@old_project_ids },
372                    "employees" => "ALL_SALESMEN");
373
374   my (%labels, @values);
375   foreach my $item (@{ $form->{"ALL_CONTACTS"} }) {
376     push(@values, $item->{"cp_id"});
377     $labels{$item->{"cp_id"}} = $item->{"cp_name"} .
378       ($item->{"cp_abteilung"} ? " ($item->{cp_abteilung})" : "");
379   }
380   my $contact =
381     NTI($cgi->popup_menu('-name' => 'cp_id', '-values' => \@values,
382                          '-labels' => \%labels, '-default' => $form->{"cp_id"}));
383
384   %labels = ();
385   @values = ("");
386   foreach my $item (@{ $form->{"ALL_SHIPTO"} }) {
387     push(@values, $item->{"shipto_id"});
388     $labels{$item->{"shipto_id"}} =
389       $item->{"shiptoname"} . " " . $item->{"shiptodepartment_1"};
390   }
391
392   my $shipto = qq|
393                 <th align=right>| . $locale->text('Shipping Address') . qq|</th>
394                 <td>| .
395     NTI($cgi->popup_menu('-name' => 'shipto_id', '-values' => \@values,
396                          '-labels' => \%labels, '-default' => $form->{"shipto_id"}))
397     . qq|</td>|;
398
399   %labels = ();
400   @values = ("");
401   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
402     push(@values, $item->{"id"});
403     $labels{$item->{"id"}} = $item->{"projectnumber"};
404   }
405   my $globalprojectnumber =
406     NTI($cgi->popup_menu('-name' => 'globalproject_id', '-values' => \@values,
407                          '-labels' => \%labels,
408                          '-default' => $form->{"globalproject_id"}));
409
410   %labels = ();
411   @values = ("");
412   foreach my $item (@{ $form->{ALL_SALESMEN} }) {
413     push(@values, $item->{id});
414     $labels{$item->{id}} = $item->{name} ne "" ? $item->{name} : $item->{login};
415   }
416
417   $salesman =
418     qq|<tr>
419           <th align="right">| . $locale->text('Salesman') . qq|</th>
420           <td>| .
421      NTI($cgi->popup_menu('-name' => 'salesman_id', '-default' => $form->{salesman_id},
422                                '-values' => \@values, '-labels' => \%labels))
423      . qq|</td>
424          </tr>|;
425
426   # set option selected
427   foreach $item (qw(AR customer currency department employee)) {
428     $form->{"select$item"} =~ s/ selected//;
429     $form->{"select$item"} =~
430       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
431   }
432
433   #quote customer Bug 133
434   $form->{selectcustomer} = $form->quote($form->{selectcustomer});
435   
436   if (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) {
437     $creditwarning = 1;
438   } else {
439     $creditwarning = 0;
440   }
441
442   $form->{exchangerate} =
443     $form->format_amount(\%myconfig, $form->{exchangerate});
444
445   $form->{creditlimit} =
446     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
447   $form->{creditremaining} =
448     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
449
450   $exchangerate = "";
451   if ($form->{currency} ne $form->{defaultcurrency}) {
452     if ($form->{forex}) {
453       $exchangerate .=
454           qq|<th align="right">|
455         . $locale->text('Exchangerate')
456         . qq|</th><td>$form->{exchangerate}<input type="hidden" name="exchangerate" value="$form->{exchangerate}"></td>|;
457     } else {
458       $exchangerate .=
459           qq|<th align="right">|
460         . $locale->text('Exchangerate')
461         . qq|</th><td><input name="exchangerate" size="10" value="$form->{exchangerate}"></td>|;
462     }
463   }
464   $exchangerate .= qq|
465 <input type="hidden" name="forex" value="$form->{forex}">
466 |;
467
468   $customer =
469     ($form->{selectcustomer})
470     ? qq|<select name="customer">$form->{selectcustomer}</select>\n<input type="hidden" name="selectcustomer" value="$form->{selectcustomer}">|
471     : qq|<input name="customer" value="$form->{customer}" size="35">|;
472
473   $department = qq|
474               <tr>
475                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
476                 <td colspan="3"><select name="department">$form->{selectdepartment}</select>
477                 <input type="hidden" name="selectdepartment" value="$form->{selectdepartment}">
478                 </td>
479               </tr>
480 | if $form->{selectdepartment};
481
482   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
483
484   if ($form->{business}) {
485     $business = qq|
486               <tr>
487                 <th align="right">| . $locale->text('Business') . qq|</th>
488                 <td>$form->{business}</td>
489                 <th align="right">| . $locale->text('Trade Discount') . qq|</th>
490                 <td>|
491       . $form->format_amount(\%myconfig, $form->{tradediscount} * 100)
492       . qq| %</td>
493               </tr>
494 |;
495   }
496
497   if ($form->{max_dunning_level}) {
498     $dunning = qq|
499               <tr>
500                 <td colspan="4">
501                 <table>
502                   <tr>
503                 <th align="right">| . $locale->text('Max. Dunning Level') . qq|:</th>
504                 <td><b>$form->{max_dunning_level}</b></td>
505                 <th align="right">| . $locale->text('Dunning Amount') . qq|:</th>
506                 <td><b>|
507       . $form->format_amount(\%myconfig, $form->{dunning_amount},2)
508       . qq|</b></td>
509               </tr>
510               </table>
511              </td>
512             </tr>
513 |;
514   }
515
516   $form->{fokus} = "invoice.customer";
517
518   # use JavaScript Calendar or not
519   $form->{jsscript} = $jscalendar;
520   $jsscript = "";
521   if ($form->{type} eq "credit_note") {
522     if ($form->{jsscript}) {
523   
524       # with JavaScript Calendar
525       $button1 = qq|
526         <td><input name="invdate" id="invdate" size="11" title="$myconfig{dateformat}" value="$form->{invdate}"></td>
527         <td><input type="button" name="invdate" id="trigger1" value="|
528         . $locale->text('button') . qq|"></td>|;
529        
530       #write Trigger
531       $jsscript =
532         Form->write_trigger(\%myconfig,     "1",
533                             "invdate",      "BL",
534                             "trigger1");
535     } else {
536   
537       # without JavaScript Calendar
538       $button1 =
539         qq|<td><input name="invdate" size="11" title="$myconfig{dateformat}" value="$form->{invdate}"></td>|;
540       $button2 =
541         qq|<td width="13"><input name="duedate" size="11" title="$myconfig{dateformat}" value="$form->{duedate}"></td>|;
542     }
543   } else {
544     if ($form->{jsscript}) {
545   
546       # with JavaScript Calendar
547       $button1 = qq|
548         <td><input name="invdate" id="invdate" size="11" title="$myconfig{dateformat}" value="$form->{invdate}"></td>
549         <td><input type="button" name="invdate" id="trigger1" value="|
550         . $locale->text('button') . qq|"></td>
551         |;
552       $button2 = qq|
553         <td width="13"><input name="duedate" id="duedate" size="11" title="$myconfig{dateformat}" value="$form->{duedate}"></td>
554         <td width="4"><input type="button" name="duedate" id="trigger2" value="|
555         . $locale->text('button') . qq|"></td></td>
556       |;
557       $button3 = qq|
558         <td width="13"><input name="deliverydate" id="deliverydate" size="11" title="$myconfig{dateformat}" value="$form->{deliverydate}"></td>
559         <td width="4"><input type="button" name="deliverydate" id="trigger3" value="|
560         . $locale->text('button') . qq|"></td></td>
561       |;
562   
563       #write Trigger
564       $jsscript =
565         Form->write_trigger(\%myconfig,     "3",
566                             "invdate",      "BL",
567                             "trigger1",     "duedate",
568                             "BL",           "trigger2",
569                             "deliverydate", "BL",
570                             "trigger3");
571     } else {
572   
573       # without JavaScript Calendar
574       $button1 =
575         qq|<td><input name="invdate" size="11" title="$myconfig{dateformat}" value="$form->{invdate}"></td>|;
576       $button2 =
577         qq|<td width="13"><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>|;
578     }
579   }
580   if ($form->{resubmit} && ($form->{format} eq "html")) {
581     $onload =
582       qq|window.open('about:blank','Beleg'); document.invoice.target = 'Beleg';document.invoice.submit()|;
583   } elsif ($form->{resubmit}) {
584     $onload = qq|document.invoice.submit()|;
585   } else {
586     $onload = "fokus()";
587   }
588
589   $credittext = $locale->text('Credit Limit exceeded!!!');
590   if ($creditwarning) {
591     $onload = qq|alert('$credittext')|;
592   }
593
594   $form->{"javascript"} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
595
596   $jsscript .=
597     $form->write_trigger(\%myconfig, 2,
598                          "orddate", "BL", "trigger_orddate",
599                          "quodate", "BL", "trigger_quodate");
600   # show history button js
601   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
602   #/show history button js
603   $form->header;
604
605   print qq|
606 <body onLoad="$onload">
607 <script type="text/javascript" src="js/common.js"></script>
608 <script type="text/javascript" src="js/delivery_customer_selection.js"></script>
609 <script type="text/javascript" src="js/vendor_selection.js"></script>
610 <script type="text/javascript" src="js/calculate_qty.js"></script>
611
612 <form method="post" name="invoice" action="$form->{script}">
613 | ;
614 map({print $cgi->hidden("-name" => $_ , "-value" => $form->{$_});} 
615      qw(id action type media format queued printed emailed title vc discount 
616         creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id)) ;
617 print ($form->{saved_message} ? qq|<p>$form->{saved_message}</p>| : "") ;
618 print qq|
619
620 <input type="hidden" name="lizenzen" value="$lizenzen">
621
622 <table width="100%">
623   <tr class="listtop">
624     <th class="listtop">$form->{title}</th>
625   </tr>
626   <tr height="5"></tr>
627   <tr>
628     <td>
629       <table width="100%">
630         <tr valign="top">
631           <td>
632             <table>
633               <tr>
634                 <th align="right" nowrap>| . $locale->text('Customer') . qq|</th>
635                 <td colspan="3">$customer</td>
636     <input type="hidden" name="customer_klass" value="$form->{customer_klass}">
637                 <input type="hidden" name="customer_id" value="$form->{customer_id}">
638                 <input type="hidden" name="oldcustomer" value="$form->{oldcustomer}">
639                 <th align="right" nowrap>|
640     . $locale->text('Contact Person') . qq|</th>
641                 <td colspan="3">$contact</td>
642               </tr>
643               <tr>
644                 <td></td>
645                 <td colspan="3">
646                   <table>
647                     <tr>
648                       <th nowrap>| . $locale->text('Credit Limit') . qq|</th>
649                       <td>$form->{creditlimit}</td>
650                       <td width="20%"></td>
651                       <th nowrap>| . $locale->text('Remaining') . qq|</th>
652                       <td class="plus$n">$form->{creditremaining}</td>
653                     </tr>
654                   </table>
655                 </td>
656                 $shipto
657               </tr>
658               $business
659               $dunning
660               <tr>
661                 <th align="right" nowrap>| . $locale->text('Record in') . qq|</th>
662                 <td colspan="3"><select name="AR" style="width:280px;">$form->{selectAR}</select></td>
663                 <input type="hidden" name="selectAR" value="$form->{selectAR}">
664               </tr>
665               $taxzone
666               $department
667               <tr>
668                 <th align="right" nowrap>| . $locale->text('Currency') . qq|</th>
669                 <td><select name="currency">$form->{selectcurrency}</select></td>
670                 <input type="hidden" name="selectcurrency" value="$form->{selectcurrency}">
671                 <input type="hidden" name="defaultcurrency" value="$form->{defaultcurrency}">
672                 <input type="hidden" name="fxgain_accno" value="$form->{fxgain_accno}">
673                 <input type="hidden" name="fxloss_accno" value="$form->{fxloss_accno}">
674                 $exchangerate
675               </tr>
676               <tr>
677                 <th align="right" nowrap>| . $locale->text('Shipping Point') . qq|</th>
678                 <td colspan="3"> | .
679                 $cgi->textfield("-name" => "shippingpoint", "-size" => 35, "-value" => $form->{shippingpoint}) .
680           qq|</tr>
681               <tr>
682                 <th align="right" nowrap>| . $locale->text('Ship via') . qq|</th>
683                 <td colspan="3"><input name="shipvia" size="35" value="$form->{shipvia}"></td>
684               </tr>|;
685 #               <tr>
686 #                 <td colspan=4>
687 #                   <table>
688 #                     <tr>
689 #                       <td colspan=2>
690 #                         <button type="button" onclick="delivery_customer_selection_window('delivery_customer_string','delivery_customer_id')">| . $locale->text('Choose Customer') . qq|</button>
691 #                       </td>
692 #                       <td colspan=2><input type=hidden name=delivery_customer_id value="$form->{delivery_customer_id}">
693 #                       <input size=45 id=delivery_customer_string name=delivery_customer_string value="$form->{delivery_customer_string}"></td>
694 #                     </tr>
695 #                     <tr>
696 #                       <td colspan=2>
697 #                         <button type="button" onclick="vendor_selection_window('delivery_vendor_string','delivery_vendor_id')">| . $locale->text('Choose Vendor') . qq|</button>
698 #                       </td>
699 #                       <td colspan=2><input type=hidden name=delivery_vendor_id value="$form->{delivery_vendor_id}">
700 #                       <input size=45 id=delivery_vendor_string name=delivery_vendor_string value="$form->{delivery_vendor_string}"></td>
701 #                     </tr>
702 #                   </table>
703 #                 </td>
704 #               </tr>
705 print qq|           </table>
706           </td>
707           <td align="right">
708             <table>
709               <tr>
710                 <th align="right" nowrap>| . $locale->text('Employee') . qq|</th>
711                 <td colspan="2"><select name="employee">$form->{selectemployee}</select></td>
712                 <input type="hidden" name="selectemployee" value="$form->{selectemployee}">
713                 <td></td>
714               </tr>
715         $salesman
716 |;
717 if ($form->{type} eq "credit_note") {
718 print qq|     <tr>
719                 <th align="right" nowrap>| . $locale->text('Credit Note Number') . qq|</th>
720                 <td><input name="invnumber" size="11" value="$form->{invnumber}"></td>
721               </tr>
722               <tr>
723                 <th align="right">| . $locale->text('Credit Note Date') . qq|</th>
724                 $button1
725               </tr>|;
726 } else {
727 print qq|     <tr>
728                 <th align="right" nowrap>| . $locale->text('Invoice Number') . qq|</th>
729                 <td><input name="invnumber" size="11" value="$form->{invnumber}"></td>
730               </tr>
731               <tr>
732                 <th align="right">| . $locale->text('Invoice Date') . qq|</th>
733                 $button1
734               </tr>
735               <tr>
736                 <th align="right">| . $locale->text('Due Date') . qq|</th>
737                 $button2
738               </tr>
739               <tr>
740                 <th align="right">| . $locale->text('Delivery Date') . qq|</th>
741                 $button3
742               </tr>|;
743 }
744 print qq|     <tr>
745                 <th align="right" nowrap>| . $locale->text('Order Number') . qq|</th>
746                 <td><input name="ordnumber" size="11" value="$form->{ordnumber}"></td>
747               </tr>
748         <tr>
749           <th align="right" nowrap>| . $locale->text('Order Date') . qq|</th>
750           <td><input name="orddate" id="orddate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{orddate}) . qq|"></td>
751           <td><input type="button" name="b_orddate" id="trigger_orddate" value="?"></td>
752         </tr>
753               <tr>
754                 <th align="right" nowrap>| . $locale->text('Quotation Number') . qq|</th>
755                 <td><input name="quonumber" size="11" value="$form->{quonumber}"></td>
756               </tr>
757         <tr>
758           <th align="right" nowrap>| . $locale->text('Quotation Date') . qq|</th>
759           <td><input name="quodate" id="quodate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{quodate}) . qq|"></td>
760           <td><input type="button" name="b_quodate" id="trigger_quodate" value="?"></td>
761         </tr>
762               <tr>
763                 <th align="right" nowrap>| . $locale->text('Customer Order Number') . qq|</th>
764                 <td><input name="cusordnumber" size="11" value="$form->{cusordnumber}"></td>
765               </tr>
766               <tr>
767           <th align="right" nowrap>| . $locale->text('Project Number') . qq|</th>
768           <td>$globalprojectnumber</td>
769               </tr>
770             </table>
771           </td>
772         </tr>
773       </table>
774     </td>
775   </tr>
776   <tr>
777     <td>
778     </td>
779   </tr>
780 | . 
781 $jsscript
782 . qq|
783 <!-- shipto are in hidden variables -->
784 | ;
785 map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } 
786        qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry 
787           shiptocontact shiptophone shiptofax shiptoemail));
788 print qq|<!-- email variables --> |;
789 map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } 
790     qw(message email subject cc bcc taxaccounts));
791 print qq|<input type="hidden" name="webdav" value="| . $webdav . qq|">|;
792
793   foreach $item (split(/ /, $form->{taxaccounts})) {
794     map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } 
795     ("${item}_rate", "${item}_description", "${item}_taxnumber"));
796   }
797   $lxdebug->leave_sub();
798 }
799
800 sub form_footer {
801   $lxdebug->enter_sub();
802
803   $form->{invtotal} = $form->{invsubtotal};
804
805   if (($rows = $form->numtextrows($form->{notes}, 26, 8)) < 2) {
806     $rows = 2;
807   }
808   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
809     $introws = 2;
810   }
811   $rows = ($rows > $introws) ? $rows : $introws;
812   $notes =
813     qq|<textarea name="notes" rows="$rows" cols="26" wrap="soft">$form->{notes}</textarea>|;
814   $intnotes =
815     qq|<textarea name="intnotes" rows="$rows" cols="35" wrap="soft">$form->{intnotes}</textarea>|;
816
817   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
818
819   $taxincluded = "";
820   if ($form->{taxaccounts}) {
821     $taxincluded = qq|
822                 <input name="taxincluded" class="checkbox" type="checkbox" value=$form->{taxincluded}> <b>|
823       . $locale->text('Tax Included') . qq|</b><br><br>|;
824   }
825
826   if (!$form->{taxincluded}) {
827
828     foreach $item (split / /, $form->{taxaccounts}) {
829       if ($form->{"${item}_base"}) {
830         $form->{"${item}_total"} =
831           $form->round_amount(
832                              $form->{"${item}_base"} * $form->{"${item}_rate"},
833                              2);
834         $form->{invtotal} += $form->{"${item}_total"};
835         $form->{"${item}_total"} =
836           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
837
838         $tax .= qq|
839               <tr>
840                 <th align="right">$form->{"${item}_description"}&nbsp;|
841                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
842                 <td align="right">$form->{"${item}_total"}</td>
843               </tr>
844 |;
845       }
846     }
847
848     $form->{invsubtotal} =
849       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
850
851     $subtotal = qq|
852               <tr>
853                 <th align="right">| . $locale->text('Subtotal') . qq|</th>
854                 <td align="right">$form->{invsubtotal}</td>
855               </tr>
856 |;
857
858   }
859
860   if ($form->{taxincluded}) {
861     foreach $item (split / /, $form->{taxaccounts}) {
862       if ($form->{"${item}_base"}) {
863         $form->{"${item}_total"} =
864           $form->round_amount(
865                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
866                               (1 + $form->{"${item}_rate"})
867                            ),
868                            2);
869         $form->{"${item}_netto"} =
870           $form->round_amount(
871                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
872                           2);
873         $form->{"${item}_total"} =
874           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
875         $form->{"${item}_netto"} =
876           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
877
878         $tax .= qq|
879               <tr>
880                 <th align="right">Enthaltene $form->{"${item}_description"}&nbsp;|
881                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
882                 <td align="right">$form->{"${item}_total"}</td>
883               </tr>
884               <tr>
885                 <th align="right">Nettobetrag</th>
886                 <td align="right">$form->{"${item}_netto"}</td>
887               </tr>
888 |;
889       }
890     }
891
892   }
893
894   $form->{oldinvtotal} = $form->{invtotal};
895   $form->{invtotal}    =
896     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
897
898   print qq|
899   <tr>
900     <td>
901       <table width="100%">
902         <tr valign="bottom">
903           <td>
904             <table>
905               <tr>
906                 <th align="left">| . $locale->text('Notes') . qq|</th>
907                 <th align="left">| . $locale->text('Internal Notes') . qq|</th>
908                 <th align="right">| . $locale->text('Payment Terms') . qq|</th>
909               </tr>
910               <tr valign="top">
911                 <td>$notes</td>
912                 <td>$intnotes</td>
913                 <td><select name="payment_id" onChange="if (this.value) set_duedate(['payment_id__' + this.value],['duedate'])">$payment
914                 </select></td>
915               </tr>
916             </table>
917           </td>
918           <td align="right" width="100%">
919             $taxincluded
920             <table width="100%">
921               $subtotal
922               $tax
923               <tr>
924                 <th align="right">| . $locale->text('Total') . qq|</th>
925                 <td align="right">$form->{invtotal}</td>
926               </tr>
927             </table>
928           </td>
929         </tr>
930       </table>
931     </td>
932   </tr>
933 |;
934   if ($webdav) {
935     $webdav_list = qq|
936   <tr>
937     <td><hr size="3" noshade></td>
938   </tr>
939   <tr>
940     <th class="listtop" align="left">Dokumente im Webdav-Repository</th>
941   </tr>
942     <table width="100%">
943       <td align="left" width="30%"><b>Dateiname</b></td>
944       <td align="left" width="70%"><b>Webdavlink</b></td>
945 |;
946     foreach $file (keys %{ $form->{WEBDAV} }) {
947       $webdav_list .= qq|
948       <tr>
949         <td align="left">$file</td>
950         <td align="left"><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
951       </tr>
952 |;
953     }
954     $webdav_list .= qq|
955     </table>
956   </tr>
957 |;
958
959     print $webdav_list;
960   }
961 if ($form->{type} eq "credit_note") {
962   print qq|
963   <tr>
964     <td>
965       <table width="100%">
966         <tr class="listheading">
967           <th colspan="6" class="listheading">|
968     . $locale->text('Payments') . qq|</th>
969         </tr>
970 |;
971 } else {
972   print qq|
973   <tr>
974     <td>
975       <table width="100%">
976         <tr class="listheading">
977           <th colspan="6" class="listheading">|
978     . $locale->text('Incoming Payments') . qq|</th>
979         </tr>
980 |;
981 }
982
983   if ($form->{currency} eq $form->{defaultcurrency}) {
984     @column_index = qw(datepaid source memo paid AR_paid);
985   } else {
986     @column_index = qw(datepaid source memo paid exchangerate AR_paid);
987   }
988
989   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
990   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
991   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
992   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
993   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
994   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
995
996   print "
997         <tr>
998 ";
999   map { print "$column_data{$_}\n" } @column_index;
1000   print "
1001         </tr>
1002 ";
1003
1004   my @triggers = ();
1005
1006   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
1007   for $i (1 .. $form->{paidaccounts}) {
1008
1009     print "
1010         <tr>\n";
1011
1012     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
1013     $form->{"selectAR_paid_$i"} =~
1014       s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
1015
1016     # format amounts
1017     $totalpaid += $form->{"paid_$i"};
1018     if ($form->{"paid_$i"}) {
1019       $form->{"paid_$i"} =
1020         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
1021     }
1022     $form->{"exchangerate_$i"} =
1023       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
1024
1025     $exchangerate = qq|&nbsp;|;
1026     if ($form->{currency} ne $form->{defaultcurrency}) {
1027       if ($form->{"forex_$i"}) {
1028         $exchangerate = qq|<input type="hidden" name="exchangerate_$i" value="$form->{"exchangerate_$i"}">$form->{"exchangerate_$i"}|;
1029       } else {
1030         $exchangerate = qq|<input name="exchangerate_$i" size="10" value="$form->{"exchangerate_$i"}">|;
1031       }
1032     }
1033
1034     $exchangerate .= qq|<input type="hidden" name="forex_$i" value="$form->{"forex_$i"}">|;
1035
1036     $column_data{"paid_$i"} =
1037       qq|<td align="center"><input name="paid_$i" size="11" value="$form->{"paid_$i"}"></td>|;
1038     $column_data{"exchangerate_$i"} = qq|<td align="center">$exchangerate</td>|;
1039     $column_data{"AR_paid_$i"}      =
1040       qq|<td align="center"><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
1041     $column_data{"datepaid_$i"} =
1042       qq|<td align="center"><input id="datepaid_$i" name="datepaid_$i"  size="11" title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}>
1043          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
1044     $column_data{"source_$i"} =
1045       qq|<td align=center><input name="source_$i" size="11" value="$form->{"source_$i"}"></td>|;
1046     $column_data{"memo_$i"} =
1047       qq|<td align="center"><input name="memo_$i" size="11" value="$form->{"memo_$i"}"></td>|;
1048
1049     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
1050     print "
1051         </tr>\n";
1052     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
1053   }
1054
1055   map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } qw(paidaccounts selectAR_paid oldinvtotal)); 
1056   print qq|<input type="hidden" name="oldtotalpaid" value="$totalpaid">
1057     </table>
1058     </td>
1059   </tr>
1060   <tr>
1061     <td><hr size="3" noshade></td>
1062   </tr>
1063   <tr>
1064     <td>
1065 |;
1066
1067   &print_options;
1068
1069   print qq|
1070     </td>
1071   </tr>
1072 </table>
1073 |;
1074
1075   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1076   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1077
1078   if ($form->{id}) {
1079     my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar");
1080
1081     print qq|
1082     <input class="submit" type="submit" accesskey="u" name="action" id="update_button" value="|
1083       . $locale->text('Update') . qq|">
1084     <input class="submit" type="submit" name="action" value="|
1085       . $locale->text('Ship to') . qq|">
1086     <input class="submit" type="submit" name="action" value="|
1087       . $locale->text('Print') . qq|">
1088     <input class="submit" type="submit" name="action" value="|
1089       . $locale->text('E-mail') . qq|"> |;
1090     print qq|<input class="submit" type="submit" name="action" value="|
1091       . $locale->text('Storno') . qq|"> | if ($show_storno);
1092     print qq|<input class="submit" type="submit" name="action" value="|
1093       . $locale->text('Post Payment') . qq|">
1094 |;
1095     print qq|<input class="submit" type="submit" name="action" value="|
1096       . $locale->text('Use As Template') . qq|">
1097 |;
1098   if ($form->{id} && !($form->{type} eq "credit_note")) {
1099     print qq|
1100     <input class="submit" type="submit" name="action" value="|
1101       . $locale->text('Credit Note') . qq|">
1102 |;
1103   }
1104     if ($form->{radier}) {
1105     print qq|
1106     <input class="submit" type="submit" name="action" value="|
1107       . $locale->text('Delete') . qq|">
1108 |;
1109   }
1110
1111
1112     if ($invdate > $closedto) {
1113       print qq|
1114       <input class="submit" type="submit" name="action" value="|
1115         . $locale->text('Order') . qq|">
1116 |;
1117     }
1118
1119   } else {
1120     if ($invdate > $closedto) {
1121       print qq|<input class="submit" type="submit" name="action" id="update_button" value="|
1122         . $locale->text('Update') . qq|">
1123       <input class="submit" type="submit" name="action" value="|
1124         . $locale->text('Ship to') . qq|">
1125       <input class="submit" type="submit" name="action" value="|
1126         . $locale->text('Preview') . qq|">
1127       <input class="submit" type="submit" name="action" value="|
1128         . $locale->text('E-mail') . qq|">
1129       <input class="submit" type="submit" name="action" value="|
1130         . $locale->text('Print and Post') . qq|">
1131       <input class="submit" type="submit" name="action" value="|
1132         . $locale->text('Post') . qq|"> | .
1133         NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
1134                          '-class' => 'submit'));
1135     }
1136   }
1137
1138   # button for saving history
1139   if($form->{id} ne "") {
1140     print qq|
1141           <input type="button" class="submit" onclick="set_history_window(|
1142           . Q($form->{id})
1143           . qq|);" name="history" id="history" value="|
1144           . $locale->text('history')
1145           . qq|">|;
1146   }
1147   # /button for saving history
1148
1149
1150   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
1151     qq|
1152
1153 <input type="hidden" name="rowcount" value="$form->{rowcount}">
1154 | .
1155 $cgi->hidden("-name" => "callback", "-value" => $form->{callback}) 
1156 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
1157 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}]);
1158 map({ print $cgi->hidden("-name" => $_ , "-value" => $form->{$_});} qw(path login password));
1159 print qq|
1160 </form>
1161
1162 </body>
1163
1164  </html>
1165 |;
1166
1167   $lxdebug->leave_sub();
1168 }
1169
1170 sub update {
1171   $lxdebug->enter_sub();
1172
1173   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1174     qw(exchangerate creditlimit creditremaining);
1175   if ($form->{second_run}) {
1176     $form->{print_and_post} = 0;
1177   }
1178
1179   $form->{update} = 1;
1180
1181   &check_name(customer);
1182
1183   $form->{exchangerate} = $exchangerate
1184     if (
1185         $form->{forex} = (
1186                        $exchangerate =
1187                          $form->check_exchangerate(
1188                          \%myconfig, $form->{currency}, $form->{invdate}, 'buy'
1189                          )));
1190
1191   for $i (1 .. $form->{paidaccounts}) {
1192     if ($form->{"paid_$i"}) {
1193       map {
1194         $form->{"${_}_$i"} =
1195           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1196       } qw(paid exchangerate);
1197
1198       $form->{"exchangerate_$i"} = $exchangerate
1199         if (
1200             $form->{"forex_$i"} = (
1201                  $exchangerate =
1202                    $form->check_exchangerate(
1203                    \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy'
1204                    )));
1205     }
1206   }
1207
1208   $i            = $form->{rowcount};
1209   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
1210
1211   # if last row empty, check the form otherwise retrieve new item
1212   if (   ($form->{"partnumber_$i"} eq "")
1213       && ($form->{"description_$i"} eq "")
1214       && ($form->{"partsgroup_$i"}  eq "")) {
1215
1216     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
1217     &check_form;
1218
1219   } else {
1220
1221     IS->retrieve_item(\%myconfig, \%$form);
1222
1223     $rows = scalar @{ $form->{item_list} };
1224
1225     $form->{"discount_$i"} =
1226       $form->format_amount(\%myconfig, $form->{discount} * 100);
1227
1228     if ($rows) {
1229       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
1230
1231       if ($rows > 1) {
1232
1233         &select_item;
1234         exit;
1235
1236       } else {
1237
1238         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1239
1240         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1241           qw(partnumber description unit);
1242         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1243           keys %{ $form->{item_list}[0] };
1244         if ($form->{"part_payment_id_$i"} ne "") {
1245           $form->{payment_id} = $form->{"part_payment_id_$i"};
1246         }
1247
1248         if ($form->{"not_discountable_$i"}) {
1249           $form->{"discount_$i"} = 0;
1250         }
1251
1252         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1253         ($dec) = ($s =~ /\.(\d+)/);
1254         $dec           = length $dec;
1255         $decimalplaces = ($dec > 2) ? $dec : 2;
1256
1257         if ($sellprice) {
1258           $form->{"sellprice_$i"} = $sellprice;
1259         } else {
1260
1261           # if there is an exchange rate adjust sellprice
1262           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1263           $form->{"sellprice_$i"} /= $exchangerate;
1264         }
1265
1266         $form->{"listprice_$i"} /= $exchangerate;
1267
1268         $amount =
1269           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1270           (1 - $form->{"discount_$i"} / 100);
1271         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1272         map { $form->{"${_}_base"} += $amount }
1273           (split / /, $form->{"taxaccounts_$i"});
1274         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1275           split / /, $form->{"taxaccounts_$i"}
1276           if !$form->{taxincluded};
1277
1278         $form->{creditremaining} -= $amount;
1279
1280         map {
1281           $form->{"${_}_$i"} =
1282             $form->format_amount(\%myconfig, $form->{"${_}_$i"},
1283                                  $decimalplaces)
1284         } qw(sellprice listprice);
1285
1286         $form->{"qty_$i"} =
1287           $form->format_amount(\%myconfig, $form->{"qty_$i"});
1288
1289         if ($lizenzen) {
1290           if ($form->{"inventory_accno_$i"} ne "") {
1291             $form->{"lizenzen_$i"} = qq|<option></option>|;
1292             foreach $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
1293               $form->{"lizenzen_$i"} .=
1294                 qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
1295             }
1296             $form->{"lizenzen_$i"} .=
1297               qq|<option value=-1>Neue Lizenz</option>|;
1298           }
1299         }
1300
1301         # get pricegroups for parts
1302         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1303
1304         # build up html code for prices_$i
1305         &set_pricegroup($i);
1306       }
1307
1308       &display_form;
1309
1310     } else {
1311
1312       # ok, so this is a new part
1313       # ask if it is a part or service item
1314
1315       if (   $form->{"partsgroup_$i"}
1316           && ($form->{"partsnumber_$i"} eq "")
1317           && ($form->{"description_$i"} eq "")) {
1318         $form->{rowcount}--;
1319         $form->{"discount_$i"} = "";
1320         &display_form;
1321           } else {
1322
1323         $form->{"id_$i"}   = 0;
1324         $form->{"unit_$i"} = $locale->text('ea');
1325
1326         &new_item;
1327
1328       }
1329     }
1330   }
1331   $lxdebug->leave_sub();
1332 }
1333
1334 sub post_payment {
1335   $lxdebug->enter_sub();
1336   for $i (1 .. $form->{paidaccounts}) {
1337     if ($form->{"paid_$i"}) {
1338       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1339
1340       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1341
1342       $form->error($locale->text('Cannot post payment for a closed period!'))
1343         if ($datepaid <= $closedto);
1344
1345       if ($form->{currency} ne $form->{defaultcurrency}) {
1346         $form->{"exchangerate_$i"} = $form->{exchangerate}
1347           if ($invdate == $datepaid);
1348         $form->isblank("exchangerate_$i",
1349                        $locale->text('Exchangerate for payment missing!'));
1350       }
1351     }
1352   }
1353
1354   ($form->{AR})      = split /--/, $form->{AR};
1355   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1356   relink_accounts();
1357   $form->redirect($locale->text(' Payment posted!'))
1358       if (IS->post_payment(\%myconfig, \%$form));
1359     $form->error($locale->text('Cannot post payment!'));
1360
1361
1362   $lxdebug->leave_sub();
1363 }
1364
1365 sub post {
1366   $lxdebug->enter_sub();
1367   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
1368   $form->isblank("customer", $locale->text('Customer missing!'));
1369
1370   # if oldcustomer ne customer redo form
1371   if (&check_name(customer)) {
1372     &update;
1373     exit;
1374   }
1375   if ($form->{second_run}) {
1376     $form->{print_and_post} = 0;
1377   }
1378
1379   &validate_items;
1380
1381   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1382   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1383
1384   $form->error($locale->text('Cannot post invoice for a closed period!'))
1385     if ($invdate <= $closedto);
1386
1387   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1388     if ($form->{currency} ne $form->{defaultcurrency});
1389
1390   for $i (1 .. $form->{paidaccounts}) {
1391     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1392       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1393
1394       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1395
1396       $form->error($locale->text('Cannot post payment for a closed period!'))
1397         if ($datepaid <= $closedto);
1398
1399       if ($form->{currency} ne $form->{defaultcurrency}) {
1400         $form->{"exchangerate_$i"} = $form->{exchangerate}
1401           if ($invdate == $datepaid);
1402         $form->isblank("exchangerate_$i",
1403                        $locale->text('Exchangerate for payment missing!'));
1404       }
1405     }
1406   }
1407
1408   ($form->{AR})      = split /--/, $form->{AR};
1409   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1410
1411   $form->{label} = $locale->text('Invoice');
1412
1413   $form->{id} = 0 if $form->{postasnew};
1414
1415   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
1416   if ($form->{postasnew}) {
1417     if ($form->{type} eq "credit_note") {
1418       undef($form->{cnnumber});
1419     } else {
1420       undef($form->{invnumber});
1421     }
1422   }
1423
1424   relink_accounts();
1425   $form->error($locale->text('Cannot post invoice!'))
1426     unless IS->post_invoice(\%myconfig, \%$form);
1427   remove_draft() if $form->{remove_draft};
1428
1429   if(!exists $form->{addition}) {
1430     $form->{addition} = $print_post     ? "PRINTED AND POSTED" :
1431                         $form->{storno} ? "STORNO"             :
1432                                           "POSTED";
1433     $form->save_history($form->dbconnect(\%myconfig));
1434   }
1435   
1436   $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
1437     unless $print_post;
1438
1439   $lxdebug->leave_sub();
1440 }
1441
1442 sub print_and_post {
1443   $lxdebug->enter_sub();
1444
1445   $old_form               = new Form;
1446   $print_post             = 1;
1447   $form->{print_and_post} = 1;
1448   &post();
1449
1450   &edit();
1451   $lxdebug->leave_sub();
1452
1453 }
1454
1455 sub use_as_template {
1456   $lxdebug->enter_sub();
1457
1458   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);
1459   $form->{paidaccounts} = 1;
1460   $form->{rowcount}--;
1461   $form->{invdate} = $form->current_date(\%myconfig);
1462   &display_form;
1463
1464   $lxdebug->leave_sub();
1465 }
1466
1467 sub storno {
1468   $lxdebug->enter_sub();
1469
1470   if ($form->{storno}) {
1471     $form->error($locale->text('Cannot storno storno invoice!'));
1472   }
1473
1474   if (IS->has_storno(\%myconfig, $form, "ar")) {
1475     $form->error($locale->text("Invoice has already been storno'd!"));
1476   }
1477
1478   map({ my $key = $_; delete($form->{$key})
1479           unless (grep({ $key eq $_ } qw(path login password id type))); }
1480       keys(%{ $form }));
1481
1482   &invoice_links;
1483   &prepare_invoice;
1484   relink_accounts();
1485
1486   $form->{storno_id} = $form->{id};
1487   $form->{storno} = 1;
1488   $form->{id} = "";
1489   $form->{invnumber} = "Storno zu " . $form->{invnumber};
1490
1491   &post();
1492   $lxdebug->leave_sub();
1493
1494 }
1495
1496 sub preview {
1497   $lxdebug->enter_sub();
1498
1499   $form->{preview} = 1;
1500   $old_form = new Form;
1501   for (keys %$form) { $old_form->{$_} = $form->{$_} }
1502   $old_form->{rowcount}++;
1503
1504   &print_form($old_form);
1505   $lxdebug->leave_sub();
1506
1507 }
1508
1509 sub delete {
1510   $lxdebug->enter_sub();
1511   if ($form->{second_run}) {
1512     $form->{print_and_post} = 0;
1513   }
1514   $form->header;
1515
1516   print qq|
1517 <body>
1518
1519 <form method="post" action="$form->{script}">
1520 |;
1521
1522   # delete action variable
1523   map { delete $form->{$_} } qw(action header);
1524
1525   foreach $key (keys %$form) {
1526     $form->{$key} =~ s/\"/&quot;/g;
1527     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1528   }
1529
1530   print qq|
1531 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
1532
1533 <h4>|
1534     . $locale->text('Are you sure you want to delete Invoice Number')
1535     . qq| $form->{invnumber}
1536 </h4>
1537
1538 <p>
1539 <input name="action" class="submit" type="submit" value="|
1540     . $locale->text('Yes') . qq|">
1541 </form>
1542 |;
1543
1544   $lxdebug->leave_sub();
1545 }
1546
1547 sub credit_note {
1548   $lxdebug->enter_sub();
1549
1550   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1551   $form->{duedate} =
1552     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1553
1554   $form->{id}     = '';
1555   $form->{rowcount}--;
1556   $form->{shipto} = 1;
1557
1558
1559   $form->{title}  = $locale->text('Add Credit Note');
1560   $form->{script} = 'is.pl';
1561   $script         = "is";
1562   $buysell        = 'buy';
1563   
1564
1565   # bo creates the id, reset it
1566   map { delete $form->{$_} }
1567     qw(id invnumber subject message cc bcc printed emailed queued);
1568   $form->{ $form->{vc} } =~ s/--.*//g;
1569   $form->{type} = "credit_note";
1570
1571
1572   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1573
1574   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1575     qw(creditlimit creditremaining);
1576
1577   $currency = $form->{currency};
1578   &invoice_links;
1579
1580   $form->{currency}     = $currency;
1581   $form->{exchangerate} = "";
1582   $form->{forex}        = "";
1583   $form->{exchangerate} = $exchangerate
1584     if (
1585         $form->{forex} = (
1586                     $exchangerate =
1587                       $form->check_exchangerate(
1588                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
1589                       )));
1590
1591   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1592
1593   &prepare_invoice;
1594
1595
1596   &display_form;
1597
1598   $lxdebug->leave_sub();
1599 }
1600
1601 sub yes {
1602   $lxdebug->enter_sub();
1603   if (IS->delete_invoice(\%myconfig, \%$form, $spool)) {
1604     # saving the history
1605         if(!exists $form->{addition}) {
1606           $form->{addition} = "DELETED";
1607           $form->save_history($form->dbconnect(\%myconfig));
1608     }
1609     # /saving the history 
1610     $form->redirect($locale->text('Invoice deleted!')); 
1611   }
1612   $form->error($locale->text('Cannot delete invoice!'));
1613
1614   $lxdebug->leave_sub();
1615 }
1616
1617 sub e_mail {
1618   $lxdebug->enter_sub();
1619
1620   $print_post = 1;
1621
1622   &post;
1623
1624   &edit_e_mail;
1625
1626   $lxdebug->leave_sub();
1627 }