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