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