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