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