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