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