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