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