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