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