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