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