Merge branch 'master' of ssh://git-mbunkus@lx-office.linet-services.de/~/lx-office-erp
[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_button" 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_button" 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_button" 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_button" 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/--\d*\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|   </td>
659               </tr>
660               <tr>
661                 <th align="right" nowrap>| . $locale->text('Ship via') . qq|</th>
662                 <td colspan="3"> | .
663                 $cgi->textfield("-name" => "shipvia", "-size" => 35, "-value" => $form->{shipvia}) .
664           qq|   </td>
665               </tr>
666               <tr>
667                 <th align="right">| . $locale->text('Transaction description') . qq|</th>
668                 <td colspan="3">| . $cgi->textfield("-name" => "transaction_description", "-size" => 35, "-value" => $form->{transaction_description}) . qq|</td>
669               </tr>|;
670 #               <tr>
671 #                 <td colspan=4>
672 #                   <table>
673 #                     <tr>
674 #                       <td colspan=2>
675 #                         <button type="button" onclick="delivery_customer_selection_window('delivery_customer_string','delivery_customer_id')">| . $locale->text('Choose Customer') . qq|</button>
676 #                       </td>
677 #                       <td colspan=2><input type=hidden name=delivery_customer_id value="$form->{delivery_customer_id}">
678 #                       <input size=45 id=delivery_customer_string name=delivery_customer_string value="$form->{delivery_customer_string}"></td>
679 #                     </tr>
680 #                     <tr>
681 #                       <td colspan=2>
682 #                         <button type="button" onclick="vendor_selection_window('delivery_vendor_string','delivery_vendor_id')">| . $locale->text('Choose Vendor') . qq|</button>
683 #                       </td>
684 #                       <td colspan=2><input type=hidden name=delivery_vendor_id value="$form->{delivery_vendor_id}">
685 #                       <input size=45 id=delivery_vendor_string name=delivery_vendor_string value="$form->{delivery_vendor_string}"></td>
686 #                     </tr>
687 #                   </table>
688 #                 </td>
689 #               </tr>
690 print qq|           </table>
691           </td>
692           <td align="right" valign="top">
693             <table>
694               $employees
695         $salesman
696 |;
697
698 #ergänzung in der maske um das feld Lieferscheinnummer (Delivery Order Number), meiner meinung nach sinnvoll ueber dem feld lieferscheindatum 12.02.2009 jb
699 if ($form->{type} eq "credit_note") {
700 print qq|     <tr>
701                 <th align="right" nowrap>| . $locale->text('Credit Note Number') . qq|</th>
702                 <td> |.
703                 $cgi->textfield("-name" => "invnumber", "-size" => 11, "-value" => $form->{invnumber}) .        
704       qq|       </td>
705               </tr>
706               <tr>
707                 <th align="right">| . $locale->text('Credit Note Date') . qq|</th>
708                 $button1
709               </tr>|;
710 } else {
711 print qq|     <tr>
712                 <th align="right" nowrap>| . $locale->text('Invoice Number') . qq|</th>
713                 <td> |.
714                 $cgi->textfield("-name" => "invnumber", "-size" => 11, "-value" => $form->{invnumber}) .        
715       qq|       </td>
716               </tr>
717               <tr>
718                 <th align="right">| . $locale->text('Invoice Date') . qq|</th>
719                 $button1
720               </tr>
721               <tr>
722                 <th align="right">| . $locale->text('Due Date') . qq|</th>
723                 $button2
724               </tr>
725               <tr>
726                 <th align="right" nowrap>| . $locale->text('Delivery Order Number') . qq|</th>
727                 <td> |.
728                 $cgi->textfield("-name" => "donumber", "-size" => 11, "-value" => $form->{donumber}) .  
729       qq|       </td>
730               </tr>
731               <tr>
732                 <th align="right">| . $locale->text('Delivery Date') . qq|</th>
733                 $button3
734               </tr>|;
735 }
736 print qq|     <tr>
737                 <th align="right" nowrap>| . $locale->text('Order Number') . qq|</th>
738                 <td> |.
739                 $cgi->textfield("-name" => "ordnumber", "-size" => 11, "-value" => $form->{ordnumber}) .        
740       qq|       </td>
741               </tr>
742         <tr>
743           <th align="right" nowrap>| . $locale->text('Order Date') . qq|</th>
744           <td><input name="orddate" id="orddate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{orddate}) . qq|" onBlur=\"check_right_date_format(this)\">
745            <input type="button" name="b_orddate" id="trigger_orddate" value="?"></td>
746         </tr>
747               <tr>
748                 <th align="right" nowrap>| . $locale->text('Quotation Number') . qq|</th>
749                 <td> |.
750                 $cgi->textfield("-name" => "quonumber", "-size" => 11, "-value" => $form->{quonumber}) .        
751       qq|       </td>
752               </tr>
753         <tr>
754           <th align="right" nowrap>| . $locale->text('Quotation Date') . qq|</th>
755           <td><input name="quodate" id="quodate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{quodate}) . qq|" onBlur=\"check_right_date_format(this)\">
756            <input type="button" name="b_quodate" id="trigger_quodate" value="?"></td>
757         </tr>
758               <tr>
759                 <th align="right" nowrap>| . $locale->text('Customer Order Number') . qq|</th>
760                 <td> |.
761                 $cgi->textfield("-name" => "cusordnumber", "-size" => 11, "-value" => $form->{cusordnumber}) .  
762       qq|       </td>
763               </tr>
764               <tr>
765           <th align="right" nowrap>| . $locale->text('Project Number') . qq|</th>
766           <td>$globalprojectnumber</td>
767               </tr>
768             </table>
769           </td>
770         </tr>
771       </table>
772     </td>
773   </tr>
774   <tr>
775     <td>
776     </td>
777   </tr> 
778   $jsscript
779 |;
780   print qq|<input type="hidden" name="webdav" value="$webdav">|;
781
782   $lxdebug->leave_sub();
783 }
784
785 sub form_footer {
786   $lxdebug->enter_sub();
787
788   $auth->assert('invoice_edit');
789
790   $form->{invtotal} = $form->{invsubtotal};
791
792   if (($rows = $form->numtextrows($form->{notes}, 26, 8)) < 2) {
793     $rows = 2;
794   }
795   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
796     $introws = 2;
797   }
798   $rows = ($rows > $introws) ? $rows : $introws;
799   $notes =
800     qq|<textarea name="notes" rows="$rows" cols="26" wrap="soft">$form->{notes}</textarea>|;
801   $intnotes =
802     qq|<textarea name="intnotes" rows="$rows" cols="35" wrap="soft">$form->{intnotes}</textarea>|;
803
804   $form->{taxincluded} = ($form->{taxincluded} ? "checked" : "");
805
806   $taxincluded = "";
807   if ($form->{taxaccounts}) {
808     $taxincluded = qq|
809                 <input name="taxincluded" class="checkbox" type="checkbox" $form->{taxincluded}> <b>|
810       . $locale->text('Tax Included') . qq|</b><br><br>|;
811   }
812
813   if (!$form->{taxincluded}) {
814
815     foreach $item (split / /, $form->{taxaccounts}) {
816       if ($form->{"${item}_base"}) {
817         $form->{"${item}_total"} =
818           $form->round_amount(
819                              $form->{"${item}_base"} * $form->{"${item}_rate"},
820                              2);
821         $form->{invtotal} += $form->{"${item}_total"};
822         $form->{"${item}_total"} =
823           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
824
825         $tax .= qq|
826               <tr>
827                 <th align="right">$form->{"${item}_description"}&nbsp;|
828                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
829                 <td align="right">$form->{"${item}_total"}</td>
830               </tr>
831 |;
832       }
833     }
834
835     $form->{invsubtotal} =
836       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
837
838     $subtotal = qq|
839               <tr>
840                 <th align="right">| . $locale->text('Subtotal') . qq|</th>
841                 <td align="right">$form->{invsubtotal}</td>
842               </tr>
843 |;
844
845   }
846
847   if ($form->{taxincluded}) {
848     foreach $item (split / /, $form->{taxaccounts}) {
849       if ($form->{"${item}_base"}) {
850         $form->{"${item}_total"} =
851           $form->round_amount(
852                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
853                               (1 + $form->{"${item}_rate"})
854                            ),
855                            2);
856         $form->{"${item}_netto"} =
857           $form->round_amount(
858                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
859                           2);
860         $form->{"${item}_total"} =
861           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
862         $form->{"${item}_netto"} =
863           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
864
865         $tax .= qq|
866               <tr>
867                 <th align="right">Enthaltene $form->{"${item}_description"}&nbsp;|
868                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
869                 <td align="right">$form->{"${item}_total"}</td>
870               </tr>
871               <tr>
872                 <th align="right">Nettobetrag</th>
873                 <td align="right">$form->{"${item}_netto"}</td>
874               </tr>
875 |;
876       }
877     }
878
879   }
880
881   $form->{oldinvtotal} = $form->{invtotal};
882   $form->{invtotal}    =
883     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
884
885   my $follow_ups_block;
886   if ($form->{id}) {
887     my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
888
889     if (@{ $follow_ups} ) {
890       my $num_due       = sum map { $_->{due} * 1 } @{ $follow_ups };
891       $follow_ups_block = qq|
892       <tr>
893         <td colspan="2">| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</td>
894       </tr>
895 |;
896     }
897   }
898
899   print qq|
900   <tr>
901     <td>
902       <table width="100%">
903         <tr valign="bottom">
904           <td>
905             <table>
906               <tr>
907                 <th align="left">| . $locale->text('Notes') . qq|</th>
908                 <th align="left">| . $locale->text('Internal Notes') . qq|</th>
909                 <th align="right">| . $locale->text('Payment Terms') . qq|</th>
910               </tr>
911               <tr valign="top">
912                 <td>$notes</td>
913                 <td>$intnotes</td>
914                 <td><select name="payment_id" onChange="if (this.value) set_duedate(['payment_id__' + this.value, 'invdate__' + invdate.value],['duedate'])">$payment
915                 </select></td>
916               </tr>
917         $follow_ups_block
918             </table>
919           </td>
920           <td>
921             <table>
922             <tr>
923               <th  align=left>| . $locale->text('Ertrag') . qq|</th>
924               <td>| .  $form->format_amount(\%myconfig, $form->{marge_total}, 2, 0) . qq|</td>
925             </tr>
926             <tr>
927               <th  align=left>| . $locale->text('Ertrag prozentual') . qq|</th>
928               <td>| .  $form->format_amount(\%myconfig, $form->{marge_percent}, 2, 0) . qq| %</td>
929             </tr>
930             <input type=hidden name="marge_total" value="$form->{"marge_total"}">
931             <input type=hidden name="marge_percent" value="$form->{"marge_percent"}">
932             </table>
933           </td>
934           <td align="right">
935             $taxincluded
936             <table>
937               $subtotal
938               $tax
939               <tr>
940                 <th align="right">| . $locale->text('Total') . qq|</th>
941                 <td align="right">$form->{invtotal}</td>
942               </tr>
943             </table>
944           </td>
945         </tr>
946       </table>
947     </td>
948   </tr>
949 |;
950   if ($webdav) {
951     $webdav_list = qq|
952   <tr>
953     <td><hr size="3" noshade></td>
954   </tr>
955   <tr>
956     <th class="listtop" align="left">Dokumente im Webdav-Repository</th>
957   </tr>
958     <table width="100%">
959       <td align="left" width="30%"><b>Dateiname</b></td>
960       <td align="left" width="70%"><b>Webdavlink</b></td>
961 |;
962     foreach $file (@{ $form->{WEBDAV} }) {
963       $webdav_list .= qq|
964       <tr>
965         <td align="left">$file->{name}</td>
966         <td align="left"><a href="$file->{link}">$file->{type}</a></td>
967       </tr>
968 |;
969     }
970     $webdav_list .= qq|
971     </table>
972   </tr>
973 |;
974
975     print $webdav_list;
976   }
977 if ($form->{type} eq "credit_note") {
978   print qq|
979   <tr>
980     <td>
981       <table width="100%">
982         <tr class="listheading">
983           <th colspan="6" class="listheading">|
984     . $locale->text('Payments') . qq|</th>
985         </tr>
986 |;
987 } else {
988   print qq|
989   <tr>
990     <td>
991       <table width="100%">
992         <tr class="listheading">
993           <th colspan="6" class="listheading">|
994     . $locale->text('Incoming Payments') . qq|</th>
995         </tr>
996 |;
997 }
998
999   if ($form->{currency} eq $form->{defaultcurrency}) {
1000     @column_index = qw(datepaid source memo paid AR_paid);
1001   } else {
1002     @column_index = qw(datepaid source memo paid exchangerate AR_paid);
1003   }
1004
1005   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
1006   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
1007   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
1008   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
1009   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
1010   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
1011
1012   print "
1013         <tr>
1014 ";
1015   map { print "$column_data{$_}\n" } @column_index;
1016   print "
1017         </tr>
1018 ";
1019
1020   my @triggers  = ();
1021   my $totalpaid = 0;
1022
1023   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
1024   for $i (1 .. $form->{paidaccounts}) {
1025
1026     print "
1027         <tr>\n";
1028
1029     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
1030     $form->{"selectAR_paid_$i"} =~
1031       s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
1032
1033     # format amounts
1034     $totalpaid += $form->{"paid_$i"};
1035     if ($form->{"paid_$i"}) {
1036       $form->{"paid_$i"} =
1037         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
1038     }
1039     $form->{"exchangerate_$i"} =
1040       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
1041
1042     if ($form->{"exchangerate_$i"} == 0) {
1043       $form->{"exchangerate_$i"} = "";
1044     }
1045     $exchangerate = qq|&nbsp;|;
1046     if ($form->{currency} ne $form->{defaultcurrency}) {
1047       if ($form->{"forex_$i"}) {
1048         $exchangerate = qq|<input type="hidden" name="exchangerate_$i" value="$form->{"exchangerate_$i"}">$form->{"exchangerate_$i"}|;
1049       } else {
1050         $exchangerate = qq|<input name="exchangerate_$i" size="10" value="$form->{"exchangerate_$i"}">|;
1051       }
1052     }
1053
1054     $exchangerate .= qq|<input type="hidden" name="forex_$i" value="$form->{"forex_$i"}">|;
1055
1056     $column_data{"paid_$i"} =
1057       qq|<td align="center"><input name="paid_$i" size="11" value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
1058     $column_data{"exchangerate_$i"} = qq|<td align="center">$exchangerate</td>|;
1059     $column_data{"AR_paid_$i"}      =
1060       qq|<td align="center"><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
1061     $column_data{"datepaid_$i"} =
1062       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)\">
1063          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
1064     $column_data{"source_$i"} =
1065       qq|<td align=center><input name="source_$i" size="11" value="$form->{"source_$i"}"></td>|;
1066     $column_data{"memo_$i"} =
1067       qq|<td align="center"><input name="memo_$i" size="11" value="$form->{"memo_$i"}"></td>|;
1068
1069     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
1070     print "
1071         </tr>\n";
1072     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
1073   }
1074
1075   my $paid_missing = $form->{oldinvtotal} - $totalpaid;
1076
1077   print qq|
1078     <tr>
1079       <td></td>
1080       <td></td>
1081       <td align="center">| . $locale->text('Total') . qq|</td>
1082       <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
1083     </tr>
1084     <tr>
1085       <td></td>
1086       <td></td>
1087       <td align="center">| . $locale->text('Missing amount') . qq|</td>
1088       <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
1089     </tr>
1090 |;
1091
1092   map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } qw(paidaccounts selectAR_paid oldinvtotal));
1093   print qq|<input type="hidden" name="oldtotalpaid" value="$totalpaid">
1094     </table>
1095     </td>
1096   </tr>
1097   <tr>
1098     <td><hr size="3" noshade></td>
1099   </tr>
1100   <tr>
1101     <td>
1102 |;
1103
1104   print_options();
1105
1106   print qq|
1107     </td>
1108   </tr>
1109 </table>
1110 |;
1111
1112   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1113   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1114
1115   if ($form->{id}) {
1116     my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && (($totalpaid == 0) || ($totalpaid eq ""));
1117
1118     print qq|
1119     <input class="submit" type="submit" accesskey="u" name="action" id="update_button" value="|
1120       . $locale->text('Update') . qq|">
1121     <input class="submit" type="submit" name="action" value="|
1122       . $locale->text('Ship to') . qq|">
1123     <input class="submit" type="submit" name="action" value="|
1124       . $locale->text('Print') . qq|">
1125     <input class="submit" type="submit" name="action" value="|
1126       . $locale->text('E-mail') . qq|"> |;
1127     print qq|<input class="submit" type="submit" name="action" value="|
1128       . $locale->text('Storno') . qq|"> | if ($show_storno);
1129     print qq|<input class="submit" type="submit" name="action" value="|
1130       . $locale->text('Post Payment') . qq|">
1131 |;
1132     print qq|<input class="submit" type="submit" name="action" value="|
1133       . $locale->text('Use As Template') . qq|">
1134 |;
1135     if ($form->{id} && !($form->{type} eq "credit_note")) {
1136       print qq|
1137     <input class="submit" type="submit" name="action" value="|
1138       . $locale->text('Credit Note') . qq|">
1139 |;
1140     }
1141     if ($form->{radier}) {
1142     print qq|
1143     <input class="submit" type="submit" name="action" value="|
1144       . $locale->text('Delete') . qq|">
1145 |;
1146     }
1147
1148
1149     if ($invdate > $closedto) {
1150       print qq|
1151       <input class="submit" type="submit" name="action" value="|
1152         . $locale->text('Order') . qq|">
1153 |;
1154     }
1155
1156     print qq|
1157       <input type="button" class="submit" onclick="follow_up_window()" value="|
1158       . $locale->text('Follow-Up')
1159       . qq|">|;
1160
1161   } else {
1162     if ($invdate > $closedto) {
1163       print qq|<input class="submit" type="submit" name="action" id="update_button" value="|
1164         . $locale->text('Update') . qq|">
1165       <input class="submit" type="submit" name="action" value="|
1166         . $locale->text('Ship to') . qq|">
1167       <input class="submit" type="submit" name="action" value="|
1168         . $locale->text('Preview') . qq|">
1169       <input class="submit" type="submit" name="action" value="|
1170         . $locale->text('E-mail') . qq|">
1171       <input class="submit" type="submit" name="action" value="|
1172         . $locale->text('Print and Post') . qq|">
1173       <input class="submit" type="submit" name="action" value="|
1174         . $locale->text('Post') . qq|"> | .
1175         NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
1176                          '-class' => 'submit'));
1177     }
1178   }
1179
1180   # button for saving history
1181   if($form->{id} ne "") {
1182     print qq|
1183           <input type="button" class="submit" onclick="set_history_window(|
1184           . Q($form->{id})
1185           . qq|);" name="history" id="history" value="|
1186           . $locale->text('history')
1187           . qq|"> |;
1188   }
1189   # /button for saving history
1190   
1191   # mark_as_paid button 
1192   if($form->{id} ne "") {  
1193     print qq|<input type="submit" class="submit" name="action" value="| 
1194           . $locale->text('mark as paid') . qq|">|;
1195   }
1196   # /mark_as_paid button
1197   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
1198     qq|
1199
1200 <input type="hidden" name="rowcount" value="$form->{rowcount}">
1201 | .
1202 $cgi->hidden("-name" => "callback", "-value" => $form->{callback})
1203 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
1204 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
1205 . $cgi->hidden('-name' => 'customer_discount', '-value' => [$form->{customer_discount}])
1206 . qq|
1207 </form>
1208
1209 </body>
1210
1211  </html>
1212 |;
1213
1214   $lxdebug->leave_sub();
1215 }
1216
1217 sub mark_as_paid {
1218   $lxdebug->enter_sub();
1219
1220   $auth->assert('invoice_edit');
1221
1222   &mark_as_paid_common(\%myconfig,"ar");  
1223
1224   $lxdebug->leave_sub();
1225 }
1226
1227 sub update {
1228   $lxdebug->enter_sub();
1229
1230   $auth->assert('invoice_edit');
1231
1232   my ($recursive_call) = shift;
1233
1234   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
1235
1236   $form->{print_and_post} = 0         if $form->{second_run};
1237   $taxincluded            = "checked" if $form->{taxincluded};
1238   $form->{update} = 1;
1239
1240   &check_name(customer);
1241
1242   $form->{taxincluded} ||= $taxincluded;
1243
1244   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
1245   $form->{exchangerate} = $form->{forex} if $form->{forex};
1246
1247   for $i (1 .. $form->{paidaccounts}) {
1248     next unless $form->{"paid_$i"};
1249     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
1250     $form->{"forex_$i"}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
1251     $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
1252   }
1253
1254   $i            = $form->{rowcount};
1255   $exchangerate = $form->{exchangerate} || 1;
1256
1257   # if last row empty, check the form otherwise retrieve new item
1258   if (   ($form->{"partnumber_$i"} eq "")
1259       && ($form->{"description_$i"} eq "")
1260       && ($form->{"partsgroup_$i"}  eq "")) {
1261
1262     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
1263     &check_form;
1264
1265   } else {
1266
1267     IS->retrieve_item(\%myconfig, \%$form);
1268
1269     $rows = scalar @{ $form->{item_list} };
1270
1271     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
1272
1273     if ($rows) {
1274       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
1275
1276       if ($rows > 1) {
1277
1278         &select_item;
1279         exit;
1280
1281       } else {
1282
1283         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1284
1285         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
1286         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
1287         
1288         $form->{payment_id}    = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
1289         $form->{"discount_$i"} = 0                             if $form->{"not_discountable_$i"};
1290
1291         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
1292
1293         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
1294         $decimalplaces = max 2, length $1;
1295
1296         if ($sellprice) {
1297           $form->{"sellprice_$i"} = $sellprice;
1298         } else {
1299           # if there is an exchange rate adjust sellprice
1300           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1301           $form->{"sellprice_$i"} /= $exchangerate;
1302         }
1303
1304         $form->{"listprice_$i"} /= $exchangerate;
1305
1306         $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
1307         map { $form->{"${_}_base"} = 0 }                                 split / /, $form->{taxaccounts};
1308         map { $form->{"${_}_base"} += $amount }                          split / /, $form->{"taxaccounts_$i"};
1309         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
1310
1311         $form->{creditremaining} -= $amount;
1312
1313         map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
1314
1315         $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
1316
1317         if ($lizenzen) {
1318           if ($form->{"inventory_accno_$i"} ne "") {
1319             $form->{"lizenzen_$i"} = qq|<option></option>|;
1320             foreach $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
1321               $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
1322             }
1323             $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
1324           }
1325         }
1326
1327         # get pricegroups for parts
1328         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1329
1330         # build up html code for prices_$i
1331         &set_pricegroup($i);
1332       }
1333
1334       &display_form;
1335
1336     } else {
1337
1338       # ok, so this is a new part
1339       # ask if it is a part or service item
1340
1341       if (   $form->{"partsgroup_$i"}
1342           && ($form->{"partsnumber_$i"} eq "")
1343           && ($form->{"description_$i"} eq "")) {
1344         $form->{rowcount}--;
1345         $form->{"discount_$i"} = "";
1346         display_form();
1347
1348       } else {
1349         $form->{"id_$i"}   = 0;
1350         new_item();
1351       }
1352     }
1353   }
1354   $lxdebug->leave_sub();
1355 }
1356
1357 sub post_payment {
1358   $lxdebug->enter_sub();
1359
1360   $auth->assert('invoice_edit');
1361
1362   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1363   for $i (1 .. $form->{paidaccounts}) {
1364     if ($form->{"paid_$i"}) {
1365       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1366
1367       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1368
1369       $form->error($locale->text('Cannot post payment for a closed period!'))
1370         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1371
1372       if ($form->{currency} ne $form->{defaultcurrency}) {
1373         $form->{"exchangerate_$i"} = $form->{exchangerate}
1374           if ($invdate == $datepaid);
1375         $form->isblank("exchangerate_$i",
1376                        $locale->text('Exchangerate for payment missing!'));
1377       }
1378     }
1379   }
1380
1381   ($form->{AR})      = split /--/, $form->{AR};
1382   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1383   relink_accounts();
1384   $form->redirect($locale->text('Payment posted!'))
1385       if (IS->post_payment(\%myconfig, \%$form));
1386     $form->error($locale->text('Cannot post payment!'));
1387
1388
1389   $lxdebug->leave_sub();
1390 }
1391
1392 sub post {
1393   $lxdebug->enter_sub();
1394
1395   $auth->assert('invoice_edit');
1396
1397   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1398   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
1399   $form->isblank("customer", $locale->text('Customer missing!'));
1400   $form->error($locale->text('Cannot post invoice for a closed period!'))
1401         if ($form->date_closed($form->{"invdate"}, \%myconfig));
1402
1403   $form->{invnumber} =~ s/^\s*//g;
1404   $form->{invnumber} =~ s/\s*$//g;
1405
1406   # if oldcustomer ne customer redo form
1407   if (&check_name(customer)) {
1408     &update;
1409     exit;
1410   }
1411   if ($form->{second_run}) {
1412     $form->{print_and_post} = 0;
1413   }
1414
1415   &validate_items;
1416
1417   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1418   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1419
1420   $form->error($locale->text('Cannot post invoice for a closed period!'))
1421     if ($invdate <= $closedto);
1422
1423   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1424     if ($form->{currency} ne $form->{defaultcurrency});
1425
1426   for $i (1 .. $form->{paidaccounts}) {
1427     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1428       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1429
1430       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1431
1432       $form->error($locale->text('Cannot post payment for a closed period!'))
1433         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1434
1435       if ($form->{currency} ne $form->{defaultcurrency}) {
1436         $form->{"exchangerate_$i"} = $form->{exchangerate}
1437           if ($invdate == $datepaid);
1438         $form->isblank("exchangerate_$i",
1439                        $locale->text('Exchangerate for payment missing!'));
1440       }
1441     }
1442   }
1443
1444   ($form->{AR})        = split /--/, $form->{AR};
1445   ($form->{AR_paid})   = split /--/, $form->{AR_paid};
1446   $form->{storno}    ||= 0;
1447
1448   $form->{label} = $locale->text('Invoice');
1449
1450   $form->{id} = 0 if $form->{postasnew};
1451
1452   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
1453   if ($form->{postasnew}) {
1454     if ($form->{type} eq "credit_note") {
1455       undef($form->{cnnumber});
1456     } else {
1457       undef($form->{invnumber});
1458     }
1459   }
1460
1461   relink_accounts();
1462   $form->error($locale->text('Cannot post invoice!'))
1463     unless IS->post_invoice(\%myconfig, \%$form);
1464   remove_draft() if $form->{remove_draft};
1465
1466   if(!exists $form->{addition}) {
1467     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1468     $form->{addition} = $print_post     ? "PRINTED AND POSTED" :
1469                         $form->{storno} ? "STORNO"             :
1470                                           "POSTED";
1471     $form->save_history($form->dbconnect(\%myconfig));
1472   }
1473
1474   $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
1475     unless $print_post;
1476
1477   $lxdebug->leave_sub();
1478 }
1479
1480 sub print_and_post {
1481   $lxdebug->enter_sub();
1482
1483   $auth->assert('invoice_edit');
1484
1485   $old_form               = new Form;
1486   $print_post             = 1;
1487   $form->{print_and_post} = 1;
1488   &post();
1489
1490   &edit();
1491   $lxdebug->leave_sub();
1492
1493 }
1494
1495 sub use_as_template {
1496   $lxdebug->enter_sub();
1497
1498   $auth->assert('invoice_edit');
1499
1500   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);
1501   $form->{paidaccounts} = 1;
1502   $form->{rowcount}--;
1503   $form->{invdate} = $form->current_date(\%myconfig);
1504   &display_form;
1505
1506   $lxdebug->leave_sub();
1507 }
1508
1509 sub storno {
1510   $lxdebug->enter_sub();
1511
1512   $auth->assert('invoice_edit');
1513
1514   if ($form->{storno}) {
1515     $form->error($locale->text('Cannot storno storno invoice!'));
1516   }
1517
1518   if (IS->has_storno(\%myconfig, $form, "ar")) {
1519     $form->error($locale->text("Invoice has already been storno'd!"));
1520   }
1521
1522   map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
1523
1524   invoice_links();
1525   prepare_invoice();
1526   relink_accounts();
1527
1528   # Payments must not be recorded for the new storno invoice.
1529   $form->{paidaccounts} = 0;
1530   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
1531
1532   $form->{storno_id} = $form->{id};
1533   $form->{storno} = 1;
1534   $form->{id} = "";
1535   $form->{invnumber} = "Storno zu " . $form->{invnumber};
1536   $form->{rowcount}++;
1537
1538   post();
1539   $lxdebug->leave_sub();
1540 }
1541
1542 sub preview {
1543   $lxdebug->enter_sub();
1544
1545   $auth->assert('invoice_edit');
1546
1547   $form->{preview} = 1;
1548   $old_form = new Form;
1549   for (keys %$form) { $old_form->{$_} = $form->{$_} }
1550
1551   &print_form($old_form);
1552   $lxdebug->leave_sub();
1553
1554 }
1555
1556 sub delete {
1557   $lxdebug->enter_sub();
1558
1559   $auth->assert('invoice_edit');
1560
1561   if ($form->{second_run}) {
1562     $form->{print_and_post} = 0;
1563   }
1564   $form->header;
1565
1566   print qq|
1567 <body>
1568
1569 <form method="post" action="$form->{script}">
1570 |;
1571
1572   # delete action variable
1573   map { delete $form->{$_} } qw(action header);
1574
1575   foreach $key (keys %$form) {
1576     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1577     $form->{$key} =~ s/\"/&quot;/g;
1578     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1579   }
1580
1581   print qq|
1582 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
1583
1584 <h4>|
1585     . $locale->text('Are you sure you want to delete Invoice Number')
1586     . qq| $form->{invnumber}
1587 </h4>
1588
1589 <p>
1590 <input name="action" class="submit" type="submit" value="|
1591     . $locale->text('Yes') . qq|">
1592 </form>
1593 |;
1594
1595   $lxdebug->leave_sub();
1596 }
1597
1598 sub credit_note {
1599   $lxdebug->enter_sub();
1600
1601   $auth->assert('invoice_edit');
1602
1603   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1604   $form->{duedate} =
1605     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1606
1607   $form->{id}     = '';
1608   $form->{rowcount}--;
1609   $form->{shipto} = 1;
1610
1611
1612   $form->{title}  = $locale->text('Add Credit Note');
1613   $form->{script} = 'is.pl';
1614   $script         = "is";
1615   $buysell        = 'buy';
1616
1617
1618   # bo creates the id, reset it
1619   map { delete $form->{$_} }
1620     qw(id invnumber subject message cc bcc printed emailed queued);
1621   $form->{ $form->{vc} } =~ s/--.*//g;
1622   $form->{type} = "credit_note";
1623
1624
1625   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1626
1627   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1628     qw(creditlimit creditremaining);
1629
1630   $currency = $form->{currency};
1631   &invoice_links;
1632
1633   $form->{currency}     = $currency;
1634   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1635   $form->{exchangerate} = $form->{forex} || '';
1636
1637   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1638
1639   &prepare_invoice;
1640
1641
1642   &display_form;
1643
1644   $lxdebug->leave_sub();
1645 }
1646
1647 sub yes {
1648   $lxdebug->enter_sub();
1649
1650   $auth->assert('invoice_edit');
1651
1652   if (IS->delete_invoice(\%myconfig, \%$form, $spool)) {
1653     # saving the history
1654         if(!exists $form->{addition}) {
1655     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1656           $form->{addition} = "DELETED";
1657           $form->save_history($form->dbconnect(\%myconfig));
1658     }
1659     # /saving the history
1660     $form->redirect($locale->text('Invoice deleted!'));
1661   }
1662   $form->error($locale->text('Cannot delete invoice!'));
1663
1664   $lxdebug->leave_sub();
1665 }
1666
1667 sub e_mail {
1668   $lxdebug->enter_sub();
1669
1670   $auth->assert('invoice_edit');
1671
1672   if (!$form->{id}) {
1673     $print_post = 1;
1674
1675     my $saved_form = save_form();
1676
1677     post();
1678
1679     restore_form($saved_form, 0, qw(id invnumber));
1680   }
1681
1682   edit_e_mail();
1683
1684   $lxdebug->leave_sub();
1685 }