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