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