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