Bei Lieferantenaufträgen war ein Button fälschlicherweise mit "Angebot" beschriftet...
[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     $form->{"selected_unit_$i"} = $form->{"unit_$i"};
1177
1178     if (   $form->{type} eq 'purchase_order'
1179         || $form->{type} eq 'request_quotation') {
1180       IR->retrieve_item(\%myconfig, \%$form);
1181     }
1182     if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
1183       IS->retrieve_item(\%myconfig, \%$form);
1184     }
1185
1186     my $rows = scalar @{ $form->{item_list} };
1187
1188     $form->{"discount_$i"} =
1189       $form->format_amount(\%myconfig, $form->{discount} * 100);
1190
1191     if ($rows) {
1192       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1193
1194       if ($rows > 1) {
1195
1196         &select_item;
1197         exit;
1198
1199       } else {
1200
1201         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1202         if ($form->{"not_discountable_$i"}) {
1203           $form->{"discount_$i"} = 0;
1204         }
1205         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1206           qw(partnumber description unit);
1207         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1208           keys %{ $form->{item_list}[0] };
1209         if ($form->{"part_payment_id_$i"} ne "") {
1210           $form->{payment_id} = $form->{"part_payment_id_$i"};
1211         }
1212
1213         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1214
1215         ($dec) = ($s =~ /\.(\d+)/);
1216         $dec           = length $dec;
1217         $decimalplaces = ($dec > 2) ? $dec : 2;
1218
1219         if ($sellprice) {
1220           $form->{"sellprice_$i"} = $sellprice;
1221         } else {
1222
1223           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1224
1225           # if there is an exchange rate adjust sellprice
1226           $form->{"sellprice_$i"} /= $exchangerate;
1227         }
1228
1229         $amount =
1230           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1231           (1 - $form->{"discount_$i"} / 100);
1232         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1233         map { $form->{"${_}_base"} += $amount }
1234           (split / /, $form->{"taxaccounts_$i"});
1235         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1236           split / /, $form->{taxaccounts}
1237           if !$form->{taxincluded};
1238
1239         $form->{creditremaining} -= $amount;
1240
1241         $form->{"sellprice_$i"} =
1242           $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1243                                $decimalplaces);
1244         $form->{"qty_$i"} =
1245           $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1246
1247         # get pricegroups for parts
1248         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1249
1250         # build up html code for prices_$i
1251         &set_pricegroup($i);
1252       }
1253
1254       &display_form;
1255
1256     } else {
1257
1258       # ok, so this is a new part
1259       # ask if it is a part or service item
1260
1261       if (   $form->{"partsgroup_$i"}
1262           && ($form->{"partsnumber_$i"} eq "")
1263           && ($form->{"description_$i"} eq "")) {
1264         $form->{rowcount}--;
1265         $form->{"discount_$i"} = "";
1266         &display_form;
1267       } else {
1268
1269         $form->{"id_$i"}   = 0;
1270         $form->{"unit_$i"} = $locale->text('ea');
1271
1272         &new_item;
1273
1274       }
1275     }
1276   }
1277
1278   $lxdebug->leave_sub();
1279 }
1280
1281 sub search {
1282   $lxdebug->enter_sub();
1283
1284   if ($form->{type} eq 'purchase_order') {
1285     $form->{title} = $locale->text('Purchase Orders');
1286     $form->{vc}    = 'vendor';
1287     $ordlabel      = $locale->text('Order Number');
1288     $ordnumber     = 'ordnumber';
1289     $employee      = $locale->text('Employee');
1290   }
1291   if ($form->{type} eq 'request_quotation') {
1292     $form->{title} = $locale->text('Request for Quotations');
1293     $form->{vc}    = 'vendor';
1294     $ordlabel      = $locale->text('RFQ Number');
1295     $ordnumber     = 'quonumber';
1296     $employee      = $locale->text('Employee');
1297   }
1298   if ($form->{type} eq 'receive_order') {
1299     $form->{title} = $locale->text('Receive Merchandise');
1300     $form->{vc}    = 'vendor';
1301     $ordlabel      = $locale->text('Order Number');
1302     $ordnumber     = 'ordnumber';
1303     $employee      = $locale->text('Employee');
1304   }
1305   if ($form->{type} eq 'sales_order') {
1306     $form->{title} = $locale->text('Sales Orders');
1307     $form->{vc}    = 'customer';
1308     $ordlabel      = $locale->text('Order Number');
1309     $ordnumber     = 'ordnumber';
1310     $employee      = $locale->text('Salesperson');
1311   }
1312   if ($form->{type} eq 'ship_order') {
1313     $form->{title} = $locale->text('Ship Merchandise');
1314     $form->{vc}    = 'customer';
1315     $ordlabel      = $locale->text('Order Number');
1316     $ordnumber     = 'ordnumber';
1317     $employee      = $locale->text('Salesperson');
1318
1319   }
1320
1321   if ($form->{type} eq 'sales_quotation') {
1322     $form->{title} = $locale->text('Quotations');
1323     $form->{vc}    = 'customer';
1324     $ordlabel      = $locale->text('Quotation Number');
1325     $ordnumber     = 'quonumber';
1326     $employee      = $locale->text('Employee');
1327   }
1328
1329   if ($form->{type} =~ /(ship|receive)_order/) {
1330     OE->get_warehouses(\%myconfig, \%$form);
1331
1332     # warehouse
1333     if (@{ $form->{all_warehouses} }) {
1334       $form->{selectwarehouse} = "<option>\n";
1335       $form->{warehouse}       = qq|$form->{warehouse}--$form->{warehouse_id}|;
1336
1337       map {
1338         $form->{selectwarehouse} .=
1339           "<option>$_->{description}--$_->{id}\n"
1340       } (@{ $form->{all_warehouses} });
1341
1342       $warehouse = qq|
1343               <tr>
1344                 <th align=right>| . $locale->text('Warehouse') . qq|</th>
1345                 <td colspan=3><select name=warehouse>$form->{selectwarehouse}</select></td>
1346                 <input type=hidden name=selectwarehouse value="$form->{selectwarehouse}">
1347               </tr>
1348 |;
1349
1350     }
1351   }
1352
1353   # setup vendor / customer selection
1354   $form->all_vc(\%myconfig, $form->{vc},
1355                 ($form->{vc} eq 'customer') ? "AR" : "AP");
1356
1357   map { $vc .= "<option>$_->{name}--$_->{id}\n" }
1358     @{ $form->{"all_$form->{vc}"} };
1359
1360   $vclabel = ucfirst $form->{vc};
1361   $vclabel = $locale->text($vclabel);
1362
1363   # $locale->text('Vendor')
1364   # $locale->text('Customer')
1365
1366   $vc =
1367     ($vc)
1368     ? qq|<select name=$form->{vc}><option>\n$vc</select>|
1369     : qq|<input name=$form->{vc} size=35>|;
1370
1371   # departments
1372   if (@{ $form->{all_departments} }) {
1373     $form->{selectdepartment} = "<option>\n";
1374
1375     map {
1376       $form->{selectdepartment} .=
1377         "<option>$_->{description}--$_->{id}\n"
1378     } (@{ $form->{all_departments} });
1379   }
1380
1381   $department = qq|
1382         <tr>
1383           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1384           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1385         </tr>
1386 | if $form->{selectdepartment};
1387
1388   if ($form->{type} !~ /(ship_order|receive_order)/) {
1389     $openclosed = qq|
1390               <tr>
1391                 <td><input name="open" class=checkbox type=checkbox value=1 checked> |
1392       . $locale->text('Open') . qq|</td>
1393                 <td><input name="closed" class=checkbox type=checkbox value=1 $form->{closed}> |
1394       . $locale->text('Closed') . qq|</td>
1395               </tr>
1396 |;
1397   } else {
1398
1399     $openclosed = qq|
1400                 <input type=hidden name="open" value=1>
1401 |;
1402   }
1403
1404   # use JavaScript Calendar or not
1405   $form->{jsscript} = $jscalendar;
1406   $jsscript = "";
1407   if ($form->{jsscript}) {
1408
1409     # with JavaScript Calendar
1410     $button1 = qq|
1411        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
1412        <input type=button name=transdatefrom id="trigger3" value=|
1413       . $locale->text('button') . qq|></td>
1414       |;
1415     $button2 = qq|
1416        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
1417        <input type=button name=transdateto name=transdateto id="trigger4" value=|
1418       . $locale->text('button') . qq|></td>
1419      |;
1420
1421     #write Trigger
1422     $jsscript =
1423       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
1424                           "transdateto", "BL", "trigger4");
1425   } else {
1426
1427     # without JavaScript Calendar
1428     $button1 = qq|
1429                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
1430     $button2 = qq|
1431                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
1432   }
1433
1434   $form->header;
1435
1436   print qq|
1437 <body>
1438
1439 <form method=post action=$form->{script}>
1440
1441 <table width=100%>
1442   <tr>
1443     <th class=listtop>$form->{title}</th>
1444   </tr>
1445   <tr height="5"></tr>
1446   <tr>
1447     <td>
1448       <table>
1449         <tr>
1450           <th align=right>$vclabel</th>
1451           <td colspan=3>$vc</td>
1452         </tr>
1453         $warehouse
1454         $department
1455         <tr>
1456           <th align=right>$ordlabel</th>
1457           <td colspan=3><input name="$ordnumber" size=20></td>
1458         </tr>
1459         <tr>
1460           <th align=right>| . $locale->text('From') . qq|</th>
1461           $button1
1462           <th align=right>| . $locale->text('Bis') . qq|</th>
1463           $button2
1464         </tr>
1465         <input type=hidden name=sort value=transdate>
1466         <tr>
1467           <th align=right>| . $locale->text('Include in Report') . qq|</th>
1468           <td colspan=5>
1469             <table>
1470               $openclosed
1471               <tr>
1472                 <td><input name="l_id" class=checkbox type=checkbox value=Y>
1473                 | . $locale->text('ID') . qq|</td>
1474                 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
1475                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> |
1476     . $locale->text('Date') . qq|</td>
1477                 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> |
1478     . $locale->text('Required by') . qq|</td>
1479               </tr>
1480               <tr>
1481                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vclabel</td>
1482                 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
1483                 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> |
1484     . $locale->text('Ship via') . qq|</td>
1485               </tr>
1486               <tr>
1487                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> |
1488     . $locale->text('Amount') . qq|</td>
1489                 <td><input name="l_tax" class=checkbox type=checkbox value=Y> |
1490     . $locale->text('Tax') . qq|</td>
1491                 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> |
1492     . $locale->text('Total') . qq|</td>
1493               </tr>
1494               <tr>
1495                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> |
1496     . $locale->text('Subtotal') . qq|</td>
1497               </tr>
1498             </table>
1499           </td>
1500         </tr>
1501       </table>
1502     </td>
1503   </tr>
1504   <tr><td colspan=4><hr size=3 noshade></td></tr>
1505 </table>
1506
1507 $jsscript
1508
1509 <br>
1510 <input type=hidden name=nextsub value=orders>
1511 <input type=hidden name=path value=$form->{path}>
1512 <input type=hidden name=login value=$form->{login}>
1513 <input type=hidden name=password value=$form->{password}>
1514 <input type=hidden name=vc value=$form->{vc}>
1515 <input type=hidden name=type value=$form->{type}>
1516
1517 <input class=submit type=submit name=action value="|
1518     . $locale->text('Continue') . qq|">
1519 </form>
1520
1521 </body>
1522 </html>
1523 |;
1524
1525   $lxdebug->leave_sub();
1526 }
1527
1528 sub orders {
1529   $lxdebug->enter_sub();
1530
1531   # split vendor / customer
1532   ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) =
1533     split(/--/, $form->{ $form->{vc} });
1534
1535   OE->transactions(\%myconfig, \%$form);
1536
1537   $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
1538
1539   $number     = $form->escape($form->{$ordnumber});
1540   $name       = $form->escape($form->{ $form->{vc} });
1541   $department = $form->escape($form->{department});
1542   $warehouse  = $form->escape($form->{warehouse});
1543
1544   # construct href
1545   $href =
1546     "$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";
1547
1548   # construct callback
1549   $number     = $form->escape($form->{$ordnumber},    1);
1550   $name       = $form->escape($form->{ $form->{vc} }, 1);
1551   $department = $form->escape($form->{department},    1);
1552   $warehouse  = $form->escape($form->{warehouse},     1);
1553
1554   $callback =
1555     "$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";
1556
1557   @columns =
1558     $form->sort_columns("transdate", "reqdate",   "id",      "$ordnumber",
1559                         "name",      "netamount", "tax",     "amount",
1560                         "curr",      "employee",  "shipvia", "open",
1561                         "closed");
1562
1563   $form->{l_open} = $form->{l_closed} = "Y"
1564     if ($form->{open} && $form->{closed});
1565
1566   foreach $item (@columns) {
1567     if ($form->{"l_$item"} eq "Y") {
1568       push @column_index, $item;
1569
1570       # add column to href and callback
1571       $callback .= "&l_$item=Y";
1572       $href     .= "&l_$item=Y";
1573     }
1574   }
1575
1576   # only show checkboxes if gotten here via sales_order form.
1577   if ($form->{type} =~ /sales_order/) {
1578     unshift @column_index, "ids";
1579   }
1580
1581   if ($form->{l_subtotal} eq 'Y') {
1582     $callback .= "&l_subtotal=Y";
1583     $href     .= "&l_subtotal=Y";
1584   }
1585
1586   if ($form->{vc} eq 'vendor') {
1587     if ($form->{type} eq 'receive_order') {
1588       $form->{title} = $locale->text('Receive Merchandise');
1589     } elsif ($form->{type} eq 'purchase_order') {
1590       $form->{title} = $locale->text('Purchase Orders');
1591     } else {
1592       $form->{title} = $locale->text('Request for Quotations');
1593     }
1594     $name     = $locale->text('Vendor');
1595     $employee = $locale->text('Employee');
1596   }
1597   if ($form->{vc} eq 'customer') {
1598     if ($form->{type} eq 'sales_order') {
1599       $form->{title} = $locale->text('Sales Orders');
1600       $employee = $locale->text('Salesperson');
1601     } elsif ($form->{type} eq 'ship_order') {
1602       $form->{title} = $locale->text('Ship Merchandise');
1603       $employee = $locale->text('Salesperson');
1604     } else {
1605       $form->{title} = $locale->text('Quotations');
1606       $employee = $locale->text('Employee');
1607     }
1608     $name = $locale->text('Customer');
1609   }
1610
1611   $column_header{id} =
1612       qq|<th><a class=listheading href=$href&sort=id>|
1613     . $locale->text('ID')
1614     . qq|</a></th>|;
1615   $column_header{transdate} =
1616       qq|<th><a class=listheading href=$href&sort=transdate>|
1617     . $locale->text('Date')
1618     . qq|</a></th>|;
1619   $column_header{reqdate} =
1620       qq|<th><a class=listheading href=$href&sort=reqdate>|
1621     . $locale->text('Required by')
1622     . qq|</a></th>|;
1623   $column_header{ordnumber} =
1624       qq|<th><a class=listheading href=$href&sort=ordnumber>|
1625     . $locale->text('Order')
1626     . qq|</a></th>|;
1627   $column_header{quonumber} =
1628       qq|<th><a class=listheading href=$href&sort=quonumber>|
1629     . $locale->text('Quotation')
1630     . qq|</a></th>|;
1631   $column_header{name} =
1632     qq|<th><a class=listheading href=$href&sort=name>$name</a></th>|;
1633   $column_header{netamount} =
1634     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1635   $column_header{tax} =
1636     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1637   $column_header{amount} =
1638     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1639   $column_header{curr} =
1640     qq|<th class=listheading>| . $locale->text('Curr') . qq|</th>|;
1641   $column_header{shipvia} =
1642       qq|<th><a class=listheading href=$href&sort=shipvia>|
1643     . $locale->text('Ship via')
1644     . qq|</a></th>|;
1645   $column_header{open} =
1646     qq|<th class=listheading>| . $locale->text('O') . qq|</th>|;
1647   $column_header{closed} =
1648     qq|<th class=listheading>| . $locale->text('C') . qq|</th>|;
1649
1650   $column_header{employee} =
1651     qq|<th><a class=listheading href=$href&sort=employee>$employee</a></th>|;
1652
1653   $column_header{ids} = qq|<th></th>|;
1654
1655   if ($form->{ $form->{vc} }) {
1656     $option = $locale->text(ucfirst $form->{vc});
1657     $option .= " : $form->{$form->{vc}}";
1658   }
1659   if ($form->{warehouse}) {
1660     ($warehouse) = split /--/, $form->{warehouse};
1661     $option .= "\n<br>" if ($option);
1662     $option .= $locale->text('Warehouse');
1663     $option .= " : $warehouse";
1664   }
1665   if ($form->{department}) {
1666     $option .= "\n<br>" if ($option);
1667     ($department) = split /--/, $form->{department};
1668     $option .= $locale->text('Department') . " : $department";
1669   }
1670   if ($form->{transdatefrom}) {
1671     $option .= "\n<br>"
1672       . $locale->text('From') . " "
1673       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1674   }
1675   if ($form->{transdateto}) {
1676     $option .= "\n<br>"
1677       . $locale->text('Bis') . " "
1678       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1679   }
1680   if ($form->{open}) {
1681     $option .= "\n<br>" if ($option);
1682     $option .= $locale->text('Open');
1683   }
1684   if ($form->{closed}) {
1685     $option .= "\n<br>" if ($option);
1686     $option .= $locale->text('Closed');
1687   }
1688
1689   $form->header;
1690
1691   print qq|
1692 <body>
1693
1694 <form method="post" action="oe.pl">
1695 <table width=100%>
1696   <tr>
1697     <th class=listtop>$form->{title}</th>
1698   </tr>
1699   <tr height="5"></tr>
1700   <tr>
1701     <td>$option</td>
1702   </tr>
1703   <tr>
1704     <td>
1705       <table width=100%>
1706         <tr class=listheading>|;
1707
1708   map { print "\n$column_header{$_}" } @column_index;
1709
1710   print qq|
1711         </tr>
1712 |;
1713
1714   # add sort and escape callback
1715   $callback_escaped = $form->escape($callback . "&sort=$form->{sort}");
1716
1717   if (@{ $form->{OE} }) {
1718     $sameitem = $form->{OE}->[0]->{ $form->{sort} };
1719   }
1720
1721   $action = "edit";
1722   $action = "ship_receive" if ($form->{type} =~ /(ship|receive)_order/);
1723
1724   $warehouse = $form->escape($form->{warehouse});
1725
1726   foreach $oe (@{ $form->{OE} }) {
1727     $form->{rowcount} = ++$j;
1728
1729     if ($form->{l_subtotal} eq 'Y') {
1730       if ($sameitem ne $oe->{ $form->{sort} }) {
1731         &subtotal;
1732         $sameitem = $oe->{ $form->{sort} };
1733       }
1734     }
1735
1736     map { $oe->{$_} *= $oe->{exchangerate} } (qw(netamount amount));
1737
1738     $column_data{netamount} =
1739         "<td align=right>"
1740       . $form->format_amount(\%myconfig, $oe->{netamount}, 2, "&nbsp;")
1741       . "</td>";
1742     $column_data{tax} = "<td align=right>"
1743       . $form->format_amount(\%myconfig, $oe->{amount} - $oe->{netamount},
1744                              2, "&nbsp;")
1745       . "</td>";
1746     $column_data{amount} =
1747       "<td align=right>"
1748       . $form->format_amount(\%myconfig, $oe->{amount}, 2, "&nbsp;") . "</td>";
1749
1750     $totalnetamount += $oe->{netamount};
1751     $totalamount    += $oe->{amount};
1752
1753     $subtotalnetamount += $oe->{netamount};
1754     $subtotalamount    += $oe->{amount};
1755
1756     $column_data{ids} =
1757       qq|<td><input name="id_$j" class=checkbox type=checkbox><input type="hidden" name="trans_id_$j" value="$oe->{id}"></td>|;
1758     $column_data{id}        = "<td>$oe->{id}</td>";
1759     $column_data{transdate} = "<td>$oe->{transdate}&nbsp;</td>";
1760     $column_data{reqdate}   = "<td>$oe->{reqdate}&nbsp;</td>";
1761
1762     $column_data{$ordnumber} =
1763       "<td><a href=oe.pl?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>";
1764     $column_data{name} = "<td>$oe->{name}</td>";
1765
1766     $column_data{employee} = "<td>$oe->{employee}&nbsp;</td>";
1767     $column_data{shipvia}  = "<td>$oe->{shipvia}&nbsp;</td>";
1768
1769     if ($oe->{closed}) {
1770       $column_data{closed} = "<td align=center>X</td>";
1771       $column_data{open}   = "<td>&nbsp;</td>";
1772     } else {
1773       $column_data{closed} = "<td>&nbsp;</td>";
1774       $column_data{open}   = "<td align=center>X</td>";
1775     }
1776
1777     $i++;
1778     $i %= 2;
1779     print "
1780         <tr class=listrow$i>";
1781
1782     map { print "\n$column_data{$_}" } @column_index;
1783
1784     print qq|
1785         </tr>
1786 |;
1787
1788   }
1789
1790   if ($form->{l_subtotal} eq 'Y') {
1791     &subtotal;
1792   }
1793
1794   # print totals
1795   print qq|
1796         <tr class=listtotal>|;
1797
1798   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1799
1800   $column_data{netamount} =
1801     "<th class=listtotal align=right>"
1802     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1803   $column_data{tax} = "<th class=listtotal align=right>"
1804     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1805                            2, "&nbsp;")
1806     . "</th>";
1807   $column_data{amount} =
1808     "<th class=listtotal align=right>"
1809     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1810
1811   map { print "\n$column_data{$_}" } @column_index;
1812
1813   print qq|
1814         </tr>
1815       </td>
1816     </table>
1817   </tr>
1818   <tr>
1819     <td><hr size=3 noshade></td>
1820   </tr>
1821 </table>|;
1822
1823   # multiple invoice edit button only if gotten there via sales_order form.
1824
1825   if ($form->{type} =~ /sales_order/) {
1826     print qq|
1827   <input type="hidden" name="path" value="$form->{path}">
1828   <input class"submit" type="submit" name="action" value="|
1829       . $locale->text('Continue') . qq|">
1830   <input type="hidden" name="nextsub" value="edit">
1831   <input type="hidden" name="type" value="$form->{type}">
1832   <input type="hidden" name="warehouse" value="$warehouse">
1833   <input type="hidden" name="vc" value="$form->{vc}">
1834   <input type="hidden" name="login" value="$form->{login}">
1835   <input type="hidden" name="password" value="$form->{password}">
1836   <input type="hidden" name="callback" value="$callback">
1837   <input type="hidden" name="rowcount" value="$form->{rowcount}">|;
1838   }
1839
1840   print qq|
1841 </form>
1842
1843 <br>
1844 <form method=post action=$form->{script}>
1845
1846 <input name=callback type=hidden value="$form->{callback}">
1847
1848 <input type=hidden name=type value=$form->{type}>
1849 <input type=hidden name=vc value=$form->{vc}>
1850
1851 <input type=hidden name=path value=$form->{path}>
1852 <input type=hidden name=login value=$form->{login}>
1853 <input type=hidden name=password value=$form->{password}>
1854 |;
1855
1856   if ($form->{type} !~ /(ship|receive)_order/) {
1857     print qq|
1858 <input class=submit type=submit name=action value="|
1859       . $locale->text('Add') . qq|">|;
1860   }
1861
1862   if ($form->{menubar}) {
1863     require "$form->{path}/menu.pl";
1864     &menubar;
1865   }
1866
1867   print qq|
1868 </form>
1869
1870 </body>
1871 </html>
1872 |;
1873
1874   $lxdebug->leave_sub();
1875 }
1876
1877 sub subtotal {
1878   $lxdebug->enter_sub();
1879
1880   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1881
1882   $column_data{netamount} =
1883       "<th class=listsubtotal align=right>"
1884     . $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")
1885     . "</th>";
1886   $column_data{tax} = "<td class=listsubtotal align=right>"
1887     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1888                            2, "&nbsp;")
1889     . "</th>";
1890   $column_data{amount} =
1891     "<th class=listsubtotal align=right>"
1892     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1893
1894   $subtotalnetamount = 0;
1895   $subtotalamount    = 0;
1896
1897   print "
1898         <tr class=listsubtotal>
1899 ";
1900
1901   map { print "\n$column_data{$_}" } @column_index;
1902
1903   print qq|
1904         </tr>
1905 |;
1906
1907   $lxdebug->leave_sub();
1908 }
1909
1910 sub save_and_close {
1911   $lxdebug->enter_sub();
1912
1913   if ($form->{type} =~ /_order$/) {
1914     $form->isblank("transdate", $locale->text('Order Date missing!'));
1915   } else {
1916     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1917   }
1918
1919   $msg = ucfirst $form->{vc};
1920   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1921
1922   # $locale->text('Customer missing!');
1923   # $locale->text('Vendor missing!');
1924
1925   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1926     if ($form->{currency} ne $form->{defaultcurrency});
1927
1928   &validate_items;
1929
1930   # if the name changed get new values
1931   if (&check_name($form->{vc})) {
1932     &update;
1933     exit;
1934   }
1935
1936   $form->{id} = 0 if $form->{saveasnew};
1937
1938   # this is for the internal notes section for the [email] Subject
1939   if ($form->{type} =~ /_order$/) {
1940     if ($form->{type} eq 'sales_order') {
1941       $form->{label} = $locale->text('Sales Order');
1942
1943       $numberfld = "sonumber";
1944       $ordnumber = "ordnumber";
1945     } else {
1946       $form->{label} = $locale->text('Purchase Order');
1947
1948       $numberfld = "ponumber";
1949       $ordnumber = "ordnumber";
1950     }
1951
1952     $err = $locale->text('Cannot save order!');
1953
1954   } else {
1955     if ($form->{type} eq 'sales_quotation') {
1956       $form->{label} = $locale->text('Quotation');
1957
1958       $numberfld = "sqnumber";
1959       $ordnumber = "quonumber";
1960     } else {
1961       $form->{label} = $locale->text('Request for Quotation');
1962
1963       $numberfld = "rfqnumber";
1964       $ordnumber = "quonumber";
1965     }
1966
1967     $err = $locale->text('Cannot save quotation!');
1968
1969   }
1970
1971   # get new number in sequence if no number is given or if saveasnew was requested
1972   if (!$form->{$ordnumber} || $form->{saveasnew}) {
1973     $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1974   }
1975
1976   relink_accounts();
1977
1978   $form->redirect(
1979             $form->{label} . " $form->{$ordnumber} " . $locale->text('saved!'))
1980     if (OE->save(\%myconfig, \%$form));
1981   $form->error($err);
1982
1983   $lxdebug->leave_sub();
1984 }
1985
1986 sub save {
1987   $lxdebug->enter_sub();
1988
1989   if ($form->{type} =~ /_order$/) {
1990     $form->isblank("transdate", $locale->text('Order Date missing!'));
1991   } else {
1992     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1993   }
1994
1995   $msg = ucfirst $form->{vc};
1996   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1997
1998   # $locale->text('Customer missing!');
1999   # $locale->text('Vendor missing!');
2000
2001   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
2002     if ($form->{currency} ne $form->{defaultcurrency});
2003
2004   &validate_items;
2005
2006   # if the name changed get new values
2007   if (&check_name($form->{vc})) {
2008     &update;
2009     exit;
2010   }
2011
2012   $form->{id} = 0 if $form->{saveasnew};
2013
2014   # this is for the internal notes section for the [email] Subject
2015   if ($form->{type} =~ /_order$/) {
2016     if ($form->{type} eq 'sales_order') {
2017       $form->{label} = $locale->text('Sales Order');
2018
2019       $numberfld = "sonumber";
2020       $ordnumber = "ordnumber";
2021     } else {
2022       $form->{label} = $locale->text('Purchase Order');
2023
2024       $numberfld = "ponumber";
2025       $ordnumber = "ordnumber";
2026     }
2027
2028     $err = $locale->text('Cannot save order!');
2029
2030   } else {
2031     if ($form->{type} eq 'sales_quotation') {
2032       $form->{label} = $locale->text('Quotation');
2033
2034       $numberfld = "sqnumber";
2035       $ordnumber = "quonumber";
2036     } else {
2037       $form->{label} = $locale->text('Request for Quotation');
2038
2039       $numberfld = "rfqnumber";
2040       $ordnumber = "quonumber";
2041     }
2042
2043     $err = $locale->text('Cannot save quotation!');
2044
2045   }
2046
2047   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
2048     unless $form->{$ordnumber};
2049
2050   relink_accounts();
2051
2052   OE->save(\%myconfig, \%$form);
2053   $form->{simple_save} = 1;
2054   if(!$form->{print_and_save}) {
2055     set_headings("edit");
2056     &update;
2057     exit;
2058   }
2059   $lxdebug->leave_sub();
2060 }
2061
2062 sub delete {
2063   $lxdebug->enter_sub();
2064
2065   $form->header;
2066
2067   if ($form->{type} =~ /_order$/) {
2068     $msg       = $locale->text('Are you sure you want to delete Order Number');
2069     $ordnumber = 'ordnumber';
2070   } else {
2071     $msg = $locale->text('Are you sure you want to delete Quotation Number');
2072     $ordnumber = 'quonumber';
2073   }
2074
2075   print qq|
2076 <body>
2077
2078 <form method=post action=$form->{script}>
2079 |;
2080
2081   # delete action variable
2082   map { delete $form->{$_} } qw(action header);
2083
2084   foreach $key (keys %$form) {
2085     $form->{$key} =~ s/\"/&quot;/g;
2086     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2087   }
2088
2089   print qq|
2090 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
2091
2092 <h4>$msg $form->{$ordnumber}</h4>
2093 <p>
2094 <input name=action class=submit type=submit value="|
2095     . $locale->text('Yes') . qq|">
2096 </form>
2097
2098 </body>
2099 </html>
2100 |;
2101
2102   $lxdebug->leave_sub();
2103 }
2104
2105 sub yes {
2106   $lxdebug->enter_sub();
2107
2108   if ($form->{type} =~ /_order$/) {
2109     $msg = $locale->text('Order deleted!');
2110     $err = $locale->text('Cannot delete order!');
2111   } else {
2112     $msg = $locale->text('Quotation deleted!');
2113     $err = $locale->text('Cannot delete quotation!');
2114   }
2115
2116   $form->redirect($msg) if (OE->delete(\%myconfig, \%$form, $spool));
2117   $form->error($err);
2118
2119   $lxdebug->leave_sub();
2120 }
2121
2122 sub invoice {
2123   $lxdebug->enter_sub();
2124
2125   if ($form->{type} =~ /_order$/) {
2126
2127     # these checks only apply if the items don't bring their own ordnumbers/transdates.
2128     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
2129     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
2130       if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
2131           ->{''});
2132     $form->isblank("transdate", $locale->text('Order Date missing!'))
2133       if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
2134           ->{''});
2135
2136     # also copy deliverydate from the order
2137     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
2138   } else {
2139     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
2140     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
2141     $form->{ordnumber} = "";
2142   }
2143
2144   # if the name changed get new values
2145   if (&check_name($form->{vc})) {
2146     &update;
2147     exit;
2148   }
2149
2150   $form->{cp_id} *= 1;
2151
2152   if (   $form->{type} =~ /_order/
2153       && $form->{currency} ne $form->{defaultcurrency}) {
2154
2155     # check if we need a new exchangerate
2156     $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
2157
2158     $orddate      = $form->current_date(\%myconfig);
2159     $exchangerate =
2160       $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate,
2161                                 $buysell);
2162
2163     if (!$exchangerate) {
2164       &backorder_exchangerate($orddate, $buysell);
2165       exit;
2166     }
2167   }
2168
2169   # close orders/quotations
2170   $form->{closed} = 1;
2171
2172   # save order if one ordnumber has been given
2173   # if not it's most likely a collective order, which can't be saved back
2174   # so they just have to be closed
2175   if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
2176     relink_accounts();
2177     OE->save(\%myconfig, \%$form);
2178   } else {
2179     OE->close_orders(\%myconfig, \%$form);
2180   }
2181
2182   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
2183   $form->{duedate} =
2184     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
2185
2186   $form->{id}     = '';
2187   $form->{closed} = 0;
2188   $form->{rowcount}--;
2189   $form->{shipto} = 1;
2190
2191   if ($form->{type} =~ /_order$/) {
2192     $form->{exchangerate} = $exchangerate;
2193     &create_backorder;
2194   }
2195
2196   if (   $form->{type} eq 'purchase_order'
2197       || $form->{type} eq 'request_quotation') {
2198     $form->{title}  = $locale->text('Add Vendor Invoice');
2199     $form->{script} = 'ir.pl';
2200     $script         = "ir";
2201     $buysell        = 'sell';
2202   }
2203   if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
2204     $form->{title}  = $locale->text('Add Sales Invoice');
2205     $form->{script} = 'is.pl';
2206     $script         = "is";
2207     $buysell        = 'buy';
2208   }
2209
2210   # bo creates the id, reset it
2211   map { delete $form->{$_} }
2212     qw(id subject message cc bcc printed emailed queued);
2213   $form->{ $form->{vc} } =~ s/--.*//g;
2214   $form->{type} = "invoice";
2215
2216   # locale messages
2217   $locale = new Locale "$myconfig{countrycode}", "$script";
2218
2219   require "$form->{path}/$form->{script}";
2220
2221   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
2222
2223   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
2224     qw(creditlimit creditremaining);
2225
2226   $currency = $form->{currency};
2227   &invoice_links;
2228
2229   $form->{currency}     = $currency;
2230   $form->{exchangerate} = "";
2231   $form->{forex}        = "";
2232   $form->{exchangerate} = $exchangerate
2233     if (
2234         $form->{forex} = (
2235                     $exchangerate =
2236                       $form->check_exchangerate(
2237                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
2238                       )));
2239
2240   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
2241
2242   &prepare_invoice;
2243
2244   # format amounts
2245   for $i (1 .. $form->{rowcount}) {
2246     $form->{"discount_$i"} =
2247       $form->format_amount(\%myconfig, $form->{"discount_$i"});
2248
2249     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
2250     $dec           = length $dec;
2251     $decimalplaces = ($dec > 2) ? $dec : 2;
2252
2253     # copy delivery date from reqdate for order -> invoice conversion
2254     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
2255       unless $form->{"deliverydate_$i"};
2256
2257     $form->{"sellprice_$i"} =
2258       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
2259                            $decimalplaces);
2260
2261     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
2262     $dec_qty = length $dec_qty;
2263     $form->{"qty_$i"} =
2264       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
2265
2266     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2267       qw(partnumber description unit);
2268
2269   }
2270
2271   &display_form;
2272
2273   $lxdebug->leave_sub();
2274 }
2275
2276 sub backorder_exchangerate {
2277   $lxdebug->enter_sub();
2278   my ($orddate, $buysell) = @_;
2279
2280   $form->header;
2281
2282   print qq|
2283 <body>
2284
2285 <form method=post action=$form->{script}>
2286 |;
2287
2288   # delete action variable
2289   map { delete $form->{$_} } qw(action header exchangerate);
2290
2291   foreach $key (keys %$form) {
2292     $form->{$key} =~ s/\"/&quot;/g;
2293     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2294   }
2295
2296   $form->{title} = $locale->text('Add Exchangerate');
2297
2298   print qq|
2299
2300 <input type=hidden name=path value=$form->{path}>
2301 <input type=hidden name=login value=$form->{login}>
2302 <input type=hidden name=password value=$form->{password}>
2303
2304 <input type=hidden name=exchangeratedate value=$orddate>
2305 <input type=hidden name=buysell value=$buysell>
2306
2307 <table width=100%>
2308   <tr><th class=listtop>$form->{title}</th></tr>
2309   <tr height="5"></tr>
2310   <tr>
2311     <td>
2312       <table>
2313         <tr>
2314           <th align=right>| . $locale->text('Currency') . qq|</th>
2315           <td>$form->{currency}</td>
2316         </tr>
2317         <tr>
2318           <th align=right>| . $locale->text('Date') . qq|</th>
2319           <td>$orddate</td>
2320         </tr>
2321         <tr>
2322           <th align=right>| . $locale->text('Exchangerate') . qq|</th>
2323           <td><input name=exchangerate size=11></td>
2324         </tr>
2325       </table>
2326     </td>
2327   </tr>
2328 </table>
2329
2330 <hr size=3 noshade>
2331
2332 <br>
2333 <input type=hidden name=nextsub value=save_exchangerate>
2334
2335 <input name=action class=submit type=submit value="|
2336     . $locale->text('Continue') . qq|">
2337
2338 </form>
2339
2340 </body>
2341 </html>
2342 |;
2343
2344   $lxdebug->leave_sub();
2345 }
2346
2347 sub save_exchangerate {
2348   $lxdebug->enter_sub();
2349
2350   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
2351   $form->{exchangerate} =
2352     $form->parse_amount(\%myconfig, $form->{exchangerate});
2353   $form->save_exchangerate(\%myconfig, $form->{currency},
2354                            $form->{exchangeratedate},
2355                            $form->{exchangerate}, $form->{buysell});
2356
2357   &invoice;
2358
2359   $lxdebug->leave_sub();
2360 }
2361
2362 sub create_backorder {
2363   $lxdebug->enter_sub();
2364
2365   $form->{shipped} = 1;
2366
2367   # figure out if we need to create a backorder
2368   # items aren't saved if qty != 0
2369
2370   for $i (1 .. $form->{rowcount}) {
2371     $totalqty  += $qty  = $form->{"qty_$i"};
2372     $totalship += $ship = $form->{"ship_$i"};
2373
2374     $form->{"qty_$i"} = $qty - $ship;
2375   }
2376
2377   if ($totalship == 0) {
2378     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
2379     $form->{ordtotal} = 0;
2380     $form->{shipped}  = 0;
2381     return;
2382   }
2383
2384   if ($totalqty == $totalship) {
2385     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
2386     $form->{ordtotal} = 0;
2387     return;
2388   }
2389
2390   @flds = (
2391     qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
2392   );
2393
2394   for $i (1 .. $form->{rowcount}) {
2395     map {
2396       $form->{"${_}_$i"} =
2397         $form->format_amount(\%myconfig, $form->{"${_}_$i"})
2398     } qw(sellprice discount);
2399   }
2400
2401   relink_accounts();
2402
2403   OE->save(\%myconfig, \%$form);
2404
2405   # rebuild rows for invoice
2406   @a     = ();
2407   $count = 0;
2408
2409   for $i (1 .. $form->{rowcount}) {
2410     $form->{"qty_$i"} = $form->{"ship_$i"};
2411
2412     if ($form->{"qty_$i"}) {
2413       push @a, {};
2414       $j = $#a;
2415       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2416       $count++;
2417     }
2418   }
2419
2420   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2421   $form->{rowcount} = $count;
2422
2423   $lxdebug->leave_sub();
2424 }
2425
2426 sub save_as_new {
2427   $lxdebug->enter_sub();
2428
2429   $form->{saveasnew} = 1;
2430   $form->{closed}    = 0;
2431   map { delete $form->{$_} } qw(printed emailed queued ordnumber quonumber);
2432
2433   &save;
2434
2435   $lxdebug->leave_sub();
2436 }
2437
2438 sub purchase_order {
2439   $lxdebug->enter_sub();
2440
2441   if (   $form->{type} eq 'sales_quotation'
2442       || $form->{type} eq 'request_quotation') {
2443     $form->{closed} = 1;
2444     relink_accounts();
2445     OE->save(\%myconfig, \%$form);
2446   }
2447
2448   $form->{cp_id} *= 1;
2449
2450   $form->{title} = $locale->text('Add Purchase Order');
2451   $form->{vc}    = "vendor";
2452   $form->{type}  = "purchase_order";
2453
2454   &poso;
2455
2456   $lxdebug->leave_sub();
2457 }
2458
2459 sub sales_order {
2460   $lxdebug->enter_sub();
2461
2462   if (   $form->{type} eq 'sales_quotation'
2463       || $form->{type} eq 'request_quotation') {
2464     $form->{closed} = 1;
2465     relink_accounts();
2466     OE->save(\%myconfig, \%$form);
2467   }
2468
2469   $form->{cp_id} *= 1;
2470
2471   $form->{title} = $locale->text('Add Sales Order');
2472   $form->{vc}    = "customer";
2473   $form->{type}  = "sales_order";
2474
2475   &poso;
2476
2477   $lxdebug->leave_sub();
2478 }
2479
2480 sub poso {
2481   $lxdebug->enter_sub();
2482
2483   $form->{transdate} = $form->current_date(\%myconfig);
2484   delete $form->{duedate};
2485
2486   $form->{closed} = 0;
2487
2488   # reset
2489   map { delete $form->{$_} }
2490     qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
2491
2492   &order_links;
2493
2494   &prepare_order;
2495
2496   # format amounts
2497   for $i (1 .. $form->{rowcount} - 1) {
2498     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2499       qw(partnumber description unit);
2500   }
2501
2502   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 0, "0") }
2503     qw(creditlimit creditremaining);
2504
2505   &update;
2506
2507   $lxdebug->leave_sub();
2508 }
2509
2510 sub ship_receive {
2511   $lxdebug->enter_sub();
2512
2513   &order_links;
2514
2515   &prepare_order;
2516
2517   OE->get_warehouses(\%myconfig, \%$form);
2518
2519   # warehouse
2520   if (@{ $form->{all_warehouses} }) {
2521     $form->{selectwarehouse} = "<option>\n";
2522
2523     map { $form->{selectwarehouse} .= "<option>$_->{description}--$_->{id}\n" }
2524       (@{ $form->{all_warehouses} });
2525
2526     if ($form->{warehouse}) {
2527       $form->{selectwarehouse} = "<option>$form->{warehouse}";
2528     }
2529   }
2530
2531   $form->{shippingdate} = $form->current_date(\%myconfig);
2532   $form->{"$form->{vc}"} =~ s/--.*//;
2533
2534   @flds  = ();
2535   @a     = ();
2536   $count = 0;
2537   foreach $key (keys %$form) {
2538     if ($key =~ /_1$/) {
2539       $key =~ s/_1//;
2540       push @flds, $key;
2541     }
2542   }
2543
2544   for $i (1 .. $form->{rowcount}) {
2545
2546     # undo formatting from prepare_order
2547     map {
2548       $form->{"${_}_$i"} =
2549         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
2550     } qw(qty ship);
2551     $n = ($form->{"qty_$i"} -= $form->{"ship_$i"});
2552     if (abs($n) > 0
2553         && ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"})) {
2554       $form->{"ship_$i"}         = "";
2555       $form->{"serialnumber_$i"} = "";
2556
2557       push @a, {};
2558       $j = $#a;
2559
2560       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2561       $count++;
2562     }
2563   }
2564
2565   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2566   $form->{rowcount} = $count;
2567
2568   &display_ship_receive;
2569
2570   $lxdebug->leave_sub();
2571 }
2572
2573 sub display_ship_receive {
2574   $lxdebug->enter_sub();
2575
2576   $vclabel = ucfirst $form->{vc};
2577   $vclabel = $locale->text($vclabel);
2578
2579   $form->{rowcount}++;
2580
2581   if ($form->{vc} eq 'customer') {
2582     $form->{title} = $locale->text('Ship Merchandise');
2583     $shipped = $locale->text('Shipping Date');
2584   } else {
2585     $form->{title} = $locale->text('Receive Merchandise');
2586     $shipped = $locale->text('Date Received');
2587   }
2588
2589   # set option selected
2590   foreach $item (warehouse, employee) {
2591     $form->{"select$item"} =~ s/ selected//;
2592     $form->{"select$item"} =~
2593       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
2594   }
2595
2596   $warehouse = qq|
2597               <tr>
2598                 <th align=right>| . $locale->text('Warehouse') . qq|</th>
2599                 <td><select name=warehouse>$form->{selectwarehouse}</select></td>
2600                 <input type=hidden name=selectwarehouse value="$form->{selectwarehouse}">
2601               </tr>
2602 | if $form->{selectwarehouse};
2603
2604   $employee = qq|
2605               <tr>
2606                 <th align=right nowrap>| . $locale->text('Contact') . qq|</th>
2607                 <td><select name=employee>$form->{selectemployee}</select></td>
2608                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
2609               </tr>
2610 |;
2611
2612   $form->header;
2613
2614   print qq|
2615 <body>
2616
2617 <form method=post action=$form->{script}>
2618
2619 <input type=hidden name=id value=$form->{id}>
2620
2621 <input type=hidden name=display_form value=display_ship_receive>
2622
2623 <input type=hidden name=type value=$form->{type}>
2624 <input type=hidden name=media value=$form->{media}>
2625 <input type=hidden name=format value=$form->{format}>
2626
2627 <input type=hidden name=queued value="$form->{queued}">
2628 <input type=hidden name=printed value="$form->{printed}">
2629 <input type=hidden name=emailed value="$form->{emailed}">
2630
2631 <input type=hidden name=vc value=$form->{vc}>
2632
2633 <table width=100%>
2634   <tr class=listtop>
2635     <th class=listtop>$form->{title}</th>
2636   </tr>
2637   <tr height="5"></tr>
2638   <tr>
2639     <td>
2640       <table width="100%">
2641         <tr valign=top>
2642           <td>
2643             <table width=100%>
2644               <tr>
2645                 <th align=right>$vclabel</th>
2646                 <td colspan=3>$form->{$form->{vc}}</td>
2647                 <input type=hidden name=$form->{vc} value="$form->{$form->{vc}}">
2648                 <input type=hidden name="$form->{vc}_id" value=$form->{"$form->{vc}_id"}>
2649               </tr>
2650               $department
2651               <tr>
2652                 <th align=right>| . $locale->text('Shipping Point') . qq|</th>
2653                 <td colspan=3>
2654                 <input name=shippingpoint size=35 value="$form->{shippingpoint}">
2655               </tr>
2656               <tr>
2657                 <th align=right>| . $locale->text('Ship via') . qq|</th>
2658                 <td colspan=3>
2659                 <input name=shipvia size=35 value="$form->{shipvia}">
2660               </tr>
2661               $warehouse
2662             </table>
2663           </td>
2664           <td align=right>
2665             <table>
2666               $employee
2667               <tr>
2668                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
2669                 <td>$form->{ordnumber}</td>
2670                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
2671               </tr>
2672               <tr>
2673                 <th align=right nowrap>| . $locale->text('Order Date') . qq|</th>
2674                 <td>$form->{transdate}</td>
2675                 <input type=hidden name=transdate value=$form->{transdate}>
2676               </tr>
2677               <tr>
2678                 <th align=right nowrap>$shipped</th>
2679                 <td><input name=shippingdate size=11 value=$form->{shippingdate}></td>
2680               </tr>
2681             </table>
2682           </td>
2683         </tr>
2684       </table>
2685     </td>
2686   </tr>
2687
2688 <!-- shipto are in hidden variables -->
2689
2690 <input type=hidden name=shiptoname value="$form->{shiptoname}">
2691 <input type=hidden name=shiptostreet value="$form->{shiptostreet}">
2692 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
2693 <input type=hidden name=shiptocity value="$form->{shiptocity}">
2694 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
2695 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
2696 <input type=hidden name=shiptophone value="$form->{shiptophone}">
2697 <input type=hidden name=shiptofax value="$form->{shiptofax}">
2698 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
2699 <input type=hidden name=shiptodepartment_1 value="$form->{shiptodepartment_1}">
2700 <input type=hidden name=shiptodepartment_2 value="$form->{shiptodepartment_2}">
2701
2702 <!-- email variables -->
2703 <input type=hidden name=message value="$form->{message}">
2704 <input type=hidden name=email value="$form->{email}">
2705 <input type=hidden name=subject value="$form->{subject}">
2706 <input type=hidden name=cc value="$form->{cc}">
2707 <input type=hidden name=bcc value="$form->{bcc}">
2708
2709 |;
2710
2711   @column_index =
2712     (partnumber, description, qty, ship, unit, bin, serialnumber);
2713
2714   if ($form->{type} eq "ship_order") {
2715     $column_data{ship} =
2716         qq|<th class=listheading align=center width="auto">|
2717       . $locale->text('Ship')
2718       . qq|</th>|;
2719   }
2720   if ($form->{type} eq "receive_order") {
2721     $column_data{ship} =
2722         qq|<th class=listheading align=center width="auto">|
2723       . $locale->text('Recd')
2724       . qq|</th>|;
2725   }
2726
2727   my $colspan = $#column_index + 1;
2728
2729   $column_data{partnumber} =
2730     qq|<th class=listheading nowrap>| . $locale->text('Number') . qq|</th>|;
2731   $column_data{description} =
2732       qq|<th class=listheading nowrap>|
2733     . $locale->text('Description')
2734     . qq|</th>|;
2735   $column_data{qty} =
2736     qq|<th class=listheading nowrap>| . $locale->text('Qty') . qq|</th>|;
2737   $column_data{unit} =
2738     qq|<th class=listheading nowrap>| . $locale->text('Unit') . qq|</th>|;
2739   $column_data{bin} =
2740     qq|<th class=listheading nowrap>| . $locale->text('Bin') . qq|</th>|;
2741   $column_data{serialnumber} =
2742       qq|<th class=listheading nowrap>|
2743     . $locale->text('Serial No.')
2744     . qq|</th>|;
2745
2746   print qq|
2747   <tr>
2748     <td>
2749       <table width=100%>
2750         <tr class=listheading>|;
2751
2752   map { print "\n$column_data{$_}" } @column_index;
2753
2754   print qq|
2755         </tr>
2756 |;
2757
2758   for $i (1 .. $form->{rowcount} - 1) {
2759
2760     # undo formatting
2761     $form->{"ship_$i"} = $form->parse_amount(\%myconfig, $form->{"ship_$i"});
2762
2763     # convert " to &quot;
2764     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2765       qw(partnumber description unit bin serialnumber);
2766
2767     $description = $form->{"description_$i"};
2768     $description =~ s/\n/<br>/g;
2769
2770     $column_data{partnumber} =
2771       qq|<td>$form->{"partnumber_$i"}<input type=hidden name="partnumber_$i" value="$form->{"partnumber_$i"}"></td>|;
2772     $column_data{description} =
2773       qq|<td>$description<input type=hidden name="description_$i" value="$form->{"description_$i"}"></td>|;
2774     $column_data{qty} =
2775         qq|<td align=right>|
2776       . $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty)
2777       . qq|<input type=hidden name="qty_$i" value="$form->{"qty_$i"}"></td>|;
2778     $column_data{ship} =
2779         qq|<td align=right><input name="ship_$i" size=5 value=|
2780       . $form->format_amount(\%myconfig, $form->{"ship_$i"})
2781       . qq|></td>|;
2782     $column_data{unit} =
2783       qq|<td>$form->{"unit_$i"}<input type=hidden name="unit_$i" value="$form->{"unit_$i"}"></td>|;
2784     $column_data{bin} =
2785       qq|<td>$form->{"bin_$i"}<input type=hidden name="bin_$i" value="$form->{"bin_$i"}"></td>|;
2786
2787     $column_data{serialnumber} =
2788       qq|<td><input name="serialnumber_$i" size=15 value="$form->{"serialnumber_$i"}"></td>|;
2789
2790     print qq|
2791         <tr valign=top>|;
2792
2793     map { print "\n$column_data{$_}" } @column_index;
2794
2795     print qq|
2796         </tr>
2797
2798 <input type=hidden name="orderitems_id_$i" value=$form->{"orderitems_id_$i"}>
2799 <input type=hidden name="id_$i" value=$form->{"id_$i"}>
2800 <input type=hidden name="assembly_$i" value="$form->{"assembly_$i"}">
2801 <input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">
2802
2803 |;
2804
2805   }
2806
2807   print qq|
2808       </table>
2809     </td>
2810   </tr>
2811   <tr>
2812     <td><hr size=3 noshade></td>
2813   </tr>
2814   <tr>
2815     <td>
2816 |;
2817
2818   $form->{copies} = 1;
2819
2820   &print_options;
2821
2822   print qq|
2823     </td>
2824   </tr>
2825 </table>
2826 <br>
2827 <input class=submit type=submit name=action value="|
2828     . $locale->text('Update') . qq|">
2829 <input class=submit type=submit name=action value="|
2830     . $locale->text('Print') . qq|">
2831 |;
2832
2833   if ($form->{type} eq 'ship_order') {
2834     print qq|
2835 <input class=submit type=submit name=action value="|
2836       . $locale->text('Ship to') . qq|">
2837 <input class=submit type=submit name=action value="|
2838       . $locale->text('E-mail') . qq|">
2839 |;
2840   }
2841
2842   print qq|
2843
2844 <input class=submit type=submit name=action value="|
2845     . $locale->text('Done') . qq|">
2846 |;
2847
2848   if ($form->{menubar}) {
2849     require "$form->{path}/menu.pl";
2850     &menubar;
2851   }
2852
2853   print qq|
2854
2855 <input type=hidden name=rowcount value=$form->{rowcount}>
2856
2857 <input name=callback type=hidden value="$callback">
2858
2859 <input type=hidden name=path value=$form->{path}>
2860 <input type=hidden name=login value=$form->{login}>
2861 <input type=hidden name=password value=$form->{password}>
2862
2863 </form>
2864
2865 </body>
2866 </html>
2867 |;
2868
2869   $lxdebug->leave_sub();
2870 }
2871
2872 sub done {
2873   $lxdebug->enter_sub();
2874
2875   if ($form->{type} eq 'ship_order') {
2876     $form->isblank("shippingdate", $locale->text('Shipping Date missing!'));
2877   } else {
2878     $form->isblank("shippingdate", $locale->text('Date received missing!'));
2879   }
2880
2881   $total = 0;
2882   map {
2883     $total += $form->{"ship_$_"} =
2884       $form->parse_amount(\%myconfig, $form->{"ship_$_"})
2885   } (1 .. $form->{rowcount} - 1);
2886
2887   $form->error($locale->text('Nothing entered!')) unless $total;
2888
2889   $form->redirect($locale->text('Inventory saved!'))
2890     if OE->save_inventory(\%myconfig, \%$form);
2891   $form->error($locale->text('Could not save!'));
2892
2893   $lxdebug->leave_sub();
2894 }
2895
2896 sub search_transfer {
2897   $lxdebug->enter_sub();
2898
2899   OE->get_warehouses(\%myconfig, \%$form);
2900
2901   # warehouse
2902   if (@{ $form->{all_warehouses} }) {
2903     $form->{selectwarehouse} = "<option>\n";
2904     $form->{warehouse}       = qq|$form->{warehouse}--$form->{warehouse_id}|;
2905
2906     map { $form->{selectwarehouse} .= "<option>$_->{description}--$_->{id}\n" }
2907       (@{ $form->{all_warehouses} });
2908   } else {
2909     $form->error($locale->text('Nothing to transfer!'));
2910   }
2911
2912   $form->{title} = $locale->text('Transfer Inventory');
2913
2914   $form->header;
2915
2916   print qq|
2917 <body>
2918
2919 <form method=post action=$form->{script}>
2920
2921 <table width=100%>
2922   <tr>
2923     <th class=listtop>$form->{title}</th>
2924   </tr>
2925   <tr height="5"></tr>
2926   <tr>
2927     <td>
2928       <table>
2929         <tr>
2930           <th align=right nowrap>| . $locale->text('Transfer to') . qq|</th>
2931           <td><select name=warehouse>$form->{selectwarehouse}</select></td>
2932         </tr>
2933         <tr>
2934           <th align="right" nowrap="true">| . $locale->text('Part Number') . qq|</th>
2935           <td><input name=partnumber size=20></td>
2936         </tr>
2937         <tr>
2938           <th align="right" nowrap="true">| . $locale->text('Description') . qq|</th>
2939           <td><input name=description size=40></td>
2940         </tr>
2941         <tr>
2942           <th align=right nowrap>| . $locale->text('Group') . qq|</th>
2943           <td><input name=partsgroup size=20></td>
2944         </tr>
2945       </table>
2946     </td>
2947   </tr>
2948   <tr>
2949     <td><hr size=3 noshade></td>
2950   </tr>
2951 </table>
2952
2953 <br>
2954 <input type=hidden name=sort value=partnumber>
2955 <input type=hidden name=nextsub value=list_transfer>
2956
2957 <input type=hidden name=path value=$form->{path}>
2958 <input type=hidden name=login value=$form->{login}>
2959 <input type=hidden name=password value=$form->{password}>
2960
2961 <input class=submit type=submit name=action value="|
2962     . $locale->text('Continue') . qq|">
2963 </form>
2964
2965 </body>
2966 </html>
2967 |;
2968
2969   $lxdebug->leave_sub();
2970 }
2971
2972 sub list_transfer {
2973   $lxdebug->enter_sub();
2974
2975   OE->get_inventory(\%myconfig, \%$form);
2976
2977   $partnumber  = $form->escape($form->{partnumber});
2978   $warehouse   = $form->escape($form->{warehouse});
2979   $description = $form->escape($form->{description});
2980   $partsgroup  = $form->escape($form->{partsgroup});
2981
2982   # construct href
2983   $href =
2984     "$form->{script}?path=$form->{path}&action=list_transfer&partnumber=$partnumber&warehouse=$warehouse&description=$description&partsgroup=$partsgroup&login=$form->{login}&password=$form->{password}";
2985
2986   # construct callback
2987   $partnumber  = $form->escape($form->{partnumber},  1);
2988   $warehouse   = $form->escape($form->{warehouse},   1);
2989   $description = $form->escape($form->{description}, 1);
2990   $partsgroup  = $form->escape($form->{partsgroup},  1);
2991
2992   $callback =
2993     "$form->{script}?path=$form->{path}&action=list_transfer&partnumber=$partnumber&warehouse=$warehouse&description=$description&partsgroup=$partsgroup&login=$form->{login}&password=$form->{password}";
2994
2995   @column_index =
2996     $form->sort_columns(
2997       qw(partnumber description partsgroup make model warehouse qty transfer));
2998
2999   $column_header{partnumber} =
3000       qq|<th><a class=listheading href=$href&sort=partnumber>|
3001     . $locale->text('Part Number')
3002     . qq|</a></th>|;
3003   $column_header{description} =
3004       qq|<th><a class=listheading href=$href&sort=description>|
3005     . $locale->text('Description')
3006     . qq|</a></th>|;
3007   $column_header{partsgroup} =
3008       qq|<th><a class=listheading href=$href&sort=partsgroup>|
3009     . $locale->text('Group')
3010     . qq|</a></th>|;
3011   $column_header{warehouse} =
3012       qq|<th><a class=listheading href=$href&sort=warehouse>|
3013     . $locale->text('From')
3014     . qq|</a></th>|;
3015   $column_header{qty} =
3016     qq|<th><a class=listheading>| . $locale->text('Qty') . qq|</a></th>|;
3017   $column_header{transfer} =
3018     qq|<th><a class=listheading>| . $locale->text('Transfer') . qq|</a></th>|;
3019
3020   $option = $locale->text('Transfer to');
3021
3022   ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
3023
3024   if ($form->{warehouse}) {
3025     $option .= " : $warehouse";
3026   }
3027   if ($form->{partnumber}) {
3028     $option .= "\n<br>" if ($option);
3029     $option .= $locale->text('Part Number') . " : $form->{partnumber}";
3030   }
3031   if ($form->{description}) {
3032     $option .= "\n<br>" if ($option);
3033     $option .= $locale->text('Description') . " : $form->{description}";
3034   }
3035   if ($form->{partsgroup}) {
3036     $option .= "\n<br>" if ($option);
3037     $option .= $locale->text('Group') . " : $form->{partsgroup}";
3038   }
3039
3040   $form->{title} = $locale->text('Transfer Inventory');
3041
3042   $form->header;
3043
3044   print qq|
3045 <body>
3046
3047 <form method=post action=$form->{script}>
3048
3049 <input type=hidden name=warehouse_id value=$warehouse_id>
3050
3051 <table width=100%>
3052   <tr>
3053     <th class=listtop>$form->{title}</th>
3054   </tr>
3055   <tr height="5"></tr>
3056   <tr>
3057     <td>$option</td>
3058   </tr>
3059   <tr>
3060     <td>
3061       <table width=100%>
3062         <tr class=listheading>|;
3063
3064   map { print "\n$column_header{$_}" } @column_index;
3065
3066   print qq|
3067         </tr>
3068 |;
3069
3070   if (@{ $form->{all_inventory} }) {
3071     $sameitem = $form->{all_inventory}->[0]->{ $form->{sort} };
3072   }
3073
3074   $i = 0;
3075   foreach $ref (@{ $form->{all_inventory} }) {
3076
3077     $i++;
3078
3079     $column_data{partnumber} =
3080       qq|<td><input type=hidden name="id_$i" value=$ref->{id}>$ref->{partnumber}</td>|;
3081     $column_data{description} = "<td>$ref->{description}&nbsp;</td>";
3082     $column_data{partsgroup}  = "<td>$ref->{partsgroup}&nbsp;</td>";
3083     $column_data{warehouse}   =
3084       qq|<td><input type=hidden name="warehouse_id_$i" value=$ref->{warehouse_id}>$ref->{warehouse}&nbsp;</td>|;
3085     $column_data{qty} =
3086         qq|<td><input type=hidden name="qty_$i" value=$ref->{qty}>|
3087       . $form->format_amount(\%myconfig, $ref->{qty}, $dec_qty)
3088       . qq|</td>|;
3089     $column_data{transfer} = qq|<td><input name="transfer_$i" size=4></td>|;
3090
3091     $j++;
3092     $j %= 2;
3093     print "
3094         <tr class=listrow$j>";
3095
3096     map { print "\n$column_data{$_}" } @column_index;
3097
3098     print qq|
3099         </tr>
3100 |;
3101
3102   }
3103
3104   print qq|
3105       </table>
3106     </td>
3107   </tr>
3108
3109   <tr>
3110     <td><hr size=3 noshade></td>
3111   </tr>
3112 </table>
3113
3114 <br>
3115
3116 <input name=callback type=hidden value="$callback">
3117
3118 <input type=hidden name=rowcount value=$i>
3119
3120 <input type=hidden name=path value=$form->{path}>
3121 <input type=hidden name=login value=$form->{login}>
3122 <input type=hidden name=password value=$form->{password}>
3123
3124 <input class=submit type=submit name=action value="|
3125     . $locale->text('Transfer') . qq|">|;
3126
3127   if ($form->{menubar}) {
3128     require "$form->{path}/menu.pl";
3129     &menubar;
3130   }
3131
3132   print qq|
3133 </form>
3134
3135 </body>
3136 </html>
3137 |;
3138
3139   $lxdebug->leave_sub();
3140 }
3141
3142 sub transfer {
3143   $lxdebug->enter_sub();
3144
3145   $form->redirect($locale->text('Inventory transferred!'))
3146     if OE->transfer(\%myconfig, \%$form);
3147   $form->error($locale->text('Could not transfer Inventory!'));
3148
3149   $lxdebug->leave_sub();
3150 }