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