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