Beim Kontenrahmen SKR04 den Steuersatz aus der Steuerschlüsselbeschreibung herausnehm...
[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"}&nbsp;|
860                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
861                 <td align=right>$form->{"${item}_total"}</td>
862               </tr>
863 |;
864       }
865     }
866
867     $form->{invsubtotal} =
868       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
869
870     $subtotal = qq|
871               <tr>
872                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
873                 <td align=right>$form->{invsubtotal}</td>
874               </tr>
875 |;
876
877   }
878
879   if ($form->{taxincluded}) {
880     foreach $item (split / /, $form->{taxaccounts}) {
881       if ($form->{"${item}_base"}) {
882         $form->{"${item}_total"} =
883           $form->round_amount(
884                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
885                               (1 + $form->{"${item}_rate"})
886                            ),
887                            2);
888         $form->{"${item}_netto"} =
889           $form->round_amount(
890                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
891                           2);
892         $form->{"${item}_total"} =
893           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
894         $form->{"${item}_netto"} =
895           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
896
897         $tax .= qq|
898               <tr>
899                 <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;|
900                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
901                 <td align=right>$form->{"${item}_total"}</td>
902               </tr>
903               <tr>
904                 <th align=right>Nettobetrag</th>
905                 <td align=right>$form->{"${item}_netto"}</td>
906               </tr>
907 |;
908       }
909     }
910
911   }
912
913   $form->{oldinvtotal} = $form->{invtotal};
914   $form->{invtotal}    =
915     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
916
917   print qq|
918   <tr>
919     <td>
920       <table width=100%>
921         <tr valign=bottom>
922           <td>
923             <table>
924               <tr>
925                 <th align=left>| . $locale->text('Notes') . qq|</th>
926                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
927                 <th align=right>| . $locale->text('Payment Terms') . qq|</th>
928               </tr>
929               <tr valign=top>
930                 <td>$notes</td>
931                 <td>$intnotes</td>
932                 <td><select name=payment_id onChange="if (this.value) set_duedate(['payment_id__' + this.value],['duedate'])">$payment
933                 </select></td>
934               </tr>
935             </table>
936           </td>
937           <td align=right width=100%>
938             $taxincluded
939             <table width=100%>
940               $subtotal
941               $tax
942               <tr>
943                 <th align=right>| . $locale->text('Total') . qq|</th>
944                 <td align=right>$form->{invtotal}</td>
945               </tr>
946             </table>
947           </td>
948         </tr>
949       </table>
950     </td>
951   </tr>
952 |;
953   if ($webdav) {
954     $webdav_list = qq|
955   <tr>
956     <td><hr size=3 noshade></td>
957   </tr>
958   <tr>
959     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
960   </tr>
961     <table width=100%>
962       <td align=left width=30%><b>Dateiname</b></td>
963       <td align=left width=70%><b>Webdavlink</b></td>
964 |;
965     foreach $file (keys %{ $form->{WEBDAV} }) {
966       $webdav_list .= qq|
967       <tr>
968         <td align=left>$file</td>
969         <td align=left><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
970       </tr>
971 |;
972     }
973     $webdav_list .= qq|
974     </table>
975   </tr>
976 |;
977
978     print $webdav_list;
979   }
980 if ($form->{type} eq "credit_note") {
981   print qq|
982   <tr>
983     <td>
984       <table width=100%>
985         <tr class=listheading>
986           <th colspan=6 class=listheading>|
987     . $locale->text('Payments') . qq|</th>
988         </tr>
989 |;
990 } else {
991   print qq|
992   <tr>
993     <td>
994       <table width=100%>
995         <tr class=listheading>
996           <th colspan=6 class=listheading>|
997     . $locale->text('Incoming Payments') . qq|</th>
998         </tr>
999 |;
1000 }
1001
1002   if ($form->{currency} eq $form->{defaultcurrency}) {
1003     @column_index = qw(datepaid source memo paid AR_paid);
1004   } else {
1005     @column_index = qw(datepaid source memo paid exchangerate AR_paid);
1006   }
1007
1008   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
1009   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
1010   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
1011   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
1012   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
1013   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
1014
1015   print "
1016         <tr>
1017 ";
1018   map { print "$column_data{$_}\n" } @column_index;
1019   print "
1020         </tr>
1021 ";
1022
1023   my @triggers = ();
1024
1025   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
1026   for $i (1 .. $form->{paidaccounts}) {
1027
1028     print "
1029         <tr>\n";
1030
1031     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
1032     $form->{"selectAR_paid_$i"} =~
1033       s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
1034
1035     # format amounts
1036     $totalpaid += $form->{"paid_$i"};
1037     if ($form->{"paid_$i"}) {
1038       $form->{"paid_$i"} =
1039         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
1040     }
1041     $form->{"exchangerate_$i"} =
1042       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
1043
1044     $exchangerate = qq|&nbsp;|;
1045     if ($form->{currency} ne $form->{defaultcurrency}) {
1046       if ($form->{"forex_$i"}) {
1047         $exchangerate =
1048           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
1049       } else {
1050         $exchangerate =
1051           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
1052       }
1053     }
1054
1055     $exchangerate .= qq|
1056 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
1057 |;
1058
1059     $column_data{"paid_$i"} =
1060       qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
1061     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
1062     $column_data{"AR_paid_$i"}      =
1063       qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
1064     $column_data{"datepaid_$i"} =
1065       qq|<td align=center><input id="datepaid_$i" name="datepaid_$i"  size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}>
1066          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
1067     $column_data{"source_$i"} =
1068       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
1069     $column_data{"memo_$i"} =
1070       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
1071
1072     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
1073     print "
1074         </tr>\n";
1075     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
1076   }
1077
1078   print qq|
1079 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
1080 <input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
1081 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
1082 <input type=hidden name=oldtotalpaid value=$totalpaid>
1083     </table>
1084     </td>
1085   </tr>
1086   <tr>
1087     <td><hr size=3 noshade></td>
1088   </tr>
1089   <tr>
1090     <td>
1091 |;
1092
1093   &print_options;
1094
1095   print qq|
1096     </td>
1097   </tr>
1098 </table>
1099 |;
1100
1101   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1102   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1103
1104   if ($form->{id}) {
1105     my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar");
1106
1107     print qq|
1108     <input class=submit type=submit accesskey="u" name=action id=update_button value="|
1109       . $locale->text('Update') . qq|">
1110     <input class=submit type=submit name=action value="|
1111       . $locale->text('Ship to') . qq|">
1112     <input class=submit type=submit name=action value="|
1113       . $locale->text('Print') . qq|">
1114     <input class=submit type=submit name=action value="|
1115       . $locale->text('E-mail') . qq|"> |;
1116     print qq|<input class=submit type=submit name=action value="|
1117       . $locale->text('Storno') . qq|"> | if ($show_storno);
1118     print qq|<input class=submit type=submit name=action value="|
1119       . $locale->text('Post Payment') . qq|">
1120 |;
1121     print qq|<input class=submit type=submit name=action value="|
1122       . $locale->text('Use As Template') . qq|">
1123 |;
1124   if ($form->{id} && !($form->{type} eq "credit_note")) {
1125     print qq|
1126     <input class=submit type=submit name=action value="|
1127       . $locale->text('Credit Note') . qq|">
1128 |;
1129   }
1130     if ($form->{radier}) {
1131     print qq|
1132     <input class=submit type=submit name=action value="|
1133       . $locale->text('Delete') . qq|">
1134 |;
1135   }
1136
1137
1138     if ($invdate > $closedto) {
1139       print qq|
1140       <input class=submit type=submit name=action value="|
1141         . $locale->text('Order') . qq|">
1142 |;
1143     }
1144
1145   } else {
1146     if ($invdate > $closedto) {
1147       print qq|<input class=submit type=submit name=action id=update_button value="|
1148         . $locale->text('Update') . qq|">
1149       <input class=submit type=submit name=action value="|
1150         . $locale->text('Ship to') . qq|">
1151       <input class=submit type=submit name=action value="|
1152         . $locale->text('Preview') . qq|">
1153       <input class=submit type=submit name=action value="|
1154         . $locale->text('E-mail') . qq|">
1155       <input class=submit type=submit name=action value="|
1156         . $locale->text('Print and Post') . qq|">
1157       <input class=submit type=submit name=action value="|
1158         . $locale->text('Post') . qq|"> | .
1159         NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
1160                          '-class' => 'submit'));
1161     }
1162   }
1163
1164   # button for saving history
1165   if($form->{id} ne "") {
1166     print qq|
1167           <input type="button" class="submit" onclick="set_history_window(|
1168           . Q($form->{id})
1169           . qq|);" name="history" id="history" value="|
1170           . $locale->text('history')
1171           . qq|">|;
1172   }
1173   # /button for saving history
1174
1175
1176   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
1177     qq|
1178
1179 <input type=hidden name=rowcount value=$form->{rowcount}>
1180
1181 <input name=callback type=hidden value="$form->{callback}">
1182 |
1183 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
1184 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
1185 . qq|
1186 <input type=hidden name=path value=$form->{path}>
1187 <input type=hidden name=login value=$form->{login}>
1188 <input type=hidden name=password value=$form->{password}>
1189
1190 </form>
1191
1192 </body>
1193
1194  </html>
1195 |;
1196
1197   $lxdebug->leave_sub();
1198 }
1199
1200 sub update {
1201   $lxdebug->enter_sub();
1202
1203   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1204     qw(exchangerate creditlimit creditremaining);
1205   if ($form->{second_run}) {
1206     $form->{print_and_post} = 0;
1207   }
1208
1209   $form->{update} = 1;
1210
1211   &check_name(customer);
1212
1213   $form->{exchangerate} = $exchangerate
1214     if (
1215         $form->{forex} = (
1216                        $exchangerate =
1217                          $form->check_exchangerate(
1218                          \%myconfig, $form->{currency}, $form->{invdate}, 'buy'
1219                          )));
1220
1221   for $i (1 .. $form->{paidaccounts}) {
1222     if ($form->{"paid_$i"}) {
1223       map {
1224         $form->{"${_}_$i"} =
1225           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1226       } qw(paid exchangerate);
1227
1228       $form->{"exchangerate_$i"} = $exchangerate
1229         if (
1230             $form->{"forex_$i"} = (
1231                  $exchangerate =
1232                    $form->check_exchangerate(
1233                    \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy'
1234                    )));
1235     }
1236   }
1237
1238   $i            = $form->{rowcount};
1239   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
1240
1241   # if last row empty, check the form otherwise retrieve new item
1242   if (   ($form->{"partnumber_$i"} eq "")
1243       && ($form->{"description_$i"} eq "")
1244       && ($form->{"partsgroup_$i"}  eq "")) {
1245
1246     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
1247     &check_form;
1248
1249   } else {
1250
1251     IS->retrieve_item(\%myconfig, \%$form);
1252
1253     $rows = scalar @{ $form->{item_list} };
1254
1255     $form->{"discount_$i"} =
1256       $form->format_amount(\%myconfig, $form->{discount} * 100);
1257
1258     if ($rows) {
1259       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
1260
1261       if ($rows > 1) {
1262
1263         &select_item;
1264         exit;
1265
1266       } else {
1267
1268         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1269
1270         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1271           qw(partnumber description unit);
1272         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1273           keys %{ $form->{item_list}[0] };
1274         if ($form->{"part_payment_id_$i"} ne "") {
1275           $form->{payment_id} = $form->{"part_payment_id_$i"};
1276         }
1277
1278         if ($form->{"not_discountable_$i"}) {
1279           $form->{"discount_$i"} = 0;
1280         }
1281
1282         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1283         ($dec) = ($s =~ /\.(\d+)/);
1284         $dec           = length $dec;
1285         $decimalplaces = ($dec > 2) ? $dec : 2;
1286
1287         if ($sellprice) {
1288           $form->{"sellprice_$i"} = $sellprice;
1289         } else {
1290
1291           # if there is an exchange rate adjust sellprice
1292           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1293           $form->{"sellprice_$i"} /= $exchangerate;
1294         }
1295
1296         $form->{"listprice_$i"} /= $exchangerate;
1297
1298         $amount =
1299           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1300           (1 - $form->{"discount_$i"} / 100);
1301         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1302         map { $form->{"${_}_base"} += $amount }
1303           (split / /, $form->{"taxaccounts_$i"});
1304         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1305           split / /, $form->{"taxaccounts_$i"}
1306           if !$form->{taxincluded};
1307
1308         $form->{creditremaining} -= $amount;
1309
1310         map {
1311           $form->{"${_}_$i"} =
1312             $form->format_amount(\%myconfig, $form->{"${_}_$i"},
1313                                  $decimalplaces)
1314         } qw(sellprice listprice);
1315
1316         $form->{"qty_$i"} =
1317           $form->format_amount(\%myconfig, $form->{"qty_$i"});
1318
1319         if ($lizenzen) {
1320           if ($form->{"inventory_accno_$i"} ne "") {
1321             $form->{"lizenzen_$i"} = qq|<option></option>|;
1322             foreach $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
1323               $form->{"lizenzen_$i"} .=
1324                 qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
1325             }
1326             $form->{"lizenzen_$i"} .=
1327               qq|<option value=-1>Neue Lizenz</option>|;
1328           }
1329         }
1330
1331         # get pricegroups for parts
1332         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1333
1334         # build up html code for prices_$i
1335         &set_pricegroup($i);
1336       }
1337
1338       &display_form;
1339
1340     } else {
1341
1342       # ok, so this is a new part
1343       # ask if it is a part or service item
1344
1345       if (   $form->{"partsgroup_$i"}
1346           && ($form->{"partsnumber_$i"} eq "")
1347           && ($form->{"description_$i"} eq "")) {
1348         $form->{rowcount}--;
1349         $form->{"discount_$i"} = "";
1350         &display_form;
1351           } else {
1352
1353         $form->{"id_$i"}   = 0;
1354         $form->{"unit_$i"} = $locale->text('ea');
1355
1356         &new_item;
1357
1358       }
1359     }
1360   }
1361   $lxdebug->leave_sub();
1362 }
1363
1364 sub post_payment {
1365   $lxdebug->enter_sub();
1366   for $i (1 .. $form->{paidaccounts}) {
1367     if ($form->{"paid_$i"}) {
1368       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1369
1370       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1371
1372       $form->error($locale->text('Cannot post payment for a closed period!'))
1373         if ($datepaid <= $closedto);
1374
1375       if ($form->{currency} ne $form->{defaultcurrency}) {
1376         $form->{"exchangerate_$i"} = $form->{exchangerate}
1377           if ($invdate == $datepaid);
1378         $form->isblank("exchangerate_$i",
1379                        $locale->text('Exchangerate for payment missing!'));
1380       }
1381     }
1382   }
1383
1384   ($form->{AR})      = split /--/, $form->{AR};
1385   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1386   relink_accounts();
1387   $form->redirect($locale->text(' Payment posted!'))
1388       if (IS->post_payment(\%myconfig, \%$form));
1389     $form->error($locale->text('Cannot post payment!'));
1390
1391
1392   $lxdebug->leave_sub();
1393 }
1394
1395 sub post {
1396   $lxdebug->enter_sub();
1397   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
1398   $form->isblank("customer", $locale->text('Customer missing!'));
1399
1400   # if oldcustomer ne customer redo form
1401   if (&check_name(customer)) {
1402     &update;
1403     exit;
1404   }
1405   if ($form->{second_run}) {
1406     $form->{print_and_post} = 0;
1407   }
1408
1409   &validate_items;
1410
1411   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1412   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1413
1414   $form->error($locale->text('Cannot post invoice for a closed period!'))
1415     if ($invdate <= $closedto);
1416
1417   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1418     if ($form->{currency} ne $form->{defaultcurrency});
1419
1420   for $i (1 .. $form->{paidaccounts}) {
1421     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1422       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1423
1424       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1425
1426       $form->error($locale->text('Cannot post payment for a closed period!'))
1427         if ($datepaid <= $closedto);
1428
1429       if ($form->{currency} ne $form->{defaultcurrency}) {
1430         $form->{"exchangerate_$i"} = $form->{exchangerate}
1431           if ($invdate == $datepaid);
1432         $form->isblank("exchangerate_$i",
1433                        $locale->text('Exchangerate for payment missing!'));
1434       }
1435     }
1436   }
1437
1438   ($form->{AR})      = split /--/, $form->{AR};
1439   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1440
1441   $form->{label} = $locale->text('Invoice');
1442
1443   $form->{id} = 0 if $form->{postasnew};
1444
1445   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
1446   if ($form->{postasnew}) {
1447     if ($form->{type} eq "credit_note") {
1448       undef($form->{cnnumber});
1449     } else {
1450       undef($form->{invnumber});
1451     }
1452   }
1453
1454   relink_accounts();
1455   if ($print_post) {
1456     if (!(IS->post_invoice(\%myconfig, \%$form))) {
1457       $form->error($locale->text('Cannot post invoice!'));
1458     }
1459     remove_draft() if $form->{remove_draft};
1460     # saving the history
1461         if(!exists $form->{addition}) {
1462           $form->{addition} = "PRINTED AND POSTED";
1463           $form->save_history($form->dbconnect(\%myconfig));
1464     }
1465     # /saving the history
1466     
1467   } else {
1468     if (IS->post_invoice(\%myconfig, \%$form)){
1469       remove_draft() if $form->{remove_draft};
1470         # saving the history
1471         if(!exists $form->{addition}) {
1472                         if($form->{storno}) {
1473                                 $form->{addition} = "STORNO";
1474                         }
1475                         else {
1476                                 $form->{addition} = "POSTED";
1477                         }
1478                         $form->save_history($form->dbconnect(\%myconfig));
1479         }
1480         # /saving the history
1481     
1482         $form->redirect(
1483             $form->{label} . " $form->{invnumber} " . $locale->text('posted!'));
1484         }
1485     $form->error($locale->text('Cannot post invoice!'));
1486   }
1487
1488   $lxdebug->leave_sub();
1489 }
1490
1491 sub print_and_post {
1492   $lxdebug->enter_sub();
1493
1494   $old_form               = new Form;
1495   $print_post             = 1;
1496   $form->{print_and_post} = 1;
1497   &post();
1498
1499   &edit();
1500   $lxdebug->leave_sub();
1501
1502 }
1503
1504 sub use_as_template {
1505   $lxdebug->enter_sub();
1506
1507   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);
1508   $form->{paidaccounts} = 1;
1509   $form->{rowcount}--;
1510   $form->{invdate} = $form->current_date(\%myconfig);
1511   &display_form;
1512
1513   $lxdebug->leave_sub();
1514 }
1515
1516 sub storno {
1517   $lxdebug->enter_sub();
1518
1519   if ($form->{storno}) {
1520     $form->error($locale->text('Cannot storno storno invoice!'));
1521   }
1522
1523   if (IS->has_storno(\%myconfig, $form, "ar")) {
1524     $form->error($locale->text("Invoice has already been storno'd!"));
1525   }
1526
1527   map({ my $key = $_; delete($form->{$key})
1528           unless (grep({ $key eq $_ } qw(path login password id type))); }
1529       keys(%{ $form }));
1530
1531   &invoice_links;
1532   &prepare_invoice;
1533   relink_accounts();
1534
1535   $form->{storno_id} = $form->{id};
1536   $form->{storno} = 1;
1537   $form->{id} = "";
1538   $form->{invnumber} = "Storno zu " . $form->{invnumber};
1539
1540   &post();
1541   $lxdebug->leave_sub();
1542
1543 }
1544
1545 sub preview {
1546   $lxdebug->enter_sub();
1547
1548   $form->{preview} = 1;
1549   $old_form = new Form;
1550   for (keys %$form) { $old_form->{$_} = $form->{$_} }
1551   $old_form->{rowcount}++;
1552
1553   &print_form($old_form);
1554   $lxdebug->leave_sub();
1555
1556 }
1557
1558 sub delete {
1559   $lxdebug->enter_sub();
1560   if ($form->{second_run}) {
1561     $form->{print_and_post} = 0;
1562   }
1563   $form->header;
1564
1565   print qq|
1566 <body>
1567
1568 <form method=post action=$form->{script}>
1569 |;
1570
1571   # delete action variable
1572   map { delete $form->{$_} } qw(action header);
1573
1574   foreach $key (keys %$form) {
1575     $form->{$key} =~ s/\"/&quot;/g;
1576     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1577   }
1578
1579   print qq|
1580 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1581
1582 <h4>|
1583     . $locale->text('Are you sure you want to delete Invoice Number')
1584     . qq| $form->{invnumber}
1585 </h4>
1586
1587 <p>
1588 <input name=action class=submit type=submit value="|
1589     . $locale->text('Yes') . qq|">
1590 </form>
1591 |;
1592
1593   $lxdebug->leave_sub();
1594 }
1595
1596 sub credit_note {
1597   $lxdebug->enter_sub();
1598
1599   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1600   $form->{duedate} =
1601     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1602
1603   $form->{id}     = '';
1604   $form->{rowcount}--;
1605   $form->{shipto} = 1;
1606
1607
1608   $form->{title}  = $locale->text('Add Credit Note');
1609   $form->{script} = 'is.pl';
1610   $script         = "is";
1611   $buysell        = 'buy';
1612   
1613
1614   # bo creates the id, reset it
1615   map { delete $form->{$_} }
1616     qw(id invnumber subject message cc bcc printed emailed queued);
1617   $form->{ $form->{vc} } =~ s/--.*//g;
1618   $form->{type} = "credit_note";
1619
1620
1621   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1622
1623   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1624     qw(creditlimit creditremaining);
1625
1626   $currency = $form->{currency};
1627   &invoice_links;
1628
1629   $form->{currency}     = $currency;
1630   $form->{exchangerate} = "";
1631   $form->{forex}        = "";
1632   $form->{exchangerate} = $exchangerate
1633     if (
1634         $form->{forex} = (
1635                     $exchangerate =
1636                       $form->check_exchangerate(
1637                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
1638                       )));
1639
1640   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1641
1642   &prepare_invoice;
1643
1644
1645   &display_form;
1646
1647   $lxdebug->leave_sub();
1648 }
1649
1650 sub yes {
1651   $lxdebug->enter_sub();
1652   if (IS->delete_invoice(\%myconfig, \%$form, $spool)) {
1653     # saving the history
1654         if(!exists $form->{addition}) {
1655           $form->{addition} = "DELETED";
1656           $form->save_history($form->dbconnect(\%myconfig));
1657     }
1658     # /saving the history 
1659     $form->redirect($locale->text('Invoice deleted!')); 
1660   }
1661   $form->error($locale->text('Cannot delete invoice!'));
1662
1663   $lxdebug->leave_sub();
1664 }