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