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