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