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