import sql-ledger 2.4.4
[freeside.git] / sql-ledger / sql-ledger / bin / mozilla / io.pl
1 ######################################################################
2 # SQL-Ledger, Accounting
3 # Copyright (c) 2002
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 # common routines used in is, ir, oe
25 #
26 #######################################################################
27
28 # any custom scripts for this one
29 if (-f "$form->{path}/custom_io.pl") {
30   eval { require "$form->{path}/custom_io.pl"; };
31 }
32 if (-f "$form->{path}/$form->{login}_io.pl") {
33   eval { require "$form->{path}/$form->{login}_io.pl"; };
34 }
35
36
37 1;
38 # end of main
39
40
41 # this is for our long dates
42 # $locale->text('January')
43 # $locale->text('February')
44 # $locale->text('March')
45 # $locale->text('April')
46 # $locale->text('May ')
47 # $locale->text('June')
48 # $locale->text('July')
49 # $locale->text('August')
50 # $locale->text('September')
51 # $locale->text('October')
52 # $locale->text('November')
53 # $locale->text('December')
54
55 # this is for our short month
56 # $locale->text('Jan')
57 # $locale->text('Feb')
58 # $locale->text('Mar')
59 # $locale->text('Apr')
60 # $locale->text('May')
61 # $locale->text('Jun')
62 # $locale->text('Jul')
63 # $locale->text('Aug')
64 # $locale->text('Sep')
65 # $locale->text('Oct')
66 # $locale->text('Nov')
67 # $locale->text('Dec')
68
69
70 sub display_row {
71   my $numrows = shift;
72
73   @column_index = qw(runningnumber partnumber description qty);
74
75   if ($form->{type} eq "sales_order") {
76     push @column_index, "ship";
77     $column_data{ship} = qq|<th class=listheading align=center width="auto">|.$locale->text('Ship').qq|</th>|;
78   }
79   if ($form->{type} eq "purchase_order") {
80     push @column_index, "ship";
81     $column_data{ship} = qq|<th class=listheading align=center width="auto">|.$locale->text('Recd').qq|</th>|;
82   }
83
84   foreach $item (qw(projectnumber partsgroup)) {
85     $form->{"select$item"} = $form->unescape($form->{"select$item"}) if $form->{"select$item"};
86   }
87       
88   if ($form->{language_code} ne $form->{oldlanguage_code}) {
89     # rebuild partsgroup
90     $form->get_partsgroup(\%myconfig, { language_code => $form->{language_code} });
91     if (@ { $form->{all_partsgroup} }) {
92       $form->{selectpartsgroup} = "<option>\n";
93       foreach $ref (@ { $form->{all_partsgroup} }) {
94         if ($ref->{translation}) {
95           $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{translation}\n|;
96         } else {
97           $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{partsgroup}\n|;
98         }
99       }
100     }
101     $form->{oldlanguage_code} = $form->{language_code};
102   }
103       
104
105   push @column_index, qw(unit sellprice discount linetotal);
106
107   my $colspan = $#column_index + 1;
108
109   $form->{invsubtotal} = 0;
110   map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
111   
112   $column_data{runningnumber} = qq|<th class=listheading nowrap>|.$locale->text('No.').qq|</th>|;
113   $column_data{partnumber} = qq|<th class=listheading nowrap>|.$locale->text('Number').qq|</th>|;
114   $column_data{description} = qq|<th class=listheading nowrap>|.$locale->text('Description').qq|</th>|;
115   $column_data{qty} = qq|<th class=listheading nowrap>|.$locale->text('Qty').qq|</th>|;
116   $column_data{unit} = qq|<th class=listheading nowrap>|.$locale->text('Unit').qq|</th>|;
117   $column_data{sellprice} = qq|<th class=listheading nowrap>|.$locale->text('Price').qq|</th>|;
118   $column_data{discount} = qq|<th class=listheading>%</th>|;
119   $column_data{linetotal} = qq|<th class=listheading nowrap>|.$locale->text('Extended').qq|</th>|;
120   $column_data{bin} = qq|<th class=listheading nowrap>|.$locale->text('Bin').qq|</th>|;
121   
122   print qq|
123   <tr>
124     <td>
125       <table width=100%>
126         <tr class=listheading>|;
127
128   map { print "\n$column_data{$_}" } @column_index;
129
130   print qq|
131         </tr>
132 |;
133
134
135   $deliverydate = $locale->text('Delivery Date');
136   $serialnumber = $locale->text('Serial No.');
137   $projectnumber = $locale->text('Project');
138   $group = $locale->text('Group');
139   $sku = $locale->text('SKU');
140
141   $delvar = 'deliverydate';
142   
143   if ($form->{type} =~ /_(order|quotation)$/) {
144     $reqdate = $locale->text('Required by');
145     $delvar = 'reqdate';
146   }
147
148   $exchangerate = $form->parse_amount(\%myconfig, $form->{exchangerate});
149   $exchangerate = ($exchangerate) ? $exchangerate : 1;
150
151   for $i (1 .. $numrows) {
152     # undo formatting
153     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(qty ship discount sellprice);
154     
155     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
156     $dec = length $dec;
157     $decimalplaces = ($dec > 2) ? $dec : 2;
158
159     if (($form->{"qty_$i"} != $form->{"oldqty_$i"}) || ($form->{currency} ne $form->{oldcurrency})) {
160       # check for a pricematrix
161       @a = split / /, $form->{"pricematrix_$i"};
162       if ((scalar @a) > 2 || $form->{currency} ne $form->{oldcurrency}) {
163         foreach $item (@a) {
164           ($q, $p) = split /:/, $item;
165           if ($p != 0 && $form->{"qty_$i"} > $q) {
166             $form->{"sellprice_$i"} = $form->round_amount($p / $exchangerate, $decimalplaces);
167           }
168         }
169       }
170     }
171     
172     $discount = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"}/100, $decimalplaces);
173     $linetotal = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
174     $linetotal = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
175
176     map { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}) } qw(partnumber sku description unit);
177     
178     $skunumber = qq|
179                 <p><b>$sku</b> $form->{"sku_$i"}| if ($form->{vc} eq 'vendor' && $form->{"sku_$i"});
180
181     
182     if ($form->{selectpartsgroup}) {
183       if ($i < $numrows) {
184         $partsgroup = qq|
185               <p><b>$group</b>
186               <input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">|;
187         ($form->{"partsgroup_$i"}) = split /--/, $form->{"partsgroup_$i"};
188         $partsgroup .= $form->{"partsgroup_$i"};
189         $partsgroup = "" unless $form->{"partsgroup_$i"};
190       }
191     }
192     
193     $delivery = qq|
194           <b>${$delvar}</b>
195           <input name="${delvar}_$i" size=11 title="$myconfig{dateformat}" value="$form->{"${delvar}_$i"}">
196 |;
197
198     $column_data{runningnumber} = qq|<td><input name="runningnumber_$i" size=3 value=$i></td>|;
199     $column_data{partnumber} = qq|<td><input name="partnumber_$i" size=15 value="$form->{"partnumber_$i"}">$skunumber</td>|;
200
201     if (($rows = $form->numtextrows($form->{"description_$i"}, 25, 6)) > 1) {
202       $column_data{description} = qq|<td><textarea name="description_$i" rows=$rows cols=25 wrap=soft>$form->{"description_$i"}</textarea>$partsgroup</td>|;
203     } else {
204       $column_data{description} = qq|<td><input name="description_$i" size=30 value="$form->{"description_$i"}">$partsgroup</td>|;
205     }
206
207     $column_data{qty} = qq|<td align=right><input name="qty_$i" size=5 value=|.$form->format_amount(\%myconfig, $form->{"qty_$i"}).qq|></td>|;
208     $column_data{ship} = qq|<td align=right><input name="ship_$i" size=5 value=|.$form->format_amount(\%myconfig, $form->{"ship_$i"}).qq|></td>|;
209     $column_data{unit} = qq|<td><input name="unit_$i" size=5 value="$form->{"unit_$i"}"></td>|;
210     $column_data{sellprice} = qq|<td align=right><input name="sellprice_$i" size=9 value=|.$form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces).qq|></td>|;
211     $column_data{discount} = qq|<td align=right><input name="discount_$i" size=3 value=|.$form->format_amount(\%myconfig, $form->{"discount_$i"}).qq|></td>|;
212     $column_data{linetotal} = qq|<td align=right>|.$form->format_amount(\%myconfig, $linetotal, 2).qq|</td>|;
213     $column_data{bin} = qq|<td>$form->{"bin_$i"}</td>|;
214     
215     print qq|
216         <tr valign=top>|;
217
218     map { print "\n$column_data{$_}" } @column_index;
219   
220     print qq|
221         </tr>
222
223 <input type=hidden name="orderitems_id_$i" value=$form->{"orderitems_id_$i"}>
224
225 <input type=hidden name="id_$i" value=$form->{"id_$i"}>
226 <input type=hidden name="inventory_accno_$i" value=$form->{"inventory_accno_$i"}>
227 <input type=hidden name="bin_$i" value="$form->{"bin_$i"}">
228 <input type=hidden name="weight_$i" value="$form->{"weight_$i"}">
229 <input type=hidden name="income_accno_$i" value=$form->{"income_accno_$i"}>
230 <input type=hidden name="expense_accno_$i" value=$form->{"expense_accno_$i"}>
231 <input type=hidden name="listprice_$i" value="$form->{"listprice_$i"}">
232 <input type=hidden name="assembly_$i" value="$form->{"assembly_$i"}">
233 <input type=hidden name="taxaccounts_$i" value="$form->{"taxaccounts_$i"}">
234 <input type=hidden name="pricematrix_$i" value="$form->{"pricematrix_$i"}">
235 <input type=hidden name="oldqty_$i" value="$form->{"qty_$i"}">
236 <input type=hidden name="sku_$i" value="$form->{"sku_$i"}">
237
238 |;
239
240     $form->{selectprojectnumber} =~ s/ selected//;
241     $form->{selectprojectnumber} =~ s/(<option value="\Q$form->{"projectnumber_$i"}\E")/$1 selected/;
242
243     $project = qq|
244                 <b>$projectnumber</b>
245                 <select name="projectnumber_$i">$form->{selectprojectnumber}</select>
246 | if $form->{selectprojectnumber};
247
248     $serial = qq|
249                 <b>$serialnumber</b> <input name="serialnumber_$i" size=15 value="$form->{"serialnumber_$i"}">| if $form->{type} !~ /_quotation/;
250                 
251     $partsgroup = "";
252     if ($i == $numrows) {
253       if ($form->{selectpartsgroup}) {
254         $partsgroup = qq|
255                 <b>$group</b>
256                 <select name="partsgroup_$i">$form->{selectpartsgroup}</select>
257 |;
258       }
259
260       $serial = "";
261       $project = "";
262       $delivery = ""
263     }
264
265         
266     # print second row
267     print qq|
268         <tr>
269           <td colspan=$colspan>
270           $delivery
271           $serial
272           $project
273           $partsgroup
274           </td>
275         </tr>
276         <tr>
277           <td colspan=$colspan><hr size=1 noshade></td>
278         </tr>
279 |;
280
281     $skunumber = "";
282     
283     map { $form->{"${_}_base"} += $linetotal } (split / /, $form->{"taxaccounts_$i"});
284   
285     $form->{invsubtotal} += $linetotal;
286   }
287
288   print qq|
289       </table>
290     </td>
291   </tr>
292 |;
293
294   print qq|
295
296 <input type=hidden name=oldcurrency value=$form->{currency}>
297 <input type=hidden name=audittrail value="$form->{audittrail}">
298
299 <input type=hidden name=selectpartsgroup value="|.$form->escape($form->{selectpartsgroup},1).qq|">
300 <input type=hidden name=selectprojectnumber value="|.$form->escape($form->{selectprojectnumber},1).qq|">
301 |;
302  
303 }
304
305
306 sub select_item {
307
308   if ($form->{vc} eq "vendor") {
309     @column_index = qw(ndx partnumber sku description partsgroup onhand sellprice);
310   } else {
311     @column_index = qw(ndx partnumber description partsgroup onhand sellprice);
312   }
313
314   $column_data{ndx} = qq|<th>&nbsp;</th>|;
315   $column_data{partnumber} = qq|<th class=listheading>|.$locale->text('Number').qq|</th>|;
316   $column_data{sku} = qq|<th class=listheading>|.$locale->text('SKU').qq|</th>|;
317   $column_data{description} = qq|<th class=listheading>|.$locale->text('Description').qq|</th>|;
318   $column_data{partsgroup} = qq|<th class=listheading>|.$locale->text('Group').qq|</th>|;
319   $column_data{sellprice} = qq|<th class=listheading>|.$locale->text('Price').qq|</th>|;
320   $column_data{onhand} = qq|<th class=listheading>|.$locale->text('Qty').qq|</th>|;
321   
322   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
323
324   # list items with radio button on a form
325   $form->header;
326
327   $title = $locale->text('Select from one of the items below');
328
329   print qq|
330 <body>
331
332 <form method=post action="$form->{script}#end">
333
334 <table width=100%>
335   <tr>
336     <th class=listtop>$title</th>
337   </tr>
338   <tr height="5"></tr>
339   <tr>
340     <td>$option</td>
341   </tr>
342   <tr>
343     <td>
344       <table width=100%>
345         <tr class=listheading>|;
346
347   map { print "\n$column_data{$_}" } @column_index;
348   
349   print qq|
350         </tr>
351 |;
352
353   my $i = 0;
354   foreach $ref (@{ $form->{item_list} }) {
355     $checked = ($i++) ? "" : "checked";
356
357     map { $ref->{$_} = $form->quote($ref->{$_}) } qw(sku partnumber description unit);
358
359     $ref->{sellprice} = $form->round_amount($ref->{sellprice} * (1 - $form->{tradediscount}), 2);
360
361     $column_data{ndx} = qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
362     $column_data{partnumber} = qq|<td>$ref->{partnumber}</td>|;
363     $column_data{sku} = qq|<td>$ref->{sku}</td>|;
364     $column_data{description} = qq|<td>$ref->{description}</td>|;
365     $column_data{partsgroup} = qq|<td>$ref->{partsgroup}</td>|;
366     $column_data{sellprice} = qq|<td align=right>|.$form->format_amount(\%myconfig, $ref->{sellprice} / $exchangerate, 2, "&nbsp;").qq|</td>|;
367     $column_data{onhand} = qq|<td align=right>|.$form->format_amount(\%myconfig, $ref->{onhand}, '', "&nbsp;").qq|</td>|;
368     
369     $j++; $j %= 2;
370     print qq|
371         <tr class=listrow$j>|;
372
373     map { print "\n$column_data{$_}" } @column_index;
374
375     print qq|
376         </tr>
377
378 <input name="new_partnumber_$i" type=hidden value="$ref->{partnumber}">
379 <input name="new_sku_$i" type=hidden value="$ref->{sku}">
380 <input name="new_description_$i" type=hidden value="$ref->{description}">
381 <input name="new_partsgroup_$i" type=hidden value="$ref->{partsgroup}">
382 <input name="new_partsgroup_id_$i" type=hidden value="$ref->{partsgroup_id}">
383 <input name="new_bin_$i" type=hidden value="$ref->{bin}">
384 <input name="new_weight_$i" type=hidden value=$ref->{weight}>
385 <input name="new_sellprice_$i" type=hidden value=$ref->{sellprice}>
386 <input name="new_listprice_$i" type=hidden value=$ref->{listprice}>
387 <input name="new_lastcost_$i" type=hidden value=$ref->{lastcost}>
388 <input name="new_onhand_$i" type=hidden value=$ref->{onhand}>
389 <input name="new_inventory_accno_$i" type=hidden value=$ref->{inventory_accno}>
390 <input name="new_income_accno_$i" type=hidden value=$ref->{income_accno}>
391 <input name="new_expense_accno_$i" type=hidden value=$ref->{expense_accno}>
392 <input name="new_unit_$i" type=hidden value="$ref->{unit}">
393 <input name="new_weight_$i" type=hidden value="$ref->{weight}">
394 <input name="new_assembly_$i" type=hidden value="$ref->{assembly}">
395 <input name="new_taxaccounts_$i" type=hidden value="$ref->{taxaccounts}">
396 <input name="new_pricematrix_$i" type=hidden value="$ref->{pricematrix}">
397
398 <input name="new_id_$i" type=hidden value=$ref->{id}>
399
400 |;
401
402   }
403   
404   print qq|
405       </table>
406     </td>
407   </tr>
408   <tr>
409     <td><hr size=3 noshade></td>
410   </tr>
411 </table>
412
413 <input name=lastndx type=hidden value=$i>
414
415 |;
416
417   # delete action variable
418   map { delete $form->{$_} } qw(action item_list header);
419
420   $form->hide_form();
421   
422   print qq|
423 <input type=hidden name=nextsub value=item_selected>
424
425 <br>
426 <input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
427 </form>
428
429 </body>
430 </html>
431 |;
432
433 }
434
435
436
437 sub item_selected {
438
439   # replace the last row with the checked row
440   $i = $form->{rowcount};
441   $i = $form->{assembly_rows} if ($form->{item} eq 'assembly');
442
443   # index for new item
444   $j = $form->{ndx};
445
446   # if there was a price entered, override it
447   $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
448   
449   map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } qw(id partnumber sku description sellprice listprice lastcost inventory_accno income_accno expense_accno bin unit weight assembly taxaccounts pricematrix);
450
451   $form->{"partsgroup_$i"} = qq|$form->{"new_partsgroup_$j"}--$form->{"new_partsgroup_id_$j"}|;
452
453   ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
454   $dec = length $dec;
455   $decimalplaces = ($dec > 2) ? $dec : 2;
456
457   if ($sellprice) {
458     $form->{"sellprice_$i"} = $sellprice;
459   } else {
460     # if there is an exchange rate adjust sellprice
461     if (($form->{exchangerate} * 1) != 0) {
462       $form->{"sellprice_$i"} /= $form->{exchangerate};
463       $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
464     }
465   }
466
467   if (($form->{exchangerate} * 1) != 0) {
468     map { $form->{"${_}_$i"} /= $form->{exchangerate} } qw(listprice lastcost);
469   }
470   
471   # this is for the assembly
472   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(sellprice listprice weight);
473
474   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
475   $form->{weight} += ($form->{"weight_$i"} * $form->{"qty_$i"});
476
477   $amount = $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) * $form->{"qty_$i"};
478   map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
479   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
480
481   $form->{creditremaining} -= $amount;
482
483   $form->{"runningnumber_$i"} = $i;
484   
485   # delete all the new_ variables
486   for $i (1 .. $form->{lastndx}) {
487     map { delete $form->{"new_${_}_$i"} } qw(partnumber sku description sellprice bin listprice lastcost inventory_accno income_accno expense_accno unit assembly taxaccounts id pricematrix weight);
488   }
489   
490   map { delete $form->{$_} } qw(ndx lastndx nextsub);
491
492   # format amounts
493   map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice lastcost) if $form->{item} ne 'assembly';
494
495   &display_form;
496
497 }
498
499
500 sub new_item {
501
502   if ($form->{language_code} && $form->{"description_$form->{rowcount}"}) {
503     $form->error($locale->text('Translation not on file!'));
504   }
505   
506   # change callback
507   $form->{old_callback} = $form->escape($form->{callback},1);
508   $form->{callback} = $form->escape("$form->{script}?action=display_form",1);
509
510   # delete action
511   delete $form->{action};
512
513   # save all other form variables in a previousform variable
514   if (!$form->{previousform}) {
515     foreach $key (keys %$form) {
516       # escape ampersands
517       $form->{$key} =~ s/&/%26/g;
518       $form->{previousform} .= qq|$key=$form->{$key}&|;
519     }
520     chop $form->{previousform};
521     $form->{previousform} = $form->escape($form->{previousform}, 1);
522   }
523
524   $i = $form->{rowcount};
525   map { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}) } qw(partnumber description);
526
527   $form->header;
528
529   print qq|
530 <body>
531
532 <h4 class=error>|.$locale->text('Item not on file!').qq|</h4>|;
533
534   if ($myconfig{acs} !~ /(Goods \& Services--Add Part|Goods \& Services--Add Service)/) {
535
536     print qq|
537 <h4>|.$locale->text('What type of item is this?').qq|</h4>
538
539 <form method=post action=ic.pl>
540
541 <p>
542
543   <input class=radio type=radio name=item value=part checked>&nbsp;|.$locale->text('Part')
544 .qq|<br>
545   <input class=radio type=radio name=item value=service>&nbsp;|.$locale->text('Service')
546
547 .qq|
548 <input type=hidden name=previousform value="$form->{previousform}">
549 <input type=hidden name=partnumber value="$form->{"partnumber_$i"}">
550 <input type=hidden name=description value="$form->{"description_$i"}">
551 <input type=hidden name=rowcount value=$form->{rowcount}>
552
553 <input type=hidden name=path value=$form->{path}>
554 <input type=hidden name=login value=$form->{login}>
555 <input type=hidden name=sessionid value=$form->{sessionid}>
556
557 <input type=hidden name=nextsub value=add>
558
559 <p>
560 <input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
561 </form>
562 |;
563   }
564
565   print qq|
566 </body>
567 </html>
568 |;
569
570 }
571
572
573
574 sub display_form {
575
576   # if we have a display_form
577   if ($form->{display_form}) {
578     &{ "$form->{display_form}" };
579     exit;
580   }
581   
582   &form_header;
583
584   $numrows = ++$form->{rowcount};
585   $subroutine = "display_row";
586
587   if ($form->{item} eq 'part') {
588     # create makemodel rows
589     &makemodel_row(++$form->{makemodel_rows});
590
591     &vendor_row(++$form->{vendor_rows});
592     
593     $numrows = ++$form->{customer_rows};
594     $subroutine = "customer_row";
595   }
596   if ($form->{item} eq 'assembly') {
597     # create makemodel rows
598     &makemodel_row(++$form->{makemodel_rows});
599     
600     $numrows = ++$form->{customer_rows};
601     $subroutine = "customer_row";
602   }
603   if ($form->{item} eq 'service') {
604     &vendor_row(++$form->{vendor_rows});
605     
606     $numrows = ++$form->{customer_rows};
607     $subroutine = "customer_row";
608   }
609   if ($form->{item} eq 'labor') {
610     $numrows = 0;
611   }
612
613   # create rows
614   &{ $subroutine }($numrows) if $numrows;
615
616   &form_footer;
617
618 }
619
620
621
622 sub check_form {
623   
624   my @a = ();
625   my $count = 0;
626   my $i;
627   my $j;
628   my @flds = qw(id partnumber sku description qty ship sellprice unit discount inventory_accno income_accno expense_accno listprice taxaccounts bin assembly weight projectnumber runningnumber serialnumber partsgroup reqdate pricematrix);
629
630   # remove any makes or model rows
631   if ($form->{item} eq 'part') {
632     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(listprice sellprice lastcost weight rop markup);
633     
634     &calc_markup;
635     
636     @flds = qw(make model);
637     $count = 0;
638     @a = ();
639     for $i (1 .. $form->{makemodel_rows}) {
640       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
641         push @a, {};
642         $j = $#a;
643
644         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
645         $count++;
646       }
647     }
648
649     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
650     $form->{makemodel_rows} = $count;
651
652     &check_vendor;
653     &check_customer;
654     
655   } elsif ($form->{item} eq 'service') {
656     
657     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(sellprice listprice lastcost markup);
658     
659     &calc_markup;
660     &check_vendor;
661     &check_customer;
662     
663   } elsif ($form->{item} eq 'assembly') {
664
665     $form->{sellprice} = 0;
666     $form->{weight} = 0;
667     $form->{lastcost} = 0;
668     $form->{listprice} = 0;
669     
670     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(rop stock markup);
671
672    
673     @flds = qw(id qty unit bom adj partnumber description sellprice listprice weight runningnumber partsgroup);
674     $count = 0;
675     @a = ();
676     
677     for my $i (1 .. ($form->{assembly_rows} - 1)) {
678       if ($form->{"qty_$i"}) {
679         push @a, {};
680         my $j = $#a;
681
682         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
683
684         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
685
686         map { $form->{$_} += ($form->{"${_}_$i"} * $form->{"qty_$i"}) } qw(sellprice listprice weight lastcost);
687         
688         $count++;
689       }
690     }
691
692     if ($form->{markup} && $form->{markup} != $form->{oldmarkup}) {
693       $form->{sellprice} = 0;
694       &calc_markup;
695     }
696  
697     map { $form->{$_} = $form->round_amount($form->{$_}, 2) } qw(sellprice lastcost listprice);
698     
699     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
700     $form->{assembly_rows} = $count;
701     
702     $count = 0;
703     @flds = qw(make model);
704     @a = ();
705     
706     for my $i (1 .. ($form->{makemodel_rows})) {
707       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
708         push @a, {};
709         my $j = $#a;
710
711         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
712         $count++;
713       }
714     }
715
716     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
717     $form->{makemodel_rows} = $count;
718
719     &check_customer;
720   
721   } else {
722
723     # this section applies to invoices and orders
724     # remove any empty numbers
725     
726     $count = 0;
727     @a = ();
728     if ($form->{rowcount}) {
729       for my $i (1 .. $form->{rowcount} - 1) {
730         if ($form->{"partnumber_$i"}) {
731           push @a, {};
732           my $j = $#a;
733
734           map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
735           $count++;
736         }
737       }
738       
739       $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
740       $form->{rowcount} = $count;
741
742       $form->{creditremaining} -= &invoicetotal;
743       
744     }
745   }
746
747   &display_form;
748
749 }
750
751
752 sub calc_markup {
753
754   if ($form->{markup}) {
755     if ($form->{markup} != $form->{oldmarkup}) {
756       if ($form->{lastcost}) {
757         $form->{sellprice} = $form->{lastcost} * (1 + $form->{markup}/100);
758         $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
759       } else {
760         $form->{lastcost} = $form->{sellprice} / (1 + $form->{markup}/100);
761         $form->{lastcost} = $form->round_amount($form->{lastcost}, 2);
762       }
763     }
764   } else {
765     if ($form->{lastcost}) {
766       $form->{markup} = $form->round_amount(((1 - $form->{sellprice} / $form->{lastcost}) * 100), 1);
767     }
768     $form->{markup} = "" if $form->{markup} == 0;
769   }
770
771 }
772
773
774 sub invoicetotal {
775
776   $form->{oldinvtotal} = 0;
777   # add all parts and deduct paid
778   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
779
780   my ($amount, $sellprice, $discount, $qty);
781   
782   for my $i (1 .. $form->{rowcount}) {
783     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
784     $discount = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
785     $qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
786
787     $amount = $sellprice * (1 - $discount / 100) * $qty;
788     map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
789     $form->{oldinvtotal} += $amount;
790   }
791
792   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
793   
794   $form->{oldtotalpaid} = 0;
795   for $i (1 .. $form->{paidaccounts}) {
796     $form->{oldtotalpaid} += $form->{"paid_$i"};
797   }
798   
799   # return total
800   ($form->{oldinvtotal} - $form->{oldtotalpaid});
801
802 }
803
804
805 sub validate_items {
806   
807   # check if items are valid
808   if ($form->{rowcount} == 1) {
809     &update;
810     exit;
811   }
812     
813   for $i (1 .. $form->{rowcount} - 1) {
814     $form->isblank("partnumber_$i", $locale->text('Number missing in Row') . " $i");
815   }
816
817 }
818
819
820
821 sub purchase_order {
822   
823   $form->{title} = $locale->text('Add Purchase Order');
824   $form->{vc} = 'vendor';
825   $form->{type} = 'purchase_order';
826   $buysell = 'sell';
827
828   &create_form;
829
830 }
831
832  
833 sub sales_order {
834
835   $form->{title} = $locale->text('Add Sales Order');
836   $form->{vc} = 'customer';
837   $form->{type} = 'sales_order';
838   $buysell = 'buy';
839
840   &create_form;
841
842 }
843
844
845 sub rfq {
846   
847   $form->{title} = $locale->text('Add Request for Quotation');
848   $form->{vc} = 'vendor';
849   $form->{type} = 'request_quotation';
850   $buysell = 'sell';
851  
852   &create_form;
853   
854 }
855
856
857 sub quotation {
858
859   $form->{title} = $locale->text('Add Quotation');
860   $form->{vc} = 'customer';
861   $form->{type} = 'sales_quotation';
862   $buysell = 'buy';
863
864   &create_form;
865
866 }
867
868
869 sub create_form {
870
871   map { delete $form->{$_} } qw(id printed emailed queued);
872  
873   $form->{script} = 'oe.pl';
874
875   $form->{shipto} = 1;
876
877   $form->{rowcount}-- if $form->{rowcount};
878
879   require "$form->{path}/$form->{script}";
880
881   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
882   
883   map { $temp{$_} = $form->{$_} } qw(currency employee department intnotes notes language_code);
884
885   &order_links;
886
887   map { $form->{$_} = $temp{$_} if $temp{$_} } keys %temp;
888
889   $form->{exchangerate} = "";
890   $form->{forex} = "";
891   if ($form->{currency} ne $form->{defaultcurrency}) {
892     $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell)));
893   }
894   
895   &prepare_order;
896
897   &display_form;
898
899 }
900
901
902
903 sub e_mail {
904
905   $bcc = qq|<input type=hidden name=bcc value="$form->{bcc}">|;
906   if ($myconfig{role} =~ /(admin|manager)/) {
907     $bcc = qq|
908           <th align=right nowrap=true>|.$locale->text('Bcc').qq|</th>
909           <td><input name=bcc size=30 value="$form->{bcc}"></td>
910 |;
911   }
912
913   if ($form->{formname} =~ /(pick|packing|bin)_list/) {
914     $form->{email} = $form->{shiptoemail} if $form->{shiptoemail};
915   }
916
917   $name = $form->{$form->{vc}};
918   $name =~ s/--.*//g;
919   $title = $locale->text('E-mail')." $name";
920   
921   $form->header;
922
923   print qq|
924 <body>
925
926 <form method=post action="$form->{script}#end">
927
928 <table width=100%>
929   <tr class=listtop>
930     <th class=listtop>$title</th>
931   </tr>
932   <tr height="5"></tr>
933   <tr>
934     <td>
935       <table width=100%>
936         <tr>
937           <th align=right nowrap>|.$locale->text('E-mail').qq|</th>
938           <td><input name=email size=30 value="$form->{email}"></td>
939           <th align=right nowrap>|.$locale->text('Cc').qq|</th>
940           <td><input name=cc size=30 value="$form->{cc}"></td>
941         </tr>
942         <tr>
943           <th align=right nowrap>|.$locale->text('Subject').qq|</th>
944           <td><input name=subject size=30 value="$form->{subject}"></td>
945           $bcc
946         </tr>
947       </table>
948     </td>
949   </tr>
950   <tr>
951     <td>
952       <table width=100%>
953         <tr>
954           <th align=left nowrap>|.$locale->text('Message').qq|</th>
955         </tr>
956         <tr>
957           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
958         </tr>
959       </table>
960     </td>
961   </tr>
962   <tr>
963     <td>
964 |;
965
966   $form->{oldmedia} = $form->{media};
967   $form->{media} = "email";
968   $form->{format} = "pdf";
969   
970   &print_options;
971   
972   map { delete $form->{$_} } qw(action email cc bcc subject message formname sendmode format header);
973   
974   $form->hide_form();
975
976   print qq|
977     </td>
978   </tr>
979   <tr>
980     <td><hr size=3 noshade></td>
981   </tr>
982 </table>
983
984 <input type=hidden name=nextsub value=send_email>
985
986 <br>
987 <input name=action class=submit type=submit value="|.$locale->text('Continue').qq|">
988 </form>
989
990 </body>
991 </html>
992 |;
993
994 }
995
996
997 sub send_email {
998
999   $old_form = new Form;
1000   
1001   map { $old_form->{$_} = $form->{$_} } keys %$form;
1002   $old_form->{media} = $old_form->{oldmedia};
1003   
1004   &print_form($old_form);
1005   
1006 }
1007   
1008
1009  
1010 sub print_options {
1011
1012   $form->{sendmode} = "attachment";
1013   $form->{copies} = 1 unless $form->{copies};
1014   
1015   $form->{PD}{$form->{formname}} = "selected";
1016   $form->{DF}{$form->{format}} = "selected";
1017   $form->{SM}{$form->{sendmode}} = "selected";
1018   
1019   if ($form->{selectlanguage}) {
1020     $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
1021     $form->{"selectlanguage"} =~ s/ selected//;
1022     $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
1023     $lang = qq|<td><select name=language_code>$form->{selectlanguage}</select></td>
1024     <input type=hidden name=oldlanguage_code value=$form->{oldlanguage_code}>
1025     <input type=hidden name=selectlanguage value="|.
1026     $form->escape($form->{selectlanguage},1).qq|">|;
1027   }
1028   
1029   if ($form->{type} eq 'purchase_order') {
1030     $type = qq|<td><select name=formname>
1031             <option value=purchase_order $form->{PD}{purchase_order}>|.$locale->text('Purchase Order').qq|
1032             <option value=bin_list $form->{PD}{bin_list}>|.$locale->text('Bin List').qq|</select></td>|;
1033   }
1034   
1035   if ($form->{type} eq 'sales_order') {
1036     $type = qq|<td><select name=formname>
1037             <option value=sales_order $form->{PD}{sales_order}>|.$locale->text('Sales Order').qq|
1038             <option value=work_order $form->{PD}{work_order}>|.$locale->text('Work Order').qq|
1039             <option value=pick_list $form->{PD}{pick_list}>|.$locale->text('Pick List').qq|
1040             <option value=packing_list $form->{PD}{packing_list}>|.$locale->text('Packing List').qq|</select></td>|;
1041   }
1042   
1043   if ($form->{type} =~ /_quotation$/) {
1044     $type = qq|<td><select name=formname>
1045             <option value="$`_quotation" $form->{PD}{"$`_quotation"}>|.$locale->text('Quotation').qq|</select></td>|;
1046   }
1047   
1048   if ($form->{type} eq 'invoice') {
1049     $type = qq|<td><select name=formname>
1050             <option value=invoice $form->{PD}{invoice}>|.$locale->text('Invoice').qq|
1051             <option value=pick_list $form->{PD}{pick_list}>|.$locale->text('Pick List').qq|
1052             <option value=packing_list $form->{PD}{packing_list}>|.$locale->text('Packing List').qq|</select></td>|;
1053   }
1054   
1055   if ($form->{type} eq 'ship_order') {
1056     $type = qq|<td><select name=formname>
1057             <option value=pick_list $form->{PD}{pick_list}>|.$locale->text('Pick List').qq|
1058             <option value=packing_list $form->{PD}{packing_list}>|.$locale->text('Packing List').qq|</select></td>|;
1059   }
1060   
1061   if ($form->{type} eq 'receive_order') {
1062     $type = qq|<td><select name=formname>
1063             <option value=bin_list $form->{PD}{bin_list}>|.$locale->text('Bin List').qq|</select></td>|;
1064   }
1065  
1066   if ($form->{media} eq 'email') {
1067     $media = qq|<td><select name=sendmode>
1068             <option value=attachment $form->{SM}{attachment}>|.$locale->text('Attachment').qq|
1069             <option value=inline $form->{SM}{inline}>|.$locale->text('In-line').qq|</select></td>|;
1070   } else {
1071     $media = qq|<td><select name=media>
1072             <option value=screen>|.$locale->text('Screen');
1073     if (%printer && $latex) {
1074       map { $media .= qq|
1075             <option value="$_">$_| } sort keys %printer;
1076     }
1077     if ($latex) {
1078       $media .= qq|
1079             <option value="queue">|.$locale->text('Queue');
1080     }
1081     $media .= qq|</select></td>|;
1082
1083     # set option selected
1084     $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
1085  
1086   }
1087
1088   $format = qq|<td><select name=format>
1089             <option value=html $form->{DF}{html}>html|;
1090
1091 #           <option value=txt $form->{DF}{txt}>txt|;
1092
1093   if ($latex) {
1094     $format .= qq|
1095             <option value=postscript $form->{DF}{postscript}>|.$locale->text('Postscript').qq|
1096             <option value=pdf $form->{DF}{pdf}>|.$locale->text('PDF');
1097   }
1098   $format .= qq|</select></td>|;
1099
1100   print qq|
1101 <table width=100% cellspacing=0 cellpadding=0>
1102   <tr>
1103     <td>
1104       <table>
1105         <tr>
1106           $type
1107           $lang
1108           $format
1109           $media
1110 |;
1111
1112   if (%printer && $latex && $form->{media} ne 'email') {
1113     print qq|
1114           <td>|.$locale->text('Copies').qq|
1115           <input name=copies size=2 value=$form->{copies}></td>
1116 |;
1117   }
1118
1119   $form->{groupprojectnumber} = "checked" if $form->{groupprojectnumber};
1120   $form->{grouppartsgroup} = "checked" if $form->{grouppartsgroup};
1121
1122   print qq|
1123           <td>|.$locale->text('Group Items').qq|</td>
1124           <td>
1125           <input name=groupprojectnumber type=checkbox class=checkbox $form->{groupprojectnumber}>
1126           |.$locale->text('Project').qq|
1127           <input name=grouppartsgroup type=checkbox class=checkbox $form->{grouppartsgroup}>
1128           |.$locale->text('Group').qq|
1129           </td>
1130         </tr>
1131       </table>
1132     </td>
1133     <td align=right>
1134 |;
1135
1136   if ($form->{printed} =~ /$form->{formname}/) {
1137     print $locale->text('Printed').qq|<br>|;
1138   }
1139   
1140   if ($form->{emailed} =~ /$form->{formname}/) {
1141     print $locale->text('E-mailed').qq|<br>|;
1142   }
1143   
1144   if ($form->{queued} =~ /$form->{formname}/) {
1145     print $locale->text('Queued');
1146   }
1147   
1148   print qq|
1149     </td>
1150   </tr>
1151 </table>
1152 |;
1153
1154
1155 }
1156
1157
1158
1159 sub print {
1160
1161   # if this goes to the printer pass through
1162   if ($form->{media} !~ /(screen|email)/) {
1163     $form->error($locale->text('Select txt, postscript or PDF!')) if ($form->{format} !~ /(txt|postscript|pdf)/);
1164
1165     $old_form = new Form;
1166     map { $old_form->{$_} = $form->{$_} } keys %$form;
1167     
1168   }
1169    
1170   &print_form($old_form);
1171
1172 }
1173
1174
1175 sub print_form {
1176   my ($old_form) = @_;
1177
1178   $inv = "inv";
1179   $due = "due";
1180
1181   $numberfld = "sinumber";
1182
1183   $display_form = ($form->{display_form}) ? $form->{display_form} : "display_form";
1184
1185   if ($form->{formname} eq "invoice") {
1186     $form->{label} = $locale->text('Invoice');
1187   }
1188   if ($form->{formname} eq 'sales_order') {
1189     $inv = "ord";
1190     $due = "req";
1191     $form->{label} = $locale->text('Sales Order');
1192     $numberfld = "sonumber";
1193     $order = 1;
1194   }
1195   if ($form->{formname} eq 'work_order') {
1196     $inv = "ord";
1197     $due = "req";
1198     $form->{label} = $locale->text('Work Order');
1199     $numberfld = "sonumber";
1200     $order = 1;
1201   }
1202   if ($form->{formname} eq 'packing_list') {
1203     # we use the same packing list as from an invoice
1204     $form->{label} = $locale->text('Packing List');
1205
1206     if ($form->{type} ne 'invoice') {
1207       $inv = "ord";
1208       $due = "req";
1209       $numberfld = "sonumber";
1210       $order = 1;
1211     }
1212   }
1213   if ($form->{formname} eq 'pick_list') {
1214     $form->{label} = $locale->text('Pick List');
1215     if ($form->{type} ne 'invoice') {
1216       $inv = "ord";
1217       $due = "req";
1218       $order = 1;
1219       $numberfld = "sonumber";
1220     }
1221   }
1222   if ($form->{formname} eq 'purchase_order') {
1223     $inv = "ord";
1224     $due = "req";
1225     $form->{label} = $locale->text('Purchase Order');
1226     $numberfld = "ponumber";
1227     $order = 1;
1228   }
1229   if ($form->{formname} eq 'bin_list') {
1230     $inv = "ord";
1231     $due = "req";
1232     $form->{label} = $locale->text('Bin List');
1233     $numberfld = "ponumber";
1234     $order = 1;
1235   }
1236   if ($form->{formname} eq 'sales_quotation') {
1237     $inv = "quo";
1238     $due = "req";
1239     $form->{label} = $locale->text('Quotation');
1240     $numberfld = "sqnumber";
1241     $order = 1;
1242   }
1243   if ($form->{formname} eq 'request_quotation') {
1244     $inv = "quo";
1245     $due = "req";
1246     $form->{label} = $locale->text('Quotation');
1247     $numberfld = "rfqnumber";
1248     $order = 1;
1249   }
1250   
1251   $form->{"${inv}date"} = $form->{transdate};
1252
1253   $form->isblank("email", $locale->text('E-mail address missing!')) if ($form->{media} eq 'email');
1254   $form->isblank("${inv}date", $locale->text($form->{label} .' Date missing!'));
1255
1256   # get next number
1257   if (! $form->{"${inv}number"}) {
1258     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1259     if ($form->{media} eq 'screen') {
1260       &update;
1261       exit;
1262     }
1263   }
1264
1265
1266 # $locale->text('Invoice Number missing!')
1267 # $locale->text('Invoice Date missing!')
1268 # $locale->text('Packing List Number missing!')
1269 # $locale->text('Packing List Date missing!')
1270 # $locale->text('Order Number missing!')
1271 # $locale->text('Order Date missing!')
1272 # $locale->text('Quotation Number missing!')
1273 # $locale->text('Quotation Date missing!')
1274
1275   &validate_items;
1276
1277   &{ "$form->{vc}_details" };
1278
1279   @a = ();
1280   foreach $i (1 .. $form->{rowcount}) {
1281     push @a, ("partnumber_$i", "description_$i", "projectnumber_$i", "partsgroup_$i", "serialnumber_$i", "bin_$i", "unit_$i");
1282   }
1283   map { push @a, "${_}_description" } split / /, $form->{taxaccounts};
1284
1285   $ARAP = ($form->{vc} eq 'customer') ? "AR" : "AP";
1286   push @a, $ARAP;
1287   
1288   # format payment dates
1289   for $i (1 .. $form->{paidaccounts} - 1) {
1290     if (exists $form->{longformat}) {
1291       $form->{"datepaid_$i"} = $locale->date(\%myconfig, $form->{"datepaid_$i"}, $form->{longformat});
1292     }
1293     
1294     push @a, "${ARAP}_paid_$i", "source_$i", "memo_$i";
1295   }
1296   
1297   $form->format_string(@a);
1298   
1299   ($form->{employee}) = split /--/, $form->{employee};
1300   ($form->{warehouse}, $form->{warehouse_id}) = split /--/, $form->{warehouse};
1301   
1302   # this is a label for the subtotals
1303   $form->{groupsubtotaldescription} = $locale->text('Subtotal') if not exists $form->{groupsubtotaldescription};
1304   delete $form->{groupsubtotaldescription} if $form->{deletegroupsubtotal};
1305
1306   # create the form variables
1307   if ($order) {
1308     OE->order_details(\%myconfig, \%$form);
1309   } else {
1310     IS->invoice_details(\%myconfig, \%$form);
1311   }
1312
1313   if (exists $form->{longformat}) {
1314     map { $form->{$_} = $locale->date(\%myconfig, $form->{$_}, $form->{longformat}) } ("${inv}date", "${due}date", "shippingdate", "transdate");
1315   }
1316   
1317   @a = qw(name address1 address2 city state zipcode country);
1318  
1319   $shipto = 1;
1320   # if there is no shipto fill it in from billto
1321   foreach $item (@a) {
1322     if ($form->{"shipto$item"}) {
1323       $shipto = 0;
1324       last;
1325     }
1326   }
1327
1328   if ($shipto) {
1329     if ($form->{formname} eq 'purchase_order' || $form->{formname} eq 'request_quotation') {
1330         $form->{shiptoname} = $myconfig{company};
1331         $form->{shiptoaddress1} = $myconfig{address};
1332     } else {
1333       if ($form->{formname} !~ /bin_list/) {
1334         map { $form->{"shipto$_"} = $form->{$_} } @a;
1335       }
1336     }
1337   }
1338
1339   $form->{notes} =~ s/^\s+//g;
1340
1341   # some of the stuff could have umlauts so we translate them
1342   push @a, qw(contact shiptoname shiptoaddress1 shiptoaddress2 shiptocity shiptostate shiptozipcode shiptocountry shiptocontact shiptoemail shippingpoint shipvia notes employee warehouse);
1343
1344   push @a, ("${inv}number", "${inv}date", "${due}date", "email", "cc", "bcc");
1345   
1346   map { $form->{$_} = $myconfig{$_} } (qw(company address tel fax signature businessnumber));
1347   map { $form->{"user$_"} = $myconfig{$_} } qw(name email);
1348   push @a, qw(company address tel fax signature businessnumber username useremail);
1349
1350   $form->format_string(@a);
1351
1352
1353   $form->{templates} = "$myconfig{templates}";
1354   $form->{IN} = "$form->{formname}.$form->{format}";
1355
1356   if ($form->{format} =~ /(postscript|pdf)/) {
1357     $form->{IN} =~ s/$&$/tex/;
1358   }
1359
1360   $form->{pre} = "<body bgcolor=#ffffff>\n<pre>" if $form->{format} eq 'txt';
1361
1362   if ($form->{media} !~ /(screen|queue|email)/) {
1363     $form->{OUT} = "| $printer{$form->{media}}";
1364     
1365     if ($form->{printed} !~ /$form->{formname}/) {
1366     
1367       $form->{printed} .= " $form->{formname}";
1368       $form->{printed} =~ s/^ //;
1369
1370       $form->update_status(\%myconfig);
1371     }
1372
1373     $old_form->{printed} = $form->{printed};
1374
1375     %audittrail = ( tablename   => ($order) ? 'oe' : lc $ARAP,
1376                     reference   => $form->{"${inv}number"},
1377                     formname    => $form->{formname},
1378                     action      => 'printed',
1379                     id          => $form->{id} );
1380  
1381     $old_form->{audittrail} .= $form->audittrail("", \%myconfig, \%audittrail);
1382     
1383   }
1384
1385
1386   if ($form->{media} eq 'email') {
1387     $form->{subject} = qq|$form->{label} $form->{"${inv}number"}| unless $form->{subject};
1388
1389     $form->{plainpaper} = 1;
1390     $form->{OUT} = "$sendmail";
1391
1392     if ($form->{emailed} !~ /$form->{formname}/) {
1393       $form->{emailed} .= " $form->{formname}";
1394       $form->{emailed} =~ s/^ //;
1395
1396       # save status
1397       $form->update_status(\%myconfig);
1398     }
1399
1400     $now = scalar localtime;
1401     $cc = $locale->text('Cc').qq|: $form->{cc}\n| if $form->{cc};
1402     $bcc = $locale->text('Bcc').qq|: $form->{bcc}\n| if $form->{bcc};
1403     
1404     $old_form->{intnotes} = qq|$old_form->{intnotes}\n\n| if $old_form->{intnotes};
1405     $old_form->{intnotes} .= qq|[email]
1406 |.$locale->text('Date').qq|: $now
1407 |.$locale->text('To').qq|: $form->{email}
1408 $cc${bcc}|.$locale->text('Subject').qq|: $form->{subject}\n|;
1409
1410     $old_form->{intnotes} .= qq|\n|.$locale->text('Message').qq|: |;
1411     $old_form->{intnotes} .= ($form->{message}) ? $form->{message} : $locale->text('sent');
1412
1413     $old_form->{message} = $form->{message};
1414     $old_form->{emailed} = $form->{emailed};
1415
1416     $old_form->{format} = "postscript" if $myconfig{printer};
1417     $old_form->{media} = $myconfig{printer};
1418
1419     $old_form->save_intnotes(\%myconfig, ($order) ? 'oe' : lc $ARAP);
1420     
1421     %audittrail = ( tablename   => ($order) ? 'oe' : lc $ARAP,
1422                     reference   => $form->{"${inv}number"},
1423                     formname    => $form->{formname},
1424                     action      => 'emailed',
1425                     id          => $form->{id} );
1426  
1427     $old_form->{audittrail} .= $form->audittrail("", \%myconfig, \%audittrail);
1428   }
1429
1430
1431   if ($form->{media} eq 'queue') {
1432     %queued = split / /, $form->{queued};
1433     
1434     if ($filename = $queued{$form->{formname}}) {
1435       $form->{queued} =~ s/$form->{formname} $filename//;
1436       unlink "$spool/$filename";
1437       $filename =~ s/\..*$//g;
1438     } else {
1439       $filename = time;
1440       $filename .= $$;
1441     }
1442
1443     $filename .= ($form->{format} eq 'postscript') ? '.ps' : '.pdf';
1444     $form->{OUT} = ">$spool/$filename";
1445
1446     $form->{queued} .= " $form->{formname} $filename";
1447     $form->{queued} =~ s/^ //;
1448
1449     # save status
1450     $form->update_status(\%myconfig);
1451
1452     $old_form->{queued} = $form->{queued};
1453     
1454     %audittrail = ( tablename   => ($order) ? 'oe' : lc $ARAP,
1455                     reference   => $form->{"${inv}number"},
1456                     formname    => $form->{formname},
1457                     action      => 'queued',
1458                     id          => $form->{id} );
1459  
1460     $old_form->{audittrail} .= $form->audittrail("", \%myconfig, \%audittrail);
1461     
1462   }
1463
1464
1465   $form->{fileid} = $form->{"${inv}number"};
1466   $form->{fileid} =~ s/(\s|\W)+//g;
1467   
1468   $form->parse_template(\%myconfig, $userspath);
1469
1470   # if we got back here restore the previous form
1471   if ($old_form) {
1472     
1473     $old_form->{"${inv}number"} = $form->{"${inv}number"};
1474     
1475     # restore and display form
1476     map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1477     delete $form->{pre};
1478     
1479     $form->{rowcount}--;
1480
1481     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
1482     
1483     for $i (1 .. $form->{paidaccounts}) {
1484       map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
1485     }
1486
1487     &{ "$display_form" };
1488
1489   }
1490
1491 }
1492
1493
1494 sub customer_details {
1495
1496   IS->customer_details(\%myconfig, \%$form);
1497
1498 }
1499
1500
1501 sub vendor_details {
1502
1503   IR->vendor_details(\%myconfig, \%$form);
1504
1505 }
1506
1507
1508 sub post_as_new {
1509
1510   $form->{postasnew} = 1;
1511   map { delete $form->{$_} } qw(printed emailed queued);
1512   
1513   &post;
1514
1515 }
1516
1517
1518 sub ship_to {
1519
1520   $title = $form->{title};
1521   $form->{title} = $locale->text('Ship to');
1522
1523   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
1524
1525   # get details for name
1526   &{ "$form->{vc}_details" };
1527
1528   $number = ($form->{vc} eq 'customer') ? $locale->text('Customer Number') : $locale->text('Vendor Number');
1529
1530   $nextsub = ($form->{display_form}) ? $form->{display_form} : "display_form";
1531
1532   $form->{rowcount}--;
1533
1534   $form->header;
1535
1536   print qq|
1537 <body>
1538
1539 <form method=post action=$form->{script}>
1540
1541 <table width=100%>
1542   <tr>
1543     <td>
1544       <table>
1545         <tr class=listheading>
1546           <th class=listheading colspan=2 width=50%>|.$locale->text('Billing Address').qq|</th>
1547           <th class=listheading width=50%>|.$locale->text('Shipping Address').qq|</th>
1548         </tr>
1549         <tr height="5"></tr>
1550         <tr>
1551           <th align=right nowrap>$number</th>
1552           <td>$form->{"$form->{vc}number"}</td>
1553         </tr>
1554         <tr>
1555           <th align=right nowrap>|.$locale->text('Company Name').qq|</th>
1556           <td>$form->{name}</td>
1557           <td><input name=shiptoname size=35 maxlength=64 value="$form->{shiptoname}"></td>
1558         </tr>
1559         <tr>
1560           <th align=right nowrap>|.$locale->text('Address').qq|</th>
1561           <td>$form->{address1}</td>
1562           <td><input name=shiptoaddress1 size=35 maxlength=32 value="$form->{shiptoaddress1}"></td>
1563         </tr>
1564         <tr>
1565           <th></th>
1566           <td>$form->{address2}</td>
1567           <td><input name=shiptoaddress2 size=35 maxlength=32 value="$form->{shiptoaddress2}"></td>
1568         </tr>
1569         <tr>
1570           <th align=right nowrap>|.$locale->text('City').qq|</th>
1571           <td>$form->{city}</td>
1572           <td><input name=shiptocity size=35 maxlength=32 value="$form->{shiptocity}"></td>
1573         </tr>
1574         <tr>
1575           <th align=right nowrap>|.$locale->text('State/Province').qq|</th>
1576           <td>$form->{state}</td>
1577           <td><input name=shiptostate size=35 maxlength=32 value="$form->{shiptostate}"></td>
1578         </tr>
1579         <tr>
1580           <th align=right nowrap>|.$locale->text('Zip/Postal Code').qq|</th>
1581           <td>$form->{zipcode}</td>
1582           <td><input name=shiptozipcode size=10 maxlength=10 value="$form->{shiptozipcode}"></td>
1583         </tr>
1584         <tr>
1585           <th align=right nowrap>|.$locale->text('Country').qq|</th>
1586           <td>$form->{country}</td>
1587           <td><input name=shiptocountry size=35 maxlength=32 value="$form->{shiptocountry}"></td>
1588         </tr>
1589         <tr>
1590           <th align=right nowrap>|.$locale->text('Contact').qq|</th>
1591           <td>$form->{contact}</td>
1592           <td><input name=shiptocontact size=35 maxlength=64 value="$form->{shiptocontact}"></td>
1593         </tr>
1594         <tr>
1595           <th align=right nowrap>|.$locale->text('Phone').qq|</th>
1596           <td>$form->{"$form->{vc}phone"}</td>
1597           <td><input name=shiptophone size=20 value="$form->{shiptophone}"></td>
1598         </tr>
1599         <tr>
1600           <th align=right nowrap>|.$locale->text('Fax').qq|</th>
1601           <td>$form->{"$form->{vc}fax"}</td>
1602           <td><input name=shiptofax size=20 value="$form->{shiptofax}"></td>
1603         </tr>
1604         <tr>
1605           <th align=right nowrap>|.$locale->text('E-mail').qq|</th>
1606           <td>$form->{email}</td>
1607           <td><input name=shiptoemail size=35 value="$form->{shiptoemail}"></td>
1608         </tr>
1609       </table>
1610     </td>
1611   </tr>
1612 </table>
1613
1614 <input type=hidden name=nextsub value=$nextsub>
1615 |;
1616
1617   # delete shipto
1618   map { delete $form->{$_} } qw(shiptoname shiptoaddress1 shiptoaddress2 shiptocity shiptostate shiptozipcode shiptocountry shiptocontact shiptophone shiptofax shiptoemail header);
1619   $form->{title} = $title;
1620   
1621   $form->hide_form();
1622
1623   print qq|
1624
1625 <hr size=3 noshade>
1626
1627 <br>
1628 <input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
1629 </form>
1630
1631 </body>
1632 </html>
1633 |;
1634
1635 }
1636
1637