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