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