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