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