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