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