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