epic-s6ts
[kivitendo-erp.git] / 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   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1947     unless $form->{$ordnumber};
1948
1949   $form->redirect(
1950             $form->{label} . " $form->{$ordnumber} " . $locale->text('saved!'))
1951     if (OE->save(\%myconfig, \%$form));
1952   $form->error($err);
1953
1954   $lxdebug->leave_sub();
1955 }
1956
1957 sub save {
1958   $lxdebug->enter_sub();
1959
1960   if ($form->{type} =~ /_order$/) {
1961     $form->isblank("transdate", $locale->text('Order Date missing!'));
1962   } else {
1963     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1964   }
1965
1966   $msg = ucfirst $form->{vc};
1967   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1968
1969   # $locale->text('Customer missing!');
1970   # $locale->text('Vendor missing!');
1971
1972   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1973     if ($form->{currency} ne $form->{defaultcurrency});
1974
1975   &validate_items;
1976
1977   # if the name changed get new values
1978   if (&check_name($form->{vc})) {
1979     &update;
1980     exit;
1981   }
1982
1983   $form->{id} = 0 if $form->{saveasnew};
1984
1985   # this is for the internal notes section for the [email] Subject
1986   if ($form->{type} =~ /_order$/) {
1987     if ($form->{type} eq 'sales_order') {
1988       $form->{label} = $locale->text('Sales Order');
1989
1990       $numberfld = "sonumber";
1991       $ordnumber = "ordnumber";
1992     } else {
1993       $form->{label} = $locale->text('Purchase Order');
1994
1995       $numberfld = "ponumber";
1996       $ordnumber = "ordnumber";
1997     }
1998
1999     $err = $locale->text('Cannot save order!');
2000
2001   } else {
2002     if ($form->{type} eq 'sales_quotation') {
2003       $form->{label} = $locale->text('Quotation');
2004
2005       $numberfld = "sqnumber";
2006       $ordnumber = "quonumber";
2007     } else {
2008       $form->{label} = $locale->text('Request for Quotation');
2009
2010       $numberfld = "rfqnumber";
2011       $ordnumber = "quonumber";
2012     }
2013
2014     $err = $locale->text('Cannot save quotation!');
2015
2016   }
2017
2018   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
2019     unless $form->{$ordnumber};
2020
2021
2022   OE->save(\%myconfig, \%$form);
2023   $form->{simple_save} = 1;
2024   if(!$form->{print_and_save}) {
2025     &update;
2026     exit;
2027   }
2028   $lxdebug->leave_sub();
2029 }
2030
2031 sub delete {
2032   $lxdebug->enter_sub();
2033
2034   $form->header;
2035
2036   if ($form->{type} =~ /_order$/) {
2037     $msg       = $locale->text('Are you sure you want to delete Order Number');
2038     $ordnumber = 'ordnumber';
2039   } else {
2040     $msg = $locale->text('Are you sure you want to delete Quotation Number');
2041     $ordnumber = 'quonumber';
2042   }
2043
2044   print qq|
2045 <body>
2046
2047 <form method=post action=$form->{script}>
2048 |;
2049
2050   # delete action variable
2051   map { delete $form->{$_} } qw(action header);
2052
2053   foreach $key (keys %$form) {
2054     $form->{$key} =~ s/\"/&quot;/g;
2055     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2056   }
2057
2058   print qq|
2059 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
2060
2061 <h4>$msg $form->{$ordnumber}</h4>
2062 <p>
2063 <input name=action class=submit type=submit value="|
2064     . $locale->text('Yes') . qq|">
2065 </form>
2066
2067 </body>
2068 </html>
2069 |;
2070
2071   $lxdebug->leave_sub();
2072 }
2073
2074 sub yes {
2075   $lxdebug->enter_sub();
2076
2077   if ($form->{type} =~ /_order$/) {
2078     $msg = $locale->text('Order deleted!');
2079     $err = $locale->text('Cannot delete order!');
2080   } else {
2081     $msg = $locale->text('Quotation deleted!');
2082     $err = $locale->text('Cannot delete quotation!');
2083   }
2084
2085   $form->redirect($msg) if (OE->delete(\%myconfig, \%$form, $spool));
2086   $form->error($err);
2087
2088   $lxdebug->leave_sub();
2089 }
2090
2091 sub invoice {
2092   $lxdebug->enter_sub();
2093
2094   if ($form->{type} =~ /_order$/) {
2095
2096     # these checks only apply if the items don't bring their own ordnumbers/transdates.
2097     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
2098     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
2099       if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
2100           ->{''});
2101     $form->isblank("transdate", $locale->text('Order Date missing!'))
2102       if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
2103           ->{''});
2104
2105     # also copy deliverydate from the order
2106     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
2107   } else {
2108     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
2109     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
2110     $form->{ordnumber} = "";
2111   }
2112
2113   # if the name changed get new values
2114   if (&check_name($form->{vc})) {
2115     &update;
2116     exit;
2117   }
2118
2119   $form->{cp_id} *= 1;
2120
2121   if (   $form->{type} =~ /_order/
2122       && $form->{currency} ne $form->{defaultcurrency}) {
2123
2124     # check if we need a new exchangerate
2125     $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
2126
2127     $orddate      = $form->current_date(\%myconfig);
2128     $exchangerate =
2129       $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate,
2130                                 $buysell);
2131
2132     if (!$exchangerate) {
2133       &backorder_exchangerate($orddate, $buysell);
2134       exit;
2135     }
2136   }
2137
2138   # close orders/quotations
2139   $form->{closed} = 1;
2140
2141   # save order if one ordnumber has been given
2142   # if not it's most likely a collective order, which can't be saved back
2143   # so they just have to be closed
2144   if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
2145     OE->save(\%myconfig, \%$form);
2146   } else {
2147     OE->close_orders(\%myconfig, \%$form);
2148   }
2149
2150   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
2151   $form->{duedate} =
2152     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
2153
2154   $form->{id}     = '';
2155   $form->{closed} = 0;
2156   $form->{rowcount}--;
2157   $form->{shipto} = 1;
2158
2159   if ($form->{type} =~ /_order$/) {
2160     $form->{exchangerate} = $exchangerate;
2161     &create_backorder;
2162   }
2163
2164   if (   $form->{type} eq 'purchase_order'
2165       || $form->{type} eq 'request_quotation') {
2166     $form->{title}  = $locale->text('Add Vendor Invoice');
2167     $form->{script} = 'ir.pl';
2168     $script         = "ir";
2169     $buysell        = 'sell';
2170   }
2171   if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
2172     $form->{title}  = $locale->text('Add Sales Invoice');
2173     $form->{script} = 'is.pl';
2174     $script         = "is";
2175     $buysell        = 'buy';
2176   }
2177
2178   # bo creates the id, reset it
2179   map { delete $form->{$_} }
2180     qw(id subject message cc bcc printed emailed queued);
2181   $form->{ $form->{vc} } =~ s/--.*//g;
2182   $form->{type} = "invoice";
2183
2184   # locale messages
2185   $locale = new Locale "$myconfig{countrycode}", "$script";
2186
2187   require "$form->{path}/$form->{script}";
2188
2189   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
2190
2191   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
2192     qw(creditlimit creditremaining);
2193
2194   $currency = $form->{currency};
2195   &invoice_links;
2196
2197   $form->{currency}     = $currency;
2198   $form->{exchangerate} = "";
2199   $form->{forex}        = "";
2200   $form->{exchangerate} = $exchangerate
2201     if (
2202         $form->{forex} = (
2203                     $exchangerate =
2204                       $form->check_exchangerate(
2205                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
2206                       )));
2207
2208   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
2209
2210   &prepare_invoice;
2211
2212   # format amounts
2213   for $i (1 .. $form->{rowcount}) {
2214     $form->{"discount_$i"} =
2215       $form->format_amount(\%myconfig, $form->{"discount_$i"});
2216
2217     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
2218     $dec           = length $dec;
2219     $decimalplaces = ($dec > 2) ? $dec : 2;
2220
2221     # copy delivery date from reqdate for order -> invoice conversion
2222     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
2223       unless $form->{"deliverydate_$i"};
2224
2225     $form->{"sellprice_$i"} =
2226       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
2227                            $decimalplaces);
2228
2229     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
2230     $dec_qty = length $dec_qty;
2231     $form->{"qty_$i"} =
2232       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
2233
2234     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2235       qw(partnumber description unit);
2236
2237   }
2238
2239   &display_form;
2240
2241   $lxdebug->leave_sub();
2242 }
2243
2244 sub backorder_exchangerate {
2245   $lxdebug->enter_sub();
2246   my ($orddate, $buysell) = @_;
2247
2248   $form->header;
2249
2250   print qq|
2251 <body>
2252
2253 <form method=post action=$form->{script}>
2254 |;
2255
2256   # delete action variable
2257   map { delete $form->{$_} } qw(action header exchangerate);
2258
2259   foreach $key (keys %$form) {
2260     $form->{$key} =~ s/\"/&quot;/g;
2261     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2262   }
2263
2264   $form->{title} = $locale->text('Add Exchangerate');
2265
2266   print qq|
2267
2268 <input type=hidden name=path value=$form->{path}>
2269 <input type=hidden name=login value=$form->{login}>
2270 <input type=hidden name=password value=$form->{password}>
2271
2272 <input type=hidden name=exchangeratedate value=$orddate>
2273 <input type=hidden name=buysell value=$buysell>
2274
2275 <table width=100%>
2276   <tr><th class=listtop>$form->{title}</th></tr>
2277   <tr height="5"></tr>
2278   <tr>
2279     <td>
2280       <table>
2281         <tr>
2282           <th align=right>| . $locale->text('Currency') . qq|</th>
2283           <td>$form->{currency}</td>
2284         </tr>
2285         <tr>
2286           <th align=right>| . $locale->text('Date') . qq|</th>
2287           <td>$orddate</td>
2288         </tr>
2289         <tr>
2290           <th align=right>| . $locale->text('Exchangerate') . qq|</th>
2291           <td><input name=exchangerate size=11></td>
2292         </tr>
2293       </table>
2294     </td>
2295   </tr>
2296 </table>
2297
2298 <hr size=3 noshade>
2299
2300 <br>
2301 <input type=hidden name=nextsub value=save_exchangerate>
2302
2303 <input name=action class=submit type=submit value="|
2304     . $locale->text('Continue') . qq|">
2305
2306 </form>
2307
2308 </body>
2309 </html>
2310 |;
2311
2312   $lxdebug->leave_sub();
2313 }
2314
2315 sub save_exchangerate {
2316   $lxdebug->enter_sub();
2317
2318   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
2319   $form->{exchangerate} =
2320     $form->parse_amount(\%myconfig, $form->{exchangerate});
2321   $form->save_exchangerate(\%myconfig, $form->{currency},
2322                            $form->{exchangeratedate},
2323                            $form->{exchangerate}, $form->{buysell});
2324
2325   &invoice;
2326
2327   $lxdebug->leave_sub();
2328 }
2329
2330 sub create_backorder {
2331   $lxdebug->enter_sub();
2332
2333   $form->{shipped} = 1;
2334
2335   # figure out if we need to create a backorder
2336   # items aren't saved if qty != 0
2337
2338   for $i (1 .. $form->{rowcount}) {
2339     $totalqty  += $qty  = $form->{"qty_$i"};
2340     $totalship += $ship = $form->{"ship_$i"};
2341
2342     $form->{"qty_$i"} = $qty - $ship;
2343   }
2344
2345   if ($totalship == 0) {
2346     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
2347     $form->{ordtotal} = 0;
2348     $form->{shipped}  = 0;
2349     return;
2350   }
2351
2352   if ($totalqty == $totalship) {
2353     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
2354     $form->{ordtotal} = 0;
2355     return;
2356   }
2357
2358   @flds = (
2359     qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
2360   );
2361
2362   for $i (1 .. $form->{rowcount}) {
2363     map {
2364       $form->{"${_}_$i"} =
2365         $form->format_amount(\%myconfig, $form->{"${_}_$i"})
2366     } qw(sellprice discount);
2367   }
2368
2369   OE->save(\%myconfig, \%$form);
2370
2371   # rebuild rows for invoice
2372   @a     = ();
2373   $count = 0;
2374
2375   for $i (1 .. $form->{rowcount}) {
2376     $form->{"qty_$i"} = $form->{"ship_$i"};
2377
2378     if ($form->{"qty_$i"}) {
2379       push @a, {};
2380       $j = $#a;
2381       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2382       $count++;
2383     }
2384   }
2385
2386   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2387   $form->{rowcount} = $count;
2388
2389   $lxdebug->leave_sub();
2390 }
2391
2392 sub save_as_new {
2393   $lxdebug->enter_sub();
2394
2395   $form->{saveasnew} = 1;
2396   $form->{closed}    = 0;
2397   map { delete $form->{$_} } qw(printed emailed queued ordnumber quonumber);
2398
2399   &save;
2400
2401   $lxdebug->leave_sub();
2402 }
2403
2404 sub purchase_order {
2405   $lxdebug->enter_sub();
2406
2407   if (   $form->{type} eq 'sales_quotation'
2408       || $form->{type} eq 'request_quotation') {
2409     $form->{closed} = 1;
2410     OE->save(\%myconfig, \%$form);
2411   }
2412
2413   $form->{cp_id} *= 1;
2414
2415   $form->{title} = $locale->text('Add Purchase Order');
2416   $form->{vc}    = "vendor";
2417   $form->{type}  = "purchase_order";
2418
2419   &poso;
2420
2421   $lxdebug->leave_sub();
2422 }
2423
2424 sub sales_order {
2425   $lxdebug->enter_sub();
2426
2427   if (   $form->{type} eq 'sales_quotation'
2428       || $form->{type} eq 'request_quotation') {
2429     $form->{closed} = 1;
2430     OE->save(\%myconfig, \%$form);
2431   }
2432
2433   $form->{cp_id} *= 1;
2434
2435   $form->{title} = $locale->text('Add Sales Order');
2436   $form->{vc}    = "customer";
2437   $form->{type}  = "sales_order";
2438
2439   &poso;
2440
2441   $lxdebug->leave_sub();
2442 }
2443
2444 sub poso {
2445   $lxdebug->enter_sub();
2446
2447   $form->{transdate} = $form->current_date(\%myconfig);
2448   delete $form->{duedate};
2449
2450   $form->{closed} = 0;
2451
2452   # reset
2453   map { delete $form->{$_} }
2454     qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
2455
2456   &order_links;
2457
2458   &prepare_order;
2459
2460   # format amounts
2461   for $i (1 .. $form->{rowcount} - 1) {
2462     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2463       qw(partnumber description unit);
2464   }
2465
2466   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 0, "0") }
2467     qw(creditlimit creditremaining);
2468
2469   &update;
2470
2471   $lxdebug->leave_sub();
2472 }
2473
2474 sub ship_receive {
2475   $lxdebug->enter_sub();
2476
2477   &order_links;
2478
2479   &prepare_order;
2480
2481   OE->get_warehouses(\%myconfig, \%$form);
2482
2483   # warehouse
2484   if (@{ $form->{all_warehouses} }) {
2485     $form->{selectwarehouse} = "<option>\n";
2486
2487     map { $form->{selectwarehouse} .= "<option>$_->{description}--$_->{id}\n" }
2488       (@{ $form->{all_warehouses} });
2489
2490     if ($form->{warehouse}) {
2491       $form->{selectwarehouse} = "<option>$form->{warehouse}";
2492     }
2493   }
2494
2495   $form->{shippingdate} = $form->current_date(\%myconfig);
2496   $form->{"$form->{vc}"} =~ s/--.*//;
2497
2498   @flds  = ();
2499   @a     = ();
2500   $count = 0;
2501   foreach $key (keys %$form) {
2502     if ($key =~ /_1$/) {
2503       $key =~ s/_1//;
2504       push @flds, $key;
2505     }
2506   }
2507
2508   for $i (1 .. $form->{rowcount}) {
2509
2510     # undo formatting from prepare_order
2511     map {
2512       $form->{"${_}_$i"} =
2513         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
2514     } qw(qty ship);
2515     $n = ($form->{"qty_$i"} -= $form->{"ship_$i"});
2516     if (abs($n) > 0
2517         && ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"})) {
2518       $form->{"ship_$i"}         = "";
2519       $form->{"serialnumber_$i"} = "";
2520
2521       push @a, {};
2522       $j = $#a;
2523
2524       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2525       $count++;
2526     }
2527   }
2528
2529   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2530   $form->{rowcount} = $count;
2531
2532   &display_ship_receive;
2533
2534   $lxdebug->leave_sub();
2535 }
2536
2537 sub display_ship_receive {
2538   $lxdebug->enter_sub();
2539
2540   $vclabel = ucfirst $form->{vc};
2541   $vclabel = $locale->text($vclabel);
2542
2543   $form->{rowcount}++;
2544
2545   if ($form->{vc} eq 'customer') {
2546     $form->{title} = $locale->text('Ship Merchandise');
2547     $shipped = $locale->text('Shipping Date');
2548   } else {
2549     $form->{title} = $locale->text('Receive Merchandise');
2550     $shipped = $locale->text('Date Received');
2551   }
2552
2553   # set option selected
2554   foreach $item (warehouse, employee) {
2555     $form->{"select$item"} =~ s/ selected//;
2556     $form->{"select$item"} =~
2557       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
2558   }
2559
2560   $warehouse = qq|
2561               <tr>
2562                 <th align=right>| . $locale->text('Warehouse') . qq|</th>
2563                 <td><select name=warehouse>$form->{selectwarehouse}</select></td>
2564                 <input type=hidden name=selectwarehouse value="$form->{selectwarehouse}">
2565               </tr>
2566 | if $form->{selectwarehouse};
2567
2568   $employee = qq|
2569               <tr>
2570                 <th align=right nowrap>| . $locale->text('Contact') . qq|</th>
2571                 <td><select name=employee>$form->{selectemployee}</select></td>
2572                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
2573               </tr>
2574 |;
2575
2576   $form->header;
2577
2578   print qq|
2579 <body>
2580
2581 <form method=post action=$form->{script}>
2582
2583 <input type=hidden name=id value=$form->{id}>
2584
2585 <input type=hidden name=display_form value=display_ship_receive>
2586
2587 <input type=hidden name=type value=$form->{type}>
2588 <input type=hidden name=media value=$form->{media}>
2589 <input type=hidden name=format value=$form->{format}>
2590
2591 <input type=hidden name=queued value="$form->{queued}">
2592 <input type=hidden name=printed value="$form->{printed}">
2593 <input type=hidden name=emailed value="$form->{emailed}">
2594
2595 <input type=hidden name=vc value=$form->{vc}>
2596
2597 <table width=100%>
2598   <tr class=listtop>
2599     <th class=listtop>$form->{title}</th>
2600   </tr>
2601   <tr height="5"></tr>
2602   <tr>
2603     <td>
2604       <table width="100%">
2605         <tr valign=top>
2606           <td>
2607             <table width=100%>
2608               <tr>
2609                 <th align=right>$vclabel</th>
2610                 <td colspan=3>$form->{$form->{vc}}</td>
2611                 <input type=hidden name=$form->{vc} value="$form->{$form->{vc}}">
2612                 <input type=hidden name="$form->{vc}_id" value=$form->{"$form->{vc}_id"}>
2613               </tr>
2614               $department
2615               <tr>
2616                 <th align=right>| . $locale->text('Shipping Point') . qq|</th>
2617                 <td colspan=3>
2618                 <input name=shippingpoint size=35 value="$form->{shippingpoint}">
2619               </tr>
2620               <tr>
2621                 <th align=right>| . $locale->text('Ship via') . qq|</th>
2622                 <td colspan=3>
2623                 <input name=shipvia size=35 value="$form->{shipvia}">
2624               </tr>
2625               $warehouse
2626             </table>
2627           </td>
2628           <td align=right>
2629             <table>
2630               $employee
2631               <tr>
2632                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
2633                 <td>$form->{ordnumber}</td>
2634                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
2635               </tr>
2636               <tr>
2637                 <th align=right nowrap>| . $locale->text('Order Date') . qq|</th>
2638                 <td>$form->{transdate}</td>
2639                 <input type=hidden name=transdate value=$form->{transdate}>
2640               </tr>
2641               <tr>
2642                 <th align=right nowrap>$shipped</th>
2643                 <td><input name=shippingdate size=11 value=$form->{shippingdate}></td>
2644               </tr>
2645             </table>
2646           </td>
2647         </tr>
2648       </table>
2649     </td>
2650   </tr>
2651
2652 <!-- shipto are in hidden variables -->
2653
2654 <input type=hidden name=shiptoname value="$form->{shiptoname}">
2655 <input type=hidden name=shiptostreet value="$form->{shiptostreet}">
2656 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
2657 <input type=hidden name=shiptocity value="$form->{shiptocity}">
2658 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
2659 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
2660 <input type=hidden name=shiptophone value="$form->{shiptophone}">
2661 <input type=hidden name=shiptofax value="$form->{shiptofax}">
2662 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
2663
2664 <!-- email variables -->
2665 <input type=hidden name=message value="$form->{message}">
2666 <input type=hidden name=email value="$form->{email}">
2667 <input type=hidden name=subject value="$form->{subject}">
2668 <input type=hidden name=cc value="$form->{cc}">
2669 <input type=hidden name=bcc value="$form->{bcc}">
2670
2671 |;
2672
2673   @column_index =
2674     (partnumber, description, qty, ship, unit, bin, serialnumber);
2675
2676   if ($form->{type} eq "ship_order") {
2677     $column_data{ship} =
2678         qq|<th class=listheading align=center width="auto">|
2679       . $locale->text('Ship')
2680       . qq|</th>|;
2681   }
2682   if ($form->{type} eq "receive_order") {
2683     $column_data{ship} =
2684         qq|<th class=listheading align=center width="auto">|
2685       . $locale->text('Recd')
2686       . qq|</th>|;
2687   }
2688
2689   my $colspan = $#column_index + 1;
2690
2691   $column_data{partnumber} =
2692     qq|<th class=listheading nowrap>| . $locale->text('Number') . qq|</th>|;
2693   $column_data{description} =
2694       qq|<th class=listheading nowrap>|
2695     . $locale->text('Description')
2696     . qq|</th>|;
2697   $column_data{qty} =
2698     qq|<th class=listheading nowrap>| . $locale->text('Qty') . qq|</th>|;
2699   $column_data{unit} =
2700     qq|<th class=listheading nowrap>| . $locale->text('Unit') . qq|</th>|;
2701   $column_data{bin} =
2702     qq|<th class=listheading nowrap>| . $locale->text('Bin') . qq|</th>|;
2703   $column_data{serialnumber} =
2704       qq|<th class=listheading nowrap>|
2705     . $locale->text('Serial No.')
2706     . qq|</th>|;
2707
2708   print qq|
2709   <tr>
2710     <td>
2711       <table width=100%>
2712         <tr class=listheading>|;
2713
2714   map { print "\n$column_data{$_}" } @column_index;
2715
2716   print qq|
2717         </tr>
2718 |;
2719
2720   for $i (1 .. $form->{rowcount} - 1) {
2721
2722     # undo formatting
2723     $form->{"ship_$i"} = $form->parse_amount(\%myconfig, $form->{"ship_$i"});
2724
2725     # convert " to &quot;
2726     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2727       qw(partnumber description unit bin serialnumber);
2728
2729     $description = $form->{"description_$i"};
2730     $description =~ s/\n/<br>/g;
2731
2732     $column_data{partnumber} =
2733       qq|<td>$form->{"partnumber_$i"}<input type=hidden name="partnumber_$i" value="$form->{"partnumber_$i"}"></td>|;
2734     $column_data{description} =
2735       qq|<td>$description<input type=hidden name="description_$i" value="$form->{"description_$i"}"></td>|;
2736     $column_data{qty} =
2737         qq|<td align=right>|
2738       . $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty)
2739       . qq|<input type=hidden name="qty_$i" value="$form->{"qty_$i"}"></td>|;
2740     $column_data{ship} =
2741         qq|<td align=right><input name="ship_$i" size=5 value=|
2742       . $form->format_amount(\%myconfig, $form->{"ship_$i"})
2743       . qq|></td>|;
2744     $column_data{unit} =
2745       qq|<td>$form->{"unit_$i"}<input type=hidden name="unit_$i" value="$form->{"unit_$i"}"></td>|;
2746     $column_data{bin} =
2747       qq|<td>$form->{"bin_$i"}<input type=hidden name="bin_$i" value="$form->{"bin_$i"}"></td>|;
2748
2749     $column_data{serialnumber} =
2750       qq|<td><input name="serialnumber_$i" size=15 value="$form->{"serialnumber_$i"}"></td>|;
2751
2752     print qq|
2753         <tr valign=top>|;
2754
2755     map { print "\n$column_data{$_}" } @column_index;
2756
2757     print qq|
2758         </tr>
2759
2760 <input type=hidden name="orderitems_id_$i" value=$form->{"orderitems_id_$i"}>
2761 <input type=hidden name="id_$i" value=$form->{"id_$i"}>
2762 <input type=hidden name="assembly_$i" value="$form->{"assembly_$i"}">
2763 <input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">
2764
2765 |;
2766
2767   }
2768
2769   print qq|
2770       </table>
2771     </td>
2772   </tr>
2773   <tr>
2774     <td><hr size=3 noshade></td>
2775   </tr>
2776   <tr>
2777     <td>
2778 |;
2779
2780   $form->{copies} = 1;
2781
2782   &print_options;
2783
2784   print qq|
2785     </td>
2786   </tr>
2787 </table>
2788 <br>
2789 <input class=submit type=submit name=action value="|
2790     . $locale->text('Update') . qq|">
2791 <input class=submit type=submit name=action value="|
2792     . $locale->text('Print') . qq|">
2793 |;
2794
2795   if ($form->{type} eq 'ship_order') {
2796     print qq|
2797 <input class=submit type=submit name=action value="|
2798       . $locale->text('Ship to') . qq|">
2799 <input class=submit type=submit name=action value="|
2800       . $locale->text('E-mail') . qq|">
2801 |;
2802   }
2803
2804   print qq|
2805
2806 <input class=submit type=submit name=action value="|
2807     . $locale->text('Done') . qq|">
2808 |;
2809
2810   if ($form->{menubar}) {
2811     require "$form->{path}/menu.pl";
2812     &menubar;
2813   }
2814
2815   print qq|
2816
2817 <input type=hidden name=rowcount value=$form->{rowcount}>
2818
2819 <input name=callback type=hidden value="$callback">
2820
2821 <input type=hidden name=path value=$form->{path}>
2822 <input type=hidden name=login value=$form->{login}>
2823 <input type=hidden name=password value=$form->{password}>
2824
2825 </form>
2826
2827 </body>
2828 </html>
2829 |;
2830
2831   $lxdebug->leave_sub();
2832 }
2833
2834 sub done {
2835   $lxdebug->enter_sub();
2836
2837   if ($form->{type} eq 'ship_order') {
2838     $form->isblank("shippingdate", $locale->text('Shipping Date missing!'));
2839   } else {
2840     $form->isblank("shippingdate", $locale->text('Date received missing!'));
2841   }
2842
2843   $total = 0;
2844   map {
2845     $total += $form->{"ship_$_"} =
2846       $form->parse_amount(\%myconfig, $form->{"ship_$_"})
2847   } (1 .. $form->{rowcount} - 1);
2848
2849   $form->error($locale->text('Nothing entered!')) unless $total;
2850
2851   $form->redirect($locale->text('Inventory saved!'))
2852     if OE->save_inventory(\%myconfig, \%$form);
2853   $form->error($locale->text('Could not save!'));
2854
2855   $lxdebug->leave_sub();
2856 }
2857
2858 sub search_transfer {
2859   $lxdebug->enter_sub();
2860
2861   OE->get_warehouses(\%myconfig, \%$form);
2862
2863   # warehouse
2864   if (@{ $form->{all_warehouses} }) {
2865     $form->{selectwarehouse} = "<option>\n";
2866     $form->{warehouse}       = qq|$form->{warehouse}--$form->{warehouse_id}|;
2867
2868     map { $form->{selectwarehouse} .= "<option>$_->{description}--$_->{id}\n" }
2869       (@{ $form->{all_warehouses} });
2870   } else {
2871     $form->error($locale->text('Nothing to transfer!'));
2872   }
2873
2874   $form->{title} = $locale->text('Transfer Inventory');
2875
2876   $form->header;
2877
2878   print qq|
2879 <body>
2880
2881 <form method=post action=$form->{script}>
2882
2883 <table width=100%>
2884   <tr>
2885     <th class=listtop>$form->{title}</th>
2886   </tr>
2887   <tr height="5"></tr>
2888   <tr>
2889     <td>
2890       <table>
2891         <tr>
2892           <th align=right nowrap>| . $locale->text('Transfer to') . qq|</th>
2893           <td><select name=warehouse>$form->{selectwarehouse}</select></td>
2894         </tr>
2895         <tr>
2896           <th align="right" nowrap="true">| . $locale->text('Part Number') . qq|</th>
2897           <td><input name=partnumber size=20></td>
2898         </tr>
2899         <tr>
2900           <th align="right" nowrap="true">| . $locale->text('Description') . qq|</th>
2901           <td><input name=description size=40></td>
2902         </tr>
2903         <tr>
2904           <th align=right nowrap>| . $locale->text('Group') . qq|</th>
2905           <td><input name=partsgroup size=20></td>
2906         </tr>
2907       </table>
2908     </td>
2909   </tr>
2910   <tr>
2911     <td><hr size=3 noshade></td>
2912   </tr>
2913 </table>
2914
2915 <br>
2916 <input type=hidden name=sort value=partnumber>
2917 <input type=hidden name=nextsub value=list_transfer>
2918
2919 <input type=hidden name=path value=$form->{path}>
2920 <input type=hidden name=login value=$form->{login}>
2921 <input type=hidden name=password value=$form->{password}>
2922
2923 <input class=submit type=submit name=action value="|
2924     . $locale->text('Continue') . qq|">
2925 </form>
2926
2927 </body>
2928 </html>
2929 |;
2930
2931   $lxdebug->leave_sub();
2932 }
2933
2934 sub list_transfer {
2935   $lxdebug->enter_sub();
2936
2937   OE->get_inventory(\%myconfig, \%$form);
2938
2939   $partnumber  = $form->escape($form->{partnumber});
2940   $warehouse   = $form->escape($form->{warehouse});
2941   $description = $form->escape($form->{description});
2942   $partsgroup  = $form->escape($form->{partsgroup});
2943
2944   # construct href
2945   $href =
2946     "$form->{script}?path=$form->{path}&action=list_transfer&partnumber=$partnumber&warehouse=$warehouse&description=$description&partsgroup=$partsgroup&login=$form->{login}&password=$form->{password}";
2947
2948   # construct callback
2949   $partnumber  = $form->escape($form->{partnumber},  1);
2950   $warehouse   = $form->escape($form->{warehouse},   1);
2951   $description = $form->escape($form->{description}, 1);
2952   $partsgroup  = $form->escape($form->{partsgroup},  1);
2953
2954   $callback =
2955     "$form->{script}?path=$form->{path}&action=list_transfer&partnumber=$partnumber&warehouse=$warehouse&description=$description&partsgroup=$partsgroup&login=$form->{login}&password=$form->{password}";
2956
2957   @column_index =
2958     $form->sort_columns(
2959       qw(partnumber description partsgroup make model warehouse qty transfer));
2960
2961   $column_header{partnumber} =
2962       qq|<th><a class=listheading href=$href&sort=partnumber>|
2963     . $locale->text('Part Number')
2964     . qq|</a></th>|;
2965   $column_header{description} =
2966       qq|<th><a class=listheading href=$href&sort=description>|
2967     . $locale->text('Description')
2968     . qq|</a></th>|;
2969   $column_header{partsgroup} =
2970       qq|<th><a class=listheading href=$href&sort=partsgroup>|
2971     . $locale->text('Group')
2972     . qq|</a></th>|;
2973   $column_header{warehouse} =
2974       qq|<th><a class=listheading href=$href&sort=warehouse>|
2975     . $locale->text('From')
2976     . qq|</a></th>|;
2977   $column_header{qty} =
2978     qq|<th><a class=listheading>| . $locale->text('Qty') . qq|</a></th>|;
2979   $column_header{transfer} =
2980     qq|<th><a class=listheading>| . $locale->text('Transfer') . qq|</a></th>|;
2981
2982   $option = $locale->text('Transfer to');
2983
2984   ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
2985
2986   if ($form->{warehouse}) {
2987     $option .= " : $warehouse";
2988   }
2989   if ($form->{partnumber}) {
2990     $option .= "\n<br>" if ($option);
2991     $option .= $locale->text('Part Number') . " : $form->{partnumber}";
2992   }
2993   if ($form->{description}) {
2994     $option .= "\n<br>" if ($option);
2995     $option .= $locale->text('Description') . " : $form->{description}";
2996   }
2997   if ($form->{partsgroup}) {
2998     $option .= "\n<br>" if ($option);
2999     $option .= $locale->text('Group') . " : $form->{partsgroup}";
3000   }
3001
3002   $form->{title} = $locale->text('Transfer Inventory');
3003
3004   $form->header;
3005
3006   print qq|
3007 <body>
3008
3009 <form method=post action=$form->{script}>
3010
3011 <input type=hidden name=warehouse_id value=$warehouse_id>
3012
3013 <table width=100%>
3014   <tr>
3015     <th class=listtop>$form->{title}</th>
3016   </tr>
3017   <tr height="5"></tr>
3018   <tr>
3019     <td>$option</td>
3020   </tr>
3021   <tr>
3022     <td>
3023       <table width=100%>
3024         <tr class=listheading>|;
3025
3026   map { print "\n$column_header{$_}" } @column_index;
3027
3028   print qq|
3029         </tr>
3030 |;
3031
3032   if (@{ $form->{all_inventory} }) {
3033     $sameitem = $form->{all_inventory}->[0]->{ $form->{sort} };
3034   }
3035
3036   $i = 0;
3037   foreach $ref (@{ $form->{all_inventory} }) {
3038
3039     $i++;
3040
3041     $column_data{partnumber} =
3042       qq|<td><input type=hidden name="id_$i" value=$ref->{id}>$ref->{partnumber}</td>|;
3043     $column_data{description} = "<td>$ref->{description}&nbsp;</td>";
3044     $column_data{partsgroup}  = "<td>$ref->{partsgroup}&nbsp;</td>";
3045     $column_data{warehouse}   =
3046       qq|<td><input type=hidden name="warehouse_id_$i" value=$ref->{warehouse_id}>$ref->{warehouse}&nbsp;</td>|;
3047     $column_data{qty} =
3048         qq|<td><input type=hidden name="qty_$i" value=$ref->{qty}>|
3049       . $form->format_amount(\%myconfig, $ref->{qty}, $dec_qty)
3050       . qq|</td>|;
3051     $column_data{transfer} = qq|<td><input name="transfer_$i" size=4></td>|;
3052
3053     $j++;
3054     $j %= 2;
3055     print "
3056         <tr class=listrow$j>";
3057
3058     map { print "\n$column_data{$_}" } @column_index;
3059
3060     print qq|
3061         </tr>
3062 |;
3063
3064   }
3065
3066   print qq|
3067       </table>
3068     </td>
3069   </tr>
3070
3071   <tr>
3072     <td><hr size=3 noshade></td>
3073   </tr>
3074 </table>
3075
3076 <br>
3077
3078 <input name=callback type=hidden value="$callback">
3079
3080 <input type=hidden name=rowcount value=$i>
3081
3082 <input type=hidden name=path value=$form->{path}>
3083 <input type=hidden name=login value=$form->{login}>
3084 <input type=hidden name=password value=$form->{password}>
3085
3086 <input class=submit type=submit name=action value="|
3087     . $locale->text('Transfer') . qq|">|;
3088
3089   if ($form->{menubar}) {
3090     require "$form->{path}/menu.pl";
3091     &menubar;
3092   }
3093
3094   print qq|
3095 </form>
3096
3097 </body>
3098 </html>
3099 |;
3100
3101   $lxdebug->leave_sub();
3102 }
3103
3104 sub transfer {
3105   $lxdebug->enter_sub();
3106
3107   $form->redirect($locale->text('Inventory transferred!'))
3108     if OE->transfer(\%myconfig, \%$form);
3109   $form->error($locale->text('Could not transfer Inventory!'));
3110
3111   $lxdebug->leave_sub();
3112 }