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