Beim Erneuern der Einkaufs- und Verkaufsmasken sowie direkt vor dem Speichern/Buchen...
[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   map { $form->{$_} =~ s/\"/&quot;/g }
292     qw(ordnumber quonumber shippingpoint shipvia notes intnotes shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact);
293   my $i = 0;
294   foreach $ref (@{ $form->{form_details} }) {
295     $form->{rowcount} = ++$i;
296
297     map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
298   }
299   for my $i (1 .. $form->{rowcount}) {
300     if ($form->{id}) {
301       $form->{"discount_$i"} =
302         $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
303     } else {
304       $form->{"discount_$i"} =
305         $form->format_amount(\%myconfig, $form->{"discount_$i"});
306     }
307     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
308     $dec           = length $dec;
309     $decimalplaces = ($dec > 2) ? $dec : 2;
310
311     # copy reqdate from deliverydate for invoice -> order conversion
312     $form->{"reqdate_$i"} = $form->{"deliverydate_$i"}
313       unless $form->{"reqdate_$i"};
314
315     $form->{"sellprice_$i"} =
316       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
317                            $decimalplaces);
318
319     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
320     $dec_qty = length $dec_qty;
321     $form->{"qty_$i"} =
322       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
323
324     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
325       qw(partnumber description unit);
326   }
327
328   $lxdebug->leave_sub();
329 }
330
331 sub form_header {
332   $lxdebug->enter_sub();
333
334   $checkedopen   = ($form->{closed}) ? ""        : "checked";
335   $checkedclosed = ($form->{closed}) ? "checked" : "";
336
337   # use JavaScript Calendar or not
338   $form->{jsscript} = $form->{jscalendar};
339   $jsscript = "";
340
341   $payment = qq|<option value=""></option>|;
342   foreach $item (@{ $form->{payment_terms} }) {
343     if ($form->{payment_id} eq $item->{id}) {
344       $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
345     } else {
346       $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
347     }
348   }
349   if ($form->{jsscript}) {
350
351     # with JavaScript Calendar
352     $button1 = qq|
353        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>
354        <td><input type=button name=transdate id="trigger1" value=|
355       . $locale->text('button') . qq|></td>
356       |;
357     $button2 = qq|
358        <td width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value=$form->{reqdate}></td>
359        <td width="4"><input type=button name=reqdate name=reqdate id="trigger2" value=|
360       . $locale->text('button') . qq|></td>
361      |;
362
363     #write Trigger
364     $jsscript =
365       Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
366                           "reqdate", "BL", "trigger2");
367
368   } else {
369
370     # without JavaScript Calendar
371     $button1 = qq|
372                               <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>|;
373     $button2 = qq|
374                               <td width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value=$form->{reqdate}></td>|;
375   }
376
377   if ($form->{id}) {
378     $openclosed = qq|
379       <tr>
380         <td colspan=2 align=center>
381           <table>
382             <tr>
383               <th nowrap><input name=closed type=radio class=radio value=0 $checkedopen> |
384       . $locale->text('Open') . qq|</th>
385               <th nowrap><input name=closed type=radio class=radio value=1 $checkedclosed> |
386       . $locale->text('Closed') . qq|</th>
387             </tr>
388           </table>
389         </td>
390       </tr>
391 |;
392   }
393
394   # set option selected
395   foreach $item ($form->{vc}, currency, department, employee) {
396     $form->{"select$item"} =~ s/ selected//;
397     $form->{"select$item"} =~
398       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
399   }
400
401   #quote select[customer|vendor] Bug 133
402   $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
403
404   #build contacts
405   if ($form->{all_contacts}) {
406
407     $form->{selectcontact} = "<option></option>";
408     foreach $item (@{ $form->{all_contacts} }) {
409       my $department = ($item->{cp_abteilung}) ? "--$item->{cp_abteilung}" : "";
410       if ($form->{cp_id} == $item->{cp_id}) {
411         $form->{selectcontact} .=
412           "<option value=$item->{cp_id} selected>$item->{cp_name}$department</option>";
413       } else {
414         $form->{selectcontact} .= "<option value=$item->{cp_id}>$item->{cp_name}$department</option>";
415       }
416     }
417   } else {
418     $form->{selectcontact} =~ s/ selected//g;
419     if ($form->{cp_id} ne "") {
420       $form->{selectcontact} =~ s/value=$form->{cp_id}/value=$form->{cp_id} selected/;
421     }
422   }
423
424
425   if (@{ $form->{SHIPTO} }) {
426     $form->{selectshipto} = "<option value=0></option>";
427     foreach $item (@{ $form->{SHIPTO} }) {
428       if ($item->{shipto_id} == $form->{shipto_id}) {
429         $form->{selectshipto} .=
430           "<option value=$item->{shipto_id} selected>$item->{shiptoname} $item->{shiptodepartment_1}</option>";
431       } else {
432         $form->{selectshipto} .=
433           "<option value=$item->{shipto_id}>$item->{shiptoname} $item->{shiptodepartment_1}</option>";
434       }
435
436     }
437   } else {
438     $form->{selectshipto} =~ s/ selected//g;
439     if ($form->{shipto_id} ne "") {
440       $form->{selectshipto} =~ s/value=$form->{shipto_id}/value=$form->{shipto_id} selected/;
441     }
442   }
443
444   $shipto = qq|
445                 <th align=right>| . $locale->text('Shipping Address') . qq|</th>
446                 <td><select name=shipto_id>$form->{selectshipto}</select></td>
447                 <input type=hidden name=selectshipto value="$form->{selectshipto}">|;
448
449
450
451
452   $form->{exchangerate} =
453     $form->format_amount(\%myconfig, $form->{exchangerate});
454
455   if (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) {
456     $creditwarning = 1;
457   } else {
458     $creditwarning = 0;
459   }
460
461   $form->{creditlimit} =
462     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
463   $form->{creditremaining} =
464     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
465
466   $contact =
467     ($form->{selectcontact})
468     ? qq|<select name=cp_id>$form->{selectcontact}</select>\n<input type=hidden name="selectcontact" value="$form->{selectcontact}">|
469     : qq|<input name=contact value="$form->{contact}" size=35>|;
470
471   $exchangerate = qq|
472 <input type=hidden name=forex value=$form->{forex}>
473 |;
474
475   if ($form->{currency} ne $form->{defaultcurrency}) {
476     if ($form->{forex}) {
477       $exchangerate .=
478           qq|<th align=right>|
479         . $locale->text('Exchangerate')
480         . qq|</th><td>$form->{exchangerate}</td>
481       <input type=hidden name=exchangerate value=$form->{exchangerate}>
482 |;
483     } else {
484       $exchangerate .=
485           qq|<th align=right>|
486         . $locale->text('Exchangerate')
487         . qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
488     }
489   }
490
491   $vclabel = ucfirst $form->{vc};
492   $vclabel = $locale->text($vclabel);
493
494   $terms = qq|
495                     <tr>
496                       <th align=right nowrap>| . $locale->text('Terms: Net') . qq|</th>
497                       <td nowrap><input name=terms size="3" maxlength="3" value=$form->{terms}> |
498     . $locale->text('days') . qq|</td>
499                     </tr>
500 |;
501
502   if ($form->{business}) {
503     $business = qq|
504               <tr>
505                 <th align=right>| . $locale->text('Business') . qq|</th>
506                 <td>$form->{business}</td>
507                 <th align=right>| . $locale->text('Trade Discount') . qq|</th>
508                 <td>|
509       . $form->format_amount(\%myconfig, $form->{tradediscount} * 100)
510       . qq| %</td>
511               </tr>
512 |;
513   }
514
515   if ($form->{max_dunning_level}) {
516     $dunning = qq|
517               <tr>
518                 <td colspan=4>
519                 <table>
520                   <tr>
521                 <th align=right>| . $locale->text('Max. Dunning Level') . qq|:</th>
522                 <td><b>$form->{max_dunning_level}</b></td>
523                 <th align=right>| . $locale->text('Dunning Amount') . qq|:</th>
524                 <td><b>|
525       . $form->format_amount(\%myconfig, $form->{dunning_amount},2)
526       . qq|</b></td>
527               </tr>
528               </table>
529              </td>
530             </tr>
531 |;
532   }
533
534   if (@{ $form->{TAXZONE} }) {
535     $form->{selecttaxzone} = "";
536     foreach $item (@{ $form->{TAXZONE} }) {
537       if ($item->{id} == $form->{taxzone_id}) {
538         $form->{selecttaxzone} .=
539           "<option value=$item->{id} selected>$item->{description}</option>";
540       } else {
541         $form->{selecttaxzone} .=
542           "<option value=$item->{id}>$item->{description}</option>";
543       }
544
545     }
546   } else {
547     $form->{selecttaxzone} =~ s/ selected//g;
548     if ($form->{taxzone_id} ne "") {
549       $form->{selecttaxzone} =~ s/value=$form->{taxzone_id}/value=$form->{taxzone_id} selected/;
550     }
551   }
552
553   if ($form->{rowcount} >0) {
554     $form->{selecttaxzone} =~ /<option value=\d+ selected>.*?<\/option>/;
555     $form->{selecttaxzone} = $&;
556   }
557   
558
559   $taxzone = qq|
560               <tr>
561                 <th align=right>| . $locale->text('Steuersatz') . qq|</th>
562                 <td><select name=taxzone_id>$form->{selecttaxzone}</select></td>
563                 <input type=hidden name=selecttaxzone value="$form->{selecttaxzone}">
564               </tr>|;
565
566
567   if ($form->{type} !~ /_quotation$/) {
568     $ordnumber = qq|
569               <tr>
570                 <th width=70% align=right nowrap>| . $locale->text('Order Number') . qq|</th>
571                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
572               </tr>
573               <tr>
574                 <th width=70% align=right nowrap>|
575       . $locale->text('Quotation Number') . qq|</th>
576                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
577               </tr>
578               <tr>
579                 <th width=70% align=right nowrap>|
580       . $locale->text('Customer Order Number') . qq|</th>
581                 <td><input name=cusordnumber size=11 value="$form->{cusordnumber}"></td>
582               </tr>
583               <tr>
584                 <th align=right nowrap>| . $locale->text('Order Date') . qq|</th>
585                 $button1
586
587               </tr>
588               <tr>
589                 <th align=right nowrap=true>| . $locale->text('Required by') . qq|</th>
590                 $button2
591               </tr>
592 |;
593
594     $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
595
596     $creditremaining = qq|
597               <tr>
598                 <td></td>
599                 <td colspan=3>
600                   <table>
601                     <tr>
602                       <th nowrap>| . $locale->text('Credit Limit') . qq|</th>
603                       <td>$form->{creditlimit}</td>
604                       <td width=20%></td>
605                       <th nowrap>| . $locale->text('Remaining') . qq|</th>
606                       <td class="plus$n">$form->{creditremaining}</td>
607                     </tr>
608                   </table>
609                 </td>
610                 $shipto
611               </tr>
612 |;
613   } else {
614     $reqlabel =
615       ($form->{type} eq 'sales_quotation')
616       ? $locale->text('Valid until')
617       : $locale->text('Required by');
618     if ($form->{type} eq 'sales_quotation') {
619       $ordnumber = qq|
620               <tr>
621                 <th width=70% align=right nowrap>|
622         . $locale->text('Quotation Number') . qq|</th>
623                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
624                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
625               </tr>
626 |;
627     } else {
628       $ordnumber = qq|
629               <tr>
630                 <th width=70% align=right nowrap>| . $locale->text('RFQ Number') . qq|</th>
631                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
632                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
633               </tr>
634 |;
635
636       $terms = "";
637     }
638
639     $ordnumber .= qq|
640               <tr>
641                 <th align=right nowrap>| . $locale->text('Quotation Date') . qq|</th>
642                 $button1
643               </tr>
644               <tr>
645                 <th align=right nowrap=true>$reqlabel</th>
646                 $button2
647               </tr>
648 |;
649     $creditremaining = qq| <tr>
650                             <td colspan=4></td>
651                             $shipto
652                           </tr>|;
653   }
654
655   $vc =
656     ($form->{"select$form->{vc}"})
657     ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}</select>\n<input type=hidden name="select$form->{vc}" value="$form->{"select$form->{vc}"}">|
658     : qq|<input name=$form->{vc} value="$form->{$form->{vc}}" size=35>|;
659
660   $department = qq|
661               <tr>
662                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
663                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
664                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
665                 </td>
666               </tr>
667 | if $form->{selectdepartment};
668
669   $employee = qq|
670               <input type=hidden name=employee value="$form->{employee}">
671 |;
672
673   if ($form->{type} eq 'sales_order') {
674     if ($form->{selectemployee}) {
675       $employee = qq|
676     <input type=hidden name=customer_klass value=$form->{customer_klass}>
677               <tr>
678                 <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
679                 <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
680                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
681                 <td></td>
682               </tr>
683 |;
684     }
685   } else {
686     $employee = qq|
687     <input type=hidden name=customer_klass value=$form->{customer_klass}>
688               <tr>
689                 <th align=right nowrap>| . $locale->text('Employee') . qq|</th>
690                 <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
691                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
692                 <td></td>
693               </tr>
694 |;
695   }
696   if ($form->{resubmit} && ($form->{format} eq "html")) {
697     $onload =
698       qq|window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()|;
699   } elsif ($form->{resubmit}) {
700     $onload = qq|document.oe.submit()|;
701   } else {
702     $onload = "fokus()";
703   }
704
705   $credittext = $locale->text('Credit Limit exceeded!!!');
706   if ($creditwarning) {
707     $onload = qq|alert('$credittext')|;
708   }
709
710   $form->header;
711
712   print qq|
713 <body onLoad="$onload">
714
715 <form method=post name=oe action=$form->{script}>
716  <script type="text/javascript" src="js/common.js"></script>
717  <script type="text/javascript" src="js/delivery_customer_selection.js"></script>
718  <script type="text/javascript" src="js/vendor_selection.js"></script>
719  <script type="text/javascript" src="js/calculate_qty.js"></script>
720
721 <input type=hidden name=id value=$form->{id}>
722 <input type=hidden name=action value=$form->{action}>
723
724 <input type=hidden name=type value=$form->{type}>
725 <input type=hidden name=formname value=$form->{formname}>
726 <input type=hidden name=media value=$form->{media}>
727 <input type=hidden name=format value=$form->{format}>
728 <input type=hidden name=proforma value=$form->{proforma}>
729
730 <input type=hidden name=queued value="$form->{queued}">
731 <input type=hidden name=printed value="$form->{printed}">
732 <input type=hidden name=emailed value="$form->{emailed}">
733
734 <input type=hidden name=vc value=$form->{vc}>
735
736 <input type=hidden name=title value="$form->{title}">
737
738 <input type=hidden name=discount value=$form->{discount}>
739 <input type=hidden name=creditlimit value=$form->{creditlimit}>
740 <input type=hidden name=creditremaining value=$form->{creditremaining}>
741
742 <input type=hidden name=tradediscount value=$form->{tradediscount}>
743 <input type=hidden name=business value=$form->{business}>
744 <input type=hidden name=webdav value=$webdav>
745
746 <table width=100%>
747   <tr class=listtop>
748     <th class=listtop>$form->{title}</th>
749   </tr>
750   <tr height="5"></tr>
751   <tr>
752     <td>
753       <table width="100%">
754         <tr valign=top>
755           <td>
756             <table width=100%>
757               <tr>
758                 <th align=right>$vclabel</th>
759                 <td colspan=3>$vc</td>
760                 <input type=hidden name=$form->{vc}_id value=$form->{"$form->{vc}_id"}>
761                 <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
762                 <th align=richt nowrap>|
763     . $locale->text('Contact Person') . qq|</th>
764                 <td colspan=3>$contact</td>
765               </tr>
766               $creditremaining
767               $business
768               $dunning
769               $taxzone
770               $department
771               <tr>
772                 <th align=right>| . $locale->text('Currency') . qq|</th>
773                 <td><select name=currency>$form->{selectcurrency}</select></td>
774                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
775                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
776                 $exchangerate
777               </tr>
778               <tr>
779                 <th align=right>| . $locale->text('Shipping Point') . qq|</th>
780                 <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
781               </tr>
782               <tr>
783                 <th align=right>| . $locale->text('Ship via') . qq|</th>
784                 <td colspan=3><input name=shipvia size=35 value="$form->{shipvia}"></td>
785               </tr>|;
786 #              <tr>
787 #                 <td colspan=4>
788 #                   <table>
789 #                     <tr>
790 #                       <td colspan=2>
791 #                         <button type="button" onclick="delivery_customer_selection_window('delivery_customer_string','delivery_customer_id')">| . $locale->text('Choose Customer') . qq|</button>
792 #                       </td>
793 #                       <td colspan=2><input type=hidden name=delivery_customer_id value="$form->{delivery_customer_id}">
794 #                       <input size=45 id=delivery_customer_string name=delivery_customer_string value="$form->{delivery_customer_string}"></td>
795 #                     </tr>
796 #                     <tr>
797 #                       <td colspan=2>
798 #                         <button type="button" onclick="vendor_selection_window('delivery_vendor_string','delivery_vendor_id')">| . $locale->text('Choose Vendor') . qq|</button>
799 #                       </td>
800 #                       <td colspan=2><input type=hidden name=delivery_vendor_id value="$form->{delivery_vendor_id}">
801 #                       <input size=45 id=vendor_string name=delivery_vendor_string value="$form->{delivery_vendor_string}"></td>
802 #                     </tr>
803 #                   </table>
804 #                 </td>
805 #               </tr>
806 print qq|           </table>
807           </td>
808           <td align=right>
809             <table>
810               $openclosed
811               $employee
812               $ordnumber
813               $terms
814             </table>
815           </td>
816         </tr>
817       </table>
818     </td>
819   </tr>
820
821 $jsscript
822
823 <!-- shipto are in hidden variables -->
824
825 <input type=hidden name=shiptoname value="$form->{shiptoname}">
826 <input type=hidden name=shiptostreet value="$form->{shiptostreet}">
827 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
828 <input type=hidden name=shiptocity value="$form->{shiptocity}">
829 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
830 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
831 <input type=hidden name=shiptophone value="$form->{shiptophone}">
832 <input type=hidden name=shiptofax value="$form->{shiptofax}">
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('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     $form->{"selected_unit_$i"} = $form->{"unit_$i"};
1172
1173     if (   $form->{type} eq 'purchase_order'
1174         || $form->{type} eq 'request_quotation') {
1175       IR->retrieve_item(\%myconfig, \%$form);
1176     }
1177     if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
1178       IS->retrieve_item(\%myconfig, \%$form);
1179     }
1180
1181     my $rows = scalar @{ $form->{item_list} };
1182
1183     $form->{"discount_$i"} =
1184       $form->format_amount(\%myconfig, $form->{discount} * 100);
1185
1186     if ($rows) {
1187       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1188
1189       if ($rows > 1) {
1190
1191         &select_item;
1192         exit;
1193
1194       } else {
1195
1196         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1197         if ($form->{"not_discountable_$i"}) {
1198           $form->{"discount_$i"} = 0;
1199         }
1200         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1201           qw(partnumber description unit);
1202         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1203           keys %{ $form->{item_list}[0] };
1204         if ($form->{"part_payment_id_$i"} ne "") {
1205           $form->{payment_id} = $form->{"part_payment_id_$i"};
1206         }
1207
1208         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1209
1210         ($dec) = ($s =~ /\.(\d+)/);
1211         $dec           = length $dec;
1212         $decimalplaces = ($dec > 2) ? $dec : 2;
1213
1214         if ($sellprice) {
1215           $form->{"sellprice_$i"} = $sellprice;
1216         } else {
1217
1218           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1219
1220           # if there is an exchange rate adjust sellprice
1221           $form->{"sellprice_$i"} /= $exchangerate;
1222         }
1223
1224         $amount =
1225           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1226           (1 - $form->{"discount_$i"} / 100);
1227         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1228         map { $form->{"${_}_base"} += $amount }
1229           (split / /, $form->{"taxaccounts_$i"});
1230         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1231           split / /, $form->{taxaccounts}
1232           if !$form->{taxincluded};
1233
1234         $form->{creditremaining} -= $amount;
1235
1236         $form->{"sellprice_$i"} =
1237           $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1238                                $decimalplaces);
1239         $form->{"qty_$i"} =
1240           $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1241
1242         # get pricegroups for parts
1243         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1244
1245         # build up html code for prices_$i
1246         &set_pricegroup($i);
1247       }
1248
1249       &display_form;
1250
1251     } else {
1252
1253       # ok, so this is a new part
1254       # ask if it is a part or service item
1255
1256       if (   $form->{"partsgroup_$i"}
1257           && ($form->{"partsnumber_$i"} eq "")
1258           && ($form->{"description_$i"} eq "")) {
1259         $form->{rowcount}--;
1260         $form->{"discount_$i"} = "";
1261         &display_form;
1262       } else {
1263
1264         $form->{"id_$i"}   = 0;
1265         $form->{"unit_$i"} = $locale->text('ea');
1266
1267         &new_item;
1268
1269       }
1270     }
1271   }
1272
1273   $lxdebug->leave_sub();
1274 }
1275
1276 sub search {
1277   $lxdebug->enter_sub();
1278
1279   if ($form->{type} eq 'purchase_order') {
1280     $form->{title} = $locale->text('Purchase Orders');
1281     $form->{vc}    = 'vendor';
1282     $ordlabel      = $locale->text('Order Number');
1283     $ordnumber     = 'ordnumber';
1284     $employee      = $locale->text('Employee');
1285   }
1286   if ($form->{type} eq 'request_quotation') {
1287     $form->{title} = $locale->text('Request for Quotations');
1288     $form->{vc}    = 'vendor';
1289     $ordlabel      = $locale->text('RFQ Number');
1290     $ordnumber     = 'quonumber';
1291     $employee      = $locale->text('Employee');
1292   }
1293   if ($form->{type} eq 'receive_order') {
1294     $form->{title} = $locale->text('Receive Merchandise');
1295     $form->{vc}    = 'vendor';
1296     $ordlabel      = $locale->text('Order Number');
1297     $ordnumber     = 'ordnumber';
1298     $employee      = $locale->text('Employee');
1299   }
1300   if ($form->{type} eq 'sales_order') {
1301     $form->{title} = $locale->text('Sales Orders');
1302     $form->{vc}    = 'customer';
1303     $ordlabel      = $locale->text('Order Number');
1304     $ordnumber     = 'ordnumber';
1305     $employee      = $locale->text('Salesperson');
1306   }
1307   if ($form->{type} eq 'ship_order') {
1308     $form->{title} = $locale->text('Ship Merchandise');
1309     $form->{vc}    = 'customer';
1310     $ordlabel      = $locale->text('Order Number');
1311     $ordnumber     = 'ordnumber';
1312     $employee      = $locale->text('Salesperson');
1313
1314   }
1315
1316   if ($form->{type} eq 'sales_quotation') {
1317     $form->{title} = $locale->text('Quotations');
1318     $form->{vc}    = 'customer';
1319     $ordlabel      = $locale->text('Quotation Number');
1320     $ordnumber     = 'quonumber';
1321     $employee      = $locale->text('Employee');
1322   }
1323
1324   if ($form->{type} =~ /(ship|receive)_order/) {
1325     OE->get_warehouses(\%myconfig, \%$form);
1326
1327     # warehouse
1328     if (@{ $form->{all_warehouses} }) {
1329       $form->{selectwarehouse} = "<option>\n";
1330       $form->{warehouse}       = qq|$form->{warehouse}--$form->{warehouse_id}|;
1331
1332       map {
1333         $form->{selectwarehouse} .=
1334           "<option>$_->{description}--$_->{id}\n"
1335       } (@{ $form->{all_warehouses} });
1336
1337       $warehouse = qq|
1338               <tr>
1339                 <th align=right>| . $locale->text('Warehouse') . qq|</th>
1340                 <td colspan=3><select name=warehouse>$form->{selectwarehouse}</select></td>
1341                 <input type=hidden name=selectwarehouse value="$form->{selectwarehouse}">
1342               </tr>
1343 |;
1344
1345     }
1346   }
1347
1348   # setup vendor / customer selection
1349   $form->all_vc(\%myconfig, $form->{vc},
1350                 ($form->{vc} eq 'customer') ? "AR" : "AP");
1351
1352   map { $vc .= "<option>$_->{name}--$_->{id}\n" }
1353     @{ $form->{"all_$form->{vc}"} };
1354
1355   $vclabel = ucfirst $form->{vc};
1356   $vclabel = $locale->text($vclabel);
1357
1358   # $locale->text('Vendor')
1359   # $locale->text('Customer')
1360
1361   $vc =
1362     ($vc)
1363     ? qq|<select name=$form->{vc}><option>\n$vc</select>|
1364     : qq|<input name=$form->{vc} size=35>|;
1365
1366   # departments
1367   if (@{ $form->{all_departments} }) {
1368     $form->{selectdepartment} = "<option>\n";
1369
1370     map {
1371       $form->{selectdepartment} .=
1372         "<option>$_->{description}--$_->{id}\n"
1373     } (@{ $form->{all_departments} });
1374   }
1375
1376   $department = qq|
1377         <tr>
1378           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1379           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1380         </tr>
1381 | if $form->{selectdepartment};
1382
1383   if ($form->{type} !~ /(ship_order|receive_order)/) {
1384     $openclosed = qq|
1385               <tr>
1386                 <td><input name="open" class=checkbox type=checkbox value=1 checked> |
1387       . $locale->text('Open') . qq|</td>
1388                 <td><input name="closed" class=checkbox type=checkbox value=1 $form->{closed}> |
1389       . $locale->text('Closed') . qq|</td>
1390               </tr>
1391 |;
1392   } else {
1393
1394     $openclosed = qq|
1395                 <input type=hidden name="open" value=1>
1396 |;
1397   }
1398
1399   # use JavaScript Calendar or not
1400   $form->{jsscript} = $jscalendar;
1401   $jsscript = "";
1402   if ($form->{jsscript}) {
1403
1404     # with JavaScript Calendar
1405     $button1 = qq|
1406        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
1407        <input type=button name=transdatefrom id="trigger3" value=|
1408       . $locale->text('button') . qq|></td>
1409       |;
1410     $button2 = qq|
1411        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
1412        <input type=button name=transdateto name=transdateto id="trigger4" value=|
1413       . $locale->text('button') . qq|></td>
1414      |;
1415
1416     #write Trigger
1417     $jsscript =
1418       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
1419                           "transdateto", "BL", "trigger4");
1420   } else {
1421
1422     # without JavaScript Calendar
1423     $button1 = qq|
1424                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
1425     $button2 = qq|
1426                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}"></td>|;
1427   }
1428
1429   $form->header;
1430
1431   print qq|
1432 <body>
1433
1434 <form method=post action=$form->{script}>
1435
1436 <table width=100%>
1437   <tr>
1438     <th class=listtop>$form->{title}</th>
1439   </tr>
1440   <tr height="5"></tr>
1441   <tr>
1442     <td>
1443       <table>
1444         <tr>
1445           <th align=right>$vclabel</th>
1446           <td colspan=3>$vc</td>
1447         </tr>
1448         $warehouse
1449         $department
1450         <tr>
1451           <th align=right>$ordlabel</th>
1452           <td colspan=3><input name="$ordnumber" size=20></td>
1453         </tr>
1454         <tr>
1455           <th align=right>| . $locale->text('From') . qq|</th>
1456           $button1
1457           <th align=right>| . $locale->text('Bis') . qq|</th>
1458           $button2
1459         </tr>
1460         <input type=hidden name=sort value=transdate>
1461         <tr>
1462           <th align=right>| . $locale->text('Include in Report') . qq|</th>
1463           <td colspan=5>
1464             <table>
1465               $openclosed
1466               <tr>
1467                 <td><input name="l_id" class=checkbox type=checkbox value=Y>
1468                 | . $locale->text('ID') . qq|</td>
1469                 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
1470                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> |
1471     . $locale->text('Date') . qq|</td>
1472                 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> |
1473     . $locale->text('Required by') . qq|</td>
1474               </tr>
1475               <tr>
1476                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vclabel</td>
1477                 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
1478                 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> |
1479     . $locale->text('Ship via') . qq|</td>
1480               </tr>
1481               <tr>
1482                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> |
1483     . $locale->text('Amount') . qq|</td>
1484                 <td><input name="l_tax" class=checkbox type=checkbox value=Y> |
1485     . $locale->text('Tax') . qq|</td>
1486                 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> |
1487     . $locale->text('Total') . qq|</td>
1488               </tr>
1489               <tr>
1490                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> |
1491     . $locale->text('Subtotal') . qq|</td>
1492               </tr>
1493             </table>
1494           </td>
1495         </tr>
1496       </table>
1497     </td>
1498   </tr>
1499   <tr><td colspan=4><hr size=3 noshade></td></tr>
1500 </table>
1501
1502 $jsscript
1503
1504 <br>
1505 <input type=hidden name=nextsub value=orders>
1506 <input type=hidden name=path value=$form->{path}>
1507 <input type=hidden name=login value=$form->{login}>
1508 <input type=hidden name=password value=$form->{password}>
1509 <input type=hidden name=vc value=$form->{vc}>
1510 <input type=hidden name=type value=$form->{type}>
1511
1512 <input class=submit type=submit name=action value="|
1513     . $locale->text('Continue') . qq|">
1514 </form>
1515
1516 </body>
1517 </html>
1518 |;
1519
1520   $lxdebug->leave_sub();
1521 }
1522
1523 sub orders {
1524   $lxdebug->enter_sub();
1525
1526   # split vendor / customer
1527   ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) =
1528     split(/--/, $form->{ $form->{vc} });
1529
1530   OE->transactions(\%myconfig, \%$form);
1531
1532   $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
1533
1534   $number     = $form->escape($form->{$ordnumber});
1535   $name       = $form->escape($form->{ $form->{vc} });
1536   $department = $form->escape($form->{department});
1537   $warehouse  = $form->escape($form->{warehouse});
1538
1539   # construct href
1540   $href =
1541     "$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";
1542
1543   # construct callback
1544   $number     = $form->escape($form->{$ordnumber},    1);
1545   $name       = $form->escape($form->{ $form->{vc} }, 1);
1546   $department = $form->escape($form->{department},    1);
1547   $warehouse  = $form->escape($form->{warehouse},     1);
1548
1549   $callback =
1550     "$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";
1551
1552   @columns =
1553     $form->sort_columns("transdate", "reqdate",   "id",      "$ordnumber",
1554                         "name",      "netamount", "tax",     "amount",
1555                         "curr",      "employee",  "shipvia", "open",
1556                         "closed");
1557
1558   $form->{l_open} = $form->{l_closed} = "Y"
1559     if ($form->{open} && $form->{closed});
1560
1561   foreach $item (@columns) {
1562     if ($form->{"l_$item"} eq "Y") {
1563       push @column_index, $item;
1564
1565       # add column to href and callback
1566       $callback .= "&l_$item=Y";
1567       $href     .= "&l_$item=Y";
1568     }
1569   }
1570
1571   # only show checkboxes if gotten here via sales_order form.
1572   if ($form->{type} =~ /sales_order/) {
1573     unshift @column_index, "ids";
1574   }
1575
1576   if ($form->{l_subtotal} eq 'Y') {
1577     $callback .= "&l_subtotal=Y";
1578     $href     .= "&l_subtotal=Y";
1579   }
1580
1581   if ($form->{vc} eq 'vendor') {
1582     if ($form->{type} eq 'receive_order') {
1583       $form->{title} = $locale->text('Receive Merchandise');
1584     } elsif ($form->{type} eq 'purchase_order') {
1585       $form->{title} = $locale->text('Purchase Orders');
1586     } else {
1587       $form->{title} = $locale->text('Request for Quotations');
1588     }
1589     $name     = $locale->text('Vendor');
1590     $employee = $locale->text('Employee');
1591   }
1592   if ($form->{vc} eq 'customer') {
1593     if ($form->{type} eq 'sales_order') {
1594       $form->{title} = $locale->text('Sales Orders');
1595       $employee = $locale->text('Salesperson');
1596     } elsif ($form->{type} eq 'ship_order') {
1597       $form->{title} = $locale->text('Ship Merchandise');
1598       $employee = $locale->text('Salesperson');
1599     } else {
1600       $form->{title} = $locale->text('Quotations');
1601       $employee = $locale->text('Employee');
1602     }
1603     $name = $locale->text('Customer');
1604   }
1605
1606   $column_header{id} =
1607       qq|<th><a class=listheading href=$href&sort=id>|
1608     . $locale->text('ID')
1609     . qq|</a></th>|;
1610   $column_header{transdate} =
1611       qq|<th><a class=listheading href=$href&sort=transdate>|
1612     . $locale->text('Date')
1613     . qq|</a></th>|;
1614   $column_header{reqdate} =
1615       qq|<th><a class=listheading href=$href&sort=reqdate>|
1616     . $locale->text('Required by')
1617     . qq|</a></th>|;
1618   $column_header{ordnumber} =
1619       qq|<th><a class=listheading href=$href&sort=ordnumber>|
1620     . $locale->text('Order')
1621     . qq|</a></th>|;
1622   $column_header{quonumber} =
1623       qq|<th><a class=listheading href=$href&sort=quonumber>|
1624     . $locale->text('Quotation')
1625     . qq|</a></th>|;
1626   $column_header{name} =
1627     qq|<th><a class=listheading href=$href&sort=name>$name</a></th>|;
1628   $column_header{netamount} =
1629     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1630   $column_header{tax} =
1631     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1632   $column_header{amount} =
1633     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1634   $column_header{curr} =
1635     qq|<th class=listheading>| . $locale->text('Curr') . qq|</th>|;
1636   $column_header{shipvia} =
1637       qq|<th><a class=listheading href=$href&sort=shipvia>|
1638     . $locale->text('Ship via')
1639     . qq|</a></th>|;
1640   $column_header{open} =
1641     qq|<th class=listheading>| . $locale->text('O') . qq|</th>|;
1642   $column_header{closed} =
1643     qq|<th class=listheading>| . $locale->text('C') . qq|</th>|;
1644
1645   $column_header{employee} =
1646     qq|<th><a class=listheading href=$href&sort=employee>$employee</a></th>|;
1647
1648   $column_header{ids} = qq|<th></th>|;
1649
1650   if ($form->{ $form->{vc} }) {
1651     $option = $locale->text(ucfirst $form->{vc});
1652     $option .= " : $form->{$form->{vc}}";
1653   }
1654   if ($form->{warehouse}) {
1655     ($warehouse) = split /--/, $form->{warehouse};
1656     $option .= "\n<br>" if ($option);
1657     $option .= $locale->text('Warehouse');
1658     $option .= " : $warehouse";
1659   }
1660   if ($form->{department}) {
1661     $option .= "\n<br>" if ($option);
1662     ($department) = split /--/, $form->{department};
1663     $option .= $locale->text('Department') . " : $department";
1664   }
1665   if ($form->{transdatefrom}) {
1666     $option .= "\n<br>"
1667       . $locale->text('From') . " "
1668       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1669   }
1670   if ($form->{transdateto}) {
1671     $option .= "\n<br>"
1672       . $locale->text('Bis') . " "
1673       . $locale->date(\%myconfig, $form->{transdateto}, 1);
1674   }
1675   if ($form->{open}) {
1676     $option .= "\n<br>" if ($option);
1677     $option .= $locale->text('Open');
1678   }
1679   if ($form->{closed}) {
1680     $option .= "\n<br>" if ($option);
1681     $option .= $locale->text('Closed');
1682   }
1683
1684   $form->header;
1685
1686   print qq|
1687 <body>
1688
1689 <form method="post" action="oe.pl">
1690 <table width=100%>
1691   <tr>
1692     <th class=listtop>$form->{title}</th>
1693   </tr>
1694   <tr height="5"></tr>
1695   <tr>
1696     <td>$option</td>
1697   </tr>
1698   <tr>
1699     <td>
1700       <table width=100%>
1701         <tr class=listheading>|;
1702
1703   map { print "\n$column_header{$_}" } @column_index;
1704
1705   print qq|
1706         </tr>
1707 |;
1708
1709   # add sort and escape callback
1710   $callback_escaped = $form->escape($callback . "&sort=$form->{sort}");
1711
1712   if (@{ $form->{OE} }) {
1713     $sameitem = $form->{OE}->[0]->{ $form->{sort} };
1714   }
1715
1716   $action = "edit";
1717   $action = "ship_receive" if ($form->{type} =~ /(ship|receive)_order/);
1718
1719   $warehouse = $form->escape($form->{warehouse});
1720
1721   foreach $oe (@{ $form->{OE} }) {
1722     $form->{rowcount} = ++$j;
1723
1724     if ($form->{l_subtotal} eq 'Y') {
1725       if ($sameitem ne $oe->{ $form->{sort} }) {
1726         &subtotal;
1727         $sameitem = $oe->{ $form->{sort} };
1728       }
1729     }
1730
1731     map { $oe->{$_} *= $oe->{exchangerate} } (qw(netamount amount));
1732
1733     $column_data{netamount} =
1734         "<td align=right>"
1735       . $form->format_amount(\%myconfig, $oe->{netamount}, 2, "&nbsp;")
1736       . "</td>";
1737     $column_data{tax} = "<td align=right>"
1738       . $form->format_amount(\%myconfig, $oe->{amount} - $oe->{netamount},
1739                              2, "&nbsp;")
1740       . "</td>";
1741     $column_data{amount} =
1742       "<td align=right>"
1743       . $form->format_amount(\%myconfig, $oe->{amount}, 2, "&nbsp;") . "</td>";
1744
1745     $totalnetamount += $oe->{netamount};
1746     $totalamount    += $oe->{amount};
1747
1748     $subtotalnetamount += $oe->{netamount};
1749     $subtotalamount    += $oe->{amount};
1750
1751     $column_data{ids} =
1752       qq|<td><input name="id_$j" class=checkbox type=checkbox><input type="hidden" name="trans_id_$j" value="$oe->{id}"></td>|;
1753     $column_data{id}        = "<td>$oe->{id}</td>";
1754     $column_data{transdate} = "<td>$oe->{transdate}&nbsp;</td>";
1755     $column_data{reqdate}   = "<td>$oe->{reqdate}&nbsp;</td>";
1756
1757     $column_data{$ordnumber} =
1758       "<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>";
1759     $column_data{name} = "<td>$oe->{name}</td>";
1760
1761     $column_data{employee} = "<td>$oe->{employee}&nbsp;</td>";
1762     $column_data{shipvia}  = "<td>$oe->{shipvia}&nbsp;</td>";
1763
1764     if ($oe->{closed}) {
1765       $column_data{closed} = "<td align=center>X</td>";
1766       $column_data{open}   = "<td>&nbsp;</td>";
1767     } else {
1768       $column_data{closed} = "<td>&nbsp;</td>";
1769       $column_data{open}   = "<td align=center>X</td>";
1770     }
1771
1772     $i++;
1773     $i %= 2;
1774     print "
1775         <tr class=listrow$i>";
1776
1777     map { print "\n$column_data{$_}" } @column_index;
1778
1779     print qq|
1780         </tr>
1781 |;
1782
1783   }
1784
1785   if ($form->{l_subtotal} eq 'Y') {
1786     &subtotal;
1787   }
1788
1789   # print totals
1790   print qq|
1791         <tr class=listtotal>|;
1792
1793   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1794
1795   $column_data{netamount} =
1796     "<th class=listtotal align=right>"
1797     . $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;") . "</th>";
1798   $column_data{tax} = "<th class=listtotal align=right>"
1799     . $form->format_amount(\%myconfig, $totalamount - $totalnetamount,
1800                            2, "&nbsp;")
1801     . "</th>";
1802   $column_data{amount} =
1803     "<th class=listtotal align=right>"
1804     . $form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;") . "</th>";
1805
1806   map { print "\n$column_data{$_}" } @column_index;
1807
1808   print qq|
1809         </tr>
1810       </td>
1811     </table>
1812   </tr>
1813   <tr>
1814     <td><hr size=3 noshade></td>
1815   </tr>
1816 </table>|;
1817
1818   # multiple invoice edit button only if gotten there via sales_order form.
1819
1820   if ($form->{type} =~ /sales_order/) {
1821     print qq|
1822   <input type="hidden" name="path" value="$form->{path}">
1823   <input class"submit" type="submit" name="action" value="|
1824       . $locale->text('Continue') . qq|">
1825   <input type="hidden" name="nextsub" value="edit">
1826   <input type="hidden" name="type" value="$form->{type}">
1827   <input type="hidden" name="warehouse" value="$warehouse">
1828   <input type="hidden" name="vc" value="$form->{vc}">
1829   <input type="hidden" name="login" value="$form->{login}">
1830   <input type="hidden" name="password" value="$form->{password}">
1831   <input type="hidden" name="callback" value="$callback">
1832   <input type="hidden" name="rowcount" value="$form->{rowcount}">|;
1833   }
1834
1835   print qq|
1836 </form>
1837
1838 <br>
1839 <form method=post action=$form->{script}>
1840
1841 <input name=callback type=hidden value="$form->{callback}">
1842
1843 <input type=hidden name=type value=$form->{type}>
1844 <input type=hidden name=vc value=$form->{vc}>
1845
1846 <input type=hidden name=path value=$form->{path}>
1847 <input type=hidden name=login value=$form->{login}>
1848 <input type=hidden name=password value=$form->{password}>
1849 |;
1850
1851   if ($form->{type} !~ /(ship|receive)_order/) {
1852     print qq|
1853 <input class=submit type=submit name=action value="|
1854       . $locale->text('Add') . qq|">|;
1855   }
1856
1857   if ($form->{menubar}) {
1858     require "$form->{path}/menu.pl";
1859     &menubar;
1860   }
1861
1862   print qq|
1863 </form>
1864
1865 </body>
1866 </html>
1867 |;
1868
1869   $lxdebug->leave_sub();
1870 }
1871
1872 sub subtotal {
1873   $lxdebug->enter_sub();
1874
1875   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1876
1877   $column_data{netamount} =
1878       "<th class=listsubtotal align=right>"
1879     . $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")
1880     . "</th>";
1881   $column_data{tax} = "<td class=listsubtotal align=right>"
1882     . $form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount,
1883                            2, "&nbsp;")
1884     . "</th>";
1885   $column_data{amount} =
1886     "<th class=listsubtotal align=right>"
1887     . $form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;") . "</th>";
1888
1889   $subtotalnetamount = 0;
1890   $subtotalamount    = 0;
1891
1892   print "
1893         <tr class=listsubtotal>
1894 ";
1895
1896   map { print "\n$column_data{$_}" } @column_index;
1897
1898   print qq|
1899         </tr>
1900 |;
1901
1902   $lxdebug->leave_sub();
1903 }
1904
1905 sub save_and_close {
1906   $lxdebug->enter_sub();
1907
1908   if ($form->{type} =~ /_order$/) {
1909     $form->isblank("transdate", $locale->text('Order Date missing!'));
1910   } else {
1911     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1912   }
1913
1914   $msg = ucfirst $form->{vc};
1915   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1916
1917   # $locale->text('Customer missing!');
1918   # $locale->text('Vendor missing!');
1919
1920   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1921     if ($form->{currency} ne $form->{defaultcurrency});
1922
1923   &validate_items;
1924
1925   # if the name changed get new values
1926   if (&check_name($form->{vc})) {
1927     &update;
1928     exit;
1929   }
1930
1931   $form->{id} = 0 if $form->{saveasnew};
1932
1933   # this is for the internal notes section for the [email] Subject
1934   if ($form->{type} =~ /_order$/) {
1935     if ($form->{type} eq 'sales_order') {
1936       $form->{label} = $locale->text('Sales Order');
1937
1938       $numberfld = "sonumber";
1939       $ordnumber = "ordnumber";
1940     } else {
1941       $form->{label} = $locale->text('Purchase Order');
1942
1943       $numberfld = "ponumber";
1944       $ordnumber = "ordnumber";
1945     }
1946
1947     $err = $locale->text('Cannot save order!');
1948
1949   } else {
1950     if ($form->{type} eq 'sales_quotation') {
1951       $form->{label} = $locale->text('Quotation');
1952
1953       $numberfld = "sqnumber";
1954       $ordnumber = "quonumber";
1955     } else {
1956       $form->{label} = $locale->text('Request for Quotation');
1957
1958       $numberfld = "rfqnumber";
1959       $ordnumber = "quonumber";
1960     }
1961
1962     $err = $locale->text('Cannot save quotation!');
1963
1964   }
1965
1966   # get new number in sequence if no number is given or if saveasnew was requested
1967   if (!$form->{$ordnumber} || $form->{saveasnew}) {
1968     $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1969   }
1970
1971   relink_accounts();
1972
1973   $form->redirect(
1974             $form->{label} . " $form->{$ordnumber} " . $locale->text('saved!'))
1975     if (OE->save(\%myconfig, \%$form));
1976   $form->error($err);
1977
1978   $lxdebug->leave_sub();
1979 }
1980
1981 sub save {
1982   $lxdebug->enter_sub();
1983
1984   if ($form->{type} =~ /_order$/) {
1985     $form->isblank("transdate", $locale->text('Order Date missing!'));
1986   } else {
1987     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1988   }
1989
1990   $msg = ucfirst $form->{vc};
1991   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1992
1993   # $locale->text('Customer missing!');
1994   # $locale->text('Vendor missing!');
1995
1996   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1997     if ($form->{currency} ne $form->{defaultcurrency});
1998
1999   &validate_items;
2000
2001   # if the name changed get new values
2002   if (&check_name($form->{vc})) {
2003     &update;
2004     exit;
2005   }
2006
2007   $form->{id} = 0 if $form->{saveasnew};
2008
2009   # this is for the internal notes section for the [email] Subject
2010   if ($form->{type} =~ /_order$/) {
2011     if ($form->{type} eq 'sales_order') {
2012       $form->{label} = $locale->text('Sales Order');
2013
2014       $numberfld = "sonumber";
2015       $ordnumber = "ordnumber";
2016     } else {
2017       $form->{label} = $locale->text('Purchase Order');
2018
2019       $numberfld = "ponumber";
2020       $ordnumber = "ordnumber";
2021     }
2022
2023     $err = $locale->text('Cannot save order!');
2024
2025   } else {
2026     if ($form->{type} eq 'sales_quotation') {
2027       $form->{label} = $locale->text('Quotation');
2028
2029       $numberfld = "sqnumber";
2030       $ordnumber = "quonumber";
2031     } else {
2032       $form->{label} = $locale->text('Request for Quotation');
2033
2034       $numberfld = "rfqnumber";
2035       $ordnumber = "quonumber";
2036     }
2037
2038     $err = $locale->text('Cannot save quotation!');
2039
2040   }
2041
2042   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
2043     unless $form->{$ordnumber};
2044
2045   relink_accounts();
2046
2047   OE->save(\%myconfig, \%$form);
2048   $form->{simple_save} = 1;
2049   if(!$form->{print_and_save}) {
2050     set_headings("edit");
2051     &update;
2052     exit;
2053   }
2054   $lxdebug->leave_sub();
2055 }
2056
2057 sub delete {
2058   $lxdebug->enter_sub();
2059
2060   $form->header;
2061
2062   if ($form->{type} =~ /_order$/) {
2063     $msg       = $locale->text('Are you sure you want to delete Order Number');
2064     $ordnumber = 'ordnumber';
2065   } else {
2066     $msg = $locale->text('Are you sure you want to delete Quotation Number');
2067     $ordnumber = 'quonumber';
2068   }
2069
2070   print qq|
2071 <body>
2072
2073 <form method=post action=$form->{script}>
2074 |;
2075
2076   # delete action variable
2077   map { delete $form->{$_} } qw(action header);
2078
2079   foreach $key (keys %$form) {
2080     $form->{$key} =~ s/\"/&quot;/g;
2081     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2082   }
2083
2084   print qq|
2085 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
2086
2087 <h4>$msg $form->{$ordnumber}</h4>
2088 <p>
2089 <input name=action class=submit type=submit value="|
2090     . $locale->text('Yes') . qq|">
2091 </form>
2092
2093 </body>
2094 </html>
2095 |;
2096
2097   $lxdebug->leave_sub();
2098 }
2099
2100 sub yes {
2101   $lxdebug->enter_sub();
2102
2103   if ($form->{type} =~ /_order$/) {
2104     $msg = $locale->text('Order deleted!');
2105     $err = $locale->text('Cannot delete order!');
2106   } else {
2107     $msg = $locale->text('Quotation deleted!');
2108     $err = $locale->text('Cannot delete quotation!');
2109   }
2110
2111   $form->redirect($msg) if (OE->delete(\%myconfig, \%$form, $spool));
2112   $form->error($err);
2113
2114   $lxdebug->leave_sub();
2115 }
2116
2117 sub invoice {
2118   $lxdebug->enter_sub();
2119
2120   if ($form->{type} =~ /_order$/) {
2121
2122     # these checks only apply if the items don't bring their own ordnumbers/transdates.
2123     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
2124     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
2125       if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
2126           ->{''});
2127     $form->isblank("transdate", $locale->text('Order Date missing!'))
2128       if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
2129           ->{''});
2130
2131     # also copy deliverydate from the order
2132     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
2133   } else {
2134     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
2135     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
2136     $form->{ordnumber} = "";
2137   }
2138
2139   # if the name changed get new values
2140   if (&check_name($form->{vc})) {
2141     &update;
2142     exit;
2143   }
2144
2145   $form->{cp_id} *= 1;
2146
2147   if (   $form->{type} =~ /_order/
2148       && $form->{currency} ne $form->{defaultcurrency}) {
2149
2150     # check if we need a new exchangerate
2151     $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
2152
2153     $orddate      = $form->current_date(\%myconfig);
2154     $exchangerate =
2155       $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate,
2156                                 $buysell);
2157
2158     if (!$exchangerate) {
2159       &backorder_exchangerate($orddate, $buysell);
2160       exit;
2161     }
2162   }
2163
2164   # close orders/quotations
2165   $form->{closed} = 1;
2166
2167   # save order if one ordnumber has been given
2168   # if not it's most likely a collective order, which can't be saved back
2169   # so they just have to be closed
2170   if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
2171     relink_accounts();
2172     OE->save(\%myconfig, \%$form);
2173   } else {
2174     OE->close_orders(\%myconfig, \%$form);
2175   }
2176
2177   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
2178   $form->{duedate} =
2179     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
2180
2181   $form->{id}     = '';
2182   $form->{closed} = 0;
2183   $form->{rowcount}--;
2184   $form->{shipto} = 1;
2185
2186   if ($form->{type} =~ /_order$/) {
2187     $form->{exchangerate} = $exchangerate;
2188     &create_backorder;
2189   }
2190
2191   if (   $form->{type} eq 'purchase_order'
2192       || $form->{type} eq 'request_quotation') {
2193     $form->{title}  = $locale->text('Add Vendor Invoice');
2194     $form->{script} = 'ir.pl';
2195     $script         = "ir";
2196     $buysell        = 'sell';
2197   }
2198   if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
2199     $form->{title}  = $locale->text('Add Sales Invoice');
2200     $form->{script} = 'is.pl';
2201     $script         = "is";
2202     $buysell        = 'buy';
2203   }
2204
2205   # bo creates the id, reset it
2206   map { delete $form->{$_} }
2207     qw(id subject message cc bcc printed emailed queued);
2208   $form->{ $form->{vc} } =~ s/--.*//g;
2209   $form->{type} = "invoice";
2210
2211   # locale messages
2212   $locale = new Locale "$myconfig{countrycode}", "$script";
2213
2214   require "$form->{path}/$form->{script}";
2215
2216   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
2217
2218   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
2219     qw(creditlimit creditremaining);
2220
2221   $currency = $form->{currency};
2222   &invoice_links;
2223
2224   $form->{currency}     = $currency;
2225   $form->{exchangerate} = "";
2226   $form->{forex}        = "";
2227   $form->{exchangerate} = $exchangerate
2228     if (
2229         $form->{forex} = (
2230                     $exchangerate =
2231                       $form->check_exchangerate(
2232                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
2233                       )));
2234
2235   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
2236
2237   &prepare_invoice;
2238
2239   # format amounts
2240   for $i (1 .. $form->{rowcount}) {
2241     $form->{"discount_$i"} =
2242       $form->format_amount(\%myconfig, $form->{"discount_$i"});
2243
2244     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
2245     $dec           = length $dec;
2246     $decimalplaces = ($dec > 2) ? $dec : 2;
2247
2248     # copy delivery date from reqdate for order -> invoice conversion
2249     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
2250       unless $form->{"deliverydate_$i"};
2251
2252     $form->{"sellprice_$i"} =
2253       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
2254                            $decimalplaces);
2255
2256     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
2257     $dec_qty = length $dec_qty;
2258     $form->{"qty_$i"} =
2259       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
2260
2261     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2262       qw(partnumber description unit);
2263
2264   }
2265
2266   &display_form;
2267
2268   $lxdebug->leave_sub();
2269 }
2270
2271 sub backorder_exchangerate {
2272   $lxdebug->enter_sub();
2273   my ($orddate, $buysell) = @_;
2274
2275   $form->header;
2276
2277   print qq|
2278 <body>
2279
2280 <form method=post action=$form->{script}>
2281 |;
2282
2283   # delete action variable
2284   map { delete $form->{$_} } qw(action header exchangerate);
2285
2286   foreach $key (keys %$form) {
2287     $form->{$key} =~ s/\"/&quot;/g;
2288     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2289   }
2290
2291   $form->{title} = $locale->text('Add Exchangerate');
2292
2293   print qq|
2294
2295 <input type=hidden name=path value=$form->{path}>
2296 <input type=hidden name=login value=$form->{login}>
2297 <input type=hidden name=password value=$form->{password}>
2298
2299 <input type=hidden name=exchangeratedate value=$orddate>
2300 <input type=hidden name=buysell value=$buysell>
2301
2302 <table width=100%>
2303   <tr><th class=listtop>$form->{title}</th></tr>
2304   <tr height="5"></tr>
2305   <tr>
2306     <td>
2307       <table>
2308         <tr>
2309           <th align=right>| . $locale->text('Currency') . qq|</th>
2310           <td>$form->{currency}</td>
2311         </tr>
2312         <tr>
2313           <th align=right>| . $locale->text('Date') . qq|</th>
2314           <td>$orddate</td>
2315         </tr>
2316         <tr>
2317           <th align=right>| . $locale->text('Exchangerate') . qq|</th>
2318           <td><input name=exchangerate size=11></td>
2319         </tr>
2320       </table>
2321     </td>
2322   </tr>
2323 </table>
2324
2325 <hr size=3 noshade>
2326
2327 <br>
2328 <input type=hidden name=nextsub value=save_exchangerate>
2329
2330 <input name=action class=submit type=submit value="|
2331     . $locale->text('Continue') . qq|">
2332
2333 </form>
2334
2335 </body>
2336 </html>
2337 |;
2338
2339   $lxdebug->leave_sub();
2340 }
2341
2342 sub save_exchangerate {
2343   $lxdebug->enter_sub();
2344
2345   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
2346   $form->{exchangerate} =
2347     $form->parse_amount(\%myconfig, $form->{exchangerate});
2348   $form->save_exchangerate(\%myconfig, $form->{currency},
2349                            $form->{exchangeratedate},
2350                            $form->{exchangerate}, $form->{buysell});
2351
2352   &invoice;
2353
2354   $lxdebug->leave_sub();
2355 }
2356
2357 sub create_backorder {
2358   $lxdebug->enter_sub();
2359
2360   $form->{shipped} = 1;
2361
2362   # figure out if we need to create a backorder
2363   # items aren't saved if qty != 0
2364
2365   for $i (1 .. $form->{rowcount}) {
2366     $totalqty  += $qty  = $form->{"qty_$i"};
2367     $totalship += $ship = $form->{"ship_$i"};
2368
2369     $form->{"qty_$i"} = $qty - $ship;
2370   }
2371
2372   if ($totalship == 0) {
2373     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
2374     $form->{ordtotal} = 0;
2375     $form->{shipped}  = 0;
2376     return;
2377   }
2378
2379   if ($totalqty == $totalship) {
2380     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
2381     $form->{ordtotal} = 0;
2382     return;
2383   }
2384
2385   @flds = (
2386     qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
2387   );
2388
2389   for $i (1 .. $form->{rowcount}) {
2390     map {
2391       $form->{"${_}_$i"} =
2392         $form->format_amount(\%myconfig, $form->{"${_}_$i"})
2393     } qw(sellprice discount);
2394   }
2395
2396   relink_accounts();
2397
2398   OE->save(\%myconfig, \%$form);
2399
2400   # rebuild rows for invoice
2401   @a     = ();
2402   $count = 0;
2403
2404   for $i (1 .. $form->{rowcount}) {
2405     $form->{"qty_$i"} = $form->{"ship_$i"};
2406
2407     if ($form->{"qty_$i"}) {
2408       push @a, {};
2409       $j = $#a;
2410       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2411       $count++;
2412     }
2413   }
2414
2415   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2416   $form->{rowcount} = $count;
2417
2418   $lxdebug->leave_sub();
2419 }
2420
2421 sub save_as_new {
2422   $lxdebug->enter_sub();
2423
2424   $form->{saveasnew} = 1;
2425   $form->{closed}    = 0;
2426   map { delete $form->{$_} } qw(printed emailed queued ordnumber quonumber);
2427
2428   &save;
2429
2430   $lxdebug->leave_sub();
2431 }
2432
2433 sub purchase_order {
2434   $lxdebug->enter_sub();
2435
2436   if (   $form->{type} eq 'sales_quotation'
2437       || $form->{type} eq 'request_quotation') {
2438     $form->{closed} = 1;
2439     relink_accounts();
2440     OE->save(\%myconfig, \%$form);
2441   }
2442
2443   $form->{cp_id} *= 1;
2444
2445   $form->{title} = $locale->text('Add Purchase Order');
2446   $form->{vc}    = "vendor";
2447   $form->{type}  = "purchase_order";
2448
2449   &poso;
2450
2451   $lxdebug->leave_sub();
2452 }
2453
2454 sub sales_order {
2455   $lxdebug->enter_sub();
2456
2457   if (   $form->{type} eq 'sales_quotation'
2458       || $form->{type} eq 'request_quotation') {
2459     $form->{closed} = 1;
2460     relink_accounts();
2461     OE->save(\%myconfig, \%$form);
2462   }
2463
2464   $form->{cp_id} *= 1;
2465
2466   $form->{title} = $locale->text('Add Sales Order');
2467   $form->{vc}    = "customer";
2468   $form->{type}  = "sales_order";
2469
2470   &poso;
2471
2472   $lxdebug->leave_sub();
2473 }
2474
2475 sub poso {
2476   $lxdebug->enter_sub();
2477
2478   $form->{transdate} = $form->current_date(\%myconfig);
2479   delete $form->{duedate};
2480
2481   $form->{closed} = 0;
2482
2483   # reset
2484   map { delete $form->{$_} }
2485     qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
2486
2487   &order_links;
2488
2489   &prepare_order;
2490
2491   # format amounts
2492   for $i (1 .. $form->{rowcount} - 1) {
2493     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2494       qw(partnumber description unit);
2495   }
2496
2497   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 0, "0") }
2498     qw(creditlimit creditremaining);
2499
2500   &update;
2501
2502   $lxdebug->leave_sub();
2503 }
2504
2505 sub ship_receive {
2506   $lxdebug->enter_sub();
2507
2508   &order_links;
2509
2510   &prepare_order;
2511
2512   OE->get_warehouses(\%myconfig, \%$form);
2513
2514   # warehouse
2515   if (@{ $form->{all_warehouses} }) {
2516     $form->{selectwarehouse} = "<option>\n";
2517
2518     map { $form->{selectwarehouse} .= "<option>$_->{description}--$_->{id}\n" }
2519       (@{ $form->{all_warehouses} });
2520
2521     if ($form->{warehouse}) {
2522       $form->{selectwarehouse} = "<option>$form->{warehouse}";
2523     }
2524   }
2525
2526   $form->{shippingdate} = $form->current_date(\%myconfig);
2527   $form->{"$form->{vc}"} =~ s/--.*//;
2528
2529   @flds  = ();
2530   @a     = ();
2531   $count = 0;
2532   foreach $key (keys %$form) {
2533     if ($key =~ /_1$/) {
2534       $key =~ s/_1//;
2535       push @flds, $key;
2536     }
2537   }
2538
2539   for $i (1 .. $form->{rowcount}) {
2540
2541     # undo formatting from prepare_order
2542     map {
2543       $form->{"${_}_$i"} =
2544         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
2545     } qw(qty ship);
2546     $n = ($form->{"qty_$i"} -= $form->{"ship_$i"});
2547     if (abs($n) > 0
2548         && ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"})) {
2549       $form->{"ship_$i"}         = "";
2550       $form->{"serialnumber_$i"} = "";
2551
2552       push @a, {};
2553       $j = $#a;
2554
2555       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2556       $count++;
2557     }
2558   }
2559
2560   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2561   $form->{rowcount} = $count;
2562
2563   &display_ship_receive;
2564
2565   $lxdebug->leave_sub();
2566 }
2567
2568 sub display_ship_receive {
2569   $lxdebug->enter_sub();
2570
2571   $vclabel = ucfirst $form->{vc};
2572   $vclabel = $locale->text($vclabel);
2573
2574   $form->{rowcount}++;
2575
2576   if ($form->{vc} eq 'customer') {
2577     $form->{title} = $locale->text('Ship Merchandise');
2578     $shipped = $locale->text('Shipping Date');
2579   } else {
2580     $form->{title} = $locale->text('Receive Merchandise');
2581     $shipped = $locale->text('Date Received');
2582   }
2583
2584   # set option selected
2585   foreach $item (warehouse, employee) {
2586     $form->{"select$item"} =~ s/ selected//;
2587     $form->{"select$item"} =~
2588       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
2589   }
2590
2591   $warehouse = qq|
2592               <tr>
2593                 <th align=right>| . $locale->text('Warehouse') . qq|</th>
2594                 <td><select name=warehouse>$form->{selectwarehouse}</select></td>
2595                 <input type=hidden name=selectwarehouse value="$form->{selectwarehouse}">
2596               </tr>
2597 | if $form->{selectwarehouse};
2598
2599   $employee = qq|
2600               <tr>
2601                 <th align=right nowrap>| . $locale->text('Contact') . qq|</th>
2602                 <td><select name=employee>$form->{selectemployee}</select></td>
2603                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
2604               </tr>
2605 |;
2606
2607   $form->header;
2608
2609   print qq|
2610 <body>
2611
2612 <form method=post action=$form->{script}>
2613
2614 <input type=hidden name=id value=$form->{id}>
2615
2616 <input type=hidden name=display_form value=display_ship_receive>
2617
2618 <input type=hidden name=type value=$form->{type}>
2619 <input type=hidden name=media value=$form->{media}>
2620 <input type=hidden name=format value=$form->{format}>
2621
2622 <input type=hidden name=queued value="$form->{queued}">
2623 <input type=hidden name=printed value="$form->{printed}">
2624 <input type=hidden name=emailed value="$form->{emailed}">
2625
2626 <input type=hidden name=vc value=$form->{vc}>
2627
2628 <table width=100%>
2629   <tr class=listtop>
2630     <th class=listtop>$form->{title}</th>
2631   </tr>
2632   <tr height="5"></tr>
2633   <tr>
2634     <td>
2635       <table width="100%">
2636         <tr valign=top>
2637           <td>
2638             <table width=100%>
2639               <tr>
2640                 <th align=right>$vclabel</th>
2641                 <td colspan=3>$form->{$form->{vc}}</td>
2642                 <input type=hidden name=$form->{vc} value="$form->{$form->{vc}}">
2643                 <input type=hidden name="$form->{vc}_id" value=$form->{"$form->{vc}_id"}>
2644               </tr>
2645               $department
2646               <tr>
2647                 <th align=right>| . $locale->text('Shipping Point') . qq|</th>
2648                 <td colspan=3>
2649                 <input name=shippingpoint size=35 value="$form->{shippingpoint}">
2650               </tr>
2651               <tr>
2652                 <th align=right>| . $locale->text('Ship via') . qq|</th>
2653                 <td colspan=3>
2654                 <input name=shipvia size=35 value="$form->{shipvia}">
2655               </tr>
2656               $warehouse
2657             </table>
2658           </td>
2659           <td align=right>
2660             <table>
2661               $employee
2662               <tr>
2663                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
2664                 <td>$form->{ordnumber}</td>
2665                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
2666               </tr>
2667               <tr>
2668                 <th align=right nowrap>| . $locale->text('Order Date') . qq|</th>
2669                 <td>$form->{transdate}</td>
2670                 <input type=hidden name=transdate value=$form->{transdate}>
2671               </tr>
2672               <tr>
2673                 <th align=right nowrap>$shipped</th>
2674                 <td><input name=shippingdate size=11 value=$form->{shippingdate}></td>
2675               </tr>
2676             </table>
2677           </td>
2678         </tr>
2679       </table>
2680     </td>
2681   </tr>
2682
2683 <!-- shipto are in hidden variables -->
2684
2685 <input type=hidden name=shiptoname value="$form->{shiptoname}">
2686 <input type=hidden name=shiptostreet value="$form->{shiptostreet}">
2687 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
2688 <input type=hidden name=shiptocity value="$form->{shiptocity}">
2689 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
2690 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
2691 <input type=hidden name=shiptophone value="$form->{shiptophone}">
2692 <input type=hidden name=shiptofax value="$form->{shiptofax}">
2693 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
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 }