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