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