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