3c55ee58f60248b7c6e95be9e63c1c421652b71a
[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
161   # get customer/vendor
162   $form->all_vc(\%myconfig, $form->{vc},
163                 ($form->{vc} eq 'customer') ? "AR" : "AP");
164
165   # retrieve order/quotation
166   $form->{webdav} = $webdav;
167   # set jscalendar
168   $form->{jscalendar} = $jscalendar;
169
170   my $editing = $form->{id};
171
172   OE->retrieve(\%myconfig, \%$form);
173
174   if ($form->{payment_id}) {
175     $payment_id = $form->{payment_id};
176   }
177   if ($form->{language_id}) {
178     $language_id = $form->{language_id};
179   }
180   if ($form->{taxzone_id}) {
181     $taxzone_id = $form->{taxzone_id};
182   }
183
184   $salesman_id = $form->{salesman_id} if ($editing);
185
186
187   # if multiple rowcounts (== collective order) then check if the
188   # there were more than one customer (in that case OE::retrieve removes
189   # the content from the field)
190   if (   $form->{rowcount}
191       && $form->{type} eq 'sales_order'
192       && defined $form->{customer}
193       && $form->{customer} eq '') {
194
195     #    $main::lxdebug->message(0, "Detected Edit order with concurrent customers");
196     $form->error(
197                  $locale->text(
198                    'Collective Orders only work for orders from one customer!')
199     );
200   }
201
202   $taxincluded = $form->{taxincluded};
203   $form->{shipto} = 1 if $form->{id};
204
205   if ($form->{"all_$form->{vc}"}) {
206     unless ($form->{"$form->{vc}_id"}) {
207       $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
208     }
209   }
210
211   $cp_id    = $form->{cp_id};
212   $intnotes = $form->{intnotes};
213
214   # get customer / vendor
215   if ($form->{type} =~ /(purchase_order|request_quotation)/) {
216     IR->get_vendor(\%myconfig, \%$form);
217
218     #quote all_vendor Bug 133
219     foreach $ref (@{ $form->{all_vendor} }) {
220       $ref->{name} = $form->quote($ref->{name});
221     }
222
223   }
224   if ($form->{type} =~ /sales_(order|quotation)/) {
225     IS->get_customer(\%myconfig, \%$form);
226
227     #quote all_vendor Bug 133
228     foreach $ref (@{ $form->{all_customer} }) {
229       $ref->{name} = $form->quote($ref->{name});
230     }
231
232   }
233   $form->{cp_id} = $cp_id;
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   # sales staff
273   if (@{ $form->{all_employees} }) {
274     $form->{selectemployee} = "";
275     map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}</option>\n" }
276       (@{ $form->{all_employees} });
277   }
278
279   # forex
280   $form->{forex} = $form->{exchangerate};
281
282   $form->{salesman_id} = $salesman_id if ($editing);
283
284   $lxdebug->leave_sub();
285 }
286
287 sub prepare_order {
288   $lxdebug->enter_sub();
289   $form->{formname} = $form->{type} unless $form->{formname};
290
291   my $i = 0;
292   foreach $ref (@{ $form->{form_details} }) {
293     $form->{rowcount} = ++$i;
294
295     map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
296   }
297   for my $i (1 .. $form->{rowcount}) {
298     if ($form->{id}) {
299       $form->{"discount_$i"} =
300         $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
301     } else {
302       $form->{"discount_$i"} =
303         $form->format_amount(\%myconfig, $form->{"discount_$i"});
304     }
305     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
306     $dec           = length $dec;
307     $decimalplaces = ($dec > 2) ? $dec : 2;
308
309     # copy reqdate from deliverydate for invoice -> order conversion
310     $form->{"reqdate_$i"} = $form->{"deliverydate_$i"}
311       unless $form->{"reqdate_$i"};
312
313     $form->{"sellprice_$i"} =
314       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
315                            $decimalplaces);
316
317     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
318     $dec_qty = length $dec_qty;
319     $form->{"qty_$i"} =
320       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
321
322     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
323       qw(partnumber description unit);
324   }
325
326   $lxdebug->leave_sub();
327 }
328
329 sub form_header {
330   $lxdebug->enter_sub();
331
332   my $checkedclosed = $form->{"closed"} ? "checked" : "";
333   my $checkeddelivered = $form->{"delivered"} ? "checked" : "";
334
335   map { $form->{$_} =~ s/\"/&quot;/g }
336     qw(ordnumber quonumber shippingpoint shipvia notes intnotes shiptoname
337        shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
338        shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
339
340   # use JavaScript Calendar or not
341   $form->{jsscript} = $form->{jscalendar};
342   $jsscript = "";
343
344   $payment = qq|<option value=""></option>|;
345   foreach $item (@{ $form->{payment_terms} }) {
346     if ($form->{payment_id} eq $item->{id}) {
347       $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
348     } else {
349       $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
350     }
351   }
352   if ($form->{jsscript}) {
353
354     # with JavaScript Calendar
355     $button1 = qq|
356        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\"></td>
357        <td><input type=button name=transdate id="trigger1" value=|
358       . $locale->text('button') . qq|></td>
359       |;
360     $button2 = qq|
361        <td width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\"></td>
362        <td width="4"><input type=button name=reqdate name=reqdate id="trigger2" value=|
363       . $locale->text('button') . qq|></td>
364      |;
365
366     #write Trigger
367     $jsscript =
368       Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
369                           "reqdate", "BL", "trigger2");
370
371   } else {
372
373     # without JavaScript Calendar
374     $button1 = qq|
375                               <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\"></td>|;
376     $button2 = qq|
377                               <td width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\"></td>|;
378   }
379
380   my @tmp;
381
382   if (($form->{"type"} eq "sales_order") ||
383       ($form->{"type"} eq "purchase_order")) {
384     push(@tmp, qq|
385           <input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" $checkeddelivered>
386           <label for="delivered">| . $locale->text('Delivered') . qq|</label>|);
387   }
388
389   if ($form->{id}) {
390     push(@tmp, qq|
391           <input name="closed" id="closed" type="checkbox" class="checkbox" value="1" $checkedclosed>
392           <label for="closed">| . $locale->text('Closed') . qq|</label>|);
393   }
394
395   if (@tmp) {
396     $openclosed .= qq|
397       <tr>
398         <td colspan=| . (2 * scalar(@tmp)) . qq| align=center>| . join("\n", @tmp) . qq|
399         </td>
400       </tr>
401 |;
402   }
403
404   # set option selected
405   foreach $item ($form->{vc}, currency, department, employee) {
406     $form->{"select$item"} =~ s/ selected//;
407     $form->{"select$item"} =~
408       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
409   }
410
411   #quote select[customer|vendor] Bug 133
412   $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
413
414   #substitute \n and \r to \s (bug 543)
415   $form->{"select$form->{vc}"} =~ s/[\n\r]/&nbsp;/g;
416   
417   my @old_project_ids = ($form->{"globalproject_id"});
418   map({ push(@old_project_ids, $form->{"project_id_$_"})
419           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
420
421   $form->get_lists("contacts" => "ALL_CONTACTS",
422                    "shipto" => "ALL_SHIPTO",
423                    "projects" => { "key" => "ALL_PROJECTS",
424                                    "all" => 0,
425                                    "old_id" => \@old_project_ids },
426                    "employees" => "ALL_SALESMEN",
427                    "taxzones" => "ALL_TAXZONES",
428                    "currencies" => "ALL_CURRENCIES");
429
430   my %labels;
431   my @values = (undef);
432   foreach my $item (@{ $form->{"ALL_CONTACTS"} }) {
433     push(@values, $item->{"cp_id"});
434     $labels{$item->{"cp_id"}} = $item->{"cp_name"} .
435       ($item->{"cp_abteilung"} ? " ($item->{cp_abteilung})" : "");
436   }
437   my $contact =
438     NTI($cgi->popup_menu('-name' => 'cp_id', '-values' => \@values,
439                          '-labels' => \%labels, '-default' => $form->{"cp_id"}));
440
441   %labels = ();
442   @values = ("");
443   foreach my $item (@{ $form->{"ALL_SHIPTO"} }) {
444     push(@values, $item->{"shipto_id"});
445     $labels{$item->{"shipto_id"}} =
446       $item->{"shiptoname"} . " " . $item->{"shiptodepartment_1"};
447   }
448
449   my $shipto = qq|
450                 <th align=right>| . $locale->text('Shipping Address') . qq|</th>
451                 <td>| .
452     NTI($cgi->popup_menu('-name' => 'shipto_id', '-values' => \@values,
453                          '-labels' => \%labels, '-default' => $form->{"shipto_id"}))
454     . qq|</td>|;
455
456   %labels = ();
457   @values = ("");
458   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
459     push(@values, $item->{"id"});
460     $labels{$item->{"id"}} = $item->{"projectnumber"};
461   }
462   my $globalprojectnumber =
463     NTI($cgi->popup_menu('-name' => 'globalproject_id', '-values' => \@values,
464                          '-labels' => \%labels,
465                          '-default' => $form->{"globalproject_id"}));
466
467   $salesman = "";
468   if ($form->{type} =~ /^sales_/) {
469     %labels = ();
470     @values = ("");
471     foreach my $item (@{ $form->{ALL_SALESMEN} }) {
472       push(@values, $item->{id});
473       $labels{$item->{id}} = $item->{name} ne "" ? $item->{name} : $item->{login};
474     }
475
476     $salesman =
477       qq|<tr>
478           <th align="right">| . $locale->text('Salesman') . qq|</th>
479           <td>| .
480       NTI($cgi->popup_menu('-name' => 'salesman_id', '-default' => $form->{salesman_id},
481                            '-values' => \@values, '-labels' => \%labels))
482       . qq|</td>
483          </tr>|;
484   }
485
486
487   %labels = ();
488   @values = ();
489   foreach my $item (@{ $form->{"ALL_TAXZONES"} }) {
490     push(@values, $item->{"id"});
491     $labels{$item->{"id"}} = $item->{"description"};
492   }
493
494   $taxzone = qq|
495     <tr>
496       <th align="right">| . $locale->text('Steuersatz') . qq|</th>
497       <td>| .
498         NTI($cgi->popup_menu('-name' => 'taxzone_id', '-default' => $form->{"taxzone_id"},
499                              '-values' => \@values, '-labels' => \%labels)) . qq|
500       </td>
501     </tr>|;
502
503   %labels = ();
504   @values = ();
505   my $i = 0;
506   foreach my $item (@{ $form->{"ALL_CURRENCIES"} }) {
507     push(@values, $item);
508     $labels{$item} = $item;
509   }
510   
511   $form->{currency}        = $form->{defaultcurrency} unless $form->{currency};
512   my $currencies = qq|
513     <tr>
514       <th align="right">| . $locale->text('Currency') . qq|</th>
515       <td>| .
516         NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
517                              '-values' => \@values, '-labels' => \%labels)) . qq|
518       </td>
519     </tr>|;
520
521
522   $form->{exchangerate} =
523     $form->format_amount(\%myconfig, $form->{exchangerate});
524
525   if (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) {
526     $creditwarning = 1;
527   } else {
528     $creditwarning = 0;
529   }
530
531   $form->{creditlimit} =
532     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
533   $form->{creditremaining} =
534     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
535
536   $exchangerate = qq|
537 <input type=hidden name=forex value=$form->{forex}>
538 |;
539
540   if ($form->{currency} ne $form->{defaultcurrency}) {
541     if ($form->{forex}) {
542       $exchangerate .=
543           qq|<th align=right>|
544         . $locale->text('Exchangerate')
545         . qq|</th><td>$form->{exchangerate}</td>
546       <input type=hidden name=exchangerate value=$form->{exchangerate}>
547 |;
548     } else {
549       $exchangerate .=
550           qq|<th align=right>|
551         . $locale->text('Exchangerate')
552         . qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
553     }
554   }
555
556   $vclabel = ucfirst $form->{vc};
557   $vclabel = $locale->text($vclabel);
558
559
560
561   if ($form->{business}) {
562     $business = qq|
563               <tr>
564                 <th align=right>| . $locale->text('Business') . qq|</th>
565                 <td>$form->{business}</td>
566                 <th align=right>| . $locale->text('Trade Discount') . qq|</th>
567                 <td>|
568       . $form->format_amount(\%myconfig, $form->{tradediscount} * 100)
569       . qq| %</td>
570               </tr>
571 |;
572   }
573
574   if ($form->{max_dunning_level}) {
575     $dunning = qq|
576               <tr>
577                 <td colspan=4>
578                 <table>
579                   <tr>
580                 <th align=right>| . $locale->text('Max. Dunning Level') . qq|:</th>
581                 <td><b>$form->{max_dunning_level}</b></td>
582                 <th align=right>| . $locale->text('Dunning Amount') . qq|:</th>
583                 <td><b>|
584       . $form->format_amount(\%myconfig, $form->{dunning_amount},2)
585       . qq|</b></td>
586               </tr>
587               </table>
588              </td>
589             </tr>
590 |;
591   }
592
593   if ($form->{type} !~ /_quotation$/) {
594     $ordnumber = qq|
595               <tr>
596                 <th width=70% align=right nowrap>| . $locale->text('Order Number') . qq|</th>
597                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
598               </tr>
599               <tr>
600                 <th width=70% align=right nowrap>|
601       . $locale->text('Quotation Number') . qq|</th>
602                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
603               </tr>
604               <tr>
605                 <th width=70% align=right nowrap>|
606       . $locale->text('Customer Order Number') . qq|</th>
607                 <td><input name=cusordnumber size=11 value="$form->{cusordnumber}"></td>
608               </tr>
609               <tr>
610                 <th align=right nowrap>| . $locale->text('Order Date') . qq|</th>
611                 $button1
612
613               </tr>
614               <tr>
615                 <th align=right nowrap=true>| . $locale->text('Required by') . qq|</th>
616                 $button2
617               </tr>
618 |;
619
620     $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
621
622     $creditremaining = qq|
623               <tr>
624                 <td></td>
625                 <td colspan=3>
626                   <table>
627                     <tr>
628                       <th nowrap>| . $locale->text('Credit Limit') . qq|</th>
629                       <td>$form->{creditlimit}</td>
630                       <td width=20%></td>
631                       <th nowrap>| . $locale->text('Remaining') . qq|</th>
632                       <td class="plus$n" nowrap>$form->{creditremaining}</td>
633                     </tr>
634                   </table>
635                 </td>
636                 $shipto
637               </tr>
638 |;
639   } else {
640     $reqlabel =
641       ($form->{type} eq 'sales_quotation')
642       ? $locale->text('Valid until')
643       : $locale->text('Required by');
644     if ($form->{type} eq 'sales_quotation') {
645       $ordnumber = qq|
646               <tr>
647                 <th width=70% align=right nowrap>|
648         . $locale->text('Quotation Number') . qq|</th>
649                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
650                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
651               </tr>
652 |;
653     } else {
654       $ordnumber = qq|
655               <tr>
656                 <th width=70% align=right nowrap>| . $locale->text('RFQ Number') . qq|</th>
657                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
658                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
659               </tr>
660 |;
661
662     }
663
664     $ordnumber .= qq|
665               <tr>
666                 <th align=right nowrap>| . $locale->text('Quotation Date') . qq|</th>
667                 $button1
668               </tr>
669               <tr>
670                 <th align=right nowrap=true>$reqlabel</th>
671                 $button2
672               </tr>
673 |;
674     $creditremaining = qq| <tr>
675                             <td colspan=4></td>
676                             $shipto
677                           </tr>|;
678   }
679
680   $vc =
681     ($form->{"select$form->{vc}"})
682     ? qq|<select name="$form->{vc}"
683 onchange="document.getElementById('update_button').click();">| .
684     qq|$form->{"select$form->{vc}"}</select>\n<input type=hidden name="select$form->{vc}" value="| .
685     Q($form->{"select$form->{vc}"}) . qq|">|
686     : qq|<input name=$form->{vc} value="$form->{$form->{vc}}" size=35>|;
687
688   $department = qq|
689               <tr>
690                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
691                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
692                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
693                 </td>
694               </tr>
695 | if $form->{selectdepartment};
696
697   $employee = qq|
698               <input type=hidden name=employee value="$form->{employee}">
699 |;
700
701   if ($form->{type} eq 'sales_order') {
702     if ($form->{selectemployee}) {
703       $employee = qq|
704     <input type=hidden name=customer_klass value=$form->{customer_klass}>
705               <tr>
706                 <th align=right nowrap>| . $locale->text('Employee') . qq|</th>
707                 <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
708                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
709                 <td></td>
710               </tr>
711 |;
712     }
713   } else {
714     $employee = qq|
715     <input type=hidden name=customer_klass value=$form->{customer_klass}>
716               <tr>
717                 <th align=right nowrap>| . $locale->text('Employee') . qq|</th>
718                 <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
719                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
720                 <td></td>
721               </tr>
722 |;
723   }
724   if ($form->{resubmit} && ($form->{format} eq "html")) {
725     $onload =
726       qq|window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()|;
727   } elsif ($form->{resubmit}) {
728     $onload = qq|document.oe.submit()|;
729   } else {
730     $onload = "focus()";
731   }
732
733   $credittext = $locale->text('Credit Limit exceeded!!!');
734   if ($creditwarning) {
735     $onload = qq|alert('$credittext')|;
736   }
737   
738   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
739   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
740   
741   $form->{"javascript"} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
742   # show history button js
743   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
744   #/show history button js
745   $form->header;
746
747   print qq|
748 <body onLoad="$onload">
749
750 <form method=post name=oe action=$form->{script}>
751  <script type="text/javascript" src="js/common.js"></script>
752  <script type="text/javascript" src="js/delivery_customer_selection.js"></script>
753  <script type="text/javascript" src="js/vendor_selection.js"></script>
754  <script type="text/javascript" src="js/calculate_qty.js"></script>
755
756 <input type=hidden name=id value=$form->{id}>
757 <input type=hidden name=action value=$form->{action}>
758
759 <input type=hidden name=type value=$form->{type}>
760 <input type=hidden name=formname value=$form->{formname}>
761 <input type=hidden name=media value=$form->{media}>
762 <input type=hidden name=format value=$form->{format}>
763 <input type=hidden name=proforma value=$form->{proforma}>
764
765 <input type=hidden name=queued value="$form->{queued}">
766 <input type=hidden name=printed value="$form->{printed}">
767 <input type=hidden name=emailed value="$form->{emailed}">
768
769 <input type=hidden name=vc value=$form->{vc}>
770
771 <input type=hidden name=title value="$form->{title}">
772
773 <input type=hidden name=discount value=$form->{discount}>
774 <input type=hidden name=creditlimit value=$form->{creditlimit}>
775 <input type=hidden name=creditremaining value=$form->{creditremaining}>
776
777 <input type=hidden name=tradediscount value=$form->{tradediscount}>
778 <input type=hidden name=business value=$form->{business}>
779 <input type=hidden name=webdav value=$webdav>
780
781 <table width=100%>
782   <tr class=listtop>
783     <th class=listtop>$form->{title}</th>
784   </tr>
785   <tr height="5"></tr>
786   <tr>
787     <td>
788       <table width="100%">
789         <tr valign=top>
790           <td>
791             <table width=100%>
792               <tr>
793                 <th align=right>$vclabel</th>
794                 <td colspan=3>$vc</td>
795                 <input type=hidden name=$form->{vc}_id value=$form->{"$form->{vc}_id"}>
796                 <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
797                 <th align=richt nowrap>|
798     . $locale->text('Contact Person') . qq|</th>
799                 <td colspan=3>$contact</td>
800               </tr>
801               $creditremaining
802               $business
803               $dunning
804               $taxzone
805               $department
806               <tr>
807                 $currencies
808                 $exchangerate
809               </tr>
810               <tr>
811                 <th align=right>| . $locale->text('Shipping Point') . qq|</th>
812                 <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
813               </tr>
814               <tr>
815                 <th align=right>| . $locale->text('Ship via') . qq|</th>
816                 <td colspan=3><input name=shipvia size=35 value="$form->{shipvia}"></td>
817               </tr>
818               <tr>
819                 <th align="right">| . $locale->text('Transaction description') . qq|</th>
820                 <td colspan="3"><input name="transaction_description" size="35" value="| . H($form->{transaction_description}) . qq|"></td>
821               </tr>|;
822 #              <tr>
823 #                 <td colspan=4>
824 #                   <table>
825 #                     <tr>
826 #                       <td colspan=2>
827 #                         <button type="button" onclick="delivery_customer_selection_window('delivery_customer_string','delivery_customer_id')">| . $locale->text('Choose Customer') . qq|</button>
828 #                       </td>
829 #                       <td colspan=2><input type=hidden name=delivery_customer_id value="$form->{delivery_customer_id}">
830 #                       <input size=45 id=delivery_customer_string name=delivery_customer_string value="$form->{delivery_customer_string}"></td>
831 #                     </tr>
832 #                     <tr>
833 #                       <td colspan=2>
834 #                         <button type="button" onclick="vendor_selection_window('delivery_vendor_string','delivery_vendor_id')">| . $locale->text('Choose Vendor') . qq|</button>
835 #                       </td>
836 #                       <td colspan=2><input type=hidden name=delivery_vendor_id value="$form->{delivery_vendor_id}">
837 #                       <input size=45 id=vendor_string name=delivery_vendor_string value="$form->{delivery_vendor_string}"></td>
838 #                     </tr>
839 #                   </table>
840 #                 </td>
841 #               </tr>
842 print qq|           </table>
843           </td>
844           <td align=right>
845             <table>
846               $openclosed
847               $employee
848         $salesman
849               $ordnumber
850               <tr>
851           <th width="70%" align="right" nowrap>| . $locale->text('Project Number') . qq|</th>
852           <td>$globalprojectnumber</td>
853               </tr>
854             </table>
855           </td>
856         </tr>
857       </table>
858     </td>
859   </tr>
860
861 $jsscript
862
863 <!-- shipto are in hidden variables -->
864
865 <input type=hidden name=shiptoname value="$form->{shiptoname}">
866 <input type=hidden name=shiptostreet value="$form->{shiptostreet}">
867 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
868 <input type=hidden name=shiptocity value="$form->{shiptocity}">
869 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
870 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
871 <input type=hidden name=shiptophone value="$form->{shiptophone}">
872 <input type=hidden name=shiptofax value="$form->{shiptofax}">
873 <input type=hidden name=shiptodepartment_1 value="$form->{shiptodepartment_1}">
874 <input type=hidden name=shiptodepartment_2 value="$form->{shiptodepartment_2}">
875 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
876
877 <!-- email variables -->
878 <input type=hidden name=message value="$form->{message}">
879 <input type=hidden name=email value="$form->{email}">
880 <input type=hidden name=subject value="$form->{subject}">
881 <input type=hidden name=cc value="$form->{cc}">
882 <input type=hidden name=bcc value="$form->{bcc}">
883
884 <input type=hidden name=taxpart value="$form->{taxpart}">
885 <input type=hidden name=taxservice value="$form->{taxservice}">
886
887 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
888 |;
889
890   foreach $item (split / /, $form->{taxaccounts}) {
891     print qq|
892 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
893 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
894 |;
895   }
896   $lxdebug->leave_sub();
897 }
898
899 sub form_footer {
900   $lxdebug->enter_sub();
901
902   $form->{invtotal} = $form->{invsubtotal};
903
904   if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
905     $rows = 2;
906   }
907   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
908     $introws = 2;
909   }
910   $rows = ($rows > $introws) ? $rows : $introws;
911   $notes =
912     qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
913   $intnotes =
914     qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
915
916   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
917
918   $taxincluded = "";
919   if ($form->{taxaccounts}) {
920     $taxincluded = qq|
921               <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
922       . $locale->text('Tax Included') . qq|</b><br><br>
923 |;
924   }
925
926   if (!$form->{taxincluded}) {
927
928     foreach $item (split / /, $form->{taxaccounts}) {
929       if ($form->{"${item}_base"}) {
930         $form->{invtotal} += $form->{"${item}_total"} =
931           $form->round_amount(
932                              $form->{"${item}_base"} * $form->{"${item}_rate"},
933                              2);
934         $form->{"${item}_total"} =
935           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
936
937         $tax .= qq|
938               <tr>
939                 <th align=right>$form->{"${item}_description"}&nbsp;|
940                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
941                 <td align=right>$form->{"${item}_total"}</td>
942               </tr>
943 |;
944       }
945     }
946
947     $form->{invsubtotal} =
948       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
949
950     $subtotal = qq|
951               <tr>
952                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
953                 <td align=right>$form->{invsubtotal}</td>
954               </tr>
955 |;
956
957   }
958
959   if ($form->{taxincluded}) {
960     foreach $item (split / /, $form->{taxaccounts}) {
961       if ($form->{"${item}_base"}) {
962         $form->{"${item}_total"} =
963           $form->round_amount(
964                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
965                               (1 + $form->{"${item}_rate"})
966                            ),
967                            2);
968         $form->{"${item}_netto"} =
969           $form->round_amount(
970                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
971                           2);
972         $form->{"${item}_total"} =
973           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
974         $form->{"${item}_netto"} =
975           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
976
977         $tax .= qq|
978               <tr>
979                 <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;|
980                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
981                 <td align=right>$form->{"${item}_total"}</td>
982               </tr>
983               <tr>
984                 <th align=right>Nettobetrag</th>
985                 <td align=right>$form->{"${item}_netto"}</td>
986               </tr>
987 |;
988       }
989     }
990
991   }
992
993   $form->{oldinvtotal} = $form->{invtotal};
994   $form->{invtotal}    =
995     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
996
997   print qq|
998   <tr>
999     <td>
1000       <table width=100%>
1001         <tr valign=bottom>
1002           <td>
1003             <table>
1004               <tr>
1005                 <th align=left>| . $locale->text('Notes') . qq|</th>
1006                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
1007               </tr>
1008               <tr valign=top>
1009                 <td>$notes</td>
1010                 <td>$intnotes</td>
1011               </tr>
1012           <th align=right>| . $locale->text('Payment Terms') . qq|</th>
1013           <td><select name=payment_id>$payment
1014                           </select></td>
1015             </table>
1016           </td>
1017           <td align=right width=100%>
1018             $taxincluded
1019             <table width=100%>
1020               $subtotal
1021               $tax
1022               <tr>
1023                 <th align=right>| . $locale->text('Total') . qq|</th>
1024                 <td align=right>$form->{invtotal}</td>
1025               </tr>
1026             </table>
1027           </td>
1028         </tr>
1029       </table>
1030     </td>
1031   </tr>
1032 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
1033 <input type=hidden name=oldtotalpaid value=$totalpaid>
1034   <tr>
1035     <td><hr size=3 noshade></td>
1036   </tr>
1037 |;
1038
1039   if ($webdav) {
1040     $webdav_list = qq|
1041
1042   <tr>
1043     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
1044   </tr>
1045     <table width=100%>
1046       <td align=left width=30%><b>Dateiname</b></td>
1047       <td align=left width=70%><b>Webdavlink</b></td>
1048 |;
1049     foreach $file (keys %{ $form->{WEBDAV} }) {
1050       $webdav_list .= qq|
1051       <tr>
1052         <td align=left>$file</td>
1053         <td align=left><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
1054       </tr>
1055 |;
1056     }
1057     $webdav_list .= qq|
1058     </table>
1059   </tr>
1060   <tr>
1061     <td><hr size=3 noshade></td>
1062   </tr>
1063 |;
1064
1065     print $webdav_list;
1066   }
1067   print qq|
1068 <input type=hidden name=jscalendar value=$form->{jscalendar}>
1069 |;
1070   print qq|
1071   <tr>
1072     <td>
1073 |;
1074   print_options();
1075
1076   print qq|
1077     </td>
1078   </tr>
1079 </table>
1080
1081 | . $locale->text("Edit the $form->{type}") . qq|<br>
1082 <input class=submit type=submit name=action id=update_button value="|
1083     . $locale->text('Update') . qq|">
1084 <input class=submit type=submit name=action value="|
1085     . $locale->text('Ship to') . qq|">
1086 <input class=submit type=submit name=action value="|
1087     . $locale->text('Print') . qq|">
1088 <input class=submit type=submit name=action value="|
1089     . $locale->text('E-mail') . qq|">
1090 <input class=submit type=submit name=action value="|
1091     . $locale->text('Save') . qq|">
1092 <input class=submit type=submit name=action value="|
1093     . $locale->text('Save and Close') . qq|">
1094 |;
1095
1096   if (($form->{id})) {
1097     print qq|
1098         <input type="button" class="submit" onclick="set_history_window(|
1099         . Q($form->{id})
1100         . qq|);" name="history" id="history" value="|
1101         . $locale->text('history')
1102         . qq|">
1103
1104 <br>| . $locale->text("Workflow $form->{type}") . qq|<br>
1105 <input class=submit type=submit name=action value="|
1106       . $locale->text('Save as new') . qq|">
1107 <input class=submit type=submit name=action value="|
1108       . $locale->text('Delete') . qq|">|;
1109     if (($form->{type} =~ /sales_quotation$/)) {
1110       print qq|
1111 <input class=submit type=submit name=action value="|
1112         . $locale->text('Sales Order') . qq|">|;
1113     }
1114     if ($form->{type} =~ /request_quotation$/) {
1115       print qq|
1116 <input class=submit type=submit name=action value="|
1117         . $locale->text('Purchase Order') . qq|">|;
1118     }
1119     if (1) {
1120     print qq|
1121 <input class=submit type=submit name=action value="|
1122       . $locale->text('Invoice') . qq|">
1123 |;
1124 }
1125
1126     if ($form->{type} =~ /sales_order$/) {
1127       print qq|
1128 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1129 <input class=submit type=submit name=action value="|
1130         . $locale->text('Purchase Order') . qq|">
1131 <input class=submit type=submit name=action value="|
1132         . $locale->text('Quotation') . qq|">
1133 |;
1134
1135     } elsif ($form->{type} =~ /purchase_order$/) {
1136       print qq|
1137 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1138 <input class=submit type=submit name=action value="|
1139         . $locale->text('Sales Order') . qq|">
1140 <input class=submit type=submit name=action value="|
1141         . $locale->text('Request for Quotation') . qq|">
1142 |;
1143
1144     } else {
1145       print qq|
1146 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1147 <input class=submit type=submit name=action value="|
1148         . $locale->text('Order') . qq|">
1149 |;
1150     }
1151   } elsif ($form->{type} =~ /sales_order$/ && $form->{rowcount} && !$form->{proforma}) {
1152     print qq|
1153 <br>Workflow  $form->{heading}<br>
1154 <input class=submit type=submit name=action value="|
1155       . $locale->text('Save as new') . qq|">
1156 <input class=submit type=submit name=action value="|
1157       . $locale->text('Invoice') . qq|">
1158 |;
1159   }
1160
1161   $form->hide_form("saved_xyznumber");
1162
1163   print qq|
1164
1165 <input type=hidden name=rowcount value=$form->{rowcount}>
1166
1167 <input name=callback type=hidden value="$form->{callback}">
1168
1169 <input type=hidden name=login value=$form->{login}>
1170 <input type=hidden name=password value=$form->{password}>
1171
1172 </form>
1173
1174 </body>
1175 </html>
1176 |;
1177   $lxdebug->leave_sub();
1178 }
1179
1180 sub update {
1181   $lxdebug->enter_sub();
1182
1183   set_headings($form->{"id"} ? "edit" : "add");
1184
1185   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1186     qw(exchangerate creditlimit creditremaining);
1187   $form->{update} = 1;
1188
1189   &check_name($form->{vc});
1190
1191   $buysell              = 'buy';
1192   $buysell              = 'sell' if ($form->{vc} eq 'vendor');
1193   $form->{exchangerate} = $exchangerate
1194     if (
1195         $form->{forex} = (
1196                   $exchangerate =
1197                     $form->check_exchangerate(
1198                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1199                     )));
1200
1201   # for pricegroups
1202   $i = $form->{rowcount};
1203
1204   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
1205
1206   if (   ($form->{"partnumber_$i"} eq "")
1207       && ($form->{"description_$i"} eq "")
1208       && ($form->{"partsgroup_$i"}  eq "")) {
1209
1210     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
1211     &check_form;
1212
1213   } else {
1214
1215     if (   $form->{type} eq 'purchase_order'
1216         || $form->{type} eq 'request_quotation') {
1217       IR->retrieve_item(\%myconfig, \%$form);
1218     }
1219     if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
1220       IS->retrieve_item(\%myconfig, \%$form);
1221     }
1222
1223     my $rows = scalar @{ $form->{item_list} };
1224
1225     $form->{"discount_$i"} =
1226       $form->format_amount(\%myconfig, $form->{discount} * 100);
1227
1228     if ($rows) {
1229       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1230
1231       if ($rows > 1) {
1232
1233         &select_item;
1234         exit;
1235
1236       } else {
1237
1238         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1239         if ($form->{"not_discountable_$i"}) {
1240           $form->{"discount_$i"} = 0;
1241         }
1242         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1243           qw(partnumber description unit);
1244         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1245           keys %{ $form->{item_list}[0] };
1246         if ($form->{"part_payment_id_$i"} ne "") {
1247           $form->{payment_id} = $form->{"part_payment_id_$i"};
1248         }
1249
1250         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1251
1252         ($dec) = ($s =~ /\.(\d+)/);
1253         $dec           = length $dec;
1254         $decimalplaces = ($dec > 2) ? $dec : 2;
1255
1256         if ($sellprice) {
1257           $form->{"sellprice_$i"} = $sellprice;
1258         } else {
1259
1260           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1261
1262           # if there is an exchange rate adjust sellprice
1263           $form->{"sellprice_$i"} /= $exchangerate;
1264         }
1265
1266         $amount =
1267           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1268           (1 - $form->{"discount_$i"} / 100);
1269         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1270         map { $form->{"${_}_base"} += $amount }
1271           (split / /, $form->{"taxaccounts_$i"});
1272         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1273           split / /, $form->{taxaccounts}
1274           if !$form->{taxincluded};
1275
1276         $form->{creditremaining} -= $amount;
1277
1278         $form->{"sellprice_$i"} =
1279           $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1280                                $decimalplaces);
1281         $form->{"qty_$i"} =
1282           $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1283
1284         # get pricegroups for parts
1285         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1286
1287         # build up html code for prices_$i
1288         &set_pricegroup($i);
1289       }
1290
1291       &display_form;
1292
1293     } else {
1294
1295       # ok, so this is a new part
1296       # ask if it is a part or service item
1297
1298       if (   $form->{"partsgroup_$i"}
1299           && ($form->{"partsnumber_$i"} eq "")
1300           && ($form->{"description_$i"} eq "")) {
1301         $form->{rowcount}--;
1302         $form->{"discount_$i"} = "";
1303         &display_form;
1304       } else {
1305
1306         $form->{"id_$i"}   = 0;
1307         $form->{"unit_$i"} = $locale->text('ea');
1308
1309         &new_item;
1310
1311       }
1312     }
1313   }
1314
1315   $lxdebug->leave_sub();
1316 }
1317
1318 sub search {
1319   $lxdebug->enter_sub();
1320
1321   if ($form->{type} eq 'purchase_order') {
1322     $form->{title} = $locale->text('Purchase Orders');
1323     $form->{vc}    = 'vendor';
1324     $ordlabel      = $locale->text('Order Number');
1325     $ordnumber     = 'ordnumber';
1326     $employee      = $locale->text('Employee');
1327   }
1328
1329   if ($form->{type} eq 'request_quotation') {
1330     $form->{title} = $locale->text('Request for Quotations');
1331     $form->{vc}    = 'vendor';
1332     $ordlabel      = $locale->text('RFQ Number');
1333     $ordnumber     = 'quonumber';
1334     $employee      = $locale->text('Employee');
1335   }
1336
1337   if ($form->{type} eq 'sales_order') {
1338     $form->{title} = $locale->text('Sales Orders');
1339     $form->{vc}    = 'customer';
1340     $ordlabel      = $locale->text('Order Number');
1341     $ordnumber     = 'ordnumber';
1342     $employee      = $locale->text('Employee');
1343   }
1344
1345   if ($form->{type} eq 'sales_quotation') {
1346     $form->{title} = $locale->text('Quotations');
1347     $form->{vc}    = 'customer';
1348     $ordlabel      = $locale->text('Quotation Number');
1349     $ordnumber     = 'quonumber';
1350     $employee      = $locale->text('Employee');
1351   }
1352
1353   # setup vendor / customer selection
1354   $form->all_vc(\%myconfig, $form->{vc},
1355                 ($form->{vc} eq 'customer') ? "AR" : "AP");
1356
1357   map { $vc .= "<option>$_->{name}--$_->{id}</option>\n" }
1358     @{ $form->{"all_$form->{vc}"} };
1359
1360   $vclabel = ucfirst $form->{vc};
1361   $vclabel = $locale->text($vclabel);
1362
1363   # $locale->text('Vendor')
1364   # $locale->text('Customer')
1365
1366   $vc =
1367     ($vc)
1368     ? qq|<select name=$form->{vc}><option>\n$vc</option></select>|
1369     : qq|<input name=$form->{vc} size=35>|;
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   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1432                                    "all" => 1 });
1433
1434   my %labels = ();
1435   my @values = ("");
1436   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
1437     push(@values, $item->{"id"});
1438     $labels{$item->{"id"}} = $item->{"projectnumber"};
1439   }
1440   my $projectnumber =
1441     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
1442                          '-labels' => \%labels));
1443
1444   $form->header;
1445
1446   print qq|
1447 <body>
1448
1449 <form method=post action=$form->{script}>
1450
1451 <table width=100%>
1452   <tr>
1453     <th class=listtop>$form->{title}</th>
1454   </tr>
1455   <tr height="5"></tr>
1456   <tr>
1457     <td>
1458       <table>
1459         <tr>
1460           <th align=right>$vclabel</th>
1461           <td colspan="3">$vc</td>
1462         </tr>
1463         $department
1464         <tr>
1465           <th align=right>$ordlabel</th>
1466           <td colspan=3><input name="$ordnumber" size=20></td>
1467         </tr>
1468         <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> $vclabel</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   $msg = ucfirst $form->{vc};
1932   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1933
1934   # $locale->text('Customer missing!');
1935   # $locale->text('Vendor missing!');
1936
1937   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1938     if ($form->{currency} ne $form->{defaultcurrency});
1939
1940   &validate_items;
1941
1942   # if the name changed get new values
1943   if (&check_name($form->{vc})) {
1944     &update;
1945     exit;
1946   }
1947
1948   $form->{id} = 0 if $form->{saveasnew};
1949
1950   # this is for the internal notes section for the [email] Subject
1951   if ($form->{type} =~ /_order$/) {
1952     if ($form->{type} eq 'sales_order') {
1953       $form->{label} = $locale->text('Sales Order');
1954
1955       $numberfld = "sonumber";
1956       $ordnumber = "ordnumber";
1957     } else {
1958       $form->{label} = $locale->text('Purchase Order');
1959
1960       $numberfld = "ponumber";
1961       $ordnumber = "ordnumber";
1962     }
1963
1964     $err = $locale->text('Cannot save order!');
1965
1966   } else {
1967     if ($form->{type} eq 'sales_quotation') {
1968       $form->{label} = $locale->text('Quotation');
1969
1970       $numberfld = "sqnumber";
1971       $ordnumber = "quonumber";
1972     } else {
1973       $form->{label} = $locale->text('Request for Quotation');
1974
1975       $numberfld = "rfqnumber";
1976       $ordnumber = "quonumber";
1977     }
1978
1979     $err = $locale->text('Cannot save quotation!');
1980
1981   }
1982
1983   # get new number in sequence if no number is given or if saveasnew was requested
1984   if (!$form->{$ordnumber} || $form->{saveasnew}) {
1985     $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1986   }
1987
1988   relink_accounts();
1989
1990   $form->error($err) if (!OE->save(\%myconfig, \%$form));
1991
1992   # saving the history
1993   if(!exists $form->{addition}) {
1994     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1995         $form->{addition} = "SAVED";
1996         $form->save_history($form->dbconnect(\%myconfig));
1997   }
1998   # /saving the history
1999
2000   $form->redirect($form->{label} . " $form->{$ordnumber} " .
2001                   $locale->text('saved!'));
2002
2003   $lxdebug->leave_sub();
2004 }
2005
2006 sub save {
2007   $lxdebug->enter_sub();
2008
2009   if ($form->{type} =~ /_order$/) {
2010     $form->isblank("transdate", $locale->text('Order Date missing!'));
2011   } else {
2012     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
2013   }
2014
2015   $msg = ucfirst $form->{vc};
2016   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
2017
2018   # $locale->text('Customer missing!');
2019   # $locale->text('Vendor missing!');
2020
2021   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
2022     if ($form->{currency} ne $form->{defaultcurrency});
2023
2024   &validate_items;
2025
2026   # if the name changed get new values
2027   if (&check_name($form->{vc})) {
2028     &update;
2029     exit;
2030   }
2031
2032   $form->{id} = 0 if $form->{saveasnew};
2033
2034   # this is for the internal notes section for the [email] Subject
2035   if ($form->{type} =~ /_order$/) {
2036     if ($form->{type} eq 'sales_order') {
2037       $form->{label} = $locale->text('Sales Order');
2038
2039       $numberfld = "sonumber";
2040       $ordnumber = "ordnumber";
2041     } else {
2042       $form->{label} = $locale->text('Purchase Order');
2043
2044       $numberfld = "ponumber";
2045       $ordnumber = "ordnumber";
2046     }
2047
2048     $err = $locale->text('Cannot save order!');
2049
2050   } else {
2051     if ($form->{type} eq 'sales_quotation') {
2052       $form->{label} = $locale->text('Quotation');
2053
2054       $numberfld = "sqnumber";
2055       $ordnumber = "quonumber";
2056     } else {
2057       $form->{label} = $locale->text('Request for Quotation');
2058
2059       $numberfld = "rfqnumber";
2060       $ordnumber = "quonumber";
2061     }
2062
2063     $err = $locale->text('Cannot save quotation!');
2064
2065   }
2066
2067   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
2068     unless $form->{$ordnumber};
2069
2070   relink_accounts();
2071
2072   OE->save(\%myconfig, \%$form);
2073
2074   # saving the history
2075   if(!exists $form->{addition}) {
2076     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
2077         $form->{addition} = "SAVED";
2078         $form->save_history($form->dbconnect(\%myconfig));
2079   }
2080   # /saving the history 
2081
2082   $form->{simple_save} = 1;
2083   if(!$form->{print_and_save}) {
2084     set_headings("edit");
2085     &update;
2086     exit;
2087   }
2088   $lxdebug->leave_sub();
2089 }
2090
2091 sub delete {
2092   $lxdebug->enter_sub();
2093
2094   $form->header;
2095
2096   if ($form->{type} =~ /_order$/) {
2097     $msg       = $locale->text('Are you sure you want to delete Order Number');
2098     $ordnumber = 'ordnumber';
2099   } else {
2100     $msg = $locale->text('Are you sure you want to delete Quotation Number');
2101     $ordnumber = 'quonumber';
2102   }
2103
2104   print qq|
2105 <body>
2106
2107 <form method=post action=$form->{script}>
2108 |;
2109
2110   # delete action variable
2111   map { delete $form->{$_} } qw(action header);
2112
2113   foreach $key (keys %$form) {
2114     $form->{$key} =~ s/\"/&quot;/g;
2115     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2116   }
2117
2118   print qq|
2119 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
2120
2121 <h4>$msg $form->{$ordnumber}</h4>
2122 <p>
2123 <input name=action class=submit type=submit value="|
2124     . $locale->text('Yes') . qq|">
2125 </form>
2126
2127 </body>
2128 </html>
2129 |;
2130
2131   $lxdebug->leave_sub();
2132 }
2133
2134 sub yes {
2135   $lxdebug->enter_sub();
2136
2137   if ($form->{type} =~ /_order$/) {
2138     $msg = $locale->text('Order deleted!');
2139     $err = $locale->text('Cannot delete order!');
2140   } else {
2141     $msg = $locale->text('Quotation deleted!');
2142     $err = $locale->text('Cannot delete quotation!');
2143   }
2144   if (OE->delete(\%myconfig, \%$form, $spool)){
2145     $form->redirect($msg);
2146     # saving the history
2147     if(!exists $form->{addition}) {
2148       $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
2149           $form->{addition} = "DELETED";
2150           $form->save_history($form->dbconnect(\%myconfig));
2151     }
2152     # /saving the history 
2153   }
2154   $form->error($err);
2155
2156   $lxdebug->leave_sub();
2157 }
2158
2159 sub invoice {
2160   $lxdebug->enter_sub();
2161
2162   if ($form->{type} =~ /_order$/) {
2163
2164     # these checks only apply if the items don't bring their own ordnumbers/transdates.
2165     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
2166     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
2167       if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
2168     $form->isblank("transdate", $locale->text('Order Date missing!'))
2169       if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
2170
2171     # also copy deliverydate from the order
2172     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
2173     $form->{orddate} = $form->{transdate};
2174   } else {
2175     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
2176     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
2177     $form->{ordnumber} = "";
2178     $form->{quodate} = $form->{transdate};
2179   }
2180
2181   # if the name changed get new values
2182   if (&check_name($form->{vc})) {
2183     &update;
2184     exit;
2185   }
2186
2187   $form->{cp_id} *= 1;
2188
2189   for $i (1 .. $form->{rowcount}) {
2190     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
2191                                                      $form->{"${_}_${i}"})
2192             if ($form->{"${_}_${i}"}) }
2193         qw(ship qty sellprice listprice basefactor));
2194   }
2195
2196   if (   $form->{type} =~ /_order/
2197       && $form->{currency} ne $form->{defaultcurrency}) {
2198
2199     # check if we need a new exchangerate
2200     $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
2201
2202     $orddate      = $form->current_date(\%myconfig);
2203     $exchangerate =
2204       $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate,
2205                                 $buysell);
2206
2207     if (!$exchangerate) {
2208       &backorder_exchangerate($orddate, $buysell);
2209       exit;
2210     }
2211   }
2212
2213   # close orders/quotations
2214   $form->{closed} = 1;
2215
2216   # save order if one ordnumber has been given
2217   # if not it's most likely a collective order, which can't be saved back
2218   # so they just have to be closed
2219   if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
2220     OE->close_order(\%myconfig, \%$form);
2221   } else {
2222     OE->close_orders(\%myconfig, \%$form);
2223   }
2224
2225   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
2226   $form->{duedate} =
2227     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
2228
2229   $form->{id}     = '';
2230   $form->{closed} = 0;
2231   $form->{rowcount}--;
2232   $form->{shipto} = 1;
2233
2234   if ($form->{type} =~ /_order$/) {
2235     $form->{exchangerate} = $exchangerate;
2236     &create_backorder;
2237   }
2238
2239   if (   $form->{type} eq 'purchase_order'
2240       || $form->{type} eq 'request_quotation') {
2241     $form->{title}  = $locale->text('Add Vendor Invoice');
2242     $form->{script} = 'ir.pl';
2243     $script         = "ir";
2244     $buysell        = 'sell';
2245   }
2246   if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
2247     $form->{title}  = $locale->text('Add Sales Invoice');
2248     $form->{script} = 'is.pl';
2249     $script         = "is";
2250     $buysell        = 'buy';
2251   }
2252
2253   # bo creates the id, reset it
2254   map { delete $form->{$_} }
2255     qw(id subject message cc bcc printed emailed queued);
2256   $form->{ $form->{vc} } =~ s/--.*//g;
2257   $form->{type} = "invoice";
2258
2259   # locale messages
2260   $locale = new Locale "$myconfig{countrycode}", "$script";
2261
2262   require "bin/mozilla/$form->{script}";
2263
2264   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
2265
2266   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
2267     qw(creditlimit creditremaining);
2268
2269   $currency = $form->{currency};
2270   &invoice_links;
2271
2272   $form->{currency}     = $currency;
2273   $form->{exchangerate} = "";
2274   $form->{forex}        = "";
2275   $form->{exchangerate} = $exchangerate
2276     if (
2277         $form->{forex} = (
2278                     $exchangerate =
2279                       $form->check_exchangerate(
2280                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
2281                       )));
2282
2283   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
2284
2285   &prepare_invoice;
2286
2287   # format amounts
2288   for $i (1 .. $form->{rowcount}) {
2289     $form->{"discount_$i"} =
2290       $form->format_amount(\%myconfig, $form->{"discount_$i"});
2291
2292     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
2293     $dec           = length $dec;
2294     $decimalplaces = ($dec > 2) ? $dec : 2;
2295
2296     # copy delivery date from reqdate for order -> invoice conversion
2297     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
2298       unless $form->{"deliverydate_$i"};
2299
2300     $form->{"sellprice_$i"} =
2301       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
2302                            $decimalplaces);
2303
2304     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
2305     $dec_qty = length $dec_qty;
2306     $form->{"qty_$i"} =
2307       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
2308
2309     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2310       qw(partnumber description unit);
2311
2312   }
2313
2314   &display_form;
2315
2316   $lxdebug->leave_sub();
2317 }
2318
2319 sub backorder_exchangerate {
2320   $lxdebug->enter_sub();
2321   my ($orddate, $buysell) = @_;
2322
2323   $form->header;
2324
2325   print qq|
2326 <body>
2327
2328 <form method=post action=$form->{script}>
2329 |;
2330
2331   # delete action variable
2332   map { delete $form->{$_} } qw(action header exchangerate);
2333
2334   foreach $key (keys %$form) {
2335     $form->{$key} =~ s/\"/&quot;/g;
2336     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2337   }
2338
2339   $form->{title} = $locale->text('Add Exchangerate');
2340
2341   print qq|
2342
2343 <input type=hidden name=login value=$form->{login}>
2344 <input type=hidden name=password value=$form->{password}>
2345
2346 <input type=hidden name=exchangeratedate value=$orddate>
2347 <input type=hidden name=buysell value=$buysell>
2348
2349 <table width=100%>
2350   <tr><th class=listtop>$form->{title}</th></tr>
2351   <tr height="5"></tr>
2352   <tr>
2353     <td>
2354       <table>
2355         <tr>
2356           <th align=right>| . $locale->text('Currency') . qq|</th>
2357           <td>$form->{currency}</td>
2358         </tr>
2359         <tr>
2360           <th align=right>| . $locale->text('Date') . qq|</th>
2361           <td>$orddate</td>
2362         </tr>
2363         <tr>
2364           <th align=right>| . $locale->text('Exchangerate') . qq|</th>
2365           <td><input name=exchangerate size=11></td>
2366         </tr>
2367       </table>
2368     </td>
2369   </tr>
2370 </table>
2371
2372 <hr size=3 noshade>
2373
2374 <br>
2375 <input type=hidden name=nextsub value=save_exchangerate>
2376
2377 <input name=action class=submit type=submit value="|
2378     . $locale->text('Continue') . qq|">
2379
2380 </form>
2381
2382 </body>
2383 </html>
2384 |;
2385
2386   $lxdebug->leave_sub();
2387 }
2388
2389 sub save_exchangerate {
2390   $lxdebug->enter_sub();
2391
2392   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
2393   $form->{exchangerate} =
2394     $form->parse_amount(\%myconfig, $form->{exchangerate});
2395   $form->save_exchangerate(\%myconfig, $form->{currency},
2396                            $form->{exchangeratedate},
2397                            $form->{exchangerate}, $form->{buysell});
2398
2399   &invoice;
2400
2401   $lxdebug->leave_sub();
2402 }
2403
2404 sub create_backorder {
2405   $lxdebug->enter_sub();
2406
2407   $form->{shipped} = 1;
2408
2409   # figure out if we need to create a backorder
2410   # items aren't saved if qty != 0
2411
2412   for $i (1 .. $form->{rowcount}) {
2413     $totalqty  += $qty  = $form->{"qty_$i"};
2414     $totalship += $ship = $form->{"ship_$i"};
2415
2416     $form->{"qty_$i"} = $qty - $ship;
2417   }
2418
2419   if ($totalship == 0) {
2420     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
2421     $form->{ordtotal} = 0;
2422     $form->{shipped}  = 0;
2423     return;
2424   }
2425
2426   if ($totalqty == $totalship) {
2427     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
2428     $form->{ordtotal} = 0;
2429     return;
2430   }
2431
2432   @flds = (
2433     qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
2434   );
2435
2436   for $i (1 .. $form->{rowcount}) {
2437     map {
2438       $form->{"${_}_$i"} =
2439         $form->format_amount(\%myconfig, $form->{"${_}_$i"})
2440     } qw(sellprice discount);
2441   }
2442
2443   relink_accounts();
2444
2445   OE->save(\%myconfig, \%$form);
2446
2447   # rebuild rows for invoice
2448   @a     = ();
2449   $count = 0;
2450
2451   for $i (1 .. $form->{rowcount}) {
2452     $form->{"qty_$i"} = $form->{"ship_$i"};
2453
2454     if ($form->{"qty_$i"}) {
2455       push @a, {};
2456       $j = $#a;
2457       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2458       $count++;
2459     }
2460   }
2461
2462   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2463   $form->{rowcount} = $count;
2464
2465   $lxdebug->leave_sub();
2466 }
2467
2468 sub save_as_new {
2469   $lxdebug->enter_sub();
2470
2471   $form->{saveasnew} = 1;
2472   $form->{closed}    = 0;
2473   map { delete $form->{$_} } qw(printed emailed queued);
2474
2475   # Let Lx-Office assign a new order number if the user hasn't changed the
2476   # previous one. If it has been changed manually then use it as-is.
2477   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
2478   if ($form->{saved_xyznumber} &&
2479       ($form->{saved_xyznumber} eq $form->{$idx})) {
2480     delete($form->{$idx});
2481   }
2482
2483   &save;
2484
2485   $lxdebug->leave_sub();
2486 }
2487
2488 sub purchase_order {
2489   $lxdebug->enter_sub();
2490
2491   if (   $form->{type} eq 'sales_quotation'
2492       || $form->{type} eq 'request_quotation') {
2493     OE->close_order(\%myconfig, \%$form);
2494   }
2495
2496   if ($form->{type} =~ /^sales_/) {
2497     delete($form->{ordnumber});
2498   }
2499
2500   $form->{cp_id} *= 1;
2501
2502   $form->{title} = $locale->text('Add Purchase Order');
2503   $form->{vc}    = "vendor";
2504   $form->{type}  = "purchase_order";
2505
2506   &poso;
2507
2508   $lxdebug->leave_sub();
2509 }
2510
2511 sub sales_order {
2512   $lxdebug->enter_sub();
2513
2514   if (   $form->{type} eq 'sales_quotation'
2515       || $form->{type} eq 'request_quotation') {
2516     OE->close_order(\%myconfig, $form);
2517   }
2518
2519   if ($form->{type} eq "purchase_order") {
2520     delete($form->{ordnumber});
2521   }
2522
2523   $form->{cp_id} *= 1;
2524
2525   $form->{title} = $locale->text('Add Sales Order');
2526   $form->{vc}    = "customer";
2527   $form->{type}  = "sales_order";
2528
2529   &poso;
2530
2531   $lxdebug->leave_sub();
2532 }
2533
2534 sub poso {
2535   $lxdebug->enter_sub();
2536
2537   $form->{transdate} = $form->current_date(\%myconfig);
2538   delete $form->{duedate};
2539
2540   $form->{closed} = 0;
2541
2542   # reset
2543   map { delete $form->{$_} }
2544     qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
2545
2546   for $i (1 .. $form->{rowcount}) {
2547     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
2548                                                      $form->{"${_}_${i}"})
2549             if ($form->{"${_}_${i}"}) }
2550         qw(ship qty sellprice listprice basefactor));
2551   }
2552
2553   &order_links;
2554
2555   &prepare_order;
2556
2557   # format amounts
2558   for $i (1 .. $form->{rowcount} - 1) {
2559     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2560       qw(partnumber description unit);
2561   }
2562
2563   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 0, "0") }
2564     qw(creditlimit creditremaining);
2565
2566   &update;
2567
2568   $lxdebug->leave_sub();
2569 }
2570
2571 sub e_mail {
2572   $lxdebug->enter_sub();
2573
2574   $form->{print_and_save} = 1;
2575
2576   if (!$form->{id}) {
2577     $print_post = 1;
2578
2579     my $saved_form = save_form();
2580
2581     save();
2582
2583     my %saved_vars;
2584     map({ $saved_vars{$_} = $form->{$_}; } qw(id ordnumber quonumber));
2585     restore_form($saved_form);
2586     map({ $form->{$_} = $saved_vars{$_}; } qw(id ordnumber quonumber));
2587   }
2588
2589   edit_e_mail();
2590
2591   $lxdebug->leave_sub();
2592 }
2593