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