Rechnungsfunktionen gehören natürlich nach IS und nicht nach IC.
[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     print qq|
1106     <input class=submit type=submit accesskey="u" name=action id=update_button value="|
1107       . $locale->text('Update') . qq|">
1108     <input class=submit type=submit name=action value="|
1109       . $locale->text('Ship to') . qq|">
1110     <input class=submit type=submit name=action value="|
1111       . $locale->text('Print') . qq|">
1112     <input class=submit type=submit name=action value="|
1113       . $locale->text('E-mail') . qq|"> |;
1114     print qq|<input class=submit type=submit name=action value="|
1115       . $locale->text('Storno') . qq|"> | unless ($form->{storno});
1116     print qq|<input class=submit type=submit name=action value="|
1117       . $locale->text('Post Payment') . qq|">
1118 |;
1119     print qq|<input class=submit type=submit name=action value="|
1120       . $locale->text('Use As Template') . qq|">
1121 |;
1122   if ($form->{id} && !($form->{type} eq "credit_note")) {
1123     print qq|
1124     <input class=submit type=submit name=action value="|
1125       . $locale->text('Credit Note') . qq|">
1126 |;
1127   }
1128     if ($form->{radier}) {
1129     print qq|
1130     <input class=submit type=submit name=action value="|
1131       . $locale->text('Delete') . qq|">
1132 |;
1133   }
1134
1135
1136     if ($invdate > $closedto) {
1137       print qq|
1138       <input class=submit type=submit name=action value="|
1139         . $locale->text('Order') . qq|">
1140 |;
1141     }
1142
1143   } else {
1144     if ($invdate > $closedto) {
1145       print qq|<input class=submit type=submit name=action id=update_button value="|
1146         . $locale->text('Update') . qq|">
1147       <input class=submit type=submit name=action value="|
1148         . $locale->text('Ship to') . qq|">
1149       <input class=submit type=submit name=action value="|
1150         . $locale->text('Preview') . qq|">
1151       <input class=submit type=submit name=action value="|
1152         . $locale->text('E-mail') . qq|">
1153       <input class=submit type=submit name=action value="|
1154         . $locale->text('Print and Post') . qq|">
1155       <input class=submit type=submit name=action value="|
1156         . $locale->text('Post') . qq|"> | .
1157         NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
1158                          '-class' => 'submit'));
1159     }
1160   }
1161
1162   # button for saving history
1163   if($form->{id} ne "") {
1164     print qq|
1165           <input type="button" class="submit" onclick="set_history_window(|
1166           . Q($form->{id})
1167           . qq|);" name="history" id="history" value="|
1168           . $locale->text('history')
1169           . qq|">|;
1170   }
1171   # /button for saving history
1172
1173
1174   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
1175     qq|
1176
1177 <input type=hidden name=rowcount value=$form->{rowcount}>
1178
1179 <input name=callback type=hidden value="$form->{callback}">
1180 |
1181 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
1182 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
1183 . qq|
1184 <input type=hidden name=path value=$form->{path}>
1185 <input type=hidden name=login value=$form->{login}>
1186 <input type=hidden name=password value=$form->{password}>
1187
1188 </form>
1189
1190 </body>
1191
1192  </html>
1193 |;
1194
1195   $lxdebug->leave_sub();
1196 }
1197
1198 sub update {
1199   $lxdebug->enter_sub();
1200
1201   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1202     qw(exchangerate creditlimit creditremaining);
1203   if ($form->{second_run}) {
1204     $form->{print_and_post} = 0;
1205   }
1206
1207   $form->{update} = 1;
1208
1209   &check_name(customer);
1210
1211   $form->{exchangerate} = $exchangerate
1212     if (
1213         $form->{forex} = (
1214                        $exchangerate =
1215                          $form->check_exchangerate(
1216                          \%myconfig, $form->{currency}, $form->{invdate}, 'buy'
1217                          )));
1218
1219   for $i (1 .. $form->{paidaccounts}) {
1220     if ($form->{"paid_$i"}) {
1221       map {
1222         $form->{"${_}_$i"} =
1223           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1224       } qw(paid exchangerate);
1225
1226       $form->{"exchangerate_$i"} = $exchangerate
1227         if (
1228             $form->{"forex_$i"} = (
1229                  $exchangerate =
1230                    $form->check_exchangerate(
1231                    \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy'
1232                    )));
1233     }
1234   }
1235
1236   $i            = $form->{rowcount};
1237   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
1238
1239   # if last row empty, check the form otherwise retrieve new item
1240   if (   ($form->{"partnumber_$i"} eq "")
1241       && ($form->{"description_$i"} eq "")
1242       && ($form->{"partsgroup_$i"}  eq "")) {
1243
1244     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
1245     &check_form;
1246
1247   } else {
1248
1249     IS->retrieve_item(\%myconfig, \%$form);
1250
1251     $rows = scalar @{ $form->{item_list} };
1252
1253     $form->{"discount_$i"} =
1254       $form->format_amount(\%myconfig, $form->{discount} * 100);
1255
1256     if ($rows) {
1257       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
1258
1259       if ($rows > 1) {
1260
1261         &select_item;
1262         exit;
1263
1264       } else {
1265
1266         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1267
1268         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1269           qw(partnumber description unit);
1270         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1271           keys %{ $form->{item_list}[0] };
1272         if ($form->{"part_payment_id_$i"} ne "") {
1273           $form->{payment_id} = $form->{"part_payment_id_$i"};
1274         }
1275
1276         if ($form->{"not_discountable_$i"}) {
1277           $form->{"discount_$i"} = 0;
1278         }
1279
1280         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1281         ($dec) = ($s =~ /\.(\d+)/);
1282         $dec           = length $dec;
1283         $decimalplaces = ($dec > 2) ? $dec : 2;
1284
1285         if ($sellprice) {
1286           $form->{"sellprice_$i"} = $sellprice;
1287         } else {
1288
1289           # if there is an exchange rate adjust sellprice
1290           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1291           $form->{"sellprice_$i"} /= $exchangerate;
1292         }
1293
1294         $form->{"listprice_$i"} /= $exchangerate;
1295
1296         $amount =
1297           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1298           (1 - $form->{"discount_$i"} / 100);
1299         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1300         map { $form->{"${_}_base"} += $amount }
1301           (split / /, $form->{"taxaccounts_$i"});
1302         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1303           split / /, $form->{"taxaccounts_$i"}
1304           if !$form->{taxincluded};
1305
1306         $form->{creditremaining} -= $amount;
1307
1308         map {
1309           $form->{"${_}_$i"} =
1310             $form->format_amount(\%myconfig, $form->{"${_}_$i"},
1311                                  $decimalplaces)
1312         } qw(sellprice listprice);
1313
1314         $form->{"qty_$i"} =
1315           $form->format_amount(\%myconfig, $form->{"qty_$i"});
1316
1317         if ($lizenzen) {
1318           if ($form->{"inventory_accno_$i"} ne "") {
1319             $form->{"lizenzen_$i"} = qq|<option></option>|;
1320             foreach $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
1321               $form->{"lizenzen_$i"} .=
1322                 qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
1323             }
1324             $form->{"lizenzen_$i"} .=
1325               qq|<option value=-1>Neue Lizenz</option>|;
1326           }
1327         }
1328
1329         # get pricegroups for parts
1330         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1331
1332         # build up html code for prices_$i
1333         &set_pricegroup($i);
1334       }
1335
1336       &display_form;
1337
1338     } else {
1339
1340       # ok, so this is a new part
1341       # ask if it is a part or service item
1342
1343       if (   $form->{"partsgroup_$i"}
1344           && ($form->{"partsnumber_$i"} eq "")
1345           && ($form->{"description_$i"} eq "")) {
1346         $form->{rowcount}--;
1347         $form->{"discount_$i"} = "";
1348         &display_form;
1349           } else {
1350
1351         $form->{"id_$i"}   = 0;
1352         $form->{"unit_$i"} = $locale->text('ea');
1353
1354         &new_item;
1355
1356       }
1357     }
1358   }
1359   $lxdebug->leave_sub();
1360 }
1361
1362 sub post_payment {
1363   $lxdebug->enter_sub();
1364   for $i (1 .. $form->{paidaccounts}) {
1365     if ($form->{"paid_$i"}) {
1366       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1367
1368       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1369
1370       $form->error($locale->text('Cannot post payment for a closed period!'))
1371         if ($datepaid <= $closedto);
1372
1373       if ($form->{currency} ne $form->{defaultcurrency}) {
1374         $form->{"exchangerate_$i"} = $form->{exchangerate}
1375           if ($invdate == $datepaid);
1376         $form->isblank("exchangerate_$i",
1377                        $locale->text('Exchangerate for payment missing!'));
1378       }
1379     }
1380   }
1381
1382   ($form->{AR})      = split /--/, $form->{AR};
1383   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1384   relink_accounts();
1385   $form->redirect($locale->text(' Payment posted!'))
1386       if (IS->post_payment(\%myconfig, \%$form));
1387     $form->error($locale->text('Cannot post payment!'));
1388
1389
1390   $lxdebug->leave_sub();
1391 }
1392
1393 sub post {
1394   $lxdebug->enter_sub();
1395   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
1396   $form->isblank("customer", $locale->text('Customer missing!'));
1397
1398   # if oldcustomer ne customer redo form
1399   if (&check_name(customer)) {
1400     &update;
1401     exit;
1402   }
1403   if ($form->{second_run}) {
1404     $form->{print_and_post} = 0;
1405   }
1406
1407   &validate_items;
1408
1409   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1410   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1411
1412   $form->error($locale->text('Cannot post invoice for a closed period!'))
1413     if ($invdate <= $closedto);
1414
1415   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1416     if ($form->{currency} ne $form->{defaultcurrency});
1417
1418   for $i (1 .. $form->{paidaccounts}) {
1419     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1420       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1421
1422       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1423
1424       $form->error($locale->text('Cannot post payment for a closed period!'))
1425         if ($datepaid <= $closedto);
1426
1427       if ($form->{currency} ne $form->{defaultcurrency}) {
1428         $form->{"exchangerate_$i"} = $form->{exchangerate}
1429           if ($invdate == $datepaid);
1430         $form->isblank("exchangerate_$i",
1431                        $locale->text('Exchangerate for payment missing!'));
1432       }
1433     }
1434   }
1435
1436   ($form->{AR})      = split /--/, $form->{AR};
1437   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1438
1439   $form->{label} = $locale->text('Invoice');
1440
1441   $form->{id} = 0 if $form->{postasnew};
1442
1443   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
1444   if ($form->{postasnew}) {
1445     if ($form->{type} eq "credit_note") {
1446       undef($form->{cnnumber});
1447     } else {
1448       undef($form->{invnumber});
1449     }
1450   }
1451
1452   relink_accounts();
1453   if ($print_post) {
1454     if (!(IS->post_invoice(\%myconfig, \%$form))) {
1455       $form->error($locale->text('Cannot post invoice!'));
1456     }
1457     remove_draft() if $form->{remove_draft};
1458     # saving the history
1459         if(!exists $form->{addition}) {
1460           $form->{addition} = "PRINTED AND POSTED";
1461           $form->save_history($form->dbconnect(\%myconfig));
1462     }
1463     # /saving the history
1464     
1465   } else {
1466     if (IS->post_invoice(\%myconfig, \%$form)){
1467       remove_draft() if $form->{remove_draft};
1468         # saving the history
1469         if(!exists $form->{addition}) {
1470                         if($form->{storno}) {
1471                                 $form->{addition} = "STORNO";
1472                         }
1473                         else {
1474                                 $form->{addition} = "POSTED";
1475                         }
1476                         $form->save_history($form->dbconnect(\%myconfig));
1477         }
1478         # /saving the history
1479     
1480         $form->redirect(
1481             $form->{label} . " $form->{invnumber} " . $locale->text('posted!'));
1482         }
1483     $form->error($locale->text('Cannot post invoice!'));
1484   }
1485
1486   $lxdebug->leave_sub();
1487 }
1488
1489 sub print_and_post {
1490   $lxdebug->enter_sub();
1491
1492   $old_form               = new Form;
1493   $print_post             = 1;
1494   $form->{print_and_post} = 1;
1495   &post();
1496
1497   &edit();
1498   $lxdebug->leave_sub();
1499
1500 }
1501
1502 sub use_as_template {
1503   $lxdebug->enter_sub();
1504
1505   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);
1506   $form->{paidaccounts} = 1;
1507   $form->{rowcount}--;
1508   $form->{invdate} = $form->current_date(\%myconfig);
1509   &display_form;
1510
1511   $lxdebug->leave_sub();
1512 }
1513
1514 sub storno {
1515   $lxdebug->enter_sub();
1516
1517   if ($form->{storno}) {
1518     $form->error($locale->text('Cannot storno storno invoice!'));
1519   }
1520
1521   if (IS->has_storno(\%myconfig, $form)) {
1522     $form->error($locale->text("Invoice has already been storno'd!"));
1523   }
1524
1525   $form->{storno_id} = $form->{id};
1526   $form->{storno} = 1;
1527   $form->{id} = "";
1528   $form->{invnumber} = "Storno zu " . $form->{invnumber};
1529
1530   &post();
1531   $lxdebug->leave_sub();
1532
1533 }
1534
1535 sub preview {
1536   $lxdebug->enter_sub();
1537
1538   $form->{preview} = 1;
1539   $old_form = new Form;
1540   for (keys %$form) { $old_form->{$_} = $form->{$_} }
1541   $old_form->{rowcount}++;
1542
1543   &print_form($old_form);
1544   $lxdebug->leave_sub();
1545
1546 }
1547
1548 sub delete {
1549   $lxdebug->enter_sub();
1550   if ($form->{second_run}) {
1551     $form->{print_and_post} = 0;
1552   }
1553   $form->header;
1554
1555   print qq|
1556 <body>
1557
1558 <form method=post action=$form->{script}>
1559 |;
1560
1561   # delete action variable
1562   map { delete $form->{$_} } qw(action header);
1563
1564   foreach $key (keys %$form) {
1565     $form->{$key} =~ s/\"/&quot;/g;
1566     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1567   }
1568
1569   print qq|
1570 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1571
1572 <h4>|
1573     . $locale->text('Are you sure you want to delete Invoice Number')
1574     . qq| $form->{invnumber}
1575 </h4>
1576
1577 <p>
1578 <input name=action class=submit type=submit value="|
1579     . $locale->text('Yes') . qq|">
1580 </form>
1581 |;
1582
1583   $lxdebug->leave_sub();
1584 }
1585
1586 sub credit_note {
1587   $lxdebug->enter_sub();
1588
1589   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1590   $form->{duedate} =
1591     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1592
1593   $form->{id}     = '';
1594   $form->{rowcount}--;
1595   $form->{shipto} = 1;
1596
1597
1598   $form->{title}  = $locale->text('Add Credit Note');
1599   $form->{script} = 'is.pl';
1600   $script         = "is";
1601   $buysell        = 'buy';
1602   
1603
1604   # bo creates the id, reset it
1605   map { delete $form->{$_} }
1606     qw(id invnumber subject message cc bcc printed emailed queued);
1607   $form->{ $form->{vc} } =~ s/--.*//g;
1608   $form->{type} = "credit_note";
1609
1610
1611   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1612
1613   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1614     qw(creditlimit creditremaining);
1615
1616   $currency = $form->{currency};
1617   &invoice_links;
1618
1619   $form->{currency}     = $currency;
1620   $form->{exchangerate} = "";
1621   $form->{forex}        = "";
1622   $form->{exchangerate} = $exchangerate
1623     if (
1624         $form->{forex} = (
1625                     $exchangerate =
1626                       $form->check_exchangerate(
1627                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
1628                       )));
1629
1630   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1631
1632   &prepare_invoice;
1633
1634
1635   &display_form;
1636
1637   $lxdebug->leave_sub();
1638 }
1639
1640 sub yes {
1641   $lxdebug->enter_sub();
1642   if (IS->delete_invoice(\%myconfig, \%$form, $spool)) {
1643     # saving the history
1644         if(!exists $form->{addition}) {
1645           $form->{addition} = "DELETED";
1646           $form->save_history($form->dbconnect(\%myconfig));
1647     }
1648     # /saving the history 
1649     $form->redirect($locale->text('Invoice deleted!')); 
1650   }
1651   $form->error($locale->text('Cannot delete invoice!'));
1652
1653   $lxdebug->leave_sub();
1654 }