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