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