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