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