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