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