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