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