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