Historie eingefügt
[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"}</th>
857                 <td align=right>$form->{"${item}_total"}</td>
858               </tr>
859 |;
860       }
861     }
862
863     $form->{invsubtotal} =
864       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
865
866     $subtotal = qq|
867               <tr>
868                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
869                 <td align=right>$form->{invsubtotal}</td>
870               </tr>
871 |;
872
873   }
874
875   if ($form->{taxincluded}) {
876     foreach $item (split / /, $form->{taxaccounts}) {
877       if ($form->{"${item}_base"}) {
878         $form->{"${item}_total"} =
879           $form->round_amount(
880                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
881                               (1 + $form->{"${item}_rate"})
882                            ),
883                            2);
884         $form->{"${item}_netto"} =
885           $form->round_amount(
886                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
887                           2);
888         $form->{"${item}_total"} =
889           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
890         $form->{"${item}_netto"} =
891           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
892
893         $tax .= qq|
894               <tr>
895                 <th align=right>Enthaltene $form->{"${item}_description"}</th>
896                 <td align=right>$form->{"${item}_total"}</td>
897               </tr>
898               <tr>
899                 <th align=right>Nettobetrag</th>
900                 <td align=right>$form->{"${item}_netto"}</td>
901               </tr>
902 |;
903       }
904     }
905
906   }
907
908   $form->{oldinvtotal} = $form->{invtotal};
909   $form->{invtotal}    =
910     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
911
912   print qq|
913   <tr>
914     <td>
915       <table width=100%>
916         <tr valign=bottom>
917           <td>
918             <table>
919               <tr>
920                 <th align=left>| . $locale->text('Notes') . qq|</th>
921                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
922                 <th align=right>| . $locale->text('Payment Terms') . qq|</th>
923               </tr>
924               <tr valign=top>
925                 <td>$notes</td>
926                 <td>$intnotes</td>
927                 <td><select name=payment_id onChange="if (this.value) set_duedate(['payment_id__' + this.value],['duedate'])">$payment
928                 </select></td>
929               </tr>
930             </table>
931           </td>
932           <td align=right width=100%>
933             $taxincluded
934             <table width=100%>
935               $subtotal
936               $tax
937               <tr>
938                 <th align=right>| . $locale->text('Total') . qq|</th>
939                 <td align=right>$form->{invtotal}</td>
940               </tr>
941             </table>
942           </td>
943         </tr>
944       </table>
945     </td>
946   </tr>
947 |;
948   if ($webdav) {
949     $webdav_list = qq|
950   <tr>
951     <td><hr size=3 noshade></td>
952   </tr>
953   <tr>
954     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
955   </tr>
956     <table width=100%>
957       <td align=left width=30%><b>Dateiname</b></td>
958       <td align=left width=70%><b>Webdavlink</b></td>
959 |;
960     foreach $file (keys %{ $form->{WEBDAV} }) {
961       $webdav_list .= qq|
962       <tr>
963         <td align=left>$file</td>
964         <td align=left><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
965       </tr>
966 |;
967     }
968     $webdav_list .= qq|
969     </table>
970   </tr>
971 |;
972
973     print $webdav_list;
974   }
975 if ($form->{type} eq "credit_note") {
976   print qq|
977   <tr>
978     <td>
979       <table width=100%>
980         <tr class=listheading>
981           <th colspan=6 class=listheading>|
982     . $locale->text('Payments') . qq|</th>
983         </tr>
984 |;
985 } else {
986   print qq|
987   <tr>
988     <td>
989       <table width=100%>
990         <tr class=listheading>
991           <th colspan=6 class=listheading>|
992     . $locale->text('Incoming Payments') . qq|</th>
993         </tr>
994 |;
995 }
996
997   if ($form->{currency} eq $form->{defaultcurrency}) {
998     @column_index = qw(datepaid source memo paid AR_paid);
999   } else {
1000     @column_index = qw(datepaid source memo paid exchangerate AR_paid);
1001   }
1002
1003   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
1004   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
1005   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
1006   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
1007   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
1008   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
1009
1010   print "
1011         <tr>
1012 ";
1013   map { print "$column_data{$_}\n" } @column_index;
1014   print "
1015         </tr>
1016 ";
1017
1018   my @triggers = ();
1019
1020   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
1021   for $i (1 .. $form->{paidaccounts}) {
1022
1023     print "
1024         <tr>\n";
1025
1026     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
1027     $form->{"selectAR_paid_$i"} =~
1028       s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
1029
1030     # format amounts
1031     $totalpaid += $form->{"paid_$i"};
1032     if ($form->{"paid_$i"}) {
1033       $form->{"paid_$i"} =
1034         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
1035     }
1036     $form->{"exchangerate_$i"} =
1037       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
1038
1039     $exchangerate = qq|&nbsp;|;
1040     if ($form->{currency} ne $form->{defaultcurrency}) {
1041       if ($form->{"forex_$i"}) {
1042         $exchangerate =
1043           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
1044       } else {
1045         $exchangerate =
1046           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
1047       }
1048     }
1049
1050     $exchangerate .= qq|
1051 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
1052 |;
1053
1054     $column_data{"paid_$i"} =
1055       qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
1056     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
1057     $column_data{"AR_paid_$i"}      =
1058       qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
1059     $column_data{"datepaid_$i"} =
1060       qq|<td align=center><input id="datepaid_$i" name="datepaid_$i"  size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}>
1061          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
1062     $column_data{"source_$i"} =
1063       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
1064     $column_data{"memo_$i"} =
1065       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
1066
1067     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
1068     print "
1069         </tr>\n";
1070     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
1071   }
1072
1073   print qq|
1074 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
1075 <input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
1076 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
1077 <input type=hidden name=oldtotalpaid value=$totalpaid>
1078     </table>
1079     </td>
1080   </tr>
1081   <tr>
1082     <td><hr size=3 noshade></td>
1083   </tr>
1084   <tr>
1085     <td>
1086 |;
1087
1088   &print_options;
1089
1090   print qq|
1091     </td>
1092   </tr>
1093 </table>
1094 |;
1095
1096   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1097   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1098
1099   if ($form->{id}) {
1100     print qq|
1101     <input class=submit type=submit accesskey="u" name=action id=update_button value="|
1102       . $locale->text('Update') . qq|">
1103     <input class=submit type=submit name=action value="|
1104       . $locale->text('Ship to') . qq|">
1105     <input class=submit type=submit name=action value="|
1106       . $locale->text('Print') . qq|">
1107     <input class=submit type=submit name=action value="|
1108       . $locale->text('E-mail') . qq|">|;
1109     print qq|<input class=submit type=submit name=action value="|
1110       . $locale->text('Storno') . qq|">| unless ($form->{storno});
1111     print qq|<input class=submit type=submit name=action value="|
1112       . $locale->text('Post Payment') . qq|">
1113 |;
1114     print qq|<input class=submit type=submit name=action value="|
1115       . $locale->text('Use As Template') . qq|">
1116 |;
1117   if ($form->{id} && !($form->{type} eq "credit_note")) {
1118     print qq|
1119     <input class=submit type=submit name=action value="|
1120       . $locale->text('Credit Note') . qq|">
1121 |;
1122   }
1123     if ($form->{radier}) {
1124     print qq|
1125     <input class=submit type=submit name=action value="|
1126       . $locale->text('Delete') . qq|">
1127 |;
1128   }
1129
1130
1131     if ($invdate > $closedto) {
1132       print qq|
1133       <input class=submit type=submit name=action value="|
1134         . $locale->text('Order') . qq|">
1135 |;
1136     }
1137
1138   } else {
1139     if ($invdate > $closedto) {
1140       print qq|<input class=submit type=submit name=action id=update_button value="|
1141         . $locale->text('Update') . qq|">
1142       <input class=submit type=submit name=action value="|
1143         . $locale->text('Ship to') . qq|">
1144       <input class=submit type=submit name=action value="|
1145         . $locale->text('Preview') . qq|">
1146       <input class=submit type=submit name=action value="|
1147         . $locale->text('E-mail') . qq|">
1148       <input class=submit type=submit name=action value="|
1149         . $locale->text('Print and Post') . qq|">
1150       <input class=submit type=submit name=action value="|
1151         . $locale->text('Post') . qq|">|;
1152     }
1153   }
1154
1155   # button for saving history
1156   if($form->{id} ne "") {
1157     print qq|
1158           <input type=button class=submit onclick=set_history_window(|
1159           . $form->{id} 
1160           . qq|); name=history id=history value=|
1161           . $locale->text('history') 
1162           . qq|>|;
1163   }
1164   # /button for saving history
1165
1166
1167   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
1168     qq|
1169
1170 <input type=hidden name=rowcount value=$form->{rowcount}>
1171
1172 <input name=callback type=hidden value="$form->{callback}">
1173
1174 <input type=hidden name=path value=$form->{path}>
1175 <input type=hidden name=login value=$form->{login}>
1176 <input type=hidden name=password value=$form->{password}>
1177
1178 </form>
1179
1180 </body>
1181
1182  </html>
1183 |;
1184
1185   $lxdebug->leave_sub();
1186 }
1187
1188 sub update {
1189   $lxdebug->enter_sub();
1190
1191   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1192     qw(exchangerate creditlimit creditremaining);
1193   if ($form->{second_run}) {
1194     $form->{print_and_post} = 0;
1195   }
1196
1197   $form->{update} = 1;
1198
1199   &check_name(customer);
1200
1201   $form->{exchangerate} = $exchangerate
1202     if (
1203         $form->{forex} = (
1204                        $exchangerate =
1205                          $form->check_exchangerate(
1206                          \%myconfig, $form->{currency}, $form->{invdate}, 'buy'
1207                          )));
1208
1209   for $i (1 .. $form->{paidaccounts}) {
1210     if ($form->{"paid_$i"}) {
1211       map {
1212         $form->{"${_}_$i"} =
1213           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1214       } qw(paid exchangerate);
1215
1216       $form->{"exchangerate_$i"} = $exchangerate
1217         if (
1218             $form->{"forex_$i"} = (
1219                  $exchangerate =
1220                    $form->check_exchangerate(
1221                    \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy'
1222                    )));
1223     }
1224   }
1225
1226   $i            = $form->{rowcount};
1227   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
1228
1229   # if last row empty, check the form otherwise retrieve new item
1230   if (   ($form->{"partnumber_$i"} eq "")
1231       && ($form->{"description_$i"} eq "")
1232       && ($form->{"partsgroup_$i"}  eq "")) {
1233
1234     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
1235     &check_form;
1236
1237   } else {
1238
1239     IS->retrieve_item(\%myconfig, \%$form);
1240
1241     $rows = scalar @{ $form->{item_list} };
1242
1243     $form->{"discount_$i"} =
1244       $form->format_amount(\%myconfig, $form->{discount} * 100);
1245
1246     if ($rows) {
1247       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
1248
1249       if ($rows > 1) {
1250
1251         &select_item;
1252         exit;
1253
1254       } else {
1255
1256         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1257
1258         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1259           qw(partnumber description unit);
1260         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1261           keys %{ $form->{item_list}[0] };
1262         if ($form->{"part_payment_id_$i"} ne "") {
1263           $form->{payment_id} = $form->{"part_payment_id_$i"};
1264         }
1265
1266         if ($form->{"not_discountable_$i"}) {
1267           $form->{"discount_$i"} = 0;
1268         }
1269
1270         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1271         ($dec) = ($s =~ /\.(\d+)/);
1272         $dec           = length $dec;
1273         $decimalplaces = ($dec > 2) ? $dec : 2;
1274
1275         if ($sellprice) {
1276           $form->{"sellprice_$i"} = $sellprice;
1277         } else {
1278
1279           # if there is an exchange rate adjust sellprice
1280           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1281           $form->{"sellprice_$i"} /= $exchangerate;
1282         }
1283
1284         $form->{"listprice_$i"} /= $exchangerate;
1285
1286         $amount =
1287           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1288           (1 - $form->{"discount_$i"} / 100);
1289         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1290         map { $form->{"${_}_base"} += $amount }
1291           (split / /, $form->{"taxaccounts_$i"});
1292         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1293           split / /, $form->{"taxaccounts_$i"}
1294           if !$form->{taxincluded};
1295
1296         $form->{creditremaining} -= $amount;
1297
1298         map {
1299           $form->{"${_}_$i"} =
1300             $form->format_amount(\%myconfig, $form->{"${_}_$i"},
1301                                  $decimalplaces)
1302         } qw(sellprice listprice);
1303
1304         $form->{"qty_$i"} =
1305           $form->format_amount(\%myconfig, $form->{"qty_$i"});
1306
1307         if ($lizenzen) {
1308           if ($form->{"inventory_accno_$i"} ne "") {
1309             $form->{"lizenzen_$i"} = qq|<option></option>|;
1310             foreach $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
1311               $form->{"lizenzen_$i"} .=
1312                 qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
1313             }
1314             $form->{"lizenzen_$i"} .=
1315               qq|<option value=-1>Neue Lizenz</option>|;
1316           }
1317         }
1318
1319         # get pricegroups for parts
1320         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1321
1322         # build up html code for prices_$i
1323         &set_pricegroup($i);
1324       }
1325
1326       &display_form;
1327
1328     } else {
1329
1330       # ok, so this is a new part
1331       # ask if it is a part or service item
1332
1333       if (   $form->{"partsgroup_$i"}
1334           && ($form->{"partsnumber_$i"} eq "")
1335           && ($form->{"description_$i"} eq "")) {
1336         $form->{rowcount}--;
1337         $form->{"discount_$i"} = "";
1338         &display_form;
1339           } else {
1340
1341         $form->{"id_$i"}   = 0;
1342         $form->{"unit_$i"} = $locale->text('ea');
1343
1344         &new_item;
1345
1346       }
1347     }
1348   }
1349   $lxdebug->leave_sub();
1350 }
1351
1352 sub post_payment {
1353   $lxdebug->enter_sub();
1354   for $i (1 .. $form->{paidaccounts}) {
1355     if ($form->{"paid_$i"}) {
1356       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1357
1358       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1359
1360       $form->error($locale->text('Cannot post payment for a closed period!'))
1361         if ($datepaid <= $closedto);
1362
1363       if ($form->{currency} ne $form->{defaultcurrency}) {
1364         $form->{"exchangerate_$i"} = $form->{exchangerate}
1365           if ($invdate == $datepaid);
1366         $form->isblank("exchangerate_$i",
1367                        $locale->text('Exchangerate for payment missing!'));
1368       }
1369     }
1370   }
1371
1372   ($form->{AR})      = split /--/, $form->{AR};
1373   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1374   relink_accounts();
1375   $form->redirect($locale->text(' Payment posted!'))
1376       if (IS->post_payment(\%myconfig, \%$form));
1377     $form->error($locale->text('Cannot post payment!'));
1378
1379
1380   $lxdebug->leave_sub();
1381 }
1382
1383 sub post {
1384   $lxdebug->enter_sub();
1385   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
1386   $form->isblank("customer", $locale->text('Customer missing!'));
1387
1388   # if oldcustomer ne customer redo form
1389   if (&check_name(customer)) {
1390     &update;
1391     exit;
1392   }
1393   if ($form->{second_run}) {
1394     $form->{print_and_post} = 0;
1395   }
1396
1397   &validate_items;
1398
1399   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1400   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1401
1402   $form->error($locale->text('Cannot post invoice for a closed period!'))
1403     if ($invdate <= $closedto);
1404
1405   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1406     if ($form->{currency} ne $form->{defaultcurrency});
1407
1408   for $i (1 .. $form->{paidaccounts}) {
1409     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1410       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1411
1412       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1413
1414       $form->error($locale->text('Cannot post payment for a closed period!'))
1415         if ($datepaid <= $closedto);
1416
1417       if ($form->{currency} ne $form->{defaultcurrency}) {
1418         $form->{"exchangerate_$i"} = $form->{exchangerate}
1419           if ($invdate == $datepaid);
1420         $form->isblank("exchangerate_$i",
1421                        $locale->text('Exchangerate for payment missing!'));
1422       }
1423     }
1424   }
1425
1426   ($form->{AR})      = split /--/, $form->{AR};
1427   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1428
1429   $form->{label} = $locale->text('Invoice');
1430
1431   $form->{id} = 0 if $form->{postasnew};
1432
1433   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
1434   if (!$form->{invnumber} || $form->{postasnew}) {
1435     if ($form->{type} eq "credit_note") {
1436       $form->{invnumber} = $form->update_defaults(\%myconfig, "cnnumber");
1437     } else {
1438       $form->{invnumber} = $form->update_defaults(\%myconfig, "invnumber");
1439     }
1440   }
1441   relink_accounts();
1442   if ($print_post) {
1443     if (!(IS->post_invoice(\%myconfig, \%$form))) {
1444       $form->error($locale->text('Cannot post invoice!'));
1445     }
1446     # saving the history
1447         if(!exists $form->{addition}) {
1448           $form->{addition} = "PRINTED AND POSTED";
1449           $form->save_history($form->dbconnect(\%myconfig));
1450     }
1451     # /saving the history
1452     
1453   } else {
1454       if (IS->post_invoice(\%myconfig, \%$form)){
1455         # saving the history
1456                 if(!exists $form->{addition}) {
1457                         if($form->{storno}) {
1458                                 $form->{addition} = "STORNO";
1459                         }
1460                         else {
1461                                 $form->{addition} = "POSTED";
1462                         }
1463                         $form->save_history($form->dbconnect(\%myconfig));
1464         }
1465         # /saving the history
1466     
1467         $form->redirect(
1468             $form->{label} . " $form->{invnumber} " . $locale->text('posted!'));
1469         }
1470     $form->error($locale->text('Cannot post invoice!'));
1471   }
1472
1473   $lxdebug->leave_sub();
1474 }
1475
1476 sub print_and_post {
1477   $lxdebug->enter_sub();
1478
1479   $old_form               = new Form;
1480   $print_post             = 1;
1481   $form->{print_and_post} = 1;
1482   &post();
1483
1484   &edit();
1485   $lxdebug->leave_sub();
1486
1487 }
1488
1489 sub use_as_template {
1490   $lxdebug->enter_sub();
1491
1492   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);
1493   $form->{paidaccounts} = 1;
1494   $form->{rowcount}--;
1495   $form->{invdate} = $form->current_date(\%myconfig);
1496   &display_form;
1497
1498   $lxdebug->leave_sub();
1499 }
1500
1501 sub storno {
1502   $lxdebug->enter_sub();
1503
1504   if ($form->{storno}) {
1505     $form->error($locale->text('Cannot storno storno invoice!'));
1506   }
1507
1508   $form->{storno_id} = $form->{id};
1509   $form->{storno} = 1;
1510   $form->{id} = "";
1511   $form->{invnumber} = "Storno zu " . $form->{invnumber};
1512
1513   &post();
1514   $lxdebug->leave_sub();
1515
1516 }
1517
1518 sub preview {
1519   $lxdebug->enter_sub();
1520
1521   $form->{preview} = 1;
1522   $old_form = new Form;
1523   for (keys %$form) { $old_form->{$_} = $form->{$_} }
1524   $old_form->{rowcount}++;
1525
1526   &print_form($old_form);
1527   $lxdebug->leave_sub();
1528
1529 }
1530
1531 sub delete {
1532   $lxdebug->enter_sub();
1533   if ($form->{second_run}) {
1534     $form->{print_and_post} = 0;
1535   }
1536   $form->header;
1537
1538   print qq|
1539 <body>
1540
1541 <form method=post action=$form->{script}>
1542 |;
1543
1544   # delete action variable
1545   map { delete $form->{$_} } qw(action header);
1546
1547   foreach $key (keys %$form) {
1548     $form->{$key} =~ s/\"/&quot;/g;
1549     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1550   }
1551
1552   print qq|
1553 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1554
1555 <h4>|
1556     . $locale->text('Are you sure you want to delete Invoice Number')
1557     . qq| $form->{invnumber}
1558 </h4>
1559
1560 <p>
1561 <input name=action class=submit type=submit value="|
1562     . $locale->text('Yes') . qq|">
1563 </form>
1564 |;
1565
1566   $lxdebug->leave_sub();
1567 }
1568
1569 sub credit_note {
1570   $lxdebug->enter_sub();
1571
1572   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1573   $form->{duedate} =
1574     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1575
1576   $form->{id}     = '';
1577   $form->{rowcount}--;
1578   $form->{shipto} = 1;
1579
1580
1581   $form->{title}  = $locale->text('Add Credit Note');
1582   $form->{script} = 'is.pl';
1583   $script         = "is";
1584   $buysell        = 'buy';
1585   
1586
1587   # bo creates the id, reset it
1588   map { delete $form->{$_} }
1589     qw(id invnumber subject message cc bcc printed emailed queued);
1590   $form->{ $form->{vc} } =~ s/--.*//g;
1591   $form->{type} = "credit_note";
1592
1593
1594   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1595
1596   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1597     qw(creditlimit creditremaining);
1598
1599   $currency = $form->{currency};
1600   &invoice_links;
1601
1602   $form->{currency}     = $currency;
1603   $form->{exchangerate} = "";
1604   $form->{forex}        = "";
1605   $form->{exchangerate} = $exchangerate
1606     if (
1607         $form->{forex} = (
1608                     $exchangerate =
1609                       $form->check_exchangerate(
1610                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
1611                       )));
1612
1613   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1614
1615   &prepare_invoice;
1616
1617
1618   &display_form;
1619
1620   $lxdebug->leave_sub();
1621 }
1622
1623 sub yes {
1624   $lxdebug->enter_sub();
1625   if (IS->delete_invoice(\%myconfig, \%$form, $spool)) {
1626     # saving the history
1627         if(!exists $form->{addition}) {
1628           $form->{addition} = "DELETED";
1629           $form->save_history($form->dbconnect(\%myconfig));
1630     }
1631     # /saving the history 
1632     $form->redirect($locale->text('Invoice deleted!')); 
1633   }
1634   $form->error($locale->text('Cannot delete invoice!'));
1635
1636   $lxdebug->leave_sub();
1637 }