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