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