Bei Auftragsbestätigungen und Lieferantenaufträgen ein Feld "Geliefert" eingeführt...
[kivitendo-erp.git] / bin / mozilla / oe.pl
1 # #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger, Accounting
9 # Copyright (c) 1998-2003
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Order entry module
31 # Quotation module
32 #======================================================================
33 use Data::Dumper;
34
35 use SL::OE;
36 use SL::IR;
37 use SL::IS;
38 use SL::PE;
39
40 require "$form->{path}/io.pl";
41 require "$form->{path}/arap.pl";
42
43 1;
44
45 # end of main
46
47 # For locales.pl:
48 # $locale->text('Edit the purchase_order');
49 # $locale->text('Edit the sales_order');
50 # $locale->text('Edit the request_quotation');
51 # $locale->text('Edit the sales_quotation');
52
53 # $locale->text('Workflow purchase_order');
54 # $locale->text('Workflow sales_order');
55 # $locale->text('Workflow request_quotation');
56 # $locale->text('Workflow sales_quotation');
57
58 sub set_headings {
59   $lxdebug->enter_sub();
60
61   my ($action) = @_;
62
63   if ($form->{type} eq 'purchase_order') {
64     $form->{title}   = $action eq "edit" ?
65       $locale->text('Edit Purchase Order') :
66       $locale->text('Add Purchase Order');
67     $form->{heading} = $locale->text('Purchase Order');
68     $form->{vc}      = 'vendor';
69   }
70   if ($form->{type} eq 'sales_order') {
71     $form->{title}   = $action eq "edit" ?
72       $locale->text('Edit Sales Order') :
73       $locale->text('Add Sales Order');
74     $form->{heading} = $locale->text('Sales Order');
75     $form->{vc}      = 'customer';
76   }
77   if ($form->{type} eq 'request_quotation') {
78     $form->{title}   = $action eq "edit" ?
79       $locale->text('Edit Request for Quotation') :
80       $locale->text('Add Request for Quotation');
81     $form->{heading} = $locale->text('Request for Quotation');
82     $form->{vc}      = 'vendor';
83   }
84   if ($form->{type} eq 'sales_quotation') {
85     $form->{title}   = $action eq "edit" ?
86       $locale->text('Edit Quotation') :
87       $locale->text('Add Quotation');
88     $form->{heading} = $locale->text('Quotation');
89     $form->{vc}      = 'customer';
90   }
91
92   $lxdebug->leave_sub();
93 }
94
95 sub add {
96   $lxdebug->enter_sub();
97
98   set_headings("add");
99
100   $form->{callback} =
101     "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}&login=$form->{login}&path=$form->{path}&password=$form->{password}"
102     unless $form->{callback};
103
104   &order_links;
105   &prepare_order;
106   &display_form;
107
108   $lxdebug->leave_sub();
109 }
110
111 sub edit {
112   $lxdebug->enter_sub();
113
114   $form->{simple_save} = 0;
115
116   set_headings("edit");
117
118   # editing without stuff to edit? try adding it first
119   if ($form->{rowcount}) {
120     map { $id++ if $form->{"id_$_"} } (1 .. $form->{rowcount});
121     if (!$id) {
122
123       # reset rowcount
124       undef $form->{rowcount};
125       &add;
126       return;
127     }
128   } else {
129     if (!$form->{id}) {
130       &add;
131       return;
132     }
133   }
134
135   if ($form->{print_and_save}) {
136     $form->{action}   = "print";
137     $form->{resubmit} = 1;
138     $language_id = $form->{language_id};
139     $printer_id = $form->{printer_id};
140   }
141
142   set_headings("edit");
143
144   &order_links;
145   &prepare_order;
146   if ($form->{print_and_save}) {
147     $form->{language_id} = $language_id;
148     $form->{printer_id} = $printer_id;
149   }
150   &display_form;
151
152   $lxdebug->leave_sub();
153 }
154
155 sub order_links {
156   $lxdebug->enter_sub();
157
158   # get customer/vendor
159   $form->all_vc(\%myconfig, $form->{vc},
160                 ($form->{vc} eq 'customer') ? "AR" : "AP");
161
162   # retrieve order/quotation
163   $form->{webdav} = $webdav;
164   # set jscalendar
165   $form->{jscalendar} = $jscalendar;
166
167   OE->retrieve(\%myconfig, \%$form);
168
169   if ($form->{payment_id}) {
170     $payment_id = $form->{payment_id};
171   }
172   if ($form->{language_id}) {
173     $language_id = $form->{language_id};
174   }
175   if ($form->{taxzone_id}) {
176     $taxzone_id = $form->{taxzone_id};
177   }
178
179
180   # if multiple rowcounts (== collective order) then check if the
181   # there were more than one customer (in that case OE::retrieve removes
182   # the content from the field)
183   if (   $form->{rowcount}
184       && $form->{type} eq 'sales_order'
185       && defined $form->{customer}
186       && $form->{customer} eq '') {
187
188     #    $main::lxdebug->message(0, "Detected Edit order with concurrent customers");
189     $form->error(
190                  $locale->text(
191                    'Collective Orders only work for orders from one customer!')
192     );
193   }
194
195   $taxincluded = $form->{taxincluded};
196   $form->{shipto} = 1 if $form->{id};
197
198   if ($form->{"all_$form->{vc}"}) {
199     unless ($form->{"$form->{vc}_id"}) {
200       $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
201     }
202   }
203
204   $cp_id    = $form->{cp_id};
205   $intnotes = $form->{intnotes};
206
207   # get customer / vendor
208   if ($form->{type} =~ /(purchase_order|request_quotation|receive_order)/) {
209     IR->get_vendor(\%myconfig, \%$form);
210
211     #quote all_vendor Bug 133
212     foreach $ref (@{ $form->{all_vendor} }) {
213       $ref->{name} = $form->quote($ref->{name});
214     }
215
216   }
217   if ($form->{type} =~ /(sales|ship)_(order|quotation)/) {
218     IS->get_customer(\%myconfig, \%$form);
219
220     #quote all_vendor Bug 133
221     foreach $ref (@{ $form->{all_customer} }) {
222       $ref->{name} = $form->quote($ref->{name});
223     }
224
225   }
226   $form->{cp_id} = $cp_id;
227   if ($payment_id) {
228     $form->{payment_id} = $payment_id;
229   }
230   if ($language_id) {
231     $form->{language_id} = $language_id;
232   }
233   if ($taxzone_id) {
234     $form->{taxzone_id} = $taxzone_id;
235   }
236   $form->{intnotes} = $intnotes;
237   ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
238   $form->{"old$form->{vc}"} =
239     qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
240
241   # build the popup menus
242   if (@{ $form->{"all_$form->{vc}"} }) {
243     $form->{ $form->{vc} } =
244       qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
245     map { $form->{"select$form->{vc}"} .= "<option>$_->{name}--$_->{id}\n" }
246       (@{ $form->{"all_$form->{vc}"} });
247   }
248
249   # currencies
250   @curr = split(/:/, $form->{currencies});
251   chomp $curr[0];
252   $form->{defaultcurrency} = $curr[0];
253   $form->{currency}        = $form->{defaultcurrency} unless $form->{currency};
254
255   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
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}\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}\n" }
276       (@{ $form->{all_employees} });
277   }
278
279   # forex
280   $form->{forex} = $form->{exchangerate};
281
282   $lxdebug->leave_sub();
283 }
284
285 sub prepare_order {
286   $lxdebug->enter_sub();
287   $form->{media}    = "screen";
288   $form->{formname} = $form->{type} unless $form->{formname};
289
290   my $i = 0;
291   foreach $ref (@{ $form->{form_details} }) {
292     $form->{rowcount} = ++$i;
293
294     map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
295   }
296   for my $i (1 .. $form->{rowcount}) {
297     if ($form->{id}) {
298       $form->{"discount_$i"} =
299         $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
300     } else {
301       $form->{"discount_$i"} =
302         $form->format_amount(\%myconfig, $form->{"discount_$i"});
303     }
304     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
305     $dec           = length $dec;
306     $decimalplaces = ($dec > 2) ? $dec : 2;
307
308     # copy reqdate from deliverydate for invoice -> order conversion
309     $form->{"reqdate_$i"} = $form->{"deliverydate_$i"}
310       unless $form->{"reqdate_$i"};
311
312     $form->{"sellprice_$i"} =
313       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
314                            $decimalplaces);
315
316     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
317     $dec_qty = length $dec_qty;
318     $form->{"qty_$i"} =
319       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
320
321     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
322       qw(partnumber description unit);
323   }
324
325   $lxdebug->leave_sub();
326 }
327
328 sub form_header {
329   $lxdebug->enter_sub();
330
331   my $checkedclosed = $form->{"closed"} ? "checked" : "";
332   my $checkeddelivered = $form->{"delivered"} ? "checked" : "";
333
334   map { $form->{$_} =~ s/\"/&quot;/g }
335     qw(ordnumber quonumber shippingpoint shipvia notes intnotes shiptoname
336        shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
337        shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
338
339   # use JavaScript Calendar or not
340   $form->{jsscript} = $form->{jscalendar};
341   $jsscript = "";
342
343   $payment = qq|<option value=""></option>|;
344   foreach $item (@{ $form->{payment_terms} }) {
345     if ($form->{payment_id} eq $item->{id}) {
346       $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
347     } else {
348       $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
349     }
350   }
351   if ($form->{jsscript}) {
352
353     # with JavaScript Calendar
354     $button1 = qq|
355        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>
356        <td><input type=button name=transdate id="trigger1" value=|
357       . $locale->text('button') . qq|></td>
358       |;
359     $button2 = qq|
360        <td width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value=$form->{reqdate}></td>
361        <td width="4"><input type=button name=reqdate name=reqdate id="trigger2" value=|
362       . $locale->text('button') . qq|></td>
363      |;
364
365     #write Trigger
366     $jsscript =
367       Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
368                           "reqdate", "BL", "trigger2");
369
370   } else {
371
372     # without JavaScript Calendar
373     $button1 = qq|
374                               <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>|;
375     $button2 = qq|
376                               <td width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value=$form->{reqdate}></td>|;
377   }
378
379   if ($form->{id}) {
380     $openclosed = qq|
381       <tr>
382         <td colspan=2 align=center>
383           <input name="closed" id="closed" type="checkbox" class="checkbox" value="1" $checkedclosed>
384           <label for="closed">| . $locale->text('Closed') . qq|</label>
385 |;
386
387     if (($form->{"type"} eq "sales_order") ||
388         ($form->{"type"} eq "purchase_order")) {
389       $openclosed .= qq|
390           <input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" $checkeddelivered>
391           <label for="delivered">| . $locale->text('Delivered') . qq|</label>
392 |;
393     }
394
395     $openclosed .= qq|
396         </td>
397       </tr>
398 |;
399   }
400
401   # set option selected
402   foreach $item ($form->{vc}, currency, department, employee) {
403     $form->{"select$item"} =~ s/ selected//;
404     $form->{"select$item"} =~
405       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
406   }
407
408   #quote select[customer|vendor] Bug 133
409   $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
410
411   #build contacts
412   if ($form->{all_contacts}) {
413
414     $form->{selectcontact} = "<option></option>";
415     foreach $item (@{ $form->{all_contacts} }) {
416       my $department = ($item->{cp_abteilung}) ? "--$item->{cp_abteilung}" : "";
417       if ($form->{cp_id} == $item->{cp_id}) {
418         $form->{selectcontact} .=
419           "<option value=$item->{cp_id} selected>$item->{cp_name}$department</option>";
420       } else {
421         $form->{selectcontact} .= "<option value=$item->{cp_id}>$item->{cp_name}$department</option>";
422       }
423     }
424   } else {
425     $form->{selectcontact} =~ s/ selected//g;
426     if ($form->{cp_id} ne "") {
427       $form->{selectcontact} =~ s/value=$form->{cp_id}/value=$form->{cp_id} selected/;
428     }
429   }
430
431
432   if (@{ $form->{SHIPTO} }) {
433     $form->{selectshipto} = "<option value=0></option>";
434     foreach $item (@{ $form->{SHIPTO} }) {
435       if ($item->{shipto_id} == $form->{shipto_id}) {
436         $form->{selectshipto} .=
437           "<option value=$item->{shipto_id} selected>$item->{shiptoname} $item->{shiptodepartment_1}</option>";
438       } else {
439         $form->{selectshipto} .=
440           "<option value=$item->{shipto_id}>$item->{shiptoname} $item->{shiptodepartment}</option>";
441       }
442
443     }
444   } else {
445     $form->{selectshipto} = $form->unquote($form->{selectshipto});
446     $form->{selectshipto} =~ s/ selected//g;
447     if ($form->{shipto_id} ne "") {
448       $form->{selectshipto} =~ s/value=$form->{shipto_id}/value=$form->{shipto_id} selected/;
449     }
450   }
451
452   $shipto = qq|
453                 <th align=right>| . $locale->text('Shipping Address') . qq|</th>
454                 <td><select name=shipto_id style="width:200px;">$form->{selectshipto}</select></td>|;
455   $form->{selectshipto} = $form->quote($form->{selectshipto});
456   $shipto .= qq| <input type=hidden name=selectshipto value="$form->{selectshipto}">|;
457
458
459
460   $form->{exchangerate} =
461     $form->format_amount(\%myconfig, $form->{exchangerate});
462
463   if (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) {
464     $creditwarning = 1;
465   } else {
466     $creditwarning = 0;
467   }
468
469   $form->{creditlimit} =
470     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
471   $form->{creditremaining} =
472     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
473
474   $contact =
475     ($form->{selectcontact})
476     ? qq|<select name=cp_id>$form->{selectcontact}</select>\n<input type=hidden name="selectcontact" value="$form->{selectcontact}">|
477     : qq|<input name=contact value="$form->{contact}" size=35>|;
478
479   $exchangerate = qq|
480 <input type=hidden name=forex value=$form->{forex}>
481 |;
482
483   if ($form->{currency} ne $form->{defaultcurrency}) {
484     if ($form->{forex}) {
485       $exchangerate .=
486           qq|<th align=right>|
487         . $locale->text('Exchangerate')
488         . qq|</th><td>$form->{exchangerate}</td>
489       <input type=hidden name=exchangerate value=$form->{exchangerate}>
490 |;
491     } else {
492       $exchangerate .=
493           qq|<th align=right>|
494         . $locale->text('Exchangerate')
495         . qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
496     }
497   }
498
499   $vclabel = ucfirst $form->{vc};
500   $vclabel = $locale->text($vclabel);
501
502
503
504   if ($form->{business}) {
505     $business = qq|
506               <tr>
507                 <th align=right>| . $locale->text('Business') . qq|</th>
508                 <td>$form->{business}</td>
509                 <th align=right>| . $locale->text('Trade Discount') . qq|</th>
510                 <td>|
511       . $form->format_amount(\%myconfig, $form->{tradediscount} * 100)
512       . qq| %</td>
513               </tr>
514 |;
515   }
516
517   if ($form->{max_dunning_level}) {
518     $dunning = qq|
519               <tr>
520                 <td colspan=4>
521                 <table>
522                   <tr>
523                 <th align=right>| . $locale->text('Max. Dunning Level') . qq|:</th>
524                 <td><b>$form->{max_dunning_level}</b></td>
525                 <th align=right>| . $locale->text('Dunning Amount') . qq|:</th>
526                 <td><b>|
527       . $form->format_amount(\%myconfig, $form->{dunning_amount},2)
528       . qq|</b></td>
529               </tr>
530               </table>
531              </td>
532             </tr>
533 |;
534   }
535
536   if (@{ $form->{TAXZONE} }) {
537     $form->{selecttaxzone} = "";
538     foreach $item (@{ $form->{TAXZONE} }) {
539       if ($item->{id} == $form->{taxzone_id}) {
540         $form->{selecttaxzone} .=
541           "<option value=$item->{id} selected>" . H($item->{description}) .
542           "</option>";
543       } else {
544         $form->{selecttaxzone} .=
545           "<option value=$item->{id}>" . H($item->{description}) . "</option>";
546       }
547
548     }
549   } else {
550     $form->{selecttaxzone} =~ s/ selected//g;
551     if ($form->{taxzone_id} ne "") {
552       $form->{selecttaxzone} =~ s/value=$form->{taxzone_id}>/value=$form->{taxzone_id} selected>/;
553     }
554   }
555
556   $taxzone = qq|
557               <tr>
558                 <th align=right>| . $locale->text('Steuersatz') . qq|</th>
559                 <td><select name=taxzone_id>$form->{selecttaxzone}</select></td>
560                 <input type=hidden name=selecttaxzone value="$form->{selecttaxzone}">
561               </tr>|;
562
563
564   if ($form->{type} !~ /_quotation$/) {
565     $ordnumber = qq|
566               <tr>
567                 <th width=70% align=right nowrap>| . $locale->text('Order Number') . qq|</th>
568                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
569               </tr>
570               <tr>
571                 <th width=70% align=right nowrap>|
572       . $locale->text('Quotation Number') . qq|</th>
573                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
574               </tr>
575               <tr>
576                 <th width=70% align=right nowrap>|
577       . $locale->text('Customer Order Number') . qq|</th>
578                 <td><input name=cusordnumber size=11 value="$form->{cusordnumber}"></td>
579               </tr>
580               <tr>
581                 <th align=right nowrap>| . $locale->text('Order Date') . qq|</th>
582                 $button1
583
584               </tr>
585               <tr>
586                 <th align=right nowrap=true>| . $locale->text('Required by') . qq|</th>
587                 $button2
588               </tr>
589 |;
590
591     $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
592
593     $creditremaining = qq|
594               <tr>
595                 <td></td>
596                 <td colspan=3>
597                   <table>
598                     <tr>
599                       <th nowrap>| . $locale->text('Credit Limit') . qq|</th>
600                       <td>$form->{creditlimit}</td>
601                       <td width=20%></td>
602                       <th nowrap>| . $locale->text('Remaining') . qq|</th>
603                       <td class="plus$n" nowrap>$form->{creditremaining}</td>
604                     </tr>
605                   </table>
606                 </td>
607                 $shipto
608               </tr>
609 |;
610   } else {
611     $reqlabel =
612       ($form->{type} eq 'sales_quotation')
613       ? $locale->text('Valid until')
614       : $locale->text('Required by');
615     if ($form->{type} eq 'sales_quotation') {
616       $ordnumber = qq|
617               <tr>
618                 <th width=70% align=right nowrap>|
619         . $locale->text('Quotation Number') . qq|</th>
620                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
621                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
622               </tr>
623 |;
624     } else {
625       $ordnumber = qq|
626               <tr>
627                 <th width=70% align=right nowrap>| . $locale->text('RFQ Number') . qq|</th>
628                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
629                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
630               </tr>
631 |;
632
633     }
634
635     $ordnumber .= qq|
636               <tr>
637                 <th align=right nowrap>| . $locale->text('Quotation Date') . qq|</th>
638                 $button1
639               </tr>
640               <tr>
641                 <th align=right nowrap=true>$reqlabel</th>
642                 $button2
643               </tr>
644 |;
645     $creditremaining = qq| <tr>
646                             <td colspan=4></td>
647                             $shipto
648                           </tr>|;
649   }
650
651   $vc =
652     ($form->{"select$form->{vc}"})
653     ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}</select>\n<input type=hidden name="select$form->{vc}" value="$form->{"select$form->{vc}"}">|
654     : qq|<input name=$form->{vc} value="$form->{$form->{vc}}" size=35>|;
655
656   $department = qq|
657               <tr>
658                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
659                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
660                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
661                 </td>
662               </tr>
663 | if $form->{selectdepartment};
664
665   $employee = qq|
666               <input type=hidden name=employee value="$form->{employee}">
667 |;
668
669   if ($form->{type} eq 'sales_order') {
670     if ($form->{selectemployee}) {
671       $employee = qq|
672     <input type=hidden name=customer_klass value=$form->{customer_klass}>
673               <tr>
674                 <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
675                 <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
676                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
677                 <td></td>
678               </tr>
679 |;
680     }
681   } else {
682     $employee = qq|
683     <input type=hidden name=customer_klass value=$form->{customer_klass}>
684               <tr>
685                 <th align=right nowrap>| . $locale->text('Employee') . qq|</th>
686                 <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
687                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
688                 <td></td>
689               </tr>
690 |;
691   }
692   if ($form->{resubmit} && ($form->{format} eq "html")) {
693     $onload =
694       qq|window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()|;
695   } elsif ($form->{resubmit}) {
696     $onload = qq|document.oe.submit()|;
697   } else {
698     $onload = "fokus()";
699   }
700
701   $credittext = $locale->text('Credit Limit exceeded!!!');
702   if ($creditwarning) {
703     $onload = qq|alert('$credittext')|;
704   }
705
706   $form->{"javascript"} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
707
708   $form->header;
709
710   print qq|
711 <body onLoad="$onload">
712
713 <form method=post name=oe action=$form->{script}>
714  <script type="text/javascript" src="js/common.js"></script>
715  <script type="text/javascript" src="js/delivery_customer_selection.js"></script>
716  <script type="text/javascript" src="js/vendor_selection.js"></script>
717  <script type="text/javascript" src="js/calculate_qty.js"></script>
718
719 <input type=hidden name=id value=$form->{id}>
720 <input type=hidden name=action value=$form->{action}>
721
722 <input type=hidden name=type value=$form->{type}>
723 <input type=hidden name=formname value=$form->{formname}>
724 <input type=hidden name=media value=$form->{media}>
725 <input type=hidden name=format value=$form->{format}>
726 <input type=hidden name=proforma value=$form->{proforma}>
727
728 <input type=hidden name=queued value="$form->{queued}">
729 <input type=hidden name=printed value="$form->{printed}">
730 <input type=hidden name=emailed value="$form->{emailed}">
731
732 <input type=hidden name=vc value=$form->{vc}>
733
734 <input type=hidden name=title value="$form->{title}">
735
736 <input type=hidden name=discount value=$form->{discount}>
737 <input type=hidden name=creditlimit value=$form->{creditlimit}>
738 <input type=hidden name=creditremaining value=$form->{creditremaining}>
739
740 <input type=hidden name=tradediscount value=$form->{tradediscount}>
741 <input type=hidden name=business value=$form->{business}>
742 <input type=hidden name=webdav value=$webdav>
743
744 <table width=100%>
745   <tr class=listtop>
746     <th class=listtop>$form->{title}</th>
747   </tr>
748   <tr height="5"></tr>
749   <tr>
750     <td>
751       <table width="100%">
752         <tr valign=top>
753           <td>
754             <table width=100%>
755               <tr>
756                 <th align=right>$vclabel</th>
757                 <td colspan=3>$vc</td>
758                 <input type=hidden name=$form->{vc}_id value=$form->{"$form->{vc}_id"}>
759                 <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
760                 <th align=richt nowrap>|
761     . $locale->text('Contact Person') . qq|</th>
762                 <td colspan=3>$contact</td>
763               </tr>
764               $creditremaining
765               $business
766               $dunning
767               $taxzone
768               $department
769               <tr>
770                 <th align=right>| . $locale->text('Currency') . qq|</th>
771                 <td><select name=currency>$form->{selectcurrency}</select></td>
772                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
773                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
774                 $exchangerate
775               </tr>
776               <tr>
777                 <th align=right>| . $locale->text('Shipping Point') . qq|</th>
778                 <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
779               </tr>
780               <tr>
781                 <th align=right>| . $locale->text('Ship via') . qq|</th>
782                 <td colspan=3><input name=shipvia size=35 value="$form->{shipvia}"></td>
783               </tr>|;
784 #              <tr>
785 #                 <td colspan=4>
786 #                   <table>
787 #                     <tr>
788 #                       <td colspan=2>
789 #                         <button type="button" onclick="delivery_customer_selection_window('delivery_customer_string','delivery_customer_id')">| . $locale->text('Choose Customer') . qq|</button>
790 #                       </td>
791 #                       <td colspan=2><input type=hidden name=delivery_customer_id value="$form->{delivery_customer_id}">
792 #                       <input size=45 id=delivery_customer_string name=delivery_customer_string value="$form->{delivery_customer_string}"></td>
793 #                     </tr>
794 #                     <tr>
795 #                       <td colspan=2>
796 #                         <button type="button" onclick="vendor_selection_window('delivery_vendor_string','delivery_vendor_id')">| . $locale->text('Choose Vendor') . qq|</button>
797 #                       </td>
798 #                       <td colspan=2><input type=hidden name=delivery_vendor_id value="$form->{delivery_vendor_id}">
799 #                       <input size=45 id=vendor_string name=delivery_vendor_string value="$form->{delivery_vendor_string}"></td>
800 #                     </tr>
801 #                   </table>
802 #                 </td>
803 #               </tr>
804 print qq|           </table>
805           </td>
806           <td align=right>
807             <table>
808               $openclosed
809               $employee
810               $ordnumber
811             </table>
812           </td>
813         </tr>
814       </table>
815     </td>
816   </tr>
817
818 $jsscript
819
820 <!-- shipto are in hidden variables -->
821
822 <input type=hidden name=shiptoname value="$form->{shiptoname}">
823 <input type=hidden name=shiptostreet value="$form->{shiptostreet}">
824 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
825 <input type=hidden name=shiptocity value="$form->{shiptocity}">
826 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
827 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
828 <input type=hidden name=shiptophone value="$form->{shiptophone}">
829 <input type=hidden name=shiptofax value="$form->{shiptofax}">
830 <input type=hidden name=shiptodepartment_1 value="$form->{shiptodepartment_1}">
831 <input type=hidden name=shiptodepartment_2 value="$form->{shiptodepartment_2}">
832 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
833
834 <!-- email variables -->
835 <input type=hidden name=message value="$form->{message}">
836 <input type=hidden name=email value="$form->{email}">
837 <input type=hidden name=subject value="$form->{subject}">
838 <input type=hidden name=cc value="$form->{cc}">
839 <input type=hidden name=bcc value="$form->{bcc}">
840
841 <input type=hidden name=taxpart value="$form->{taxpart}">
842 <input type=hidden name=taxservice value="$form->{taxservice}">
843
844 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
845 |;
846
847   foreach $item (split / /, $form->{taxaccounts}) {
848     print qq|
849 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
850 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
851 |;
852   }
853   $lxdebug->leave_sub();
854 }
855
856 sub form_footer {
857   $lxdebug->enter_sub();
858
859   $form->{invtotal} = $form->{invsubtotal};
860
861   if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
862     $rows = 2;
863   }
864   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
865     $introws = 2;
866   }
867   $rows = ($rows > $introws) ? $rows : $introws;
868   $notes =
869     qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
870   $intnotes =
871     qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
872
873   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
874
875   $taxincluded = "";
876   if ($form->{taxaccounts}) {
877     $taxincluded = qq|
878               <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
879       . $locale->text('Tax Included') . qq|</b><br><br>
880 |;
881   }
882
883   if (!$form->{taxincluded}) {
884
885     foreach $item (split / /, $form->{taxaccounts}) {
886       if ($form->{"${item}_base"}) {
887         $form->{invtotal} += $form->{"${item}_total"} =
888           $form->round_amount(
889                              $form->{"${item}_base"} * $form->{"${item}_rate"},
890                              2);
891         $form->{"${item}_total"} =
892           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
893
894         $tax .= qq|
895               <tr>
896                 <th align=right>$form->{"${item}_description"}</th>
897                 <td align=right>$form->{"${item}_total"}</td>
898               </tr>
899 |;
900       }
901     }
902
903     $form->{invsubtotal} =
904       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
905
906     $subtotal = qq|
907               <tr>
908                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
909                 <td align=right>$form->{invsubtotal}</td>
910               </tr>
911 |;
912
913   }
914
915   if ($form->{taxincluded}) {
916     foreach $item (split / /, $form->{taxaccounts}) {
917       if ($form->{"${item}_base"}) {
918         $form->{"${item}_total"} =
919           $form->round_amount(
920                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
921                               (1 + $form->{"${item}_rate"})
922                            ),
923                            2);
924         $form->{"${item}_netto"} =
925           $form->round_amount(
926                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
927                           2);
928         $form->{"${item}_total"} =
929           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
930         $form->{"${item}_netto"} =
931           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
932
933         $tax .= qq|
934               <tr>
935                 <th align=right>Enthaltene $form->{"${item}_description"}</th>
936                 <td align=right>$form->{"${item}_total"}</td>
937               </tr>
938               <tr>
939                 <th align=right>Nettobetrag</th>
940                 <td align=right>$form->{"${item}_netto"}</td>
941               </tr>
942 |;
943       }
944     }
945
946   }
947
948   $form->{oldinvtotal} = $form->{invtotal};
949   $form->{invtotal}    =
950     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
951
952   print qq|
953   <tr>
954     <td>
955       <table width=100%>
956         <tr valign=bottom>
957           <td>
958             <table>
959               <tr>
960                 <th align=left>| . $locale->text('Notes') . qq|</th>
961                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
962               </tr>
963               <tr valign=top>
964                 <td>$notes</td>
965                 <td>$intnotes</td>
966               </tr>
967           <th align=right>| . $locale->text('Payment Terms') . qq|</th>
968           <td><select name=payment_id tabindex=24>$payment
969                           </select></td>
970             </table>
971           </td>
972           <td align=right width=100%>
973             $taxincluded
974             <table width=100%>
975               $subtotal
976               $tax
977               <tr>
978                 <th align=right>| . $locale->text('Total') . qq|</th>
979                 <td align=right>$form->{invtotal}</td>
980               </tr>
981             </table>
982           </td>
983         </tr>
984       </table>
985     </td>
986   </tr>
987 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
988 <input type=hidden name=oldtotalpaid value=$totalpaid>
989   <tr>
990     <td><hr size=3 noshade></td>
991   </tr>
992 |;
993
994   if ($webdav) {
995     $webdav_list = qq|
996
997   <tr>
998     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
999   </tr>
1000     <table width=100%>
1001       <td align=left width=30%><b>Dateiname</b></td>
1002       <td align=left width=70%><b>Webdavlink</b></td>
1003 |;
1004     foreach $file (keys %{ $form->{WEBDAV} }) {
1005       $webdav_list .= qq|
1006       <tr>
1007         <td align=left>$file</td>
1008         <td align=left><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
1009       </tr>
1010 |;
1011     }
1012     $webdav_list .= qq|
1013     </table>
1014   </tr>
1015   <tr>
1016     <td><hr size=3 noshade></td>
1017   </tr>
1018 |;
1019
1020     print $webdav_list;
1021   }
1022   print qq|
1023 <input type=hidden name=jscalendar value=$form->{jscalendar}>
1024 |;
1025   print qq|
1026   <tr>
1027     <td>
1028 |;
1029   &print_options;
1030
1031   print qq|
1032     </td>
1033   </tr>
1034 </table>
1035
1036 | . $locale->text("Edit the $form->{type}") . qq|<br>
1037 <input class=submit type=submit name=action id=update_button value="|
1038     . $locale->text('Update') . qq|">
1039 <input class=submit type=submit name=action value="|
1040     . $locale->text('Ship to') . qq|">
1041 <input class=submit type=submit name=action value="|
1042     . $locale->text('Print') . qq|">
1043 <input class=submit type=submit name=action value="|
1044     . $locale->text('E-mail') . qq|">
1045 <input class=submit type=submit name=action value="|
1046     . $locale->text('Save') . qq|">
1047 <input class=submit type=submit name=action value="|
1048     . $locale->text('Save and Close') . qq|">
1049 |;
1050
1051   if (($form->{id})) {
1052     print qq|
1053 <br>| . $locale->text("Workflow $form->{type}") . qq|<br>
1054 <input class=submit type=submit name=action value="|
1055       . $locale->text('Save as new') . qq|">
1056 <input class=submit type=submit name=action value="|
1057       . $locale->text('Delete') . qq|">|;
1058     if (($form->{type} =~ /sales_quotation$/)) {
1059       print qq|
1060 <input class=submit type=submit name=action value="|
1061         . $locale->text('Sales Order') . qq|">|;
1062     }
1063     if ($form->{type} =~ /request_quotation$/) {
1064       print qq|
1065 <input class=submit type=submit name=action value="|
1066         . $locale->text('Purchase Order') . qq|">|;
1067     }
1068     if (1) {
1069     print qq|
1070 <input class=submit type=submit name=action value="|
1071       . $locale->text('Invoice') . qq|">
1072 |;
1073 }
1074
1075     if ($form->{type} =~ /sales_order$/) {
1076       print qq|
1077 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1078 <input class=submit type=submit name=action value="|
1079         . $locale->text('Purchase Order') . qq|">
1080 <input class=submit type=submit name=action value="|
1081         . $locale->text('Quotation') . qq|">
1082 |;
1083
1084     } elsif ($form->{type} =~ /purchase_order$/) {
1085       print qq|
1086 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1087 <input class=submit type=submit name=action value="|
1088         . $locale->text('Sales Order') . qq|">
1089 <input class=submit type=submit name=action value="|
1090         . $locale->text('Request for Quotation') . qq|">
1091 |;
1092
1093     } else {
1094       print qq|
1095 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1096 <input class=submit type=submit name=action value="|
1097         . $locale->text('Order') . qq|">
1098 |;
1099     }
1100   } elsif ($form->{type} =~ /sales_order$/ && $form->{rowcount} && !$form->{proforma}) {
1101     print qq|
1102 <br>Workflow  $form->{heading}<br>
1103 <input class=submit type=submit name=action value="|
1104       . $locale->text('Save as new') . qq|">
1105 <input class=submit type=submit name=action value="|
1106       . $locale->text('Invoice') . qq|">
1107 |;
1108   }
1109
1110   print qq|
1111
1112 <input type=hidden name=rowcount value=$form->{rowcount}>
1113
1114 <input name=callback type=hidden value="$form->{callback}">
1115
1116 <input type=hidden name=path value=$form->{path}>
1117 <input type=hidden name=login value=$form->{login}>
1118 <input type=hidden name=password value=$form->{password}>
1119
1120 </form>
1121
1122 </body>
1123 </html>
1124 |;
1125   $lxdebug->leave_sub();
1126 }
1127
1128 sub update {
1129   $lxdebug->enter_sub();
1130
1131   set_headings($form->{"id"} ? "edit" : "add");
1132
1133   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1134     qw(exchangerate creditlimit creditremaining);
1135   $form->{update} = 1;
1136
1137   &check_name($form->{vc});
1138
1139   &check_project;
1140
1141   $buysell              = 'buy';
1142   $buysell              = 'sell' if ($form->{vc} eq 'vendor');
1143   $form->{exchangerate} = $exchangerate
1144     if (
1145         $form->{forex} = (
1146                   $exchangerate =
1147                     $form->check_exchangerate(
1148                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1149                     )));
1150
1151   # for pricegroups
1152   $i = $form->{rowcount};
1153
1154   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
1155
1156   if (   ($form->{"partnumber_$i"} eq "")
1157       && ($form->{"description_$i"} eq "")
1158       && ($form->{"partsgroup_$i"}  eq "")) {
1159
1160     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
1161     &check_form;
1162
1163   } else {
1164
1165     if (   $form->{type} eq 'purchase_order'
1166         || $form->{type} eq 'request_quotation') {
1167       IR->retrieve_item(\%myconfig, \%$form);
1168     }
1169     if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
1170       IS->retrieve_item(\%myconfig, \%$form);
1171     }
1172
1173     my $rows = scalar @{ $form->{item_list} };
1174
1175     $form->{"discount_$i"} =
1176       $form->format_amount(\%myconfig, $form->{discount} * 100);
1177
1178     if ($rows) {
1179       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1180
1181       if ($rows > 1) {
1182
1183         &select_item;
1184         exit;
1185
1186       } else {
1187
1188         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1189         if ($form->{"not_discountable_$i"}) {
1190           $form->{"discount_$i"} = 0;
1191         }
1192         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1193           qw(partnumber description unit);
1194         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1195           keys %{ $form->{item_list}[0] };
1196         if ($form->{"part_payment_id_$i"} ne "") {
1197           $form->{payment_id} = $form->{"part_payment_id_$i"};
1198         }
1199
1200         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1201
1202         ($dec) = ($s =~ /\.(\d+)/);
1203         $dec           = length $dec;
1204         $decimalplaces = ($dec > 2) ? $dec : 2;
1205
1206         if ($sellprice) {
1207           $form->{"sellprice_$i"} = $sellprice;
1208         } else {
1209
1210           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1211
1212           # if there is an exchange rate adjust sellprice
1213           $form->{"sellprice_$i"} /= $exchangerate;
1214         }
1215
1216         $amount =
1217           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1218           (1 - $form->{"discount_$i"} / 100);
1219         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1220         map { $form->{"${_}_base"} += $amount }
1221           (split / /, $form->{"taxaccounts_$i"});
1222         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1223           split / /, $form->{taxaccounts}
1224           if !$form->{taxincluded};
1225
1226         $form->{creditremaining} -= $amount;
1227
1228         $form->{"sellprice_$i"} =
1229           $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1230                                $decimalplaces);
1231         $form->{"qty_$i"} =
1232           $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1233
1234         # get pricegroups for parts
1235         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1236
1237         # build up html code for prices_$i
1238         &set_pricegroup($i);
1239       }
1240
1241       &display_form;
1242
1243     } else {
1244
1245       # ok, so this is a new part
1246       # ask if it is a part or service item
1247
1248       if (   $form->{"partsgroup_$i"}
1249           && ($form->{"partsnumber_$i"} eq "")
1250           && ($form->{"description_$i"} eq "")) {
1251         $form->{rowcount}--;
1252         $form->{"discount_$i"} = "";
1253         &display_form;
1254       } else {
1255
1256         $form->{"id_$i"}   = 0;
1257         $form->{"unit_$i"} = $locale->text('ea');
1258
1259         &new_item;
1260
1261       }
1262     }
1263   }
1264
1265   $lxdebug->leave_sub();
1266 }
1267
1268 sub search {
1269   $lxdebug->enter_sub();
1270
1271   if ($form->{type} eq 'purchase_order') {
1272     $form->{title} = $locale->text('Purchase Orders');
1273     $form->{vc}    = 'vendor';
1274     $ordlabel      = $locale->text('Order Number');
1275     $ordnumber     = 'ordnumber';
1276     $employee      = $locale->text('Employee');
1277   }
1278   if ($form->{type} eq 'request_quotation') {
1279     $form->{title} = $locale->text('Request for Quotations');
1280     $form->{vc}    = 'vendor';
1281     $ordlabel      = $locale->text('RFQ Number');
1282     $ordnumber     = 'quonumber';
1283     $employee      = $locale->text('Employee');
1284   }
1285   if ($form->{type} eq 'receive_order') {
1286     $form->{title} = $locale->text('Receive Merchandise');
1287     $form->{vc}    = 'vendor';
1288     $ordlabel      = $locale->text('Order Number');
1289     $ordnumber     = 'ordnumber';
1290     $employee      = $locale->text('Employee');
1291   }
1292   if ($form->{type} eq 'sales_order') {
1293     $form->{title} = $locale->text('Sales Orders');
1294     $form->{vc}    = 'customer';
1295     $ordlabel      = $locale->text('Order Number');
1296     $ordnumber     = 'ordnumber';
1297     $employee      = $locale->text('Salesperson');
1298   }
1299   if ($form->{type} eq 'ship_order') {
1300     $form->{title} = $locale->text('Ship Merchandise');
1301     $form->{vc}    = 'customer';
1302     $ordlabel      = $locale->text('Order Number');
1303     $ordnumber     = 'ordnumber';
1304     $employee      = $locale->text('Salesperson');
1305
1306   }
1307
1308   if ($form->{type} eq 'sales_quotation') {
1309     $form->{title} = $locale->text('Quotations');
1310     $form->{vc}    = 'customer';
1311     $ordlabel      = $locale->text('Quotation Number');
1312     $ordnumber     = 'quonumber';
1313     $employee      = $locale->text('Employee');
1314   }
1315
1316   if ($form->{type} =~ /(ship|receive)_order/) {
1317     OE->get_warehouses(\%myconfig, \%$form);
1318
1319     # warehouse
1320     if (@{ $form->{all_warehouses} }) {
1321       $form->{selectwarehouse} = "<option>\n";
1322       $form->{warehouse}       = qq|$form->{warehouse}--$form->{warehouse_id}|;
1323
1324       map {
1325         $form->{selectwarehouse} .=
1326           "<option>$_->{description}--$_->{id}\n"
1327       } (@{ $form->{all_warehouses} });
1328
1329       $warehouse = qq|
1330               <tr>
1331                 <th align=right>| . $locale->text('Warehouse') . qq|</th>
1332                 <td colspan=3><select name=warehouse>$form->{selectwarehouse}</select></td>
1333                 <input type=hidden name=selectwarehouse value="$form->{selectwarehouse}">
1334               </tr>
1335 |;
1336
1337     }
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}\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</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}\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   if ($form->{type} !~ /(ship_order|receive_order)/) {
1376     $openclosed = qq|
1377               <tr>
1378                 <td><input name="open" class=checkbox type=checkbox value=1 checked> |
1379       . $locale->text('Open') . qq|</td>
1380                 <td><input name="closed" class=checkbox type=checkbox value=1 $form->{closed}> |
1381       . $locale->text('Closed') . qq|</td>
1382               </tr>
1383 |;
1384   } else {
1385
1386     $openclosed = qq|
1387                 <input type=hidden name="open" value=1>
1388 |;
1389   }
1390
1391   my $delivered;
1392   if (($form->{"type"} eq "sales_order") ||
1393       ($form->{"type"} eq "purchase_order")) {
1394     $delivered = qq|
1395         <tr>
1396           <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
1397             <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
1398           <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
1399             <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
1400         </tr>
1401 |;
1402   }
1403
1404   # use JavaScript Calendar or not
1405   $form->{jsscript} = $jscalendar;
1406   $jsscript = "";
1407   if ($form->{jsscript}) {
1408
1409     # with JavaScript Calendar
1410     $button1 = qq|
1411        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
1412        <input type=button name=transdatefrom id="trigger3" value=|
1413       . $locale->text('button') . qq|></td>
1414       |;
1415     $button2 = qq|
1416        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
1417        <input type=button name=transdateto name=transdateto id="trigger4" value=|
1418       . $locale->text('button') . qq|></td>
1419      |;
1420
1421     #write Trigger
1422     $jsscript =
1423       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
1424                           "transdateto", "BL", "trigger4");
1425   } else {
1426
1427     # without JavaScript Calendar
1428     $button1 = qq|
1429                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
1430     $button2 = qq|
1431                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
1432   }
1433
1434   $form->header;
1435
1436   print qq|
1437 <body>
1438
1439 <form method=post action=$form->{script}>
1440
1441 <table width=100%>
1442   <tr>
1443     <th class=listtop>$form->{title}</th>
1444   </tr>
1445   <tr height="5"></tr>
1446   <tr>
1447     <td>
1448       <table>
1449         <tr>
1450           <th align=right>$vclabel</th>
1451           <td colspan=3>$vc</td>
1452         </tr>
1453         $warehouse
1454         $department
1455         <tr>
1456           <th align=right>$ordlabel</th>
1457           <td colspan=3><input name="$ordnumber" size=20></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               $openclosed
1471         $delivered
1472               <tr>
1473                 <td><input name="l_id" class=checkbox type=checkbox value=Y>
1474                 | . $locale->text('ID') . qq|</td>
1475                 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
1476                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> |
1477     . $locale->text('Date') . qq|</td>
1478                 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> |
1479     . $locale->text('Required by') . qq|</td>
1480               </tr>
1481               <tr>
1482                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vclabel</td>
1483                 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
1484                 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> |
1485     . $locale->text('Ship via') . qq|</td>
1486               </tr>
1487               <tr>
1488                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> |
1489     . $locale->text('Amount') . qq|</td>
1490                 <td><input name="l_tax" class=checkbox type=checkbox value=Y> |
1491     . $locale->text('Tax') . qq|</td>
1492                 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> |
1493     . $locale->text('Total') . qq|</td>
1494               </tr>
1495               <tr>
1496                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> |
1497     . $locale->text('Subtotal') . qq|</td>
1498               </tr>
1499             </table>
1500           </td>
1501         </tr>
1502       </table>
1503     </td>
1504   </tr>
1505   <tr><td colspan=4><hr size=3 noshade></td></tr>
1506 </table>
1507
1508 $jsscript
1509
1510 <br>
1511 <input type=hidden name=nextsub value=orders>
1512 <input type=hidden name=path value=$form->{path}>
1513 <input type=hidden name=login value=$form->{login}>
1514 <input type=hidden name=password value=$form->{password}>
1515 <input type=hidden name=vc value=$form->{vc}>
1516 <input type=hidden name=type value=$form->{type}>
1517
1518 <input class=submit type=submit name=action value="|
1519     . $locale->text('Continue') . qq|">
1520 </form>
1521
1522 </body>
1523 </html>
1524 |;
1525
1526   $lxdebug->leave_sub();
1527 }
1528
1529 sub orders {
1530   $lxdebug->enter_sub();
1531
1532   # split vendor / customer
1533   ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) =
1534     split(/--/, $form->{ $form->{vc} });
1535
1536   OE->transactions(\%myconfig, \%$form);
1537
1538   $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
1539
1540   $number     = $form->escape($form->{$ordnumber});
1541   $name       = $form->escape($form->{ $form->{vc} });
1542   $department = $form->escape($form->{department});
1543   $warehouse  = $form->escape($form->{warehouse});
1544
1545   # construct href
1546   $href =
1547     "$form->{script}?path=$form->{path}&action=orders&type=$form->{type}&vc=$form->{vc}&login=$form->{login}&password=$form->{password}&transdatefrom=$form->{transdatefrom}&transdateto=$form->{transdateto}&open=$form->{open}&closed=$form->{closed}&notdelivered=$form->{notdelivered}&delivered=$form->{delivered}&$ordnumber=$number&$form->{vc}=$name&department=$department&warehouse=$warehouse";
1548
1549   # construct callback
1550   $number     = $form->escape($form->{$ordnumber},    1);
1551   $name       = $form->escape($form->{ $form->{vc} }, 1);
1552   $department = $form->escape($form->{department},    1);
1553   $warehouse  = $form->escape($form->{warehouse},     1);
1554
1555   $callback =
1556     "$form->{script}?path=$form->{path}&action=orders&type=$form->{type}&vc=$form->{vc}&login=$form->{login}&password=$form->{password}&transdatefrom=$form->{transdatefrom}&transdateto=$form->{transdateto}&open=$form->{open}&closed=$form->{closed}&notdelivered=$form->{notdelivered}&delivered=$form->{delivered}&$ordnumber=$number&$form->{vc}=$name&department=$department&warehouse=$warehouse";
1557
1558   @columns =
1559     $form->sort_columns("transdate", "reqdate",   "id",      "$ordnumber",
1560                         "name",      "netamount", "tax",     "amount",
1561                         "curr",      "employee",  "shipvia", "open",
1562                         "closed",    "delivered");
1563
1564   $form->{l_open} = $form->{l_closed} = "Y"
1565     if ($form->{open} && $form->{closed});
1566
1567   $form->{"l_delivered"} = "Y"
1568     if ($form->{"delivered"} && $form->{"notdelivered"});
1569
1570   foreach $item (@columns) {
1571     if ($form->{"l_$item"} eq "Y") {
1572       push @column_index, $item;
1573
1574       # add column to href and callback
1575       $callback .= "&l_$item=Y";
1576       $href     .= "&l_$item=Y";
1577     }
1578   }
1579
1580   # only show checkboxes if gotten here via sales_order form.
1581   if ($form->{type} =~ /sales_order/) {
1582     unshift @column_index, "ids";
1583   }
1584
1585   if ($form->{l_subtotal} eq 'Y') {
1586     $callback .= "&l_subtotal=Y";
1587     $href     .= "&l_subtotal=Y";
1588   }
1589
1590   if ($form->{vc} eq 'vendor') {
1591     if ($form->{type} eq 'receive_order') {
1592       $form->{title} = $locale->text('Receive Merchandise');
1593     } elsif ($form->{type} eq 'purchase_order') {
1594       $form->{title} = $locale->text('Purchase Orders');
1595     } else {
1596       $form->{title} = $locale->text('Request for Quotations');
1597     }
1598     $name     = $locale->text('Vendor');
1599     $employee = $locale->text('Employee');
1600   }
1601   if ($form->{vc} eq 'customer') {
1602     if ($form->{type} eq 'sales_order') {
1603       $form->{title} = $locale->text('Sales Orders');
1604       $employee = $locale->text('Salesperson');
1605     } elsif ($form->{type} eq 'ship_order') {
1606       $form->{title} = $locale->text('Ship Merchandise');
1607       $employee = $locale->text('Salesperson');
1608     } else {
1609       $form->{title} = $locale->text('Quotations');
1610       $employee = $locale->text('Employee');
1611     }
1612     $name = $locale->text('Customer');
1613   }
1614
1615   $column_header{id} =
1616       qq|<th><a class=listheading href=$href&sort=id>|
1617     . $locale->text('ID')
1618     . qq|</a></th>|;
1619   $column_header{transdate} =
1620       qq|<th><a class=listheading href=$href&sort=transdate>|
1621     . $locale->text('Date')
1622     . qq|</a></th>|;
1623   $column_header{reqdate} =
1624       qq|<th><a class=listheading href=$href&sort=reqdate>|
1625     . $locale->text('Required by')
1626     . qq|</a></th>|;
1627   $column_header{ordnumber} =
1628       qq|<th><a class=listheading href=$href&sort=ordnumber>|
1629     . $locale->text('Order')
1630     . qq|</a></th>|;
1631   $column_header{quonumber} =
1632       qq|<th><a class=listheading href=$href&sort=quonumber>|
1633     . ($form->{"type"} eq "request_quotation" ?
1634        $locale->text('RFQ') :
1635        $locale->text('Quotation'))
1636     . qq|</a></th>|;
1637   $column_header{name} =
1638     qq|<th><a class=listheading href=$href&sort=name>$name</a></th>|;
1639   $column_header{netamount} =
1640     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1641   $column_header{tax} =
1642     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1643   $column_header{amount} =
1644     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1645   $column_header{curr} =
1646     qq|<th class=listheading>| . $locale->text('Curr') . qq|</th>|;
1647   $column_header{shipvia} =
1648       qq|<th><a class=listheading href=$href&sort=shipvia>|
1649     . $locale->text('Ship via')
1650     . qq|</a></th>|;
1651   $column_header{open} =
1652     qq|<th class=listheading>| . $locale->text('O') . qq|</th>|;
1653   $column_header{closed} =
1654     qq|<th class=listheading>| . $locale->text('C') . qq|</th>|;
1655   $column_header{"delivered"} =
1656     qq|<th class="listheading">| . $locale->text("Delivered") . qq|</th>|;
1657
1658   $column_header{employee} =
1659     qq|<th><a class=listheading href=$href&sort=employee>$employee</a></th>|;
1660
1661   $column_header{ids} = qq|<th></th>|;
1662
1663   if ($form->{ $form->{vc} }) {
1664     $option = $locale->text(ucfirst $form->{vc});
1665     $option .= " : $form->{$form->{vc}}";
1666   }
1667   if ($form->{warehouse}) {
1668     ($warehouse) = split /--/, $form->{warehouse};
1669     $option .= "\n<br>" if ($option);
1670     $option .= $locale->text('Warehouse');
1671     $option .= " : $warehouse";
1672   }
1673   if ($form->{department}) {
1674     $option .= "\n<br>" if ($option);
1675     ($department) = split /--/, $form->{department};
1676     $option .= $locale->text('Department') . " : $department";
1677   }
1678   if ($form->{transdatefrom}) {
1679     $option .= "\n<br>"
1680       . $locale->text('From') . " "
1681       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1682   }
1683   if ($form->{transdateto}) {
1684     $option .= "\n<br>"
1685       . $locale->text('Bis') . " "
1686       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1687   }
1688   if ($form->{open}) {
1689     $option .= "\n<br>" if ($option);
1690     $option .= $locale->text('Open');
1691   }
1692   if ($form->{closed}) {
1693     $option .= "\n<br>" if ($option);
1694     $option .= $locale->text('Closed');
1695   }
1696
1697   $form->header;
1698
1699   print qq|
1700 <body>
1701
1702 <form method="post" action="oe.pl">
1703 <table width=100%>
1704   <tr>
1705     <th class=listtop>$form->{title}</th>
1706   </tr>
1707   <tr height="5"></tr>
1708   <tr>
1709     <td>$option</td>
1710   </tr>
1711   <tr>
1712     <td>
1713       <table width=100%>
1714         <tr class=listheading>|;
1715
1716   map { print "\n$column_header{$_}" } @column_index;
1717
1718   print qq|
1719         </tr>
1720 |;
1721
1722   # add sort and escape callback
1723   $callback_escaped = $form->escape($callback . "&sort=$form->{sort}");
1724
1725   if (@{ $form->{OE} }) {
1726     $sameitem = $form->{OE}->[0]->{ $form->{sort} };
1727   }
1728
1729   $action = "edit";
1730   $action = "ship_receive" if ($form->{type} =~ /(ship|receive)_order/);
1731
1732   $warehouse = $form->escape($form->{warehouse});
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="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?path=$form->{path}&action=$action&type=$form->{type}&id=$oe->{id}&warehouse=$warehouse&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
1777     if ($oe->{closed}) {
1778       $column_data{closed} = "<td align=center>X</td>";
1779       $column_data{open}   = "<td>&nbsp;</td>";
1780     } else {
1781       $column_data{closed} = "<td>&nbsp;</td>";
1782       $column_data{open}   = "<td align=center>X</td>";
1783     }
1784     $column_data{"delivered"} = "<td>" .
1785       ($oe->{"delivered"} ? $locale->text("Yes") : $locale->text("No")) .
1786       "</td>";
1787
1788     $i++;
1789     $i %= 2;
1790     print "
1791         <tr class=listrow$i>";
1792
1793     map { print "\n$column_data{$_}" } @column_index;
1794
1795     print qq|
1796         </tr>
1797 |;
1798
1799   }
1800
1801   if ($form->{l_subtotal} eq 'Y') {
1802     &subtotal;
1803   }
1804
1805   # print totals
1806   print qq|
1807         <tr class=listtotal>|;
1808
1809   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1810
1811   $column_data{netamount} =
1812     "<th class=listtotal align=right>"
1813     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1814   $column_data{tax} = "<th class=listtotal align=right>"
1815     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1816                            2, "&nbsp;")
1817     . "</th>";
1818   $column_data{amount} =
1819     "<th class=listtotal align=right>"
1820     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1821
1822   map { print "\n$column_data{$_}" } @column_index;
1823
1824   print qq|
1825         </tr>
1826       </td>
1827     </table>
1828   </tr>
1829   <tr>
1830     <td><hr size=3 noshade></td>
1831   </tr>
1832 </table>|;
1833
1834   # multiple invoice edit button only if gotten there via sales_order form.
1835
1836   if ($form->{type} =~ /sales_order/) {
1837     print qq|
1838   <input type="hidden" name="path" value="$form->{path}">
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="warehouse" value="$warehouse">
1844   <input type="hidden" name="vc" value="$form->{vc}">
1845   <input type="hidden" name="login" value="$form->{login}">
1846   <input type="hidden" name="password" value="$form->{password}">
1847   <input type="hidden" name="callback" value="$callback">
1848   <input type="hidden" name="rowcount" value="$form->{rowcount}">|;
1849   }
1850
1851   print qq|
1852 </form>
1853
1854 <br>
1855 <form method=post action=$form->{script}>
1856
1857 <input name=callback type=hidden value="$form->{callback}">
1858
1859 <input type=hidden name=type value=$form->{type}>
1860 <input type=hidden name=vc value=$form->{vc}>
1861
1862 <input type=hidden name=path value=$form->{path}>
1863 <input type=hidden name=login value=$form->{login}>
1864 <input type=hidden name=password value=$form->{password}>
1865 |;
1866
1867   if ($form->{type} !~ /(ship|receive)_order/) {
1868     print qq|
1869 <input class=submit type=submit name=action value="|
1870       . $locale->text('Add') . qq|">|;
1871   }
1872
1873   print qq|
1874 </form>
1875
1876 </body>
1877 </html>
1878 |;
1879
1880   $lxdebug->leave_sub();
1881 }
1882
1883 sub subtotal {
1884   $lxdebug->enter_sub();
1885
1886   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1887
1888   $column_data{netamount} =
1889       "<th class=listsubtotal align=right>"
1890     . $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")
1891     . "</th>";
1892   $column_data{tax} = "<td class=listsubtotal align=right>"
1893     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1894                            2, "&nbsp;")
1895     . "</th>";
1896   $column_data{amount} =
1897     "<th class=listsubtotal align=right>"
1898     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1899
1900   $subtotalnetamount = 0;
1901   $subtotalamount    = 0;
1902
1903   print "
1904         <tr class=listsubtotal>
1905 ";
1906
1907   map { print "\n$column_data{$_}" } @column_index;
1908
1909   print qq|
1910         </tr>
1911 |;
1912
1913   $lxdebug->leave_sub();
1914 }
1915
1916 sub save_and_close {
1917   $lxdebug->enter_sub();
1918
1919   if ($form->{type} =~ /_order$/) {
1920     $form->isblank("transdate", $locale->text('Order Date missing!'));
1921   } else {
1922     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1923   }
1924
1925   $msg = ucfirst $form->{vc};
1926   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1927
1928   # $locale->text('Customer missing!');
1929   # $locale->text('Vendor missing!');
1930
1931   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1932     if ($form->{currency} ne $form->{defaultcurrency});
1933
1934   &validate_items;
1935
1936   # if the name changed get new values
1937   if (&check_name($form->{vc})) {
1938     &update;
1939     exit;
1940   }
1941
1942   $form->{id} = 0 if $form->{saveasnew};
1943
1944   # this is for the internal notes section for the [email] Subject
1945   if ($form->{type} =~ /_order$/) {
1946     if ($form->{type} eq 'sales_order') {
1947       $form->{label} = $locale->text('Sales Order');
1948
1949       $numberfld = "sonumber";
1950       $ordnumber = "ordnumber";
1951     } else {
1952       $form->{label} = $locale->text('Purchase Order');
1953
1954       $numberfld = "ponumber";
1955       $ordnumber = "ordnumber";
1956     }
1957
1958     $err = $locale->text('Cannot save order!');
1959
1960   } else {
1961     if ($form->{type} eq 'sales_quotation') {
1962       $form->{label} = $locale->text('Quotation');
1963
1964       $numberfld = "sqnumber";
1965       $ordnumber = "quonumber";
1966     } else {
1967       $form->{label} = $locale->text('Request for Quotation');
1968
1969       $numberfld = "rfqnumber";
1970       $ordnumber = "quonumber";
1971     }
1972
1973     $err = $locale->text('Cannot save quotation!');
1974
1975   }
1976
1977   # get new number in sequence if no number is given or if saveasnew was requested
1978   if (!$form->{$ordnumber} || $form->{saveasnew}) {
1979     $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1980   }
1981
1982   relink_accounts();
1983
1984   $form->redirect(
1985             $form->{label} . " $form->{$ordnumber} " . $locale->text('saved!'))
1986     if (OE->save(\%myconfig, \%$form));
1987   $form->error($err);
1988
1989   $lxdebug->leave_sub();
1990 }
1991
1992 sub save {
1993   $lxdebug->enter_sub();
1994
1995   if ($form->{type} =~ /_order$/) {
1996     $form->isblank("transdate", $locale->text('Order Date missing!'));
1997   } else {
1998     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1999   }
2000
2001   $msg = ucfirst $form->{vc};
2002   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
2003
2004   # $locale->text('Customer missing!');
2005   # $locale->text('Vendor missing!');
2006
2007   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
2008     if ($form->{currency} ne $form->{defaultcurrency});
2009
2010   &validate_items;
2011
2012   # if the name changed get new values
2013   if (&check_name($form->{vc})) {
2014     &update;
2015     exit;
2016   }
2017
2018   $form->{id} = 0 if $form->{saveasnew};
2019
2020   # this is for the internal notes section for the [email] Subject
2021   if ($form->{type} =~ /_order$/) {
2022     if ($form->{type} eq 'sales_order') {
2023       $form->{label} = $locale->text('Sales Order');
2024
2025       $numberfld = "sonumber";
2026       $ordnumber = "ordnumber";
2027     } else {
2028       $form->{label} = $locale->text('Purchase Order');
2029
2030       $numberfld = "ponumber";
2031       $ordnumber = "ordnumber";
2032     }
2033
2034     $err = $locale->text('Cannot save order!');
2035
2036   } else {
2037     if ($form->{type} eq 'sales_quotation') {
2038       $form->{label} = $locale->text('Quotation');
2039
2040       $numberfld = "sqnumber";
2041       $ordnumber = "quonumber";
2042     } else {
2043       $form->{label} = $locale->text('Request for Quotation');
2044
2045       $numberfld = "rfqnumber";
2046       $ordnumber = "quonumber";
2047     }
2048
2049     $err = $locale->text('Cannot save quotation!');
2050
2051   }
2052
2053   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
2054     unless $form->{$ordnumber};
2055
2056   relink_accounts();
2057
2058   OE->save(\%myconfig, \%$form);
2059   $form->{simple_save} = 1;
2060   if(!$form->{print_and_save}) {
2061     set_headings("edit");
2062     &update;
2063     exit;
2064   }
2065   $lxdebug->leave_sub();
2066 }
2067
2068 sub delete {
2069   $lxdebug->enter_sub();
2070
2071   $form->header;
2072
2073   if ($form->{type} =~ /_order$/) {
2074     $msg       = $locale->text('Are you sure you want to delete Order Number');
2075     $ordnumber = 'ordnumber';
2076   } else {
2077     $msg = $locale->text('Are you sure you want to delete Quotation Number');
2078     $ordnumber = 'quonumber';
2079   }
2080
2081   print qq|
2082 <body>
2083
2084 <form method=post action=$form->{script}>
2085 |;
2086
2087   # delete action variable
2088   map { delete $form->{$_} } qw(action header);
2089
2090   foreach $key (keys %$form) {
2091     $form->{$key} =~ s/\"/&quot;/g;
2092     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2093   }
2094
2095   print qq|
2096 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
2097
2098 <h4>$msg $form->{$ordnumber}</h4>
2099 <p>
2100 <input name=action class=submit type=submit value="|
2101     . $locale->text('Yes') . qq|">
2102 </form>
2103
2104 </body>
2105 </html>
2106 |;
2107
2108   $lxdebug->leave_sub();
2109 }
2110
2111 sub yes {
2112   $lxdebug->enter_sub();
2113
2114   if ($form->{type} =~ /_order$/) {
2115     $msg = $locale->text('Order deleted!');
2116     $err = $locale->text('Cannot delete order!');
2117   } else {
2118     $msg = $locale->text('Quotation deleted!');
2119     $err = $locale->text('Cannot delete quotation!');
2120   }
2121
2122   $form->redirect($msg) if (OE->delete(\%myconfig, \%$form, $spool));
2123   $form->error($err);
2124
2125   $lxdebug->leave_sub();
2126 }
2127
2128 sub invoice {
2129   $lxdebug->enter_sub();
2130
2131   if ($form->{type} =~ /_order$/) {
2132
2133     # these checks only apply if the items don't bring their own ordnumbers/transdates.
2134     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
2135     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
2136       if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
2137           ->{''});
2138     $form->isblank("transdate", $locale->text('Order Date missing!'))
2139       if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
2140           ->{''});
2141
2142     # also copy deliverydate from the order
2143     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
2144   } else {
2145     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
2146     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
2147     $form->{ordnumber} = "";
2148   }
2149
2150   # if the name changed get new values
2151   if (&check_name($form->{vc})) {
2152     &update;
2153     exit;
2154   }
2155
2156   $form->{cp_id} *= 1;
2157
2158   for $i (1 .. $form->{rowcount}) {
2159     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
2160                                                      $form->{"${_}_${i}"})
2161             if ($form->{"${_}_${i}"}) }
2162         qw(ship qty sellprice listprice basefactor));
2163   }
2164
2165   if (   $form->{type} =~ /_order/
2166       && $form->{currency} ne $form->{defaultcurrency}) {
2167
2168     # check if we need a new exchangerate
2169     $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
2170
2171     $orddate      = $form->current_date(\%myconfig);
2172     $exchangerate =
2173       $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate,
2174                                 $buysell);
2175
2176     if (!$exchangerate) {
2177       &backorder_exchangerate($orddate, $buysell);
2178       exit;
2179     }
2180   }
2181
2182   # close orders/quotations
2183   $form->{closed} = 1;
2184
2185   # save order if one ordnumber has been given
2186   # if not it's most likely a collective order, which can't be saved back
2187   # so they just have to be closed
2188   if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
2189     OE->close_order(\%myconfig, \%$form);
2190   } else {
2191     OE->close_orders(\%myconfig, \%$form);
2192   }
2193
2194   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
2195   $form->{duedate} =
2196     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
2197
2198   $form->{id}     = '';
2199   $form->{closed} = 0;
2200   $form->{rowcount}--;
2201   $form->{shipto} = 1;
2202
2203   if ($form->{type} =~ /_order$/) {
2204     $form->{exchangerate} = $exchangerate;
2205     &create_backorder;
2206   }
2207
2208   if (   $form->{type} eq 'purchase_order'
2209       || $form->{type} eq 'request_quotation') {
2210     $form->{title}  = $locale->text('Add Vendor Invoice');
2211     $form->{script} = 'ir.pl';
2212     $script         = "ir";
2213     $buysell        = 'sell';
2214   }
2215   if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
2216     $form->{title}  = $locale->text('Add Sales Invoice');
2217     $form->{script} = 'is.pl';
2218     $script         = "is";
2219     $buysell        = 'buy';
2220   }
2221
2222   # bo creates the id, reset it
2223   map { delete $form->{$_} }
2224     qw(id subject message cc bcc printed emailed queued);
2225   $form->{ $form->{vc} } =~ s/--.*//g;
2226   $form->{type} = "invoice";
2227
2228   # locale messages
2229   $locale = new Locale "$myconfig{countrycode}", "$script";
2230
2231   require "$form->{path}/$form->{script}";
2232
2233   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
2234
2235   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
2236     qw(creditlimit creditremaining);
2237
2238   $currency = $form->{currency};
2239   &invoice_links;
2240
2241   $form->{currency}     = $currency;
2242   $form->{exchangerate} = "";
2243   $form->{forex}        = "";
2244   $form->{exchangerate} = $exchangerate
2245     if (
2246         $form->{forex} = (
2247                     $exchangerate =
2248                       $form->check_exchangerate(
2249                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
2250                       )));
2251
2252   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
2253
2254   &prepare_invoice;
2255
2256   # format amounts
2257   for $i (1 .. $form->{rowcount}) {
2258     $form->{"discount_$i"} =
2259       $form->format_amount(\%myconfig, $form->{"discount_$i"});
2260
2261     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
2262     $dec           = length $dec;
2263     $decimalplaces = ($dec > 2) ? $dec : 2;
2264
2265     # copy delivery date from reqdate for order -> invoice conversion
2266     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
2267       unless $form->{"deliverydate_$i"};
2268
2269     $form->{"sellprice_$i"} =
2270       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
2271                            $decimalplaces);
2272
2273     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
2274     $dec_qty = length $dec_qty;
2275     $form->{"qty_$i"} =
2276       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
2277
2278     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2279       qw(partnumber description unit);
2280
2281   }
2282
2283   &display_form;
2284
2285   $lxdebug->leave_sub();
2286 }
2287
2288 sub backorder_exchangerate {
2289   $lxdebug->enter_sub();
2290   my ($orddate, $buysell) = @_;
2291
2292   $form->header;
2293
2294   print qq|
2295 <body>
2296
2297 <form method=post action=$form->{script}>
2298 |;
2299
2300   # delete action variable
2301   map { delete $form->{$_} } qw(action header exchangerate);
2302
2303   foreach $key (keys %$form) {
2304     $form->{$key} =~ s/\"/&quot;/g;
2305     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2306   }
2307
2308   $form->{title} = $locale->text('Add Exchangerate');
2309
2310   print qq|
2311
2312 <input type=hidden name=path value=$form->{path}>
2313 <input type=hidden name=login value=$form->{login}>
2314 <input type=hidden name=password value=$form->{password}>
2315
2316 <input type=hidden name=exchangeratedate value=$orddate>
2317 <input type=hidden name=buysell value=$buysell>
2318
2319 <table width=100%>
2320   <tr><th class=listtop>$form->{title}</th></tr>
2321   <tr height="5"></tr>
2322   <tr>
2323     <td>
2324       <table>
2325         <tr>
2326           <th align=right>| . $locale->text('Currency') . qq|</th>
2327           <td>$form->{currency}</td>
2328         </tr>
2329         <tr>
2330           <th align=right>| . $locale->text('Date') . qq|</th>
2331           <td>$orddate</td>
2332         </tr>
2333         <tr>
2334           <th align=right>| . $locale->text('Exchangerate') . qq|</th>
2335           <td><input name=exchangerate size=11></td>
2336         </tr>
2337       </table>
2338     </td>
2339   </tr>
2340 </table>
2341
2342 <hr size=3 noshade>
2343
2344 <br>
2345 <input type=hidden name=nextsub value=save_exchangerate>
2346
2347 <input name=action class=submit type=submit value="|
2348     . $locale->text('Continue') . qq|">
2349
2350 </form>
2351
2352 </body>
2353 </html>
2354 |;
2355
2356   $lxdebug->leave_sub();
2357 }
2358
2359 sub save_exchangerate {
2360   $lxdebug->enter_sub();
2361
2362   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
2363   $form->{exchangerate} =
2364     $form->parse_amount(\%myconfig, $form->{exchangerate});
2365   $form->save_exchangerate(\%myconfig, $form->{currency},
2366                            $form->{exchangeratedate},
2367                            $form->{exchangerate}, $form->{buysell});
2368
2369   &invoice;
2370
2371   $lxdebug->leave_sub();
2372 }
2373
2374 sub create_backorder {
2375   $lxdebug->enter_sub();
2376
2377   $form->{shipped} = 1;
2378
2379   # figure out if we need to create a backorder
2380   # items aren't saved if qty != 0
2381
2382   for $i (1 .. $form->{rowcount}) {
2383     $totalqty  += $qty  = $form->{"qty_$i"};
2384     $totalship += $ship = $form->{"ship_$i"};
2385
2386     $form->{"qty_$i"} = $qty - $ship;
2387   }
2388
2389   if ($totalship == 0) {
2390     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
2391     $form->{ordtotal} = 0;
2392     $form->{shipped}  = 0;
2393     return;
2394   }
2395
2396   if ($totalqty == $totalship) {
2397     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
2398     $form->{ordtotal} = 0;
2399     return;
2400   }
2401
2402   @flds = (
2403     qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
2404   );
2405
2406   for $i (1 .. $form->{rowcount}) {
2407     map {
2408       $form->{"${_}_$i"} =
2409         $form->format_amount(\%myconfig, $form->{"${_}_$i"})
2410     } qw(sellprice discount);
2411   }
2412
2413   relink_accounts();
2414
2415   OE->save(\%myconfig, \%$form);
2416
2417   # rebuild rows for invoice
2418   @a     = ();
2419   $count = 0;
2420
2421   for $i (1 .. $form->{rowcount}) {
2422     $form->{"qty_$i"} = $form->{"ship_$i"};
2423
2424     if ($form->{"qty_$i"}) {
2425       push @a, {};
2426       $j = $#a;
2427       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2428       $count++;
2429     }
2430   }
2431
2432   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2433   $form->{rowcount} = $count;
2434
2435   $lxdebug->leave_sub();
2436 }
2437
2438 sub save_as_new {
2439   $lxdebug->enter_sub();
2440
2441   $form->{saveasnew} = 1;
2442   $form->{closed}    = 0;
2443   map { delete $form->{$_} } qw(printed emailed queued ordnumber quonumber);
2444
2445   &save;
2446
2447   $lxdebug->leave_sub();
2448 }
2449
2450 sub purchase_order {
2451   $lxdebug->enter_sub();
2452
2453   if (   $form->{type} eq 'sales_quotation'
2454       || $form->{type} eq 'request_quotation') {
2455     OE->close_order(\%myconfig, \%$form);
2456   }
2457
2458   $form->{cp_id} *= 1;
2459
2460   $form->{title} = $locale->text('Add Purchase Order');
2461   $form->{vc}    = "vendor";
2462   $form->{type}  = "purchase_order";
2463
2464   &poso;
2465
2466   $lxdebug->leave_sub();
2467 }
2468
2469 sub sales_order {
2470   $lxdebug->enter_sub();
2471
2472   if (   $form->{type} eq 'sales_quotation'
2473       || $form->{type} eq 'request_quotation') {
2474     OE->close_order(\%myconfig, $form);
2475   }
2476
2477   $form->{cp_id} *= 1;
2478
2479   $form->{title} = $locale->text('Add Sales Order');
2480   $form->{vc}    = "customer";
2481   $form->{type}  = "sales_order";
2482
2483   &poso;
2484
2485   $lxdebug->leave_sub();
2486 }
2487
2488 sub poso {
2489   $lxdebug->enter_sub();
2490
2491   $form->{transdate} = $form->current_date(\%myconfig);
2492   delete $form->{duedate};
2493
2494   $form->{closed} = 0;
2495
2496   # reset
2497   map { delete $form->{$_} }
2498     qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
2499
2500   for $i (1 .. $form->{rowcount}) {
2501     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
2502                                                      $form->{"${_}_${i}"})
2503             if ($form->{"${_}_${i}"}) }
2504         qw(ship qty sellprice listprice basefactor));
2505   }
2506
2507   &order_links;
2508
2509   &prepare_order;
2510
2511   # format amounts
2512   for $i (1 .. $form->{rowcount} - 1) {
2513     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2514       qw(partnumber description unit);
2515   }
2516
2517   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 0, "0") }
2518     qw(creditlimit creditremaining);
2519
2520   &update;
2521
2522   $lxdebug->leave_sub();
2523 }
2524
2525 sub ship_receive {
2526   $lxdebug->enter_sub();
2527
2528   &order_links;
2529
2530   &prepare_order;
2531
2532   OE->get_warehouses(\%myconfig, \%$form);
2533
2534   # warehouse
2535   if (@{ $form->{all_warehouses} }) {
2536     $form->{selectwarehouse} = "<option>\n";
2537
2538     map { $form->{selectwarehouse} .= "<option>$_->{description}--$_->{id}\n" }
2539       (@{ $form->{all_warehouses} });
2540
2541     if ($form->{warehouse}) {
2542       $form->{selectwarehouse} = "<option>$form->{warehouse}";
2543     }
2544   }
2545
2546   $form->{shippingdate} = $form->current_date(\%myconfig);
2547   $form->{"$form->{vc}"} =~ s/--.*//;
2548
2549   @flds  = ();
2550   @a     = ();
2551   $count = 0;
2552   foreach $key (keys %$form) {
2553     if ($key =~ /_1$/) {
2554       $key =~ s/_1//;
2555       push @flds, $key;
2556     }
2557   }
2558
2559   for $i (1 .. $form->{rowcount}) {
2560
2561     # undo formatting from prepare_order
2562     map {
2563       $form->{"${_}_$i"} =
2564         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
2565     } qw(qty ship);
2566     $n = ($form->{"qty_$i"} -= $form->{"ship_$i"});
2567     if (abs($n) > 0
2568         && ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"})) {
2569       $form->{"ship_$i"}         = "";
2570       $form->{"serialnumber_$i"} = "";
2571
2572       push @a, {};
2573       $j = $#a;
2574
2575       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2576       $count++;
2577     }
2578   }
2579
2580   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2581   $form->{rowcount} = $count;
2582
2583   &display_ship_receive;
2584
2585   $lxdebug->leave_sub();
2586 }
2587
2588 sub display_ship_receive {
2589   $lxdebug->enter_sub();
2590
2591   $vclabel = ucfirst $form->{vc};
2592   $vclabel = $locale->text($vclabel);
2593
2594   $form->{rowcount}++;
2595
2596   if ($form->{vc} eq 'customer') {
2597     $form->{title} = $locale->text('Ship Merchandise');
2598     $shipped = $locale->text('Shipping Date');
2599   } else {
2600     $form->{title} = $locale->text('Receive Merchandise');
2601     $shipped = $locale->text('Date Received');
2602   }
2603
2604   # set option selected
2605   foreach $item (warehouse, employee) {
2606     $form->{"select$item"} =~ s/ selected//;
2607     $form->{"select$item"} =~
2608       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
2609   }
2610
2611   $warehouse = qq|
2612               <tr>
2613                 <th align=right>| . $locale->text('Warehouse') . qq|</th>
2614                 <td><select name=warehouse>$form->{selectwarehouse}</select></td>
2615                 <input type=hidden name=selectwarehouse value="$form->{selectwarehouse}">
2616               </tr>
2617 | if $form->{selectwarehouse};
2618
2619   $employee = qq|
2620               <tr>
2621                 <th align=right nowrap>| . $locale->text('Contact') . qq|</th>
2622                 <td><select name=employee>$form->{selectemployee}</select></td>
2623                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
2624               </tr>
2625 |;
2626
2627   $form->header;
2628
2629   print qq|
2630 <body>
2631
2632 <form method=post action=$form->{script}>
2633
2634 <input type=hidden name=id value=$form->{id}>
2635
2636 <input type=hidden name=display_form value=display_ship_receive>
2637
2638 <input type=hidden name=type value=$form->{type}>
2639 <input type=hidden name=media value=$form->{media}>
2640 <input type=hidden name=format value=$form->{format}>
2641
2642 <input type=hidden name=queued value="$form->{queued}">
2643 <input type=hidden name=printed value="$form->{printed}">
2644 <input type=hidden name=emailed value="$form->{emailed}">
2645
2646 <input type=hidden name=vc value=$form->{vc}>
2647
2648 <table width=100%>
2649   <tr class=listtop>
2650     <th class=listtop>$form->{title}</th>
2651   </tr>
2652   <tr height="5"></tr>
2653   <tr>
2654     <td>
2655       <table width="100%">
2656         <tr valign=top>
2657           <td>
2658             <table width=100%>
2659               <tr>
2660                 <th align=right>$vclabel</th>
2661                 <td colspan=3>$form->{$form->{vc}}</td>
2662                 <input type=hidden name=$form->{vc} value="$form->{$form->{vc}}">
2663                 <input type=hidden name="$form->{vc}_id" value=$form->{"$form->{vc}_id"}>
2664               </tr>
2665               $department
2666               <tr>
2667                 <th align=right>| . $locale->text('Shipping Point') . qq|</th>
2668                 <td colspan=3>
2669                 <input name=shippingpoint size=35 value="$form->{shippingpoint}">
2670               </tr>
2671               <tr>
2672                 <th align=right>| . $locale->text('Ship via') . qq|</th>
2673                 <td colspan=3>
2674                 <input name=shipvia size=35 value="$form->{shipvia}">
2675               </tr>
2676               $warehouse
2677             </table>
2678           </td>
2679           <td align=right>
2680             <table>
2681               $employee
2682               <tr>
2683                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
2684                 <td>$form->{ordnumber}</td>
2685                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
2686               </tr>
2687               <tr>
2688                 <th align=right nowrap>| . $locale->text('Order Date') . qq|</th>
2689                 <td>$form->{transdate}</td>
2690                 <input type=hidden name=transdate value=$form->{transdate}>
2691               </tr>
2692               <tr>
2693                 <th align=right nowrap>$shipped</th>
2694                 <td><input name=shippingdate size=11 value=$form->{shippingdate}></td>
2695               </tr>
2696             </table>
2697           </td>
2698         </tr>
2699       </table>
2700     </td>
2701   </tr>
2702
2703 <!-- shipto are in hidden variables -->
2704
2705 <input type=hidden name=shiptoname value="$form->{shiptoname}">
2706 <input type=hidden name=shiptostreet value="$form->{shiptostreet}">
2707 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
2708 <input type=hidden name=shiptocity value="$form->{shiptocity}">
2709 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
2710 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
2711 <input type=hidden name=shiptophone value="$form->{shiptophone}">
2712 <input type=hidden name=shiptofax value="$form->{shiptofax}">
2713 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
2714 <input type=hidden name=shiptodepartment_1 value="$form->{shiptodepartment_1}">
2715 <input type=hidden name=shiptodepartment_2 value="$form->{shiptodepartment_2}">
2716
2717 <!-- email variables -->
2718 <input type=hidden name=message value="$form->{message}">
2719 <input type=hidden name=email value="$form->{email}">
2720 <input type=hidden name=subject value="$form->{subject}">
2721 <input type=hidden name=cc value="$form->{cc}">
2722 <input type=hidden name=bcc value="$form->{bcc}">
2723
2724 |;
2725
2726   @column_index =
2727     (partnumber, description, qty, ship, unit, bin, serialnumber);
2728
2729   if ($form->{type} eq "ship_order") {
2730     $column_data{ship} =
2731         qq|<th class=listheading align=center width="auto">|
2732       . $locale->text('Ship')
2733       . qq|</th>|;
2734   }
2735   if ($form->{type} eq "receive_order") {
2736     $column_data{ship} =
2737         qq|<th class=listheading align=center width="auto">|
2738       . $locale->text('Recd')
2739       . qq|</th>|;
2740   }
2741
2742   my $colspan = $#column_index + 1;
2743
2744   $column_data{partnumber} =
2745     qq|<th class=listheading nowrap>| . $locale->text('Number') . qq|</th>|;
2746   $column_data{description} =
2747       qq|<th class=listheading nowrap>|
2748     . $locale->text('Description')
2749     . qq|</th>|;
2750   $column_data{qty} =
2751     qq|<th class=listheading nowrap>| . $locale->text('Qty') . qq|</th>|;
2752   $column_data{unit} =
2753     qq|<th class=listheading nowrap>| . $locale->text('Unit') . qq|</th>|;
2754   $column_data{bin} =
2755     qq|<th class=listheading nowrap>| . $locale->text('Bin') . qq|</th>|;
2756   $column_data{serialnumber} =
2757       qq|<th class=listheading nowrap>|
2758     . $locale->text('Serial No.')
2759     . qq|</th>|;
2760
2761   print qq|
2762   <tr>
2763     <td>
2764       <table width=100%>
2765         <tr class=listheading>|;
2766
2767   map { print "\n$column_data{$_}" } @column_index;
2768
2769   print qq|
2770         </tr>
2771 |;
2772
2773   for $i (1 .. $form->{rowcount} - 1) {
2774
2775     # undo formatting
2776     $form->{"ship_$i"} = $form->parse_amount(\%myconfig, $form->{"ship_$i"});
2777
2778     # convert " to &quot;
2779     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2780       qw(partnumber description unit bin serialnumber);
2781
2782     $description = $form->{"description_$i"};
2783     $description =~ s/\n/<br>/g;
2784
2785     $column_data{partnumber} =
2786       qq|<td>$form->{"partnumber_$i"}<input type=hidden name="partnumber_$i" value="$form->{"partnumber_$i"}"></td>|;
2787     $column_data{description} =
2788       qq|<td>$description<input type=hidden name="description_$i" value="$form->{"description_$i"}"></td>|;
2789     $column_data{qty} =
2790         qq|<td align=right>|
2791       . $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty)
2792       . qq|<input type=hidden name="qty_$i" value="$form->{"qty_$i"}"></td>|;
2793     $column_data{ship} =
2794         qq|<td align=right><input name="ship_$i" size=5 value=|
2795       . $form->format_amount(\%myconfig, $form->{"ship_$i"})
2796       . qq|></td>|;
2797     $column_data{unit} =
2798       qq|<td>$form->{"unit_$i"}<input type=hidden name="unit_$i" value="$form->{"unit_$i"}"></td>|;
2799     $column_data{bin} =
2800       qq|<td>$form->{"bin_$i"}<input type=hidden name="bin_$i" value="$form->{"bin_$i"}"></td>|;
2801
2802     $column_data{serialnumber} =
2803       qq|<td><input name="serialnumber_$i" size=15 value="$form->{"serialnumber_$i"}"></td>|;
2804
2805     print qq|
2806         <tr valign=top>|;
2807
2808     map { print "\n$column_data{$_}" } @column_index;
2809
2810     print qq|
2811         </tr>
2812
2813 <input type=hidden name="orderitems_id_$i" value=$form->{"orderitems_id_$i"}>
2814 <input type=hidden name="id_$i" value=$form->{"id_$i"}>
2815 <input type=hidden name="assembly_$i" value="$form->{"assembly_$i"}">
2816 <input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">
2817
2818 |;
2819
2820   }
2821
2822   print qq|
2823       </table>
2824     </td>
2825   </tr>
2826   <tr>
2827     <td><hr size=3 noshade></td>
2828   </tr>
2829   <tr>
2830     <td>
2831 |;
2832
2833   $form->{copies} = 1;
2834
2835   &print_options;
2836
2837   print qq|
2838     </td>
2839   </tr>
2840 </table>
2841 <br>
2842 <input class=submit type=submit name=action value="|
2843     . $locale->text('Update') . qq|">
2844 <input class=submit type=submit name=action value="|
2845     . $locale->text('Print') . qq|">
2846 |;
2847
2848   if ($form->{type} eq 'ship_order') {
2849     print qq|
2850 <input class=submit type=submit name=action value="|
2851       . $locale->text('Ship to') . qq|">
2852 <input class=submit type=submit name=action value="|
2853       . $locale->text('E-mail') . qq|">
2854 |;
2855   }
2856
2857   print qq|
2858
2859 <input class=submit type=submit name=action value="|
2860     . $locale->text('Done') . qq|">
2861
2862 <input type=hidden name=rowcount value=$form->{rowcount}>
2863
2864 <input name=callback type=hidden value="$callback">
2865
2866 <input type=hidden name=path value=$form->{path}>
2867 <input type=hidden name=login value=$form->{login}>
2868 <input type=hidden name=password value=$form->{password}>
2869
2870 </form>
2871
2872 </body>
2873 </html>
2874 |;
2875
2876   $lxdebug->leave_sub();
2877 }
2878
2879 sub done {
2880   $lxdebug->enter_sub();
2881
2882   if ($form->{type} eq 'ship_order') {
2883     $form->isblank("shippingdate", $locale->text('Shipping Date missing!'));
2884   } else {
2885     $form->isblank("shippingdate", $locale->text('Date received missing!'));
2886   }
2887
2888   $total = 0;
2889   map {
2890     $total += $form->{"ship_$_"} =
2891       $form->parse_amount(\%myconfig, $form->{"ship_$_"})
2892   } (1 .. $form->{rowcount} - 1);
2893
2894   $form->error($locale->text('Nothing entered!')) unless $total;
2895
2896   $form->redirect($locale->text('Inventory saved!'))
2897     if OE->save_inventory(\%myconfig, \%$form);
2898   $form->error($locale->text('Could not save!'));
2899
2900   $lxdebug->leave_sub();
2901 }
2902
2903 sub search_transfer {
2904   $lxdebug->enter_sub();
2905
2906   OE->get_warehouses(\%myconfig, \%$form);
2907
2908   # warehouse
2909   if (@{ $form->{all_warehouses} }) {
2910     $form->{selectwarehouse} = "<option>\n";
2911     $form->{warehouse}       = qq|$form->{warehouse}--$form->{warehouse_id}|;
2912
2913     map { $form->{selectwarehouse} .= "<option>$_->{description}--$_->{id}\n" }
2914       (@{ $form->{all_warehouses} });
2915   } else {
2916     $form->error($locale->text('Nothing to transfer!'));
2917   }
2918
2919   $form->{title} = $locale->text('Transfer Inventory');
2920
2921   $form->header;
2922
2923   print qq|
2924 <body>
2925
2926 <form method=post action=$form->{script}>
2927
2928 <table width=100%>
2929   <tr>
2930     <th class=listtop>$form->{title}</th>
2931   </tr>
2932   <tr height="5"></tr>
2933   <tr>
2934     <td>
2935       <table>
2936         <tr>
2937           <th align=right nowrap>| . $locale->text('Transfer to') . qq|</th>
2938           <td><select name=warehouse>$form->{selectwarehouse}</select></td>
2939         </tr>
2940         <tr>
2941           <th align="right" nowrap="true">| . $locale->text('Part Number') . qq|</th>
2942           <td><input name=partnumber size=20></td>
2943         </tr>
2944         <tr>
2945           <th align="right" nowrap="true">| . $locale->text('Description') . qq|</th>
2946           <td><input name=description size=40></td>
2947         </tr>
2948         <tr>
2949           <th align=right nowrap>| . $locale->text('Group') . qq|</th>
2950           <td><input name=partsgroup size=20></td>
2951         </tr>
2952       </table>
2953     </td>
2954   </tr>
2955   <tr>
2956     <td><hr size=3 noshade></td>
2957   </tr>
2958 </table>
2959
2960 <br>
2961 <input type=hidden name=sort value=partnumber>
2962 <input type=hidden name=nextsub value=list_transfer>
2963
2964 <input type=hidden name=path value=$form->{path}>
2965 <input type=hidden name=login value=$form->{login}>
2966 <input type=hidden name=password value=$form->{password}>
2967
2968 <input class=submit type=submit name=action value="|
2969     . $locale->text('Continue') . qq|">
2970 </form>
2971
2972 </body>
2973 </html>
2974 |;
2975
2976   $lxdebug->leave_sub();
2977 }
2978
2979 sub list_transfer {
2980   $lxdebug->enter_sub();
2981
2982   OE->get_inventory(\%myconfig, \%$form);
2983
2984   $partnumber  = $form->escape($form->{partnumber});
2985   $warehouse   = $form->escape($form->{warehouse});
2986   $description = $form->escape($form->{description});
2987   $partsgroup  = $form->escape($form->{partsgroup});
2988
2989   # construct href
2990   $href =
2991     "$form->{script}?path=$form->{path}&action=list_transfer&partnumber=$partnumber&warehouse=$warehouse&description=$description&partsgroup=$partsgroup&login=$form->{login}&password=$form->{password}";
2992
2993   # construct callback
2994   $partnumber  = $form->escape($form->{partnumber},  1);
2995   $warehouse   = $form->escape($form->{warehouse},   1);
2996   $description = $form->escape($form->{description}, 1);
2997   $partsgroup  = $form->escape($form->{partsgroup},  1);
2998
2999   $callback =
3000     "$form->{script}?path=$form->{path}&action=list_transfer&partnumber=$partnumber&warehouse=$warehouse&description=$description&partsgroup=$partsgroup&login=$form->{login}&password=$form->{password}";
3001
3002   @column_index =
3003     $form->sort_columns(
3004       qw(partnumber description partsgroup make model warehouse qty transfer));
3005
3006   $column_header{partnumber} =
3007       qq|<th><a class=listheading href=$href&sort=partnumber>|
3008     . $locale->text('Part Number')
3009     . qq|</a></th>|;
3010   $column_header{description} =
3011       qq|<th><a class=listheading href=$href&sort=description>|
3012     . $locale->text('Description')
3013     . qq|</a></th>|;
3014   $column_header{partsgroup} =
3015       qq|<th><a class=listheading href=$href&sort=partsgroup>|
3016     . $locale->text('Group')
3017     . qq|</a></th>|;
3018   $column_header{warehouse} =
3019       qq|<th><a class=listheading href=$href&sort=warehouse>|
3020     . $locale->text('From')
3021     . qq|</a></th>|;
3022   $column_header{qty} =
3023     qq|<th><a class=listheading>| . $locale->text('Qty') . qq|</a></th>|;
3024   $column_header{transfer} =
3025     qq|<th><a class=listheading>| . $locale->text('Transfer') . qq|</a></th>|;
3026
3027   $option = $locale->text('Transfer to');
3028
3029   ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
3030
3031   if ($form->{warehouse}) {
3032     $option .= " : $warehouse";
3033   }
3034   if ($form->{partnumber}) {
3035     $option .= "\n<br>" if ($option);
3036     $option .= $locale->text('Part Number') . " : $form->{partnumber}";
3037   }
3038   if ($form->{description}) {
3039     $option .= "\n<br>" if ($option);
3040     $option .= $locale->text('Description') . " : $form->{description}";
3041   }
3042   if ($form->{partsgroup}) {
3043     $option .= "\n<br>" if ($option);
3044     $option .= $locale->text('Group') . " : $form->{partsgroup}";
3045   }
3046
3047   $form->{title} = $locale->text('Transfer Inventory');
3048
3049   $form->header;
3050
3051   print qq|
3052 <body>
3053
3054 <form method=post action=$form->{script}>
3055
3056 <input type=hidden name=warehouse_id value=$warehouse_id>
3057
3058 <table width=100%>
3059   <tr>
3060     <th class=listtop>$form->{title}</th>
3061   </tr>
3062   <tr height="5"></tr>
3063   <tr>
3064     <td>$option</td>
3065   </tr>
3066   <tr>
3067     <td>
3068       <table width=100%>
3069         <tr class=listheading>|;
3070
3071   map { print "\n$column_header{$_}" } @column_index;
3072
3073   print qq|
3074         </tr>
3075 |;
3076
3077   if (@{ $form->{all_inventory} }) {
3078     $sameitem = $form->{all_inventory}->[0]->{ $form->{sort} };
3079   }
3080
3081   $i = 0;
3082   foreach $ref (@{ $form->{all_inventory} }) {
3083
3084     $i++;
3085
3086     $column_data{partnumber} =
3087       qq|<td><input type=hidden name="id_$i" value=$ref->{id}>$ref->{partnumber}</td>|;
3088     $column_data{description} = "<td>$ref->{description}&nbsp;</td>";
3089     $column_data{partsgroup}  = "<td>$ref->{partsgroup}&nbsp;</td>";
3090     $column_data{warehouse}   =
3091       qq|<td><input type=hidden name="warehouse_id_$i" value=$ref->{warehouse_id}>$ref->{warehouse}&nbsp;</td>|;
3092     $column_data{qty} =
3093         qq|<td><input type=hidden name="qty_$i" value=$ref->{qty}>|
3094       . $form->format_amount(\%myconfig, $ref->{qty}, $dec_qty)
3095       . qq|</td>|;
3096     $column_data{transfer} = qq|<td><input name="transfer_$i" size=4></td>|;
3097
3098     $j++;
3099     $j %= 2;
3100     print "
3101         <tr class=listrow$j>";
3102
3103     map { print "\n$column_data{$_}" } @column_index;
3104
3105     print qq|
3106         </tr>
3107 |;
3108
3109   }
3110
3111   print qq|
3112       </table>
3113     </td>
3114   </tr>
3115
3116   <tr>
3117     <td><hr size=3 noshade></td>
3118   </tr>
3119 </table>
3120
3121 <br>
3122
3123 <input name=callback type=hidden value="$callback">
3124
3125 <input type=hidden name=rowcount value=$i>
3126
3127 <input type=hidden name=path value=$form->{path}>
3128 <input type=hidden name=login value=$form->{login}>
3129 <input type=hidden name=password value=$form->{password}>
3130
3131 <input class=submit type=submit name=action value="|
3132     . $locale->text('Transfer') . qq|">
3133
3134 </form>
3135
3136 </body>
3137 </html>
3138 |;
3139
3140   $lxdebug->leave_sub();
3141 }
3142
3143 sub transfer {
3144   $lxdebug->enter_sub();
3145
3146   $form->redirect($locale->text('Inventory transferred!'))
3147     if OE->transfer(\%myconfig, \%$form);
3148   $form->error($locale->text('Could not transfer Inventory!'));
3149
3150   $lxdebug->leave_sub();
3151 }