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