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