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