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