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