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