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