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