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