Optik in den Einkaufs- und Verkaufsformularen etwas verbessert:
[kivitendo-erp.git] / bin / mozilla / oe.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-2003
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 # Order entry module
31 # Quotation module
32 #======================================================================
33 use Data::Dumper;
34
35 use SL::OE;
36 use SL::IR;
37 use SL::IS;
38 use SL::PE;
39
40 require "bin/mozilla/io.pl";
41 require "bin/mozilla/arap.pl";
42
43 1;
44
45 # end of main
46
47 # For locales.pl:
48 # $locale->text('Edit the purchase_order');
49 # $locale->text('Edit the sales_order');
50 # $locale->text('Edit the request_quotation');
51 # $locale->text('Edit the sales_quotation');
52
53 # $locale->text('Workflow purchase_order');
54 # $locale->text('Workflow sales_order');
55 # $locale->text('Workflow request_quotation');
56 # $locale->text('Workflow sales_quotation');
57
58 sub set_headings {
59   $lxdebug->enter_sub();
60
61   my ($action) = @_;
62
63   if ($form->{type} eq 'purchase_order') {
64     $form->{title}   = $action eq "edit" ?
65       $locale->text('Edit Purchase Order') :
66       $locale->text('Add Purchase Order');
67     $form->{heading} = $locale->text('Purchase Order');
68     $form->{vc}      = 'vendor';
69   }
70   if ($form->{type} eq 'sales_order') {
71     $form->{title}   = $action eq "edit" ?
72       $locale->text('Edit Sales Order') :
73       $locale->text('Add Sales Order');
74     $form->{heading} = $locale->text('Sales Order');
75     $form->{vc}      = 'customer';
76   }
77   if ($form->{type} eq 'request_quotation') {
78     $form->{title}   = $action eq "edit" ?
79       $locale->text('Edit Request for Quotation') :
80       $locale->text('Add Request for Quotation');
81     $form->{heading} = $locale->text('Request for Quotation');
82     $form->{vc}      = 'vendor';
83   }
84   if ($form->{type} eq 'sales_quotation') {
85     $form->{title}   = $action eq "edit" ?
86       $locale->text('Edit Quotation') :
87       $locale->text('Add Quotation');
88     $form->{heading} = $locale->text('Quotation');
89     $form->{vc}      = 'customer';
90   }
91
92   $lxdebug->leave_sub();
93 }
94
95 sub add {
96   $lxdebug->enter_sub();
97
98   set_headings("add");
99
100   $form->{callback} =
101     "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}&login=$form->{login}&password=$form->{password}"
102     unless $form->{callback};
103
104   &order_links;
105   &prepare_order;
106   &display_form;
107
108   $lxdebug->leave_sub();
109 }
110
111 sub edit {
112   $lxdebug->enter_sub();
113   # show history button
114   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
115   #/show hhistory button
116
117   $form->{simple_save} = 0;
118
119   set_headings("edit");
120
121   # editing without stuff to edit? try adding it first
122   if ($form->{rowcount}) {
123     map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
124     if (!$id) {
125
126       # reset rowcount
127       undef $form->{rowcount};
128       &add;
129       $lxdebug->leave_sub();
130       return;
131     }
132   } elsif (!$form->{id}) {
133     &add;
134     $lxdebug->leave_sub();
135     return;
136   }
137
138   if ($form->{print_and_save}) {
139     $form->{action}   = "print";
140     $form->{resubmit} = 1;
141     $language_id = $form->{language_id};
142     $printer_id = $form->{printer_id};
143   }
144
145   set_headings("edit");
146
147   &order_links;
148   &prepare_order;
149   if ($form->{print_and_save}) {
150     $form->{language_id} = $language_id;
151     $form->{printer_id} = $printer_id;
152   }
153   &display_form;
154
155   $lxdebug->leave_sub();
156 }
157
158 sub order_links {
159   $lxdebug->enter_sub();
160   # get customer/vendor
161   $form->all_vc(\%myconfig, $form->{vc},
162                 ($form->{vc} eq 'customer') ? "AR" : "AP");
163
164   # retrieve order/quotation
165   $form->{webdav} = $webdav;
166   # set jscalendar
167   $form->{jscalendar} = $jscalendar;
168
169   my $editing = $form->{id};
170
171   OE->retrieve(\%myconfig, \%$form);
172
173   if ($form->{payment_id}) {
174     $payment_id = $form->{payment_id};
175   }
176   if ($form->{language_id}) {
177     $language_id = $form->{language_id};
178   }
179   if ($form->{taxzone_id}) {
180     $taxzone_id = $form->{taxzone_id};
181   }
182
183   $salesman_id = $form->{salesman_id} if ($editing);
184
185
186   # if multiple rowcounts (== collective order) then check if the
187   # there were more than one customer (in that case OE::retrieve removes
188   # the content from the field)
189   if (   $form->{rowcount}
190       && $form->{type} eq 'sales_order'
191       && defined $form->{customer}
192       && $form->{customer} eq '') {
193
194     #    $main::lxdebug->message(0, "Detected Edit order with concurrent customers");
195     $form->error(
196                  $locale->text(
197                    'Collective Orders only work for orders from one customer!')
198     );
199   }
200
201   $taxincluded = $form->{taxincluded};
202   $form->{shipto} = 1 if $form->{id};
203
204   if ($form->{"all_$form->{vc}"}) {
205     unless ($form->{"$form->{vc}_id"}) {
206       $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
207     }
208   }
209
210   $cp_id    = $form->{cp_id};
211   $intnotes = $form->{intnotes};
212
213   # get customer / vendor
214   if ($form->{type} =~ /(purchase_order|request_quotation)/) {
215     IR->get_vendor(\%myconfig, \%$form);
216
217     #quote all_vendor Bug 133
218     foreach $ref (@{ $form->{all_vendor} }) {
219       $ref->{name} = $form->quote($ref->{name});
220     }
221
222   }
223   if ($form->{type} =~ /sales_(order|quotation)/) {
224     IS->get_customer(\%myconfig, \%$form);
225
226     #quote all_vendor Bug 133
227     foreach $ref (@{ $form->{all_customer} }) {
228       $ref->{name} = $form->quote($ref->{name});
229     }
230
231   }
232   $form->{cp_id} = $cp_id;
233
234   if ($payment_id) {
235     $form->{payment_id} = $payment_id;
236   }
237   if ($language_id) {
238     $form->{language_id} = $language_id;
239   }
240   if ($taxzone_id) {
241     $form->{taxzone_id} = $taxzone_id;
242   }
243   $form->{intnotes} = $intnotes if $intnotes;
244   ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
245   $form->{"old$form->{vc}"} =
246     qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
247
248   # build the popup menus
249   if (@{ $form->{"all_$form->{vc}"} }) {
250     $form->{ $form->{vc} } =
251       qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
252     map { $form->{"select$form->{vc}"} .=
253 "<option>$_->{name}--$_->{id}</option>\n" }
254       (@{ $form->{"all_$form->{vc}"} });
255   }
256
257   $form->{taxincluded} = $taxincluded if ($form->{id});
258
259   # departments
260   if (@{ $form->{all_departments} }) {
261     $form->{selectdepartment} = "<option>\n";
262     $form->{department}       = "$form->{department}--$form->{department_id}";
263
264     map {
265       $form->{selectdepartment} .=
266         "<option>$_->{description}--$_->{id}</option>\n"
267     } (@{ $form->{all_departments} });
268   }
269
270   $form->{employee} = "$form->{employee}--$form->{employee_id}";
271
272   # forex
273   $form->{forex} = $form->{exchangerate};
274
275   $form->{salesman_id} = $salesman_id if ($editing);
276
277   $lxdebug->leave_sub();
278 }
279
280 sub prepare_order {
281   $lxdebug->enter_sub();
282   $form->{formname} = $form->{type} unless $form->{formname};
283
284   my $i = 0;
285   foreach $ref (@{ $form->{form_details} }) {
286     $form->{rowcount} = ++$i;
287
288     map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
289   }
290   for my $i (1 .. $form->{rowcount}) {
291     if ($form->{id}) {
292       $form->{"discount_$i"} =
293         $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
294     } else {
295       $form->{"discount_$i"} =
296         $form->format_amount(\%myconfig, $form->{"discount_$i"});
297     }
298     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
299     $dec           = length $dec;
300     $decimalplaces = ($dec > 2) ? $dec : 2;
301
302     # copy reqdate from deliverydate for invoice -> order conversion
303     $form->{"reqdate_$i"} = $form->{"deliverydate_$i"}
304       unless $form->{"reqdate_$i"};
305
306     $form->{"sellprice_$i"} =
307       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
308                            $decimalplaces);
309
310     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
311     $dec_qty = length $dec_qty;
312     $form->{"qty_$i"} =
313       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
314
315     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
316       qw(partnumber description unit);
317   }
318
319   $lxdebug->leave_sub();
320 }
321
322 sub form_header {
323   $lxdebug->enter_sub();
324
325   my $checkedclosed = $form->{"closed"} ? "checked" : "";
326   my $checkeddelivered = $form->{"delivered"} ? "checked" : "";
327
328   map { $form->{$_} =~ s/\"/&quot;/g }
329     qw(ordnumber quonumber shippingpoint shipvia notes intnotes shiptoname
330        shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
331        shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
332
333   # use JavaScript Calendar or not
334   $form->{jsscript} = $form->{jscalendar};
335   $jsscript = "";
336
337   if ($form->{jsscript}) {
338
339     # with JavaScript Calendar
340     $button1 = qq|
341        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\">
342         <input type=button name=transdate id="trigger1" value=|
343       . $locale->text('button') . qq|></td>
344       |;
345     $button2 = qq|
346        <td width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\">
347         <input type=button name=reqdate name=reqdate id="trigger2" value=|
348       . $locale->text('button') . qq|></td>
349      |;
350
351     #write Trigger
352     $jsscript =
353       Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
354                           "reqdate", "BL", "trigger2");
355
356   } else {
357
358     # without JavaScript Calendar
359     $button1 = qq|
360                               <td><input name="transdate" id="transdate" size="11" title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\"></td>|;
361     $button2 = qq|
362                               <td width="13"><input name="reqdate" id="reqdate" size="11" title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\"></td>|;
363   }
364
365   my @tmp;
366
367   if (($form->{"type"} eq "sales_order") ||
368       ($form->{"type"} eq "purchase_order")) {
369     push(@tmp, qq|
370           <input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" $checkeddelivered>
371           <label for="delivered">| . $locale->text('Delivered') . qq|</label>|);
372   }
373
374   if ($form->{id}) {
375     push(@tmp, qq|
376           <input name="closed" id="closed" type="checkbox" class="checkbox" value="1" $checkedclosed>
377           <label for="closed">| . $locale->text('Closed') . qq|</label>|);
378   }
379
380   if (@tmp) {
381     $openclosed .= qq|
382       <tr>
383         <td colspan=| . (2 * scalar(@tmp)) . qq| align=center>| . join("\n", @tmp) . qq|
384         </td>
385       </tr>
386 |;
387   }
388
389   # set option selected
390   foreach $item ($form->{vc}, currency, department, employee, ($form->{vc} eq "customer" ? customer : vendor)) {
391     $form->{"select$item"} =~ s/ selected//;
392     $form->{"select$item"} =~
393       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
394   }
395
396   #quote select[customer|vendor] Bug 133
397   $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
398
399   #substitute \n and \r to \s (bug 543)
400   $form->{"select$form->{vc}"} =~ s/[\n\r]/&nbsp;/g;
401   
402   my @old_project_ids = ($form->{"globalproject_id"});
403   map({ push(@old_project_ids, $form->{"project_id_$_"})
404           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
405
406   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
407   $form->get_lists("contacts" => "ALL_CONTACTS",
408                    "shipto" => "ALL_SHIPTO",
409                    "projects" => { "key" => "ALL_PROJECTS",
410                                    "all" => 0,
411                                    "old_id" => \@old_project_ids },
412                    "employees" => "ALL_SALESMEN",
413                    "taxzones" => "ALL_TAXZONES",
414                    "payments" => "ALL_PAYMENTS",
415                    "currencies" => "ALL_CURRENCIES",
416                    $vc => "ALL_" . uc($vc));
417
418   my %labels;
419   my @values = (undef);
420   foreach my $item (@{ $form->{"ALL_CONTACTS"} }) {
421     push(@values, $item->{"cp_id"});
422     $labels{$item->{"cp_id"}} = $item->{"cp_name"} .
423       ($item->{"cp_abteilung"} ? " ($item->{cp_abteilung})" : "");
424   }
425
426   my $contact;
427   if (scalar @values > 1) {
428     $contact = qq|
429     <tr>
430       <th align="right">| . $locale->text('Contact Person') . qq|</th>
431       <td>| .
432       NTI($cgi->popup_menu('-name' => 'cp_id', '-values' => \@values,
433                            '-labels' => \%labels, '-default' => $form->{"cp_id"}))
434       . qq|
435       </td>
436     </tr>|;
437   }
438
439   %labels = ();
440   @values = ();
441
442   foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
443     push(@values, $item->{name}.qq|--|.$item->{"id"});
444     $labels{$item->{"id"}} = $item->{name}.qq|--|.$item->{"id"};
445   }
446
447   $vc = qq|
448       <input type="hidden" name="$form->{vc}_id" value="| . H($form->{"$form->{vc}_id"}) . qq|">
449       <input type="hidden" name="old$form->{vc}" value="| . H($form->{"old$form->{vc}"}) . qq|">
450       <th align="right">| . $locale->text(ucfirst($form->{vc})) . qq|</th>
451       <td>| . 
452         (($myconfig{vclimit} <=  scalar(@values)) 
453               ? qq|<input type="text" value="| . H($form->{"old$form->{vc}"}) . qq|" name="$form->{vc}">| 
454               : (NTI($cgi->popup_menu('-name' => "$form->{vc}", '-default' => $form->{"old$form->{vc}"}, 
455                              '-onChange' => 'document.getElementById(\'update_button\').click();',
456                              '-values' => \@values, '-labels' => \%labels)))) . qq|
457       </td><input type=hidden name="select$form->{vc}" value="| .
458     Q($form->{"select$form->{vc}"}) . qq|">|;
459
460   %labels = ();
461   @values = ("");
462   foreach my $item (@{ $form->{"ALL_PAYMENTS"} }) {
463     push(@values, $item->{"id"});
464     $labels{$item->{"id"}} = $item->{"description"};
465   }
466   
467   $payments = qq|
468     <th align="right">| . $locale->text('Payment Terms') . qq|</th>
469     <td>| .
470     NTI($cgi->popup_menu('-name' => 'payment_id', '-values' => \@values,
471                          '-labels' => \%labels, '-default' => $form->{payment_id}))
472     . qq|</td>|;
473
474   %labels = ();
475   @values = ("");
476   foreach my $item (@{ $form->{"ALL_SHIPTO"} }) {
477     push(@values, $item->{"shipto_id"});
478     $labels{$item->{"shipto_id"}} =
479       $item->{"shiptoname"} . " " . $item->{"shiptodepartment_1"};
480   }
481
482   my $shipto;
483   if (scalar @values > 1) {
484     $shipto = qq|
485     <tr>
486       <th align="right">| . $locale->text('Shipping Address') . qq|</th>
487       <td>| .
488       NTI($cgi->popup_menu('-name' => 'shipto_id', '-values' => \@values,
489                            '-labels' => \%labels, '-default' => $form->{"shipto_id"}))
490     . qq|</td>|;
491   }
492
493   %labels = ();
494   @values = ("");
495   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
496     push(@values, $item->{"id"});
497     $labels{$item->{"id"}} = $item->{"projectnumber"};
498   }
499   my $globalprojectnumber =
500     NTI($cgi->popup_menu('-name' => 'globalproject_id', '-values' => \@values,
501                          '-labels' => \%labels,
502                          '-default' => $form->{"globalproject_id"}));
503
504   $salesman = "";
505   if ($form->{type} =~ /^sales_/) {
506     %labels = ();
507     @values = ("");
508     foreach my $item (@{ $form->{ALL_SALESMEN} }) {
509       push(@values, $item->{id});
510       $labels{$item->{id}} = $item->{name} ne "" ? $item->{name} : $item->{login};
511     }
512
513     $salesman =
514       qq|<tr>
515           <th align="right">| . $locale->text('Salesman') . qq|</th>
516           <td>| .
517       NTI($cgi->popup_menu('-name' => 'salesman_id', '-default' => $form->{salesman_id},
518                            '-values' => \@values, '-labels' => \%labels))
519       . qq|</td>
520          </tr>|;
521   }
522
523   %labels = ();
524   @values = ();
525   foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
526     push(@values, $item->{"id"});
527     $labels{$item->{"id"}} = $item->{"name"};
528   }
529
530   my $employees = qq|
531     <tr>
532       <th align="right">| . $locale->text('Employee') . qq|</th>
533       <td>| .
534         NTI($cgi->popup_menu('-name' => 'employee', '-default' => $form->{"employee"},
535                              '-values' => \@values, '-labels' => \%labels)) . qq|
536       </td>
537     </tr>|;
538
539   %labels = ();
540   @values = ();
541   foreach my $item (@{ $form->{"ALL_TAXZONES"} }) {
542     push(@values, $item->{"id"});
543     $labels{$item->{"id"}} = $item->{"description"};
544   }
545
546   $taxzone = qq|
547     <tr>
548       <th align="right">| . $locale->text('Steuersatz') . qq|</th>
549       <td>| .
550         NTI($cgi->popup_menu('-name' => 'taxzone_id', '-default' => $form->{"taxzone_id"},
551                              '-values' => \@values, '-labels' => \%labels)) . qq|
552       </td>
553     </tr>|;
554
555   %labels = ();
556   @values = ();
557   my $i = 0;
558   foreach my $item (@{ $form->{"ALL_CURRENCIES"} }) {
559     push(@values, $item);
560     $labels{$item} = $item;
561   }
562
563   $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
564   my $currencies;
565   if (scalar @values) {
566     $currencies = qq|
567     <tr>
568       <th align="right">| . $locale->text('Currency') . qq|</th>
569       <td>| .
570         NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
571                              '-values' => \@values, '-labels' => \%labels)) . qq|
572       </td>
573     </tr>|;
574   }
575
576   $form->{exchangerate} =
577     $form->format_amount(\%myconfig, $form->{exchangerate});
578
579   if (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) {
580     $creditwarning = 1;
581   } else {
582     $creditwarning = 0;
583   }
584
585   $form->{creditlimit} =
586     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
587   $form->{creditremaining} =
588     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
589
590   $exchangerate = qq|
591 <input type=hidden name=forex value=$form->{forex}>
592 |;
593
594   if ($form->{currency} ne $form->{defaultcurrency}) {
595     if ($form->{forex}) {
596       $exchangerate .=
597           qq|<th align=right>|
598         . $locale->text('Exchangerate')
599         . qq|</th><td>$form->{exchangerate}</td>
600       <input type=hidden name=exchangerate value=$form->{exchangerate}>
601 |;
602     } else {
603       $exchangerate .=
604           qq|<th align=right>|
605         . $locale->text('Exchangerate')
606         . qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
607     }
608   }
609
610   if ($form->{business}) {
611     $business = qq|
612               <tr>
613                 <th align=right>| . $locale->text('Business') . qq|</th>
614                 <td>$form->{business}</td>
615                 <th align=right>| . $locale->text('Trade Discount') . qq|</th>
616                 <td>|
617       . $form->format_amount(\%myconfig, $form->{tradediscount} * 100)
618       . qq| %</td>
619               </tr>
620 |;
621   }
622
623   if ($form->{max_dunning_level}) {
624     $dunning = qq|
625               <tr>
626                 <td colspan=4>
627                 <table>
628                   <tr>
629                 <th align=right>| . $locale->text('Max. Dunning Level') . qq|:</th>
630                 <td><b>$form->{max_dunning_level}</b></td>
631                 <th align=right>| . $locale->text('Dunning Amount') . qq|:</th>
632                 <td><b>|
633       . $form->format_amount(\%myconfig, $form->{dunning_amount},2)
634       . qq|</b></td>
635               </tr>
636               </table>
637              </td>
638             </tr>
639 |;
640   }
641
642   if ($form->{type} !~ /_quotation$/) {
643     $ordnumber = qq|
644               <tr>
645                 <th width=70% align=right nowrap>| . $locale->text('Order Number') . qq|</th>
646                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
647               </tr>
648               <tr>
649                 <th width=70% align=right nowrap>|
650       . $locale->text('Quotation Number') . qq|</th>
651                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
652               </tr>
653               <tr>
654                 <th width=70% align=right nowrap>|
655       . $locale->text('Customer Order Number') . qq|</th>
656                 <td><input name=cusordnumber size=11 value="$form->{cusordnumber}"></td>
657               </tr>
658               <tr>
659                 <th align=right nowrap>| . $locale->text('Order Date') . qq|</th>
660                 $button1
661
662               </tr>
663               <tr>
664                 <th align=right nowrap=true>| . $locale->text('Required by') . qq|</th>
665                 $button2
666               </tr>
667 |;
668
669     $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
670
671     $creditremaining = qq|
672               <tr>
673                 <td></td>
674                 <td colspan=3>
675                   <table>
676                     <tr>
677                       <th nowrap>| . $locale->text('Credit Limit') . qq|</th>
678                       <td>$form->{creditlimit}</td>
679                       <td width=20%></td>
680                       <th nowrap>| . $locale->text('Remaining') . qq|</th>
681                       <td class="plus$n" nowrap>$form->{creditremaining}</td>
682                     </tr>
683                   </table>
684                 </td>
685                 $shipto
686               </tr>
687 |;
688   } else {
689     $reqlabel =
690       ($form->{type} eq 'sales_quotation')
691       ? $locale->text('Valid until')
692       : $locale->text('Required by');
693     if ($form->{type} eq 'sales_quotation') {
694       $ordnumber = qq|
695               <tr>
696                 <th width=70% align=right nowrap>|
697         . $locale->text('Quotation Number') . qq|</th>
698                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
699                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
700               </tr>
701 |;
702     } else {
703       $ordnumber = qq|
704               <tr>
705                 <th width=70% align=right nowrap>| . $locale->text('RFQ Number') . qq|</th>
706                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
707                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
708               </tr>
709 |;
710
711     }
712
713     $ordnumber .= qq|
714               <tr>
715                 <th align=right nowrap>| . $locale->text('Quotation Date') . qq|</th>
716                 $button1
717               </tr>
718               <tr>
719                 <th align=right nowrap=true>$reqlabel</th>
720                 $button2
721               </tr>
722 |;
723     $creditremaining = qq| <tr>
724                             <td colspan=4></td>
725                             $shipto
726                           </tr>|;
727   }
728
729   $department = qq|
730               <tr>
731                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
732                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
733                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
734                 </td>
735               </tr> | if $form->{selectdepartment};
736
737   if ($form->{type} eq 'sales_order') {
738     if ($form->{selectemployee}) {
739       $employee = qq|
740     <input type=hidden name=customer_klass value=$form->{customer_klass}>
741         $employees
742 |;
743     }
744   } else {
745     $employee = qq|
746     <input type=hidden name=customer_klass value=$form->{customer_klass}>
747         $employees
748 |;
749   }
750   if ($form->{resubmit} && ($form->{format} eq "html")) {
751     $onload =
752       qq|window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()|;
753   } elsif ($form->{resubmit}) {
754     $onload = qq|document.oe.submit()|;
755   } else {
756     $onload = "focus()";
757   }
758
759   $credittext = $locale->text('Credit Limit exceeded!!!');
760   if ($creditwarning) {
761     $onload = qq|alert('$credittext')|;
762   }
763   
764   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
765   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
766   
767   $form->{"javascript"} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
768   # show history button js
769   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
770   #/show history button js
771   $form->header;
772
773   print qq|
774 <body onLoad="$onload">
775
776 <form method=post name=oe action=$form->{script}>
777  <script type="text/javascript" src="js/common.js"></script>
778  <script type="text/javascript" src="js/delivery_customer_selection.js"></script>
779  <script type="text/javascript" src="js/vendor_selection.js"></script>
780  <script type="text/javascript" src="js/calculate_qty.js"></script>
781
782 <input type=hidden name=id value=$form->{id}>
783 <input type=hidden name=action value=$form->{action}>
784
785 <input type=hidden name=type value=$form->{type}>
786 <input type=hidden name=formname value=$form->{formname}>
787 <input type=hidden name=media value=$form->{media}>
788 <input type=hidden name=format value=$form->{format}>
789 <input type=hidden name=proforma value=$form->{proforma}>
790
791 <input type=hidden name=queued value="$form->{queued}">
792 <input type=hidden name=printed value="$form->{printed}">
793 <input type=hidden name=emailed value="$form->{emailed}">
794
795 <input type=hidden name=vc value=$form->{vc}>
796
797 <input type=hidden name=title value="$form->{title}">
798
799 <input type=hidden name=discount value=$form->{discount}>
800 <input type=hidden name=creditlimit value=$form->{creditlimit}>
801 <input type=hidden name=creditremaining value=$form->{creditremaining}>
802
803 <input type=hidden name=tradediscount value=$form->{tradediscount}>
804 <input type=hidden name=business value=$form->{business}>
805 <input type=hidden name=webdav value=$webdav>
806
807 <table width=100%>
808   <tr class=listtop>
809     <th class=listtop>$form->{title}</th>
810   </tr>
811   <tr height="5"></tr>
812   <tr>
813     <td>
814       <table width="100%">
815         <tr valign=top>
816           <td>
817             <table width=100%>
818               <tr>
819         $vc
820         $contact
821               $creditremaining
822               $business
823               $dunning
824               $taxzone
825               $department
826               <tr>
827                 $currencies
828                 $exchangerate
829               </tr>
830               <tr>
831                 <th align=right>| . $locale->text('Shipping Point') . qq|</th>
832                 <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
833               </tr>
834               <tr>
835                 <th align=right>| . $locale->text('Ship via') . qq|</th>
836                 <td colspan=3><input name=shipvia size=35 value="$form->{shipvia}"></td>
837               </tr>
838               <tr>
839                 <th align="right">| . $locale->text('Transaction description') . qq|</th>
840                 <td colspan="3"><input name="transaction_description" size="35" value="| . H($form->{transaction_description}) . qq|"></td>
841               </tr>|;
842 #              <tr>
843 #                 <td colspan=4>
844 #                   <table>
845 #                     <tr>
846 #                       <td colspan=2>
847 #                         <button type="button" onclick="delivery_customer_selection_window('delivery_customer_string','delivery_customer_id')">| . $locale->text('Choose Customer') . qq|</button>
848 #                       </td>
849 #                       <td colspan=2><input type=hidden name=delivery_customer_id value="$form->{delivery_customer_id}">
850 #                       <input size=45 id=delivery_customer_string name=delivery_customer_string value="$form->{delivery_customer_string}"></td>
851 #                     </tr>
852 #                     <tr>
853 #                       <td colspan=2>
854 #                         <button type="button" onclick="vendor_selection_window('delivery_vendor_string','delivery_vendor_id')">| . $locale->text('Choose Vendor') . qq|</button>
855 #                       </td>
856 #                       <td colspan=2><input type=hidden name=delivery_vendor_id value="$form->{delivery_vendor_id}">
857 #                       <input size=45 id=vendor_string name=delivery_vendor_string value="$form->{delivery_vendor_string}"></td>
858 #                     </tr>
859 #                   </table>
860 #                 </td>
861 #               </tr>
862 print qq|           </table>
863           </td>
864           <td align=right>
865             <table>
866               $openclosed
867               $employee
868         $salesman
869               $ordnumber
870               <tr>
871           <th width="70%" align="right" nowrap>| . $locale->text('Project Number') . qq|</th>
872           <td>$globalprojectnumber</td>
873               </tr>
874             </table>
875           </td>
876         </tr>
877       </table>
878     </td>
879   </tr>
880
881 $jsscript
882
883 <!-- shipto are in hidden variables -->
884
885 <input type=hidden name=shiptoname value="$form->{shiptoname}">
886 <input type=hidden name=shiptostreet value="$form->{shiptostreet}">
887 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
888 <input type=hidden name=shiptocity value="$form->{shiptocity}">
889 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
890 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
891 <input type=hidden name=shiptophone value="$form->{shiptophone}">
892 <input type=hidden name=shiptofax value="$form->{shiptofax}">
893 <input type=hidden name=shiptodepartment_1 value="$form->{shiptodepartment_1}">
894 <input type=hidden name=shiptodepartment_2 value="$form->{shiptodepartment_2}">
895 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
896
897 <!-- email variables -->
898 <input type=hidden name=message value="$form->{message}">
899 <input type=hidden name=email value="$form->{email}">
900 <input type=hidden name=subject value="$form->{subject}">
901 <input type=hidden name=cc value="$form->{cc}">
902 <input type=hidden name=bcc value="$form->{bcc}">
903
904 <input type=hidden name=taxpart value="$form->{taxpart}">
905 <input type=hidden name=taxservice value="$form->{taxservice}">
906
907 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
908 |;
909
910   foreach $item (split / /, $form->{taxaccounts}) {
911     print qq|
912 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
913 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
914 |;
915   }
916   $lxdebug->leave_sub();
917 }
918
919 sub form_footer {
920   $lxdebug->enter_sub();
921
922   $form->{invtotal} = $form->{invsubtotal};
923
924   if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
925     $rows = 2;
926   }
927   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
928     $introws = 2;
929   }
930   $rows = ($rows > $introws) ? $rows : $introws;
931   $notes =
932     qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
933   $intnotes =
934     qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
935
936   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
937
938   $taxincluded = "";
939   if ($form->{taxaccounts}) {
940     $taxincluded = qq|
941               <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
942       . $locale->text('Tax Included') . qq|</b><br><br>
943 |;
944   }
945
946   if (!$form->{taxincluded}) {
947
948     foreach $item (split / /, $form->{taxaccounts}) {
949       if ($form->{"${item}_base"}) {
950         $form->{invtotal} += $form->{"${item}_total"} =
951           $form->round_amount(
952                              $form->{"${item}_base"} * $form->{"${item}_rate"},
953                              2);
954         $form->{"${item}_total"} =
955           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
956
957         $tax .= qq|
958               <tr>
959                 <th align=right>$form->{"${item}_description"}&nbsp;|
960                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
961                 <td align=right>$form->{"${item}_total"}</td>
962               </tr>
963 |;
964       }
965     }
966
967     $form->{invsubtotal} =
968       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
969
970     $subtotal = qq|
971               <tr>
972                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
973                 <td align=right>$form->{invsubtotal}</td>
974               </tr>
975 |;
976
977   }
978
979   if ($form->{taxincluded}) {
980     foreach $item (split / /, $form->{taxaccounts}) {
981       if ($form->{"${item}_base"}) {
982         $form->{"${item}_total"} =
983           $form->round_amount(
984                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
985                               (1 + $form->{"${item}_rate"})
986                            ),
987                            2);
988         $form->{"${item}_netto"} =
989           $form->round_amount(
990                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
991                           2);
992         $form->{"${item}_total"} =
993           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
994         $form->{"${item}_netto"} =
995           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
996
997         $tax .= qq|
998               <tr>
999                 <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;|
1000                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
1001                 <td align=right>$form->{"${item}_total"}</td>
1002               </tr>
1003               <tr>
1004                 <th align=right>Nettobetrag</th>
1005                 <td align=right>$form->{"${item}_netto"}</td>
1006               </tr>
1007 |;
1008       }
1009     }
1010
1011   }
1012
1013   $form->{oldinvtotal} = $form->{invtotal};
1014   $form->{invtotal}    =
1015     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
1016
1017   print qq|
1018   <tr>
1019     <td>
1020       <table width=100%>
1021         <tr valign=bottom>
1022           <td>
1023             <table>
1024               <tr>
1025                 <th align=left>| . $locale->text('Notes') . qq|</th>
1026                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
1027               </tr>
1028               <tr valign=top>
1029                 <td>$notes</td>
1030                 <td>$intnotes</td>
1031               </tr>
1032           <tr>
1033     $payments
1034             </tr>
1035       </table>
1036           </td>
1037           <td align=right width=100%>
1038             $taxincluded
1039             <table width=100%>
1040               $subtotal
1041               $tax
1042               <tr>
1043                 <th align=right>| . $locale->text('Total') . qq|</th>
1044                 <td align=right>$form->{invtotal}</td>
1045               </tr>
1046             </table>
1047           </td>
1048         </tr>
1049       </table>
1050     </td>
1051   </tr>
1052 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
1053 <input type=hidden name=oldtotalpaid value=$totalpaid>
1054   <tr>
1055     <td><hr size=3 noshade></td>
1056   </tr>
1057 |;
1058
1059   if ($webdav) {
1060     $webdav_list = qq|
1061
1062   <tr>
1063     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
1064   </tr>
1065     <table width=100%>
1066       <td align=left width=30%><b>Dateiname</b></td>
1067       <td align=left width=70%><b>Webdavlink</b></td>
1068 |;
1069     foreach $file (keys %{ $form->{WEBDAV} }) {
1070       $webdav_list .= qq|
1071       <tr>
1072         <td align=left>$file</td>
1073         <td align=left><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
1074       </tr>
1075 |;
1076     }
1077     $webdav_list .= qq|
1078     </table>
1079   </tr>
1080   <tr>
1081     <td><hr size=3 noshade></td>
1082   </tr>
1083 |;
1084
1085     print $webdav_list;
1086   }
1087   print qq|
1088 <input type=hidden name=jscalendar value=$form->{jscalendar}>
1089 |;
1090   print qq|
1091   <tr>
1092     <td>
1093 |;
1094   print_options();
1095
1096   print qq|
1097     </td>
1098   </tr>
1099 </table>
1100
1101 | . $locale->text("Edit the $form->{type}") . qq|<br>
1102 <input class=submit type=submit name=action id=update_button value="|
1103     . $locale->text('Update') . qq|">
1104 <input class=submit type=submit name=action value="|
1105     . $locale->text('Ship to') . qq|">
1106 <input class=submit type=submit name=action value="|
1107     . $locale->text('Print') . qq|">
1108 <input class=submit type=submit name=action value="|
1109     . $locale->text('E-mail') . qq|">
1110 <input class=submit type=submit name=action value="|
1111     . $locale->text('Save') . qq|">
1112 <input class=submit type=submit name=action value="|
1113     . $locale->text('Save and Close') . qq|">
1114 |;
1115
1116   if (($form->{id})) {
1117     print qq|
1118         <input type="button" class="submit" onclick="set_history_window(|
1119         . Q($form->{id})
1120         . qq|);" name="history" id="history" value="|
1121         . $locale->text('history')
1122         . qq|">
1123
1124 <br>| . $locale->text("Workflow $form->{type}") . qq|<br>
1125 <input class=submit type=submit name=action value="|
1126       . $locale->text('Save as new') . qq|">
1127 <input class=submit type=submit name=action value="|
1128       . $locale->text('Delete') . qq|">|;
1129     if (($form->{type} =~ /sales_quotation$/)) {
1130       print qq|
1131 <input class=submit type=submit name=action value="|
1132         . $locale->text('Sales Order') . qq|">|;
1133     }
1134     if ($form->{type} =~ /request_quotation$/) {
1135       print qq|
1136 <input class=submit type=submit name=action value="|
1137         . $locale->text('Purchase Order') . qq|">|;
1138     }
1139     if (1) {
1140     print qq|
1141 <input class=submit type=submit name=action value="|
1142       . $locale->text('Invoice') . qq|">
1143 |;
1144 }
1145
1146     if ($form->{type} =~ /sales_order$/) {
1147       print qq|
1148 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1149 <input class=submit type=submit name=action value="|
1150         . $locale->text('Purchase Order') . qq|">
1151 <input class=submit type=submit name=action value="|
1152         . $locale->text('Quotation') . qq|">
1153 |;
1154
1155     } elsif ($form->{type} =~ /purchase_order$/) {
1156       print qq|
1157 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1158 <input class=submit type=submit name=action value="|
1159         . $locale->text('Sales Order') . qq|">
1160 <input class=submit type=submit name=action value="|
1161         . $locale->text('Request for Quotation') . qq|">
1162 |;
1163
1164     } else {
1165       print qq|
1166 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1167 <input class=submit type=submit name=action value="|
1168         . $locale->text('Order') . qq|">
1169 |;
1170     }
1171   } elsif ($form->{type} =~ /sales_order$/ && $form->{rowcount} && !$form->{proforma}) {
1172     print qq|
1173 <br>Workflow  $form->{heading}<br>
1174 <input class=submit type=submit name=action value="|
1175       . $locale->text('Save as new') . qq|">
1176 <input class=submit type=submit name=action value="|
1177       . $locale->text('Invoice') . qq|">
1178 |;
1179   }
1180
1181   $form->hide_form("saved_xyznumber");
1182
1183   print qq|
1184
1185 <input type=hidden name=rowcount value=$form->{rowcount}>
1186
1187 <input name=callback type=hidden value="$form->{callback}">
1188
1189 <input type=hidden name=login value=$form->{login}>
1190 <input type=hidden name=password value=$form->{password}>
1191
1192 </form>
1193
1194 </body>
1195 </html>
1196 |;
1197   $lxdebug->leave_sub();
1198 }
1199
1200 sub update {
1201   $lxdebug->enter_sub();
1202
1203   set_headings($form->{"id"} ? "edit" : "add");
1204
1205   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1206     qw(exchangerate creditlimit creditremaining);
1207   $form->{update} = 1;
1208       
1209   if($form->{payment_id}) { 
1210     $payment_id = $form->{payment_id};
1211   }
1212   
1213   &check_name($form->{vc});
1214   
1215   if($form->{payment_id} eq "") { 
1216     $form->{payment_id} = $payment_id;
1217   }
1218   
1219   $buysell              = 'buy';
1220   $buysell              = 'sell' if ($form->{vc} eq 'vendor');
1221   $form->{exchangerate} = $exchangerate
1222     if (
1223         $form->{forex} = (
1224                   $exchangerate =
1225                     $form->check_exchangerate(
1226                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1227                     )));
1228
1229   # for pricegroups
1230   $i = $form->{rowcount};
1231
1232   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
1233
1234   if (   ($form->{"partnumber_$i"} eq "")
1235       && ($form->{"description_$i"} eq "")
1236       && ($form->{"partsgroup_$i"}  eq "")) {
1237
1238     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
1239     &check_form;
1240
1241   } else {
1242
1243     if (   $form->{type} eq 'purchase_order'
1244         || $form->{type} eq 'request_quotation') {
1245       IR->retrieve_item(\%myconfig, \%$form);
1246     }
1247     if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
1248       IS->retrieve_item(\%myconfig, \%$form);
1249     }
1250
1251     my $rows = scalar @{ $form->{item_list} };
1252
1253     $form->{"discount_$i"} =
1254       $form->format_amount(\%myconfig, $form->{discount} * 100);
1255
1256     if ($rows) {
1257       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1258
1259       if ($rows > 1) {
1260
1261         &select_item;
1262         exit;
1263
1264       } else {
1265
1266         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1267         if ($form->{"not_discountable_$i"}) {
1268           $form->{"discount_$i"} = 0;
1269         }
1270         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1271           qw(partnumber description unit);
1272         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1273           keys %{ $form->{item_list}[0] };
1274         if ($form->{"part_payment_id_$i"} ne "") {
1275           $form->{payment_id} = $form->{"part_payment_id_$i"};
1276         }
1277
1278         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1279
1280         ($dec) = ($s =~ /\.(\d+)/);
1281         $dec           = length $dec;
1282         $decimalplaces = ($dec > 2) ? $dec : 2;
1283
1284         if ($sellprice) {
1285           $form->{"sellprice_$i"} = $sellprice;
1286         } else {
1287
1288           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1289
1290           # if there is an exchange rate adjust sellprice
1291           $form->{"sellprice_$i"} /= $exchangerate;
1292         }
1293
1294         $amount =
1295           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1296           (1 - $form->{"discount_$i"} / 100);
1297         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1298         map { $form->{"${_}_base"} += $amount }
1299           (split / /, $form->{"taxaccounts_$i"});
1300         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1301           split / /, $form->{taxaccounts}
1302           if !$form->{taxincluded};
1303
1304         $form->{creditremaining} -= $amount;
1305
1306         $form->{"sellprice_$i"} =
1307           $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1308                                $decimalplaces);
1309         $form->{"qty_$i"} =
1310           $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1311
1312         # get pricegroups for parts
1313         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1314
1315         # build up html code for prices_$i
1316         &set_pricegroup($i);
1317       }
1318
1319       &display_form;
1320
1321     } else {
1322
1323       # ok, so this is a new part
1324       # ask if it is a part or service item
1325
1326       if (   $form->{"partsgroup_$i"}
1327           && ($form->{"partsnumber_$i"} eq "")
1328           && ($form->{"description_$i"} eq "")) {
1329         $form->{rowcount}--;
1330         $form->{"discount_$i"} = "";
1331         &display_form;
1332       } else {
1333
1334         $form->{"id_$i"}   = 0;
1335         $form->{"unit_$i"} = $locale->text('ea');
1336
1337         &new_item;
1338
1339       }
1340     }
1341   }
1342
1343   $lxdebug->leave_sub();
1344 }
1345
1346 sub search {
1347   $lxdebug->enter_sub();
1348
1349   if ($form->{type} eq 'purchase_order') {
1350     $form->{title} = $locale->text('Purchase Orders');
1351     $form->{vc}    = 'vendor';
1352     $ordlabel      = $locale->text('Order Number');
1353     $ordnumber     = 'ordnumber';
1354     $employee      = $locale->text('Employee');
1355   }
1356
1357   if ($form->{type} eq 'request_quotation') {
1358     $form->{title} = $locale->text('Request for Quotations');
1359     $form->{vc}    = 'vendor';
1360     $ordlabel      = $locale->text('RFQ Number');
1361     $ordnumber     = 'quonumber';
1362     $employee      = $locale->text('Employee');
1363   }
1364
1365   if ($form->{type} eq 'sales_order') {
1366     $form->{title} = $locale->text('Sales Orders');
1367     $form->{vc}    = 'customer';
1368     $ordlabel      = $locale->text('Order Number');
1369     $ordnumber     = 'ordnumber';
1370     $employee      = $locale->text('Employee');
1371   }
1372
1373   if ($form->{type} eq 'sales_quotation') {
1374     $form->{title} = $locale->text('Quotations');
1375     $form->{vc}    = 'customer';
1376     $ordlabel      = $locale->text('Quotation Number');
1377     $ordnumber     = 'quonumber';
1378     $employee      = $locale->text('Employee');
1379   }
1380
1381   # setup vendor / customer selection
1382   $form->all_vc(\%myconfig, $form->{vc},
1383                 ($form->{vc} eq 'customer') ? "AR" : "AP");
1384
1385   # departments
1386   if (@{ $form->{all_departments} }) {
1387     $form->{selectdepartment} = "<option>\n";
1388
1389     map {
1390       $form->{selectdepartment} .=
1391         "<option>$_->{description}--$_->{id}</option>\n"
1392     } (@{ $form->{all_departments} });
1393   }
1394
1395   $department = qq|
1396         <tr>
1397           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1398           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1399         </tr>
1400 | if $form->{selectdepartment};
1401
1402   my $delivered;
1403   if (($form->{"type"} eq "sales_order") ||
1404       ($form->{"type"} eq "purchase_order")) {
1405     $delivered = qq|
1406         <tr>
1407           <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
1408             <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
1409           <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
1410             <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
1411         </tr>
1412 |;
1413   }
1414
1415   # use JavaScript Calendar or not
1416   $form->{jsscript} = $jscalendar;
1417   $jsscript = "";
1418   if ($form->{jsscript}) {
1419
1420     # with JavaScript Calendar
1421     $button1 = qq|
1422        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1423        <input type=button name=transdatefrom id="trigger3" value=|
1424       . $locale->text('button') . qq|></td>
1425       |;
1426     $button2 = qq|
1427        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1428        <input type=button name=transdateto name=transdateto id="trigger4" value=|
1429       . $locale->text('button') . qq|></td>
1430      |;
1431
1432     #write Trigger
1433     $jsscript =
1434       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
1435                           "transdateto", "BL", "trigger4");
1436   } else {
1437
1438     # without JavaScript Calendar
1439     $button1 = qq|
1440                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
1441     $button2 = qq|
1442                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\></td>|;
1443   }
1444
1445   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
1446
1447   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1448                                    "all" => 1 },
1449                    $vc => "ALL_" . uc($vc));
1450
1451   my %labels = ();
1452   my @values = ("");
1453   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
1454     push(@values, $item->{"id"});
1455     $labels{$item->{"id"}} = $item->{"projectnumber"};
1456   }
1457   my $projectnumber =
1458     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
1459                          '-labels' => \%labels));
1460
1461   %labels = ();
1462   @values = ("");
1463
1464   foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
1465     push(@values, $item->{name}.qq|--|.$item->{"id"});
1466     $labels{$item->{"id"}} = $item->{name}.qq|--|.$item->{"id"};
1467   }
1468
1469   my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
1470   $vc =
1471     $myconfig{vclimit} <=  scalar(@values)
1472     ? qq|<input type="text" value="| . H($form->{"old$form->{vc}"}) . qq|" name="$form->{vc}">|
1473     : NTI($cgi->popup_menu('-name' => "$form->{vc}",
1474                            '-default' => $form->{"old$form->{vc}"},
1475                            '-onChange' => 'document.getElementById(\'update_button\').click();',
1476                            '-values' => \@values,
1477                            '-labels' => \%labels));
1478
1479   $form->header;
1480
1481   print qq|
1482 <body>
1483
1484 <form method=post action=$form->{script}>
1485
1486 <table width=100%>
1487   <tr>
1488     <th class=listtop>$form->{title}</th>
1489   </tr>
1490   <tr height="5"></tr>
1491   <tr>
1492     <td>
1493       <table>
1494         <tr>
1495           <th align=right>$vc_label</th>
1496           <td colspan=3>$vc</td>
1497         </tr>
1498         $department
1499         <tr>
1500           <th align=right>$ordlabel</th>
1501           <td colspan=3><input name="$ordnumber" size=20></td>
1502         </tr>
1503         <tr>
1504           <th align="right">| . $locale->text('Transaction description') . qq|</th>
1505           <td colspan="3"><input name="transaction_description" size=20></td>
1506         </tr>
1507         <tr>
1508           <th align="right">| . $locale->text("Project Number") . qq|</th>
1509           <td colspan="3">$projectnumber</td>
1510         </tr>
1511         <tr>
1512           <th align=right>| . $locale->text('From') . qq|</th>
1513           $button1
1514           <th align=right>| . $locale->text('Bis') . qq|</th>
1515           $button2
1516         </tr>
1517         <input type=hidden name=sort value=transdate>
1518         <tr>
1519           <th align=right>| . $locale->text('Include in Report') . qq|</th>
1520           <td colspan=5>
1521             <table>
1522         <tr>
1523           <td><input type="checkbox" name="open" value="1" id="open" checked>
1524             <label for="open">| . $locale->text("Open") . qq|</td>
1525           <td><input type="checkbox" name="closed" value="1" id="closed">
1526             <label for="closed">| . $locale->text("Closed") . qq|</td>
1527         </tr>
1528         $delivered
1529               <tr>
1530                 <td><input name="l_id" class=checkbox type=checkbox value=Y>
1531                 | . $locale->text('ID') . qq|</td>
1532                 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
1533                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> |
1534     . $locale->text('Date') . qq|</td>
1535                 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> |
1536     . $locale->text('Required by') . qq|</td>
1537               </tr>
1538               <tr>
1539                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> ucfirst($form->{vc})</td>
1540                 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
1541                 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> |
1542     . $locale->text('Ship via') . qq|</td>
1543                 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
1544               </tr>
1545               <tr>
1546                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> |
1547     . $locale->text('Amount') . qq|</td>
1548                 <td><input name="l_tax" class=checkbox type=checkbox value=Y> |
1549     . $locale->text('Tax') . qq|</td>
1550                 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> |
1551     . $locale->text('Total') . qq|</td>
1552               </tr>
1553               <tr>
1554           <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> |
1555           . $locale->text('Project Number') . qq|</td>
1556           <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> |
1557           . $locale->text('Transaction description') . qq|</td>
1558               </tr>
1559               <tr>
1560                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> |
1561     . $locale->text('Subtotal') . qq|</td>
1562               </tr>
1563             </table>
1564           </td>
1565         </tr>
1566       </table>
1567     </td>
1568   </tr>
1569   <tr><td colspan=4><hr size=3 noshade></td></tr>
1570 </table>
1571
1572 $jsscript
1573
1574 <br>
1575 <input type=hidden name=nextsub value=orders>
1576 <input type=hidden name=login value=$form->{login}>
1577 <input type=hidden name=password value=$form->{password}>
1578 <input type=hidden name=vc value=$form->{vc}>
1579 <input type=hidden name=type value=$form->{type}>
1580
1581 <input class=submit type=submit name=action value="|
1582     . $locale->text('Continue') . qq|">
1583 </form>
1584
1585 </body>
1586 </html>
1587 |;
1588
1589   $lxdebug->leave_sub();
1590 }
1591
1592 sub orders {
1593   $lxdebug->enter_sub();
1594
1595   $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
1596
1597   # construct href
1598   my @fields =
1599     qw(type vc login password transdatefrom transdateto
1600        open closed notdelivered delivered department
1601        transaction_description);
1602   push @fields, $form->{vc};
1603   $href = "$form->{script}?action=orders&"
1604     . join("&", map { "${_}=" . E($form->{$_}) } @fields)
1605     . "&${ordnumber}=" . E($form->{$ordnumber});
1606   $callback = $href;
1607
1608   # split vendor / customer
1609   ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) =
1610     split(/--/, $form->{ $form->{vc} });
1611
1612   OE->transactions(\%myconfig, \%$form);
1613
1614   @columns = (
1615     "transdate",               "reqdate",
1616     "id",                      "$ordnumber",
1617     "name",                    "netamount",
1618     "tax",                     "amount",
1619     "curr",                    "employee",
1620     "shipvia",                 "globalprojectnumber",
1621     "transaction_description", "open",
1622     "closed",                  "delivered"
1623   );
1624
1625   $form->{l_open} = $form->{l_closed} = "Y"
1626     if ($form->{open} && $form->{closed});
1627
1628   $form->{"l_delivered"} = "Y"
1629     if ($form->{"delivered"} && $form->{"notdelivered"});
1630
1631   foreach $item (@columns) {
1632     if ($form->{"l_$item"} eq "Y") {
1633       push @column_index, $item;
1634
1635       # add column to href and callback
1636       $callback .= "&l_$item=Y";
1637       $href     .= "&l_$item=Y";
1638     }
1639   }
1640
1641   # only show checkboxes if gotten here via sales_order form.
1642   if ($form->{type} =~ /sales_order/) {
1643     unshift @column_index, "ids";
1644   }
1645
1646   if ($form->{l_subtotal} eq 'Y') {
1647     $callback .= "&l_subtotal=Y";
1648     $href     .= "&l_subtotal=Y";
1649   }
1650
1651   if ($form->{vc} eq 'vendor') {
1652     if ($form->{type} eq 'purchase_order') {
1653       $form->{title} = $locale->text('Purchase Orders');
1654     } else {
1655       $form->{title} = $locale->text('Request for Quotations');
1656     }
1657     $name     = $locale->text('Vendor');
1658     $employee = $locale->text('Employee');
1659   }
1660   if ($form->{vc} eq 'customer') {
1661     if ($form->{type} eq 'sales_order') {
1662       $form->{title} = $locale->text('Sales Orders');
1663     } else {
1664       $form->{title} = $locale->text('Quotations');
1665     }
1666     $employee = $locale->text('Employee');
1667     $name = $locale->text('Customer');
1668   }
1669
1670   $column_header{id} =
1671       qq|<th><a class=listheading href=$href&sort=id>|
1672     . $locale->text('ID')
1673     . qq|</a></th>|;
1674   $column_header{transdate} =
1675       qq|<th><a class=listheading href=$href&sort=transdate>|
1676     . $locale->text('Date')
1677     . qq|</a></th>|;
1678   $column_header{reqdate} =
1679       qq|<th><a class=listheading href=$href&sort=reqdate>|
1680     . $locale->text('Required by')
1681     . qq|</a></th>|;
1682   $column_header{ordnumber} =
1683       qq|<th><a class=listheading href=$href&sort=ordnumber>|
1684     . $locale->text('Order')
1685     . qq|</a></th>|;
1686   $column_header{quonumber} =
1687       qq|<th><a class=listheading href=$href&sort=quonumber>|
1688     . ($form->{"type"} eq "request_quotation" ?
1689        $locale->text('RFQ') :
1690        $locale->text('Quotation'))
1691     . qq|</a></th>|;
1692   $column_header{name} =
1693     qq|<th><a class=listheading href=$href&sort=name>$name</a></th>|;
1694   $column_header{netamount} =
1695     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1696   $column_header{tax} =
1697     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1698   $column_header{amount} =
1699     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1700   $column_header{curr} =
1701     qq|<th class=listheading>| . $locale->text('Curr') . qq|</th>|;
1702   $column_header{shipvia} =
1703       qq|<th><a class=listheading href=$href&sort=shipvia>|
1704     . $locale->text('Ship via')
1705     . qq|</a></th>|;
1706   $column_header{globalprojectnumber} =
1707     qq|<th class="listheading">| . $locale->text('Project Number') . qq|</th>|;
1708   $column_header{open} =
1709     qq|<th class=listheading>| . $locale->text('O') . qq|</th>|;
1710   $column_header{closed} =
1711     qq|<th class=listheading>| . $locale->text('C') . qq|</th>|;
1712   $column_header{"delivered"} =
1713     qq|<th class="listheading">| . $locale->text("Delivered") . qq|</th>|;
1714
1715   $column_header{employee} =
1716     qq|<th><a class=listheading href=$href&sort=employee>$employee</a></th>|;
1717   $column_header{transaction_description} =
1718     qq|<th><a class=listheading href="$href&sort=transaction_description">|
1719     . $locale->text("Transaction description") . qq|</a></th>|;
1720
1721   $column_header{ids} = qq|<th></th>|;
1722
1723   if ($form->{ $form->{vc} }) {
1724     $option = $locale->text(ucfirst $form->{vc});
1725     $option .= " : $form->{$form->{vc}}";
1726   }
1727   if ($form->{department}) {
1728     $option .= "\n<br>" if ($option);
1729     ($department) = split /--/, $form->{department};
1730     $option .= $locale->text('Department') . " : $department";
1731   }
1732   if ($form->{transdatefrom}) {
1733     $option .= "\n<br>"
1734       . $locale->text('From') . " "
1735       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1736   }
1737   if ($form->{transdateto}) {
1738     $option .= "\n<br>"
1739       . $locale->text('Bis') . " "
1740       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1741   }
1742   if ($form->{open}) {
1743     $option .= "\n<br>" if ($option);
1744     $option .= $locale->text('Open');
1745   }
1746   if ($form->{closed}) {
1747     $option .= "\n<br>" if ($option);
1748     $option .= $locale->text('Closed');
1749   }
1750
1751   $form->header;
1752
1753   print qq|
1754 <body>
1755
1756 <form method="post" action="oe.pl">
1757 <table width=100%>
1758   <tr>
1759     <th class=listtop>$form->{title}</th>
1760   </tr>
1761   <tr height="5"></tr>
1762   <tr>
1763     <td>$option</td>
1764   </tr>
1765   <tr>
1766     <td>
1767       <table width=100%>
1768         <tr class=listheading>|;
1769
1770   map { print "\n$column_header{$_}" } @column_index;
1771
1772   print qq|
1773         </tr>
1774 |;
1775
1776   # add sort and escape callback
1777   $callback_escaped = $form->escape($callback . "&sort=$form->{sort}");
1778
1779   if (@{ $form->{OE} }) {
1780     $sameitem = $form->{OE}->[0]->{ $form->{sort} };
1781   }
1782
1783   $action = "edit";
1784
1785   foreach $oe (@{ $form->{OE} }) {
1786     $form->{rowcount} = ++$j;
1787
1788     if ($form->{l_subtotal} eq 'Y') {
1789       if ($sameitem ne $oe->{ $form->{sort} }) {
1790         &subtotal;
1791         $sameitem = $oe->{ $form->{sort} };
1792       }
1793     }
1794
1795     map { $oe->{$_} *= $oe->{exchangerate} } (qw(netamount amount));
1796
1797     $column_data{netamount} =
1798         "<td align=right>"
1799       . $form->format_amount(\%myconfig, $oe->{netamount}, 2, "&nbsp;")
1800       . "</td>";
1801     $column_data{tax} = "<td align=right>"
1802       . $form->format_amount(\%myconfig, $oe->{amount} - $oe->{netamount},
1803                              2, "&nbsp;")
1804       . "</td>";
1805     $column_data{amount} =
1806       "<td align=right>"
1807       . $form->format_amount(\%myconfig, $oe->{amount}, 2, "&nbsp;") . "</td>";
1808
1809     $totalnetamount += $oe->{netamount};
1810     $totalamount    += $oe->{amount};
1811
1812     $subtotalnetamount += $oe->{netamount};
1813     $subtotalamount    += $oe->{amount};
1814
1815     $column_data{ids} =
1816       qq|<td><input name="multi_id_$j" class=checkbox type=checkbox><input type="hidden" name="trans_id_$j" value="$oe->{id}"></td>|;
1817     $column_data{id}        = "<td>$oe->{id}</td>";
1818     $column_data{transdate} = "<td>$oe->{transdate}&nbsp;</td>";
1819     $column_data{reqdate}   = "<td>$oe->{reqdate}&nbsp;</td>";
1820
1821     $column_data{$ordnumber} =
1822       "<td><a href=oe.pl?action=$action&type=$form->{type}&id=$oe->{id}&vc=$form->{vc}&login=$form->{login}&password=$form->{password}&callback=$callback_escaped>$oe->{$ordnumber}</a></td>";
1823     $column_data{name} = "<td>$oe->{name}</td>";
1824
1825     $column_data{employee} = "<td>$oe->{employee}&nbsp;</td>";
1826     $column_data{shipvia}  = "<td>$oe->{shipvia}&nbsp;</td>";
1827     $column_data{globalprojectnumber}  = "<td>" . H($oe->{globalprojectnumber}) . "</td>";
1828
1829     if ($oe->{closed}) {
1830       $column_data{closed} = "<td align=center>X</td>";
1831       $column_data{open}   = "<td>&nbsp;</td>";
1832     } else {
1833       $column_data{closed} = "<td>&nbsp;</td>";
1834       $column_data{open}   = "<td align=center>X</td>";
1835     }
1836     $column_data{"delivered"} = "<td>" .
1837       ($oe->{"delivered"} ? $locale->text("Yes") : $locale->text("No")) .
1838       "</td>";
1839     $column_data{transaction_description} = "<td>" . H($oe->{transaction_description}) . "</td>";
1840
1841     $i++;
1842     $i %= 2;
1843     print "
1844         <tr class=listrow$i>";
1845
1846     map { print "\n$column_data{$_}" } @column_index;
1847
1848     print qq|
1849         </tr>
1850 |;
1851
1852   }
1853
1854   if ($form->{l_subtotal} eq 'Y') {
1855     &subtotal;
1856   }
1857
1858   # print totals
1859   print qq|
1860         <tr class=listtotal>|;
1861
1862   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1863
1864   $column_data{netamount} =
1865     "<th class=listtotal align=right>"
1866     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1867   $column_data{tax} = "<th class=listtotal align=right>"
1868     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1869                            2, "&nbsp;")
1870     . "</th>";
1871   $column_data{amount} =
1872     "<th class=listtotal align=right>"
1873     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1874
1875   map { print "\n$column_data{$_}" } @column_index;
1876
1877   print qq|
1878         </tr>
1879       </td>
1880     </table>
1881   </tr>
1882   <tr>
1883     <td><hr size=3 noshade></td>
1884   </tr>
1885 </table>|;
1886
1887   # multiple invoice edit button only if gotten there via sales_order form.
1888
1889   if ($form->{type} =~ /sales_order/) {
1890     print qq|
1891   <input class"submit" type="submit" name="action" value="|
1892       . $locale->text('Continue') . qq|">
1893   <input type="hidden" name="nextsub" value="edit">
1894   <input type="hidden" name="type" value="$form->{type}">
1895   <input type="hidden" name="vc" value="$form->{vc}">
1896   <input type="hidden" name="login" value="$form->{login}">
1897   <input type="hidden" name="password" value="$form->{password}">
1898   <input type="hidden" name="callback" value="$callback">
1899   <input type="hidden" name="rowcount" value="$form->{rowcount}">|;
1900   }
1901
1902   print qq|
1903 </form>
1904
1905 <br>
1906 <form method=post action=$form->{script}>
1907
1908 <input name=callback type=hidden value="$form->{callback}">
1909
1910 <input type=hidden name=type value=$form->{type}>
1911 <input type=hidden name=vc value=$form->{vc}>
1912
1913 <input type=hidden name=login value=$form->{login}>
1914 <input type=hidden name=password value=$form->{password}>
1915
1916 </form>
1917
1918 </body>
1919 </html>
1920 |;
1921
1922   $lxdebug->leave_sub();
1923 }
1924
1925 sub subtotal {
1926   $lxdebug->enter_sub();
1927
1928   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1929
1930   $column_data{netamount} =
1931       "<th class=listsubtotal align=right>"
1932     . $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")
1933     . "</th>";
1934   $column_data{tax} = "<td class=listsubtotal align=right>"
1935     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1936                            2, "&nbsp;")
1937     . "</th>";
1938   $column_data{amount} =
1939     "<th class=listsubtotal align=right>"
1940     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1941
1942   $subtotalnetamount = 0;
1943   $subtotalamount    = 0;
1944
1945   print "
1946         <tr class=listsubtotal>
1947 ";
1948
1949   map { print "\n$column_data{$_}" } @column_index;
1950
1951   print qq|
1952         </tr>
1953 |;
1954
1955   $lxdebug->leave_sub();
1956 }
1957
1958 sub save_and_close {
1959   $lxdebug->enter_sub();
1960
1961   if ($form->{type} =~ /_order$/) {
1962     $form->isblank("transdate", $locale->text('Order Date missing!'));
1963   } else {
1964     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1965   }
1966
1967   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1968   $form->{$idx} =~ s/^\s*//g;
1969   $form->{$idx} =~ s/\s*$//g;
1970
1971   $msg = ucfirst $form->{vc};
1972   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1973
1974   # $locale->text('Customer missing!');
1975   # $locale->text('Vendor missing!');
1976
1977   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1978     if ($form->{currency} ne $form->{defaultcurrency});
1979
1980   &validate_items;
1981   
1982   if($form->{payment_id}) { 
1983     $payment_id = $form->{payment_id};
1984   }
1985   
1986   # if the name changed get new values
1987   if (&check_name($form->{vc})) {
1988     if($form->{payment_id} eq "") { 
1989       $form->{payment_id} = $payment_id;
1990     }
1991     &update;
1992     exit;
1993   }
1994
1995   $form->{id} = 0 if $form->{saveasnew};
1996
1997   # this is for the internal notes section for the [email] Subject
1998   if ($form->{type} =~ /_order$/) {
1999     if ($form->{type} eq 'sales_order') {
2000       $form->{label} = $locale->text('Sales Order');
2001
2002       $numberfld = "sonumber";
2003       $ordnumber = "ordnumber";
2004     } else {
2005       $form->{label} = $locale->text('Purchase Order');
2006
2007       $numberfld = "ponumber";
2008       $ordnumber = "ordnumber";
2009     }
2010
2011     $err = $locale->text('Cannot save order!');
2012
2013   } else {
2014     if ($form->{type} eq 'sales_quotation') {
2015       $form->{label} = $locale->text('Quotation');
2016
2017       $numberfld = "sqnumber";
2018       $ordnumber = "quonumber";
2019     } else {
2020       $form->{label} = $locale->text('Request for Quotation');
2021
2022       $numberfld = "rfqnumber";
2023       $ordnumber = "quonumber";
2024     }
2025
2026     $err = $locale->text('Cannot save quotation!');
2027
2028   }
2029
2030   # get new number in sequence if no number is given or if saveasnew was requested
2031   if (!$form->{$ordnumber} || $form->{saveasnew}) {
2032     $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
2033   }
2034
2035   relink_accounts();
2036
2037   $form->error($err) if (!OE->save(\%myconfig, \%$form));
2038
2039   # saving the history
2040   if(!exists $form->{addition}) {
2041     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
2042         $form->{addition} = "SAVED";
2043         $form->save_history($form->dbconnect(\%myconfig));
2044   }
2045   # /saving the history
2046
2047   $form->redirect($form->{label} . " $form->{$ordnumber} " .
2048                   $locale->text('saved!'));
2049
2050   $lxdebug->leave_sub();
2051 }
2052
2053 sub save {
2054   $lxdebug->enter_sub();
2055
2056   if ($form->{type} =~ /_order$/) {
2057     $form->isblank("transdate", $locale->text('Order Date missing!'));
2058   } else {
2059     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
2060   }
2061
2062   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
2063   $form->{$idx} =~ s/^\s*//g;
2064   $form->{$idx} =~ s/\s*$//g;
2065
2066   $msg = ucfirst $form->{vc};
2067   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
2068
2069   # $locale->text('Customer missing!');
2070   # $locale->text('Vendor missing!');
2071
2072   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
2073     if ($form->{currency} ne $form->{defaultcurrency});
2074
2075   &validate_items;
2076   
2077   if($form->{payment_id}) { 
2078     $payment_id = $form->{payment_id};
2079   }
2080   
2081   # if the name changed get new values
2082   if (&check_name($form->{vc})) {
2083     if($form->{payment_id} eq "") { 
2084       $form->{payment_id} = $payment_id;
2085     }
2086     &update;
2087     exit;
2088   }
2089
2090   $form->{id} = 0 if $form->{saveasnew};
2091
2092   # this is for the internal notes section for the [email] Subject
2093   if ($form->{type} =~ /_order$/) {
2094     if ($form->{type} eq 'sales_order') {
2095       $form->{label} = $locale->text('Sales Order');
2096
2097       $numberfld = "sonumber";
2098       $ordnumber = "ordnumber";
2099     } else {
2100       $form->{label} = $locale->text('Purchase Order');
2101
2102       $numberfld = "ponumber";
2103       $ordnumber = "ordnumber";
2104     }
2105
2106     $err = $locale->text('Cannot save order!');
2107
2108   } else {
2109     if ($form->{type} eq 'sales_quotation') {
2110       $form->{label} = $locale->text('Quotation');
2111
2112       $numberfld = "sqnumber";
2113       $ordnumber = "quonumber";
2114     } else {
2115       $form->{label} = $locale->text('Request for Quotation');
2116
2117       $numberfld = "rfqnumber";
2118       $ordnumber = "quonumber";
2119     }
2120
2121     $err = $locale->text('Cannot save quotation!');
2122
2123   }
2124
2125   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
2126     unless $form->{$ordnumber};
2127
2128   relink_accounts();
2129
2130   OE->save(\%myconfig, \%$form);
2131
2132   # saving the history
2133   if(!exists $form->{addition}) {
2134     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
2135         $form->{addition} = "SAVED";
2136         $form->save_history($form->dbconnect(\%myconfig));
2137   }
2138   # /saving the history 
2139
2140   $form->{simple_save} = 1;
2141   if(!$form->{print_and_save}) {
2142     set_headings("edit");
2143     &update;
2144     exit;
2145   }
2146   $lxdebug->leave_sub();
2147 }
2148
2149 sub delete {
2150   $lxdebug->enter_sub();
2151
2152   $form->header;
2153
2154   if ($form->{type} =~ /_order$/) {
2155     $msg       = $locale->text('Are you sure you want to delete Order Number');
2156     $ordnumber = 'ordnumber';
2157   } else {
2158     $msg = $locale->text('Are you sure you want to delete Quotation Number');
2159     $ordnumber = 'quonumber';
2160   }
2161
2162   print qq|
2163 <body>
2164
2165 <form method=post action=$form->{script}>
2166 |;
2167
2168   # delete action variable
2169   map { delete $form->{$_} } qw(action header);
2170
2171   foreach $key (keys %$form) {
2172     $form->{$key} =~ s/\"/&quot;/g;
2173     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2174   }
2175
2176   print qq|
2177 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
2178
2179 <h4>$msg $form->{$ordnumber}</h4>
2180 <p>
2181 <input name=action class=submit type=submit value="|
2182     . $locale->text('Yes') . qq|">
2183 </form>
2184
2185 </body>
2186 </html>
2187 |;
2188
2189   $lxdebug->leave_sub();
2190 }
2191
2192 sub yes {
2193   $lxdebug->enter_sub();
2194
2195   if ($form->{type} =~ /_order$/) {
2196     $msg = $locale->text('Order deleted!');
2197     $err = $locale->text('Cannot delete order!');
2198   } else {
2199     $msg = $locale->text('Quotation deleted!');
2200     $err = $locale->text('Cannot delete quotation!');
2201   }
2202   if (OE->delete(\%myconfig, \%$form, $spool)){
2203     $form->redirect($msg);
2204     # saving the history
2205     if(!exists $form->{addition}) {
2206       $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
2207           $form->{addition} = "DELETED";
2208           $form->save_history($form->dbconnect(\%myconfig));
2209     }
2210     # /saving the history 
2211   }
2212   $form->error($err);
2213
2214   $lxdebug->leave_sub();
2215 }
2216
2217 sub invoice {
2218   $lxdebug->enter_sub();
2219
2220   if ($form->{type} =~ /_order$/) {
2221
2222     # these checks only apply if the items don't bring their own ordnumbers/transdates.
2223     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
2224     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
2225       if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
2226     $form->isblank("transdate", $locale->text('Order Date missing!'))
2227       if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
2228
2229     # also copy deliverydate from the order
2230     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
2231     $form->{orddate} = $form->{transdate};
2232   } else {
2233     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
2234     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
2235     $form->{ordnumber} = "";
2236     $form->{quodate} = $form->{transdate};
2237   }
2238   
2239   if($form->{payment_id}) { 
2240     $payment_id = $form->{payment_id};
2241   }
2242   
2243   # if the name changed get new values
2244   if (&check_name($form->{vc})) {
2245     if($form->{payment_id} eq "") { 
2246       $form->{payment_id} = $payment_id;
2247     }
2248     &update;
2249     exit;
2250   }
2251
2252   $form->{cp_id} *= 1;
2253
2254   for $i (1 .. $form->{rowcount}) {
2255     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
2256                                                      $form->{"${_}_${i}"})
2257             if ($form->{"${_}_${i}"}) }
2258         qw(ship qty sellprice listprice basefactor));
2259   }
2260
2261   if (   $form->{type} =~ /_order/
2262       && $form->{currency} ne $form->{defaultcurrency}) {
2263
2264     # check if we need a new exchangerate
2265     $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
2266
2267     $orddate      = $form->current_date(\%myconfig);
2268     $exchangerate =
2269       $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate,
2270                                 $buysell);
2271
2272     if (!$exchangerate) {
2273       &backorder_exchangerate($orddate, $buysell);
2274       exit;
2275     }
2276   }
2277
2278   # close orders/quotations
2279   $form->{closed} = 1;
2280
2281   # save order if one ordnumber has been given
2282   # if not it's most likely a collective order, which can't be saved back
2283   # so they just have to be closed
2284   if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
2285     OE->close_order(\%myconfig, \%$form);
2286   } else {
2287     OE->close_orders(\%myconfig, \%$form);
2288   }
2289
2290   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
2291   $form->{duedate} =
2292     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
2293
2294   $form->{id}     = '';
2295   $form->{closed} = 0;
2296   $form->{rowcount}--;
2297   $form->{shipto} = 1;
2298
2299   if ($form->{type} =~ /_order$/) {
2300     $form->{exchangerate} = $exchangerate;
2301     &create_backorder;
2302   }
2303
2304   if (   $form->{type} eq 'purchase_order'
2305       || $form->{type} eq 'request_quotation') {
2306     $form->{title}  = $locale->text('Add Vendor Invoice');
2307     $form->{script} = 'ir.pl';
2308     $script         = "ir";
2309     $buysell        = 'sell';
2310   }
2311   if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
2312     $form->{title}  = $locale->text('Add Sales Invoice');
2313     $form->{script} = 'is.pl';
2314     $script         = "is";
2315     $buysell        = 'buy';
2316   }
2317
2318   # bo creates the id, reset it
2319   map { delete $form->{$_} }
2320     qw(id subject message cc bcc printed emailed queued);
2321   $form->{ $form->{vc} } =~ s/--.*//g;
2322   $form->{type} = "invoice";
2323
2324   # locale messages
2325   $locale = new Locale "$myconfig{countrycode}", "$script";
2326
2327   require "bin/mozilla/$form->{script}";
2328
2329   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
2330
2331   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
2332     qw(creditlimit creditremaining);
2333
2334   $currency = $form->{currency};
2335   &invoice_links;
2336
2337   $form->{currency}     = $currency;
2338   $form->{exchangerate} = "";
2339   $form->{forex}        = "";
2340   $form->{exchangerate} = $exchangerate
2341     if (
2342         $form->{forex} = (
2343                     $exchangerate =
2344                       $form->check_exchangerate(
2345                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
2346                       )));
2347
2348   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
2349
2350   &prepare_invoice;
2351
2352   # format amounts
2353   for $i (1 .. $form->{rowcount}) {
2354     $form->{"discount_$i"} =
2355       $form->format_amount(\%myconfig, $form->{"discount_$i"});
2356
2357     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
2358     $dec           = length $dec;
2359     $decimalplaces = ($dec > 2) ? $dec : 2;
2360
2361     # copy delivery date from reqdate for order -> invoice conversion
2362     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
2363       unless $form->{"deliverydate_$i"};
2364
2365     $form->{"sellprice_$i"} =
2366       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
2367                            $decimalplaces);
2368
2369     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
2370     $dec_qty = length $dec_qty;
2371     $form->{"qty_$i"} =
2372       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
2373
2374     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2375       qw(partnumber description unit);
2376
2377   }
2378
2379   &display_form;
2380
2381   $lxdebug->leave_sub();
2382 }
2383
2384 sub backorder_exchangerate {
2385   $lxdebug->enter_sub();
2386   my ($orddate, $buysell) = @_;
2387
2388   $form->header;
2389
2390   print qq|
2391 <body>
2392
2393 <form method=post action=$form->{script}>
2394 |;
2395
2396   # delete action variable
2397   map { delete $form->{$_} } qw(action header exchangerate);
2398
2399   foreach $key (keys %$form) {
2400     $form->{$key} =~ s/\"/&quot;/g;
2401     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2402   }
2403
2404   $form->{title} = $locale->text('Add Exchangerate');
2405
2406   print qq|
2407
2408 <input type=hidden name=login value=$form->{login}>
2409 <input type=hidden name=password value=$form->{password}>
2410
2411 <input type=hidden name=exchangeratedate value=$orddate>
2412 <input type=hidden name=buysell value=$buysell>
2413
2414 <table width=100%>
2415   <tr><th class=listtop>$form->{title}</th></tr>
2416   <tr height="5"></tr>
2417   <tr>
2418     <td>
2419       <table>
2420         <tr>
2421           <th align=right>| . $locale->text('Currency') . qq|</th>
2422           <td>$form->{currency}</td>
2423         </tr>
2424         <tr>
2425           <th align=right>| . $locale->text('Date') . qq|</th>
2426           <td>$orddate</td>
2427         </tr>
2428         <tr>
2429           <th align=right>| . $locale->text('Exchangerate') . qq|</th>
2430           <td><input name=exchangerate size=11></td>
2431         </tr>
2432       </table>
2433     </td>
2434   </tr>
2435 </table>
2436
2437 <hr size=3 noshade>
2438
2439 <br>
2440 <input type=hidden name=nextsub value=save_exchangerate>
2441
2442 <input name=action class=submit type=submit value="|
2443     . $locale->text('Continue') . qq|">
2444
2445 </form>
2446
2447 </body>
2448 </html>
2449 |;
2450
2451   $lxdebug->leave_sub();
2452 }
2453
2454 sub save_exchangerate {
2455   $lxdebug->enter_sub();
2456
2457   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
2458   $form->{exchangerate} =
2459     $form->parse_amount(\%myconfig, $form->{exchangerate});
2460   $form->save_exchangerate(\%myconfig, $form->{currency},
2461                            $form->{exchangeratedate},
2462                            $form->{exchangerate}, $form->{buysell});
2463
2464   &invoice;
2465
2466   $lxdebug->leave_sub();
2467 }
2468
2469 sub create_backorder {
2470   $lxdebug->enter_sub();
2471
2472   $form->{shipped} = 1;
2473
2474   # figure out if we need to create a backorder
2475   # items aren't saved if qty != 0
2476
2477   for $i (1 .. $form->{rowcount}) {
2478     $totalqty  += $qty  = $form->{"qty_$i"};
2479     $totalship += $ship = $form->{"ship_$i"};
2480
2481     $form->{"qty_$i"} = $qty - $ship;
2482   }
2483
2484   if ($totalship == 0) {
2485     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
2486     $form->{ordtotal} = 0;
2487     $form->{shipped}  = 0;
2488     return;
2489   }
2490
2491   if ($totalqty == $totalship) {
2492     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
2493     $form->{ordtotal} = 0;
2494     return;
2495   }
2496
2497   @flds = (
2498     qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
2499   );
2500
2501   for $i (1 .. $form->{rowcount}) {
2502     map {
2503       $form->{"${_}_$i"} =
2504         $form->format_amount(\%myconfig, $form->{"${_}_$i"})
2505     } qw(sellprice discount);
2506   }
2507
2508   relink_accounts();
2509
2510   OE->save(\%myconfig, \%$form);
2511
2512   # rebuild rows for invoice
2513   @a     = ();
2514   $count = 0;
2515
2516   for $i (1 .. $form->{rowcount}) {
2517     $form->{"qty_$i"} = $form->{"ship_$i"};
2518
2519     if ($form->{"qty_$i"}) {
2520       push @a, {};
2521       $j = $#a;
2522       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2523       $count++;
2524     }
2525   }
2526
2527   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2528   $form->{rowcount} = $count;
2529
2530   $lxdebug->leave_sub();
2531 }
2532
2533 sub save_as_new {
2534   $lxdebug->enter_sub();
2535
2536   $form->{saveasnew} = 1;
2537   $form->{closed}    = 0;
2538   map { delete $form->{$_} } qw(printed emailed queued);
2539
2540   # Let Lx-Office assign a new order number if the user hasn't changed the
2541   # previous one. If it has been changed manually then use it as-is.
2542   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
2543   $form->{$idx} =~ s/^\s*//g;
2544   $form->{$idx} =~ s/\s*$//g;
2545   if ($form->{saved_xyznumber} &&
2546       ($form->{saved_xyznumber} eq $form->{$idx})) {
2547     delete($form->{$idx});
2548   }
2549
2550   &save;
2551
2552   $lxdebug->leave_sub();
2553 }
2554
2555 sub purchase_order {
2556   $lxdebug->enter_sub();
2557
2558   if (   $form->{type} eq 'sales_quotation'
2559       || $form->{type} eq 'request_quotation') {
2560     OE->close_order(\%myconfig, \%$form);
2561   }
2562
2563   if ($form->{type} =~ /^sales_/) {
2564     delete($form->{ordnumber});
2565   }
2566
2567   $form->{cp_id} *= 1;
2568
2569   $form->{title} = $locale->text('Add Purchase Order');
2570   $form->{vc}    = "vendor";
2571   $form->{type}  = "purchase_order";
2572
2573   &poso;
2574
2575   $lxdebug->leave_sub();
2576 }
2577
2578 sub sales_order {
2579   $lxdebug->enter_sub();
2580
2581   if (   $form->{type} eq 'sales_quotation'
2582       || $form->{type} eq 'request_quotation') {
2583     OE->close_order(\%myconfig, $form);
2584   }
2585
2586   if ($form->{type} eq "purchase_order") {
2587     delete($form->{ordnumber});
2588   }
2589
2590   $form->{cp_id} *= 1;
2591
2592   $form->{title} = $locale->text('Add Sales Order');
2593   $form->{vc}    = "customer";
2594   $form->{type}  = "sales_order";
2595
2596   &poso;
2597
2598   $lxdebug->leave_sub();
2599 }
2600
2601 sub poso {
2602   $lxdebug->enter_sub();
2603
2604   $form->{transdate} = $form->current_date(\%myconfig);
2605   delete $form->{duedate};
2606
2607   $form->{closed} = 0;
2608
2609   # reset
2610   map { delete $form->{$_} }
2611     qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
2612
2613   for $i (1 .. $form->{rowcount}) {
2614     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
2615                                                      $form->{"${_}_${i}"})
2616             if ($form->{"${_}_${i}"}) }
2617         qw(ship qty sellprice listprice basefactor));
2618   }
2619
2620   &order_links;
2621
2622   &prepare_order;
2623
2624   # format amounts
2625   for $i (1 .. $form->{rowcount} - 1) {
2626     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2627       qw(partnumber description unit);
2628   }
2629
2630   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 0, "0") }
2631     qw(creditlimit creditremaining);
2632
2633   &update;
2634
2635   $lxdebug->leave_sub();
2636 }
2637
2638 sub e_mail {
2639   $lxdebug->enter_sub();
2640
2641   $form->{print_and_save} = 1;
2642
2643   if (!$form->{id}) {
2644     $print_post = 1;
2645
2646     my $saved_form = save_form();
2647
2648     save();
2649
2650     my %saved_vars;
2651     map({ $saved_vars{$_} = $form->{$_}; } qw(id ordnumber quonumber));
2652     restore_form($saved_form);
2653     map({ $form->{$_} = $saved_vars{$_}; } qw(id ordnumber quonumber));
2654   }
2655
2656   edit_e_mail();
2657
2658   $lxdebug->leave_sub();
2659 }
2660