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