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