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