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