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