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