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