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