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