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