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