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