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