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