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