cdc03029715e9189298972d97b273fcd8ba4ef95
[kivitendo-erp.git] / bin / mozilla / io.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #############################################################################
7 # Veraendert 2005-01-05 - Marco Welter <mawe@linux-studio.de> - Neue Optik  #
8 #############################################################################
9 # SQL-Ledger, Accounting
10 # Copyright (c) 1998-2002
11 #
12 #  Author: Dieter Simader
13 #   Email: dsimader@sql-ledger.org
14 #     Web: http://www.sql-ledger.org
15 #
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #
30 #######################################################################
31 #
32 # common routines used in is, ir, oe
33 #
34 #######################################################################
35
36 use SL::IC;
37
38 # any custom scripts for this one
39 if (-f "$form->{path}/custom_io.pl") {
40   eval { require "$form->{path}/custom_io.pl"; };
41 }
42 if (-f "$form->{path}/$form->{login}_io.pl") {
43   eval { require "$form->{path}/$form->{login}_io.pl"; };
44 }
45
46 1;
47
48 # end of main
49
50 # this is for our long dates
51 # $locale->text('January')
52 # $locale->text('February')
53 # $locale->text('March')
54 # $locale->text('April')
55 # $locale->text('May ')
56 # $locale->text('June')
57 # $locale->text('July')
58 # $locale->text('August')
59 # $locale->text('September')
60 # $locale->text('October')
61 # $locale->text('November')
62 # $locale->text('December')
63
64 # this is for our short month
65 # $locale->text('Jan')
66 # $locale->text('Feb')
67 # $locale->text('Mar')
68 # $locale->text('Apr')
69 # $locale->text('May')
70 # $locale->text('Jun')
71 # $locale->text('Jul')
72 # $locale->text('Aug')
73 # $locale->text('Sep')
74 # $locale->text('Oct')
75 # $locale->text('Nov')
76 # $locale->text('Dec')
77 use SL::IS;
78 use SL::PE;
79 use SL::AM;
80 use Data::Dumper;
81 ########################################
82 # Eintrag fuer Version 2.2.0 geaendert #
83 # neue Optik im Rechnungsformular      #
84 ########################################
85 sub display_row {
86   $lxdebug->enter_sub();
87   my $numrows = shift;
88   if ($lizenzen && $form->{vc} eq "customer") {
89     if ($form->{type} =~ /sales_order/) {
90       @column_index = (runningnumber, partnumber, description, ship, qty);
91     } elsif ($form->{type} =~ /sales_quotation/) {
92       @column_index = (runningnumber, partnumber, description, qty);
93     } else {
94       @column_index = (runningnumber, partnumber, description, qty);
95     }
96   } else {
97     if (   ($form->{type} =~ /purchase_order/)
98         || ($form->{type} =~ /sales_order/)) {
99       @column_index = (runningnumber, partnumber, description, ship, qty);
100         } else {
101       @column_index = (runningnumber, partnumber, description, qty);
102     }
103   }
104 ############## ENDE Neueintrag ##################
105
106   my $dimension_units = AM->retrieve_units(\%myconfig, $form, "dimension");
107   my $service_units = AM->retrieve_units(\%myconfig, $form, "service");
108   my $all_units = AM->retrieve_units(\%myconfig, $form);
109
110   push @column_index, qw(unit);
111
112   #for pricegroups column
113   if (   $form->{type} =~ (/sales_quotation/)
114       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
115       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
116       or ($form->{type} =~ /sales_order/)) {
117     push @column_index, qw(sellprice_pg);
118   }
119
120   push @column_index, qw(sellprice);
121
122   if ($form->{vc} eq 'customer') {
123     push @column_index, qw(discount);
124   }
125
126   push @column_index, "linetotal";
127
128   my $colspan = $#column_index + 1;
129
130   $form->{invsubtotal} = 0;
131   map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
132
133 ########################################
134   # Eintrag fuer Version 2.2.0 geaendert #
135   # neue Optik im Rechnungsformular      #
136 ########################################
137   $column_data{runningnumber} =
138       qq|<th align=left nowrap width=5 class=listheading>|
139     . $locale->text('No.')
140     . qq|</th>|;
141   $column_data{partnumber} =
142       qq|<th align=left nowrap width=12 class=listheading>|
143     . $locale->text('Number')
144     . qq|</th>|;
145   $column_data{description} =
146       qq|<th align=left nowrap width=30 class=listheading>|
147     . $locale->text('Part Description')
148     . qq|</th>|;
149   $column_data{ship} =
150       qq|<th align=left nowrap width=5 class=listheading>|
151     . $locale->text('Ship')
152     . qq|</th>|;
153   $column_data{qty} =
154       qq|<th align=left nowrap width=5 class=listheading>|
155     . $locale->text('Qty')
156     . qq|</th>|;
157   $column_data{unit} =
158       qq|<th align=left nowrap width=5 class=listheading>|
159     . $locale->text('Unit')
160     . qq|</th>|;
161   $column_data{license} =
162       qq|<th align=left nowrap width=10 class=listheading>|
163     . $locale->text('License')
164     . qq|</th>|;
165   $column_data{serialnr} =
166       qq|<th align=left nowrap width=10 class=listheading>|
167     . $locale->text('Serial No.')
168     . qq|</th>|;
169   $column_data{projectnr} =
170       qq|<th align=left nowrap width=10 class=listheading>|
171     . $locale->text('Project')
172     . qq|</th>|;
173   $column_data{sellprice} =
174       qq|<th align=left nowrap width=15 class=listheading>|
175     . $locale->text('Price')
176     . qq|</th>|;
177   $column_data{sellprice_pg} =
178       qq|<th align=left nowrap width=15 class=listheading>|
179     . $locale->text('Pricegroup')
180     . qq|</th>|;
181   $column_data{discount} =
182       qq|<th align=left class=listheading>|
183     . $locale->text('Discount')
184     . qq|</th>|;
185   $column_data{linetotal} =
186       qq|<th align=left nowrap width=10 class=listheading>|
187     . $locale->text('Extended')
188     . qq|</th>|;
189   $column_data{bin} =
190       qq|<th align=left nowrap width=10 class=listheading>|
191     . $locale->text('Bin')
192     . qq|</th>|;
193 ############## ENDE Neueintrag ##################
194
195   print qq|
196   <tr>
197     <td>
198       <table width=100%>
199         <tr class=listheading>|;
200
201   map { print "\n$column_data{$_}" } @column_index;
202
203   print qq|
204         </tr>
205 |;
206
207   $runningnumber = $locale->text('No.');
208   $deliverydate  = $locale->text('Delivery Date');
209   $serialnumber  = $locale->text('Serial No.');
210   $projectnumber = $locale->text('Project');
211   $partsgroup    = $locale->text('Group');
212   $reqdate       = $locale->text('Reqdate');
213
214   $delvar = 'deliverydate';
215
216   if ($form->{type} =~ /_order$/ || $form->{type} =~ /_quotation$/) {
217     $deliverydate = $locale->text('Required by');
218     $delvar       = 'reqdate';
219   }
220
221   for $i (1 .. $numrows) {
222
223     # undo formatting
224     map {
225       $form->{"${_}_$i"} =
226         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
227     } qw(qty ship discount sellprice price_new price_old) unless ($form->{simple_save});
228
229     if (!$form->{"unit_old_$i"}) {
230       # Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
231       # Einheit, wie sie in den Stammdaten hinterlegt wurde.
232       # Es sollte also angenommen werden, dass diese ausgewaehlt war.
233       $form->{"unit_old_$i"} = $form->{"unit_$i"};
234     }
235
236
237
238     # Die zuletzt ausgewaehlte mit der aktuell ausgewaehlten Einheit
239     # vergleichen und bei Unterschied den Preis entsprechend umrechnen.
240     $form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
241
242     my $check_units = $form->{"inventory_accno_$i"} ? $dimension_units : $service_units;
243     if (!$check_units->{$form->{"selected_unit_$i"}} ||
244         ($check_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
245          $all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
246       # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
247       # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
248       # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
249       $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};
250     }
251     if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
252       if ($form->{"unit_old_$i"} ne $form->{"selected_unit_$i"}) {
253         my $basefactor = 1;
254         if (defined($all_units->{$form->{"unit_old_$i"}}->{"factor"}) &&
255             $all_units->{$form->{"unit_old_$i"}}->{"factor"}) {
256           $basefactor = $all_units->{$form->{"selected_unit_$i"}}->{"factor"} /
257             $all_units->{$form->{"unit_old_$i"}}->{"factor"};
258         }
259         $form->{"sellprice_$i"} *= $basefactor;
260         $form->{"unit_old_$i"} = $form->{"selected_unit_$i"};
261       }
262     }
263     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
264     $dec           = length $dec;
265     $decimalplaces = ($dec > 2) ? $dec : 2;
266
267     $discount =
268       $form->round_amount(
269                         $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100,
270                         $decimalplaces);
271
272     $linetotal =
273       $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
274     $linetotal = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
275
276     # convert " to &quot;
277     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
278       qw(partnumber description unit unit_old);
279
280 ########################################
281     # Eintrag fuer Version 2.2.0 geaendert #
282     # neue Optik im Rechnungsformular      #
283 ########################################
284     $column_data{runningnumber} =
285       qq|<td><input name="runningnumber_$i" size=5 value=$i></td>|;    # HuT
286 ############## ENDE Neueintrag ##################
287
288     $column_data{partnumber} =
289       qq|<td><input name="partnumber_$i" size=12 value="$form->{"partnumber_$i"}"></td>|;
290
291     if (($rows = $form->numtextrows($form->{"description_$i"}, 30, 6)) > 1) {
292       $column_data{description} =
293         qq|<td><textarea name="description_$i" rows=$rows cols=30 wrap=soft>$form->{"description_$i"}</textarea><button type="button" onclick="set_longdescription_window('longdescription_$i')">| . $locale->text('L') . qq|</button></td>|;
294     } else {
295       $column_data{description} =
296         qq|<td><input name="description_$i" size=30 value="$form->{"description_$i"}"><button type="button" onclick="set_longdescription_window('longdescription_$i')">| . $locale->text('L') . qq|</button></td>|;
297     }
298
299     (my $qty_dec) = ($form->{"qty_$i"} =~ /\.(\d+)/);
300     $qty_dec = length $qty_dec;
301
302     $column_data{qty} =
303         qq|<td align=right><input name="qty_$i" size=5 value=|
304       . $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec) .qq|>|;
305     if ($form->{"formel_$i"}) {
306     $column_data{qty} .= qq|<button type="button" onclick="calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)">| . $locale->text('*/') . qq|</button>
307           <input type=hidden name="formel_$i" value="$form->{"formel_$i"}"><input type=hidden name="alu_$i" value="$form->{"alu_$i"}"></td>|;
308     }
309     $column_data{ship} =
310         qq|<td align=right><input name="ship_$i" size=5 value=|
311       . $form->format_amount(\%myconfig, $form->{"ship_$i"})
312       . qq|></td>|;
313
314     my $is_part = $form->{"inventory_accno_$i"};
315     my $is_assigned = $form->{"id_$i"};
316     my $this_unit = $form->{"unit_$i"};
317     if ($form->{"selected_unit_$i"} && $this_unit &&
318         $all_units->{$form->{"selected_unit_$i"}} && $all_units->{$this_unit} &&
319         ($all_units->{$form->{"selected_unit_$i"}}->{"base_unit"} eq $all_units->{$this_unit}->{"base_unit"})) {
320       $this_unit = $form->{"selected_unit_$i"};
321     } elsif (!$is_assigned ||
322              ($is_part && !$this_unit && ($all_units->{$this_unit} && ($all_units->{$this_unit}->{"base_unit"} eq $all_units->{"kg"}->{"base_unit"})))) {
323       $this_unit = "kg";
324     }
325
326     $column_data{"unit"} = "<td>" .
327       ($qty_readonly ? "&nbsp;" :
328        AM->unit_select_html($is_part ? $dimension_units :
329                             $is_assigned ? $service_units : $all_units,
330                             "unit_$i", $this_unit,
331                             $is_assigned ? $form->{"unit_$i"} : undef))
332       . "</td>";
333
334     # build in drop down list for pricesgroups
335     if ($form->{"prices_$i"}) {
336       if  ($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"}) {
337         $price_tmp = $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces);
338       } else {
339         $price_tmp = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
340       }
341
342       $column_data{sellprice_pg} =
343         qq|<td align=right><select name="sellprice_pg_$i">$form->{"prices_$i"}</select></td>|;
344       $column_data{sellprice} =
345         qq|<td><input name="sellprice_$i" size=10 value=$price_tmp></td>|;
346     } else {
347
348       # for last row and report
349       # set pricegroup drop down list from report menu
350       if ($form->{"sellprice_$i"} != 0) {
351         $prices =
352           qq|<option value="$form->{"sellprice_$i"}--$form->{"pricegroup_id_$i"}" selected>$form->{"pricegroup_$i"}</option>\n|;
353
354         $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
355
356         $column_data{sellprice_pg} =
357           qq|<td align=right><select name="sellprice_pg_$i">$prices</select></td>|;
358
359       } else {
360
361         # for last row
362         $column_data{sellprice_pg} = qq|<td align=right>&nbsp;</td>|;
363       }
364
365       $column_data{sellprice} =
366         qq|<td><input name="sellprice_$i" size=10 value=|
367         . $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
368                                $decimalplaces)
369         . qq|></td>|;
370     }
371     $column_data{discount} =
372         qq|<td align=right><input name="discount_$i" size=3 value=|
373       . $form->format_amount(\%myconfig, $form->{"discount_$i"})
374       . qq|></td>|;
375     $column_data{linetotal} =
376         qq|<td align=right>|
377       . $form->format_amount(\%myconfig, $linetotal, 2)
378       . qq|</td>|;
379     $column_data{bin} = qq|<td>$form->{"bin_$i"}</td>|;
380
381 ########################################
382     # Eintrag fuer Version 2.2.0 geaendert #
383     # neue Optik im Rechnungsformular      #
384 ########################################
385     #     if ($lizenzen &&  $form->{type} eq "invoice" &&  $form->{vc} eq "customer") {
386     #     $column_data{license} = qq|<td><select name="licensenumber_$i">$form->{"lizenzen_$i"}></select></td>|;
387     #     }
388     #
389     #     if ($form->{type} !~ /_quotation/) {
390     #     $column_data{serialnr} = qq|<td><input name="serialnumber_$i" size=10 value="$form->{"serialnumber_$i"}"></td>|;
391     #     }
392     #
393     #     $column_data{projectnr} = qq|<td><input name="projectnumber_$i" size=10 value="$form->{"projectnumber_$i"}"></td>|;
394 ############## ENDE Neueintrag ##################
395     my $j = $i % 2;
396     print qq|
397
398         <tr valign=top class=listrow$j>|;
399
400     map { print "\n$column_data{$_}" } @column_index;
401
402     print qq|
403         </tr>
404
405 <input type=hidden name="orderitems_id_$i" value=$form->{"orderitems_id_$i"}>
406 <input type=hidden name="bo_$i" value=$form->{"bo_$i"}>
407
408 <input type=hidden name="pricegroup_old_$i" value=$form->{"pricegroup_old_$i"}>
409 <input type=hidden name="price_old_$i" value=$form->{"price_old_$i"}>
410 <input type=hidden name="unit_old_$i" value="$form->{"selected_unit_$i"}">
411 <input type=hidden name="price_new_$i" value=|
412       . $form->format_amount(\%myconfig, $form->{"price_new_$i"}) . qq|>
413
414 <input type=hidden name="id_$i" value=$form->{"id_$i"}>
415 <input type=hidden name="inventory_accno_$i" value=$form->{"inventory_accno_$i"}>
416 <input type=hidden name="bin_$i" value="$form->{"bin_$i"}">
417 <input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">
418 <input type=hidden name="partnotes_$i" value="$form->{"partnotes_$i"}">
419 <input type=hidden name="income_accno_$i" value=$form->{"income_accno_$i"}>
420 <input type=hidden name="expense_accno_$i" value=$form->{"expense_accno_$i"}>
421 <input type=hidden name="listprice_$i" value="$form->{"listprice_$i"}">
422 <input type=hidden name="assembly_$i" value="$form->{"assembly_$i"}">
423 <input type=hidden name="taxaccounts_$i" value="$form->{"taxaccounts_$i"}">
424 <input type=hidden name="ordnumber_$i" value="$form->{"ordnumber_$i"}">
425 <input type=hidden name="transdate_$i" value="$form->{"transdate_$i"}">
426 <input type=hidden name="cusordnumber_$i" value="$form->{"cusordnumber_$i"}">
427 <input type=hidden name="longdescription_$i" value="$form->{"longdescription_$i"}">
428 <input type=hidden name="basefactor_$i" value="$form->{"basefactor_$i"}">
429
430 |;
431
432 ########################################
433     # Eintrag fuer Version 2.2.0 geaendert #
434     # neue Optik im Rechnungsformular      #
435 ########################################
436     # print second row
437     print qq|
438         <tr  class=listrow$j>
439           <td colspan=$colspan>
440 |;
441     if ($lizenzen && $form->{type} eq "invoice" && $form->{vc} eq "customer") {
442       my $selected = $form->{"licensenumber_$i"};
443       my $lizenzen_quoted;
444       $form->{"lizenzen_$i"} =~ s/ selected//g;
445       $form->{"lizenzen_$i"} =~
446         s/value="${selected}"\>/value="${selected}" selected\>/;
447       $lizenzen_quoted = $form->{"lizenzen_$i"};
448       $lizenzen_quoted =~ s/\"/&quot;/g;
449       print qq|
450         <b>Lizenz\#</b>&nbsp;<select name="licensenumber_$i" size=1>
451         $form->{"lizenzen_$i"}
452         </select>
453         <input type=hidden name="lizenzen_$i" value="${lizenzen_quoted}">
454 |;
455     }
456     if ($form->{type} !~ /_quotation/) {
457       print qq|
458           <b>$serialnumber</b>&nbsp;<input name="serialnumber_$i" size=15 value="$form->{"serialnumber_$i"}">|;
459     }
460
461     print qq|
462           <b>$projectnumber</b>&nbsp;<input name="projectnumber_$i" size=10 value="$form->{"projectnumber_$i"}">
463                   <input type=hidden name="oldprojectnumber_$i" value="$form->{"oldprojectnumber_$i"}">
464                   <input type=hidden name="project_id_$i" value="$form->{"project_id_$i"}">
465 |;
466     if ($form->{type} eq 'invoice' or $form->{type} =~ /order/) {
467       my $reqdate_term =
468         ($form->{type} eq 'invoice')
469         ? 'deliverydate'
470         : 'reqdate';    # invoice uses a different term for the same thing.
471       print qq|
472         <b>${$reqdate_term}</b>&nbsp;<input name="${reqdate_term}_$i" size=11 value="$form->{"${reqdate_term}_$i"}">
473 |;
474     }
475     my $subtotalchecked = ($form->{"subtotal_$i"}) ? "checked" : "";
476     print qq|
477           <b>|.$locale->text('Subtotal').qq|</b>&nbsp;<input type="checkbox" name="subtotal_$i" value="1" "$subtotalchecked">
478           </td>
479         </tr>
480
481 |;
482
483 ############## ENDE Neueintrag ##################
484
485     map { $form->{"${_}_base"} += $linetotal }
486       (split / /, $form->{"taxaccounts_$i"});
487
488     $form->{invsubtotal} += $linetotal;
489   }
490
491   print qq|
492       </table>
493     </td>
494   </tr>
495 |;
496
497   $lxdebug->leave_sub();
498 }
499
500 ##################################################
501 # build html-code for pricegroups in variable $form->{prices_$j}
502
503 sub set_pricegroup {
504   $lxdebug->enter_sub();
505   my $rowcount = shift;
506   for $j (1 .. $rowcount) {
507     my $pricegroup_old = $form->{"pricegroup_old_$i"};
508     if ($form->{PRICES}{$j}) {
509       $len    = 0;
510       $prices = '<option value="--">' . $locale->text("none (pricegroup)") . '</option>';
511       $price  = 0;
512       foreach $item (@{ $form->{PRICES}{$j} }) {
513
514         #$price = $form->round_amount($myconfig,  $item->{price}, 5);
515         #$price = $form->format_amount($myconfig, $item->{price}, 2);
516         $price         = $item->{price};
517         $pricegroup_id = $item->{pricegroup_id};
518         $pricegroup    = $item->{pricegroup};
519
520         # build drop down list for pricegroups
521         $prices .=
522           qq|<option value="$price--$pricegroup_id"$item->{selected}>$pricegroup</option>\n|;
523
524         $len += 1;
525
526         #        map {
527         #               $form->{"${_}_$j"} =
528         #               $form->format_amount(\%myconfig, $form->{"${_}_$j"})
529         #              } qw(sellprice price_new price_old);
530
531         # set new selectedpricegroup_id and prices for "Preis"
532         if ($item->{selected} && ($pricegroup_id != 0)) {
533           $form->{"pricegroup_old_$j"} = $pricegroup_id;
534           $form->{"price_new_$j"}      = $price;
535           $form->{"sellprice_$j"}      = $price;
536         }
537         if ($pricegroup_id == 0) {
538           $form->{"price_new_$j"} = $form->{"sellprice_$j"};
539         }
540       }
541       $form->{"prices_$j"} = $prices;
542     }
543   }
544   $lxdebug->leave_sub();
545 }
546
547 sub select_item {
548   $lxdebug->enter_sub();
549   @column_index = qw(ndx partnumber description onhand sellprice);
550
551   $column_data{ndx}        = qq|<th>&nbsp;</th>|;
552   $column_data{partnumber} =
553     qq|<th class=listheading>| . $locale->text('Number') . qq|</th>|;
554   $column_data{description} =
555     qq|<th class=listheading>| . $locale->text('Part Description') . qq|</th>|;
556   $column_data{sellprice} =
557     qq|<th class=listheading>| . $locale->text('Price') . qq|</th>|;
558   $column_data{onhand} =
559     qq|<th class=listheading>| . $locale->text('Qty') . qq|</th>|;
560
561   # list items with radio button on a form
562   $form->header;
563
564   $title   = $locale->text('Select from one of the items below');
565   $colspan = $#column_index + 1;
566
567   print qq|
568 <body>
569
570 <form method=post action=$form->{script}>
571
572 <table width=100%>
573   <tr>
574     <th class=listtop colspan=$colspan>$title</th>
575   </tr>
576   <tr height="5"></tr>
577   <tr class=listheading>|;
578
579   map { print "\n$column_data{$_}" } @column_index;
580
581   print qq|</tr>|;
582
583   my $i = 0;
584   foreach $ref (@{ $form->{item_list} }) {
585     $checked = ($i++) ? "" : "checked";
586
587     if ($lizenzen) {
588       if ($ref->{inventory_accno} > 0) {
589         $ref->{"lizenzen"} = qq|<option></option>|;
590         foreach $item (@{ $form->{LIZENZEN}{ $ref->{"id"} } }) {
591           $ref->{"lizenzen"} .=
592             qq|<option value=\"$item->{"id"}\">$item->{"licensenumber"}</option>|;
593         }
594         $ref->{"lizenzen"} .= qq|<option value=-1>Neue Lizenz</option>|;
595         $ref->{"lizenzen"} =~ s/\"/&quot;/g;
596       }
597     }
598
599     map { $ref->{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
600
601     #sk tradediscount
602     $ref->{sellprice} =
603       $form->round_amount($ref->{sellprice} * (1 - $form->{tradediscount}), 2);
604     $column_data{ndx} =
605       qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
606     $column_data{partnumber} =
607       qq|<td><input name="new_partnumber_$i" type=hidden value="$ref->{partnumber}">$ref->{partnumber}</td>|;
608     $column_data{description} =
609       qq|<td><input name="new_description_$i" type=hidden value="$ref->{description}">$ref->{description}</td>|;
610     $column_data{sellprice} =
611       qq|<td align=right><input name="new_sellprice_$i" type=hidden value=$ref->{sellprice}>|
612       . $form->format_amount(\%myconfig, $ref->{sellprice}, 2, "&nbsp;")
613       . qq|</td>|;
614     $column_data{onhand} =
615       qq|<td align=right><input name="new_onhand_$i" type=hidden value=$ref->{onhand}>|
616       . $form->format_amount(\%myconfig, $ref->{onhand}, '', "&nbsp;")
617       . qq|</td>|;
618
619     $j++;
620     $j %= 2;
621     print qq|
622 <tr class=listrow$j>|;
623
624     map { print "\n$column_data{$_}" } @column_index;
625
626     print qq|
627 </tr>
628
629 <input name="new_bin_$i" type=hidden value="$ref->{bin}">
630 <input name="new_listprice_$i" type=hidden value=$ref->{listprice}>
631 <input name="new_inventory_accno_$i" type=hidden value=$ref->{inventory_accno}>
632 <input name="new_income_accno_$i" type=hidden value=$ref->{income_accno}>
633 <input name="new_expense_accno_$i" type=hidden value=$ref->{expense_accno}>
634 <input name="new_unit_$i" type=hidden value="$ref->{unit}">
635 <input name="new_weight_$i" type=hidden value="$ref->{weight}">
636 <input name="new_assembly_$i" type=hidden value="$ref->{assembly}">
637 <input name="new_taxaccounts_$i" type=hidden value="$ref->{taxaccounts}">
638 <input name="new_partsgroup_$i" type=hidden value="$ref->{partsgroup}">
639 <input name="new_formel_$i" type=hidden value="$ref->{formel}">
640 <input name="new_longdescription_$i" type=hidden value="$ref->{longdescription}">
641 <input name="new_not_discountable_$i" type=hidden value="$ref->{not_discountable}">
642 <input name="new_part_payment_id_$i" type=hidden value="$ref->{part_payment_id}">
643
644 <input name="new_id_$i" type=hidden value=$ref->{id}>
645
646 |;
647     if ($lizenzen) {
648       print qq|
649 <input name="new_lizenzen_$i" type=hidden value="$ref->{lizenzen}">
650 |;
651     }
652
653   }
654
655   print qq|
656 <tr><td colspan=8><hr size=3 noshade></td></tr>
657 </table>
658
659 <input name=lastndx type=hidden value=$i>
660
661 |;
662
663   # delete action variable
664   map { delete $form->{$_} } qw(action item_list header);
665
666   # save all other form variables
667   foreach $key (keys %${form}) {
668     $form->{$key} =~ s/\"/&quot;/g;
669     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
670   }
671
672   print qq|
673 <input type=hidden name=nextsub value=item_selected>
674
675 <br>
676 <input class=submit type=submit name=action value="|
677     . $locale->text('Continue') . qq|">
678 </form>
679
680 </body>
681 </html>
682 |;
683
684   $lxdebug->leave_sub();
685 }
686
687 sub item_selected {
688   $lxdebug->enter_sub();
689
690   # replace the last row with the checked row
691   $i = $form->{rowcount};
692   $i = $form->{assembly_rows} if ($form->{item} eq 'assembly');
693
694   # index for new item
695   $j = $form->{ndx};
696
697   #sk
698   #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
699   #$form->{"sellprice_$i"} = $form->{"sellprice_$i"};
700
701   # if there was a price entered, override it
702   $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
703
704   map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} }
705     qw(id partnumber description sellprice listprice inventory_accno income_accno expense_accno bin unit weight assembly taxaccounts partsgroup formel longdescription not_discountable);
706   if ($form->{"part_payment_id_$i"} ne "") {
707     $form->{payment_id} = $form->{"part_payment_id_$i"};
708   }
709
710   if ($lizenzen) {
711     map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } qw(lizenzen);
712   }
713
714   ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
715   $dec           = length $dec;
716   $decimalplaces = ($dec > 2) ? $dec : 2;
717
718   if ($sellprice) {
719     $form->{"sellprice_$i"} = $sellprice;
720   } else {
721
722     # if there is an exchange rate adjust sellprice
723     if (($form->{exchangerate} * 1) != 0) {
724       $form->{"sellprice_$i"} /= $form->{exchangerate};
725       $form->{"sellprice_$i"} =
726         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
727     }
728   }
729
730   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
731     qw(sellprice listprice weight);
732
733   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
734   $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
735   
736   if ($form->{"not_discountable_$i"}) {
737     $form->{"discount_$i"} = 0;
738   }
739
740   $amount =
741     $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
742     $form->{"qty_$i"};
743   map { $form->{"${_}_base"} += $amount }
744     (split / /, $form->{"taxaccounts_$i"});
745   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
746     $form->{"taxaccounts_$i"}
747     if !$form->{taxincluded};
748
749   $form->{creditremaining} -= $amount;
750
751   $form->{"runningnumber_$i"} = $i;
752
753   # delete all the new_ variables
754   for $i (1 .. $form->{lastndx}) {
755     map { delete $form->{"new_${_}_$i"} }
756       qw(partnumber description sellprice bin listprice inventory_accno income_accno expense_accno unit assembly taxaccounts id);
757   }
758
759   map { delete $form->{$_} } qw(ndx lastndx nextsub);
760
761   # format amounts
762   map {
763     $form->{"${_}_$i"} =
764       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
765   } qw(sellprice listprice) if $form->{item} ne 'assembly';
766
767   # get pricegroups for parts
768   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
769
770   # build up html code for prices_$i
771   set_pricegroup($form->{rowcount});
772
773   &display_form;
774
775   $lxdebug->leave_sub();
776 }
777
778 sub new_item {
779   $lxdebug->enter_sub();
780
781   # change callback
782   $form->{old_callback} = $form->escape($form->{callback}, 1);
783   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
784
785   # delete action
786   delete $form->{action};
787
788   # save all other form variables in a previousform variable
789   foreach $key (keys %$form) {
790
791     # escape ampersands
792     $form->{$key} =~ s/&/%26/g;
793     $previousform .= qq|$key=$form->{$key}&|;
794   }
795   chop $previousform;
796   $previousform = $form->escape($previousform, 1);
797
798   $i = $form->{rowcount};
799   map { $form->{"${_}_$i"} =~ s/\"/&quot;/g } qw(partnumber description);
800
801   $form->header;
802
803   print qq|
804 <body>
805
806 <h4 class=error>| . $locale->text('Item not on file!') . qq|
807
808 <p>
809 | . $locale->text('What type of item is this?') . qq|</h4>
810
811 <form method=post action=ic.pl>
812
813 <p>
814
815   <input class=radio type=radio name=item value=part checked>&nbsp;|
816     . $locale->text('Part') . qq|<br>
817   <input class=radio type=radio name=item value=service>&nbsp;|
818     . $locale->text('Service')
819
820     . qq|
821 <input type=hidden name=previousform value="$previousform">
822 <input type=hidden name=partnumber value="$form->{"partnumber_$i"}">
823 <input type=hidden name=description value="$form->{"description_$i"}">
824 <input type=hidden name=rowcount value=$form->{rowcount}>
825 <input type=hidden name=taxaccount2 value=$form->{taxaccounts}>
826 <input type=hidden name=vc value=$form->{vc}>
827
828 <input type=hidden name=path value=$form->{path}>
829 <input type=hidden name=login value=$form->{login}>
830 <input type=hidden name=password value=$form->{password}>
831
832 <input type=hidden name=nextsub value=add>
833
834 <p>
835 <input class=submit type=submit name=action value="|
836     . $locale->text('Continue') . qq|">
837 </form>
838
839 </body>
840 </html>
841 |;
842
843   $lxdebug->leave_sub();
844 }
845
846 sub display_form {
847   $lxdebug->enter_sub();
848
849   relink_accounts();
850
851   $form->language_payment(\%myconfig);
852
853   # if we have a display_form
854   if ($form->{display_form}) {
855     &{"$form->{display_form}"};
856     exit;
857   }
858
859   #   if (   $form->{print_and_post}
860   #       && $form->{second_run}
861   #       && ($form->{action} eq "display_form")) {
862   #     for (keys %$form) { $old_form->{$_} = $form->{$_} }
863   #     $old_form->{rowcount}++;
864   #
865   #     #$form->{rowcount}--;
866   #     #$form->{rowcount}--;
867   #
868   #     $form->{print_and_post} = 0;
869   #
870   #     &print_form($old_form);
871   #     exit;
872   #   }
873   #
874   #   $form->{action}   = "";
875   #   $form->{resubmit} = 0;
876   #
877   #   if ($form->{print_and_post} && !$form->{second_run}) {
878   #     $form->{second_run} = 1;
879   #     $form->{action}     = "display_form";
880   #     $form->{rowcount}--;
881   #     my $rowcount = $form->{rowcount};
882   #
883   #     # get pricegroups for parts
884   #     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
885   #
886   #     # build up html code for prices_$i
887   #     set_pricegroup($rowcount);
888   #
889   #     $form->{resubmit} = 1;
890   #
891   #   }
892   &form_header;
893
894   $numrows    = ++$form->{rowcount};
895   $subroutine = "display_row";
896
897   if ($form->{item} eq 'part') {
898
899     #set preisgruppenanzahl
900     $numrows    = $form->{price_rows};
901     $subroutine = "price_row";
902
903     &{$subroutine}($numrows);
904
905     $numrows    = ++$form->{makemodel_rows};
906     $subroutine = "makemodel_row";
907   }
908   if ($form->{item} eq 'assembly') {
909     $numrows    = $form->{price_rows};
910     $subroutine = "price_row";
911
912     &{$subroutine}($numrows);
913
914     $numrows    = ++$form->{makemodel_rows};
915     $subroutine = "makemodel_row";
916
917     # create makemodel rows
918     &{$subroutine}($numrows);
919
920     $numrows    = ++$form->{assembly_rows};
921     $subroutine = "assembly_row";
922   }
923   if ($form->{item} eq 'service') {
924     $numrows    = $form->{price_rows};
925     $subroutine = "price_row";
926
927     &{$subroutine}($numrows);
928
929     $numrows = 0;
930   }
931
932   # create rows
933   &{$subroutine}($numrows) if $numrows;
934
935   &form_footer;
936
937   $lxdebug->leave_sub();
938 }
939
940 sub check_form {
941   $lxdebug->enter_sub();
942   my @a     = ();
943   my $count = 0;
944   my @flds  = (
945     qw(id partnumber description qty ship sellprice unit discount inventory_accno income_accno expense_accno listprice taxaccounts bin assembly weight projectnumber project_id oldprojectnumber runningnumber serialnumber partsgroup payment_id not_discountable shop ve gv buchungsgruppen_id language_values)
946   );
947
948
949   # remove any makes or model rows
950   if ($form->{item} eq 'part') {
951     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
952       qw(listprice sellprice lastcost weight rop);
953
954     @flds = (make, model);
955     for my $i (1 .. ($form->{makemodel_rows})) {
956       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
957         push @a, {};
958         my $j = $#a;
959
960         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
961         $count++;
962       }
963     }
964
965     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
966     $form->{makemodel_rows} = $count;
967
968   } elsif ($form->{item} eq 'assembly') {
969
970     $form->{sellprice} = 0;
971     $form->{weight}    = 0;
972     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
973       qw(listprice rop stock);
974
975     @flds =
976       qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup);
977
978     for my $i (1 .. ($form->{assembly_rows} - 1)) {
979       if ($form->{"qty_$i"}) {
980         push @a, {};
981         my $j = $#a;
982
983         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
984
985         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
986
987         #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
988
989         $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"});
990         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"});
991         $count++;
992       }
993     }
994
995     $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
996
997     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
998     $form->{assembly_rows} = $count;
999
1000     $count = 0;
1001     @flds  = qw(make model);
1002     @a     = ();
1003
1004     for my $i (1 .. ($form->{makemodel_rows})) {
1005       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
1006         push @a, {};
1007         my $j = $#a;
1008
1009         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1010         $count++;
1011       }
1012     }
1013
1014     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
1015     $form->{makemodel_rows} = $count;
1016
1017   } else {
1018
1019     # this section applies to invoices and orders
1020     # remove any empty numbers
1021     if ($form->{rowcount}) {
1022       for my $i (1 .. $form->{rowcount} - 1) {
1023         if ($form->{"partnumber_$i"}) {
1024           push @a, {};
1025           my $j = $#a;
1026
1027           map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1028           $count++;
1029           if ($lizenzen) {
1030             if ($form->{"licensenumber_$i"} == -1) {
1031               &new_license($i);
1032               exit;
1033             }
1034           }
1035         }
1036       }
1037
1038       $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1039       $form->{rowcount} = $count;
1040
1041       $form->{creditremaining} -= &invoicetotal;
1042
1043     }
1044   }
1045
1046   #sk
1047   # if pricegroups
1048   if (   $form->{type} =~ (/sales_quotation/)
1049       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
1050       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
1051       or ($form->{type} =~ /sales_order/)) {
1052
1053     # get pricegroups for parts
1054     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1055
1056     # build up html code for prices_$i
1057     set_pricegroup($form->{rowcount});
1058
1059   }
1060
1061   &display_form;
1062
1063   $lxdebug->leave_sub();
1064 }
1065
1066 sub invoicetotal {
1067   $lxdebug->enter_sub();
1068
1069   $form->{oldinvtotal} = 0;
1070
1071   # add all parts and deduct paid
1072   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
1073
1074   my ($amount, $sellprice, $discount, $qty);
1075
1076   for my $i (1 .. $form->{rowcount}) {
1077     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1078     $discount  = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
1079     $qty       = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1080
1081     #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
1082
1083     $amount = $sellprice * (1 - $discount / 100) * $qty;
1084     map { $form->{"${_}_base"} += $amount }
1085       (split / /, $form->{"taxaccounts_$i"});
1086     $form->{oldinvtotal} += $amount;
1087   }
1088
1089   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1090     split / /, $form->{taxaccounts}
1091     if !$form->{taxincluded};
1092
1093   $form->{oldtotalpaid} = 0;
1094   for $i (1 .. $form->{paidaccounts}) {
1095     $form->{oldtotalpaid} += $form->{"paid_$i"};
1096   }
1097
1098   $lxdebug->leave_sub();
1099
1100   # return total
1101   return ($form->{oldinvtotal} - $form->{oldtotalpaid});
1102 }
1103
1104 sub validate_items {
1105   $lxdebug->enter_sub();
1106
1107   # check if items are valid
1108   if ($form->{rowcount} == 1) {
1109     &update;
1110     exit;
1111   }
1112
1113   for $i (1 .. $form->{rowcount} - 1) {
1114     $form->isblank("partnumber_$i",
1115                    $locale->text('Number missing in Row') . " $i");
1116   }
1117
1118   $lxdebug->leave_sub();
1119 }
1120
1121 sub order {
1122   $lxdebug->enter_sub();
1123   if ($form->{second_run}) {
1124     $form->{print_and_post} = 0;
1125   }
1126   $form->{ordnumber} = $form->{invnumber};
1127
1128   map { delete $form->{$_} } qw(id printed emailed queued);
1129   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
1130     $form->{title} = $locale->text('Add Purchase Order');
1131     $form->{vc}    = 'vendor';
1132     $form->{type}  = 'purchase_order';
1133     $buysell       = 'sell';
1134   }
1135   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
1136     $form->{title} = $locale->text('Add Sales Order');
1137     $form->{vc}    = 'customer';
1138     $form->{type}  = 'sales_order';
1139     $buysell       = 'buy';
1140   }
1141   $form->{script} = 'oe.pl';
1142
1143   $form->{shipto} = 1;
1144
1145   $form->{rowcount}--;
1146
1147   $form->{cp_id} *= 1;
1148
1149   require "$form->{path}/$form->{script}";
1150
1151   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1152
1153   $currency = $form->{currency};
1154
1155   &order_links;
1156
1157   $form->{currency}     = $currency;
1158   $form->{exchangerate} = "";
1159   $form->{forex}        = "";
1160   $form->{exchangerate} = $exchangerate
1161     if (
1162         $form->{forex} = (
1163                   $exchangerate =
1164                     $form->check_exchangerate(
1165                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1166                     )));
1167
1168   &prepare_order;
1169   &display_form;
1170
1171   $lxdebug->leave_sub();
1172 }
1173
1174 sub quotation {
1175   $lxdebug->enter_sub();
1176   if ($form->{second_run}) {
1177     $form->{print_and_post} = 0;
1178   }
1179   map { delete $form->{$_} } qw(id printed emailed queued);
1180
1181   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
1182     $form->{title} = $locale->text('Add Request for Quotation');
1183     $form->{vc}    = 'vendor';
1184     $form->{type}  = 'request_quotation';
1185     $buysell       = 'sell';
1186   }
1187   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
1188     $form->{title} = $locale->text('Add Quotation');
1189     $form->{vc}    = 'customer';
1190     $form->{type}  = 'sales_quotation';
1191     $buysell       = 'buy';
1192   }
1193
1194   $form->{cp_id} *= 1;
1195
1196   $form->{script} = 'oe.pl';
1197
1198   $form->{shipto} = 1;
1199
1200   $form->{rowcount}--;
1201
1202   require "$form->{path}/$form->{script}";
1203
1204   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1205
1206   $currency = $form->{currency};
1207
1208   &order_links;
1209
1210   $form->{currency}     = $currency;
1211   $form->{exchangerate} = "";
1212   $form->{forex}        = "";
1213   $form->{exchangerate} = $exchangerate
1214     if (
1215         $form->{forex} = (
1216                   $exchangerate =
1217                     $form->check_exchangerate(
1218                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1219                     )));
1220
1221   &prepare_order;
1222   &display_form;
1223
1224   $lxdebug->leave_sub();
1225 }
1226
1227 sub e_mail {
1228   $lxdebug->enter_sub();
1229   if ($form->{second_run}) {
1230     $form->{print_and_post} = 0;
1231     $form->{resubmit}       = 0;
1232   }
1233   if ($myconfig{role} eq 'admin') {
1234     $bcc = qq|
1235           <th align=right nowrap=true>| . $locale->text('Bcc') . qq|</th>
1236           <td><input name=bcc size=30 value="$form->{bcc}"></td>
1237 |;
1238   }
1239
1240   if ($form->{formname} =~ /(pick|packing|bin)_list/) {
1241     $form->{email} = $form->{shiptoemail} if $form->{shiptoemail};
1242   }
1243
1244   $name = $form->{ $form->{vc} };
1245   $name =~ s/--.*//g;
1246   $title = $locale->text('E-mail') . " $name";
1247
1248   $form->{oldmedia} = $form->{media};
1249   $form->{media}    = "email";
1250
1251   $form->header;
1252
1253   print qq|
1254 <body>
1255
1256 <form method=post action=$form->{script}>
1257
1258 <table width=100%>
1259   <tr class=listtop>
1260     <th class=listtop>$title</th>
1261   </tr>
1262   <tr height="5"></tr>
1263   <tr>
1264     <td>
1265       <table width=100%>
1266         <tr>
1267           <th align=right nowrap>| . $locale->text('To') . qq|</th>
1268           <td><input name=email size=30 value="$form->{email}"></td>
1269           <th align=right nowrap>| . $locale->text('Cc') . qq|</th>
1270           <td><input name=cc size=30 value="$form->{cc}"></td>
1271         </tr>
1272         <tr>
1273           <th align=right nowrap>| . $locale->text('Subject') . qq|</th>
1274           <td><input name=subject size=30 value="$form->{subject}"></td>
1275           $bcc
1276         </tr>
1277       </table>
1278     </td>
1279   </tr>
1280   <tr>
1281     <td>
1282       <table width=100%>
1283         <tr>
1284           <th align=left nowrap>| . $locale->text('Message') . qq|</th>
1285         </tr>
1286         <tr>
1287           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
1288         </tr>
1289       </table>
1290     </td>
1291   </tr>
1292   <tr>
1293     <td>
1294 |;
1295
1296   &print_options;
1297
1298   map { delete $form->{$_} }
1299     qw(action email cc bcc subject message formname sendmode format header override);
1300
1301   # save all other variables
1302   foreach $key (keys %$form) {
1303     $form->{$key} =~ s/\"/&quot;/g;
1304     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1305   }
1306
1307   print qq|
1308     </td>
1309   </tr>
1310   <tr>
1311     <td><hr size=3 noshade></td>
1312   </tr>
1313 </table>
1314
1315 <input type=hidden name=nextsub value=send_email>
1316
1317 <br>
1318 <input name=action class=submit type=submit value="|
1319     . $locale->text('Continue') . qq|">
1320 </form>
1321
1322 </body>
1323 </html>
1324 |;
1325
1326   $lxdebug->leave_sub();
1327 }
1328
1329 sub send_email {
1330   $lxdebug->enter_sub();
1331
1332   $old_form = new Form;
1333
1334   map { $old_form->{$_} = $form->{$_} } keys %$form;
1335   $old_form->{media} = $form->{oldmedia};
1336
1337   &print_form($old_form);
1338
1339   $lxdebug->leave_sub();
1340 }
1341
1342 sub print_options {
1343   $lxdebug->enter_sub();
1344   $form->{sendmode} = "attachment";
1345   $form->{copies}   = 3 unless $form->{copies};
1346
1347   $form->{PD}{ $form->{formname} } = "selected";
1348   $form->{DF}{ $form->{format} }   = "selected";
1349   $form->{OP}{ $form->{media} }    = "selected";
1350   $form->{SM}{ $form->{sendmode} } = "selected";
1351
1352   if ($form->{type} eq 'purchase_order') {
1353     $type = qq|<select name=formname>
1354             <option value=purchase_order $form->{PD}{purchase_order}>|
1355       . $locale->text('Purchase Order') . qq|
1356             <option value=bin_list $form->{PD}{bin_list}>|
1357       . $locale->text('Bin List');
1358   }
1359
1360   if ($form->{type} eq 'credit_note') {
1361     $type = qq|<select name=formname>
1362             <option value=credit_note $form->{PD}{credit_note}>|
1363       . $locale->text('Credit Note');
1364   }
1365
1366   if ($form->{type} eq 'sales_order') {
1367     $type = qq|<select name=formname>
1368             <option value=sales_order $form->{PD}{sales_order}>|
1369       . $locale->text('Confirmation') . qq|
1370       <option value=proforma $form->{PD}{proforma}>|
1371       . $locale->text('Proforma Invoice') . qq|
1372             <option value=pick_list $form->{PD}{pick_list}>|
1373       . $locale->text('Pick List') . qq|
1374             <option value=packing_list $form->{PD}{packing_list}>|
1375       . $locale->text('Packing List');
1376   }
1377
1378   if ($form->{type} =~ /_quotation$/) {
1379     $type = qq|<select name=formname>
1380             <option value="$`_quotation" $form->{PD}{"$`_quotation"}>|
1381       . $locale->text('Quotation');
1382   }
1383
1384   if ($form->{type} eq 'invoice') {
1385     $type = qq|<select name=formname>
1386             <option value=invoice $form->{PD}{invoice}>|
1387       . $locale->text('Invoice') . qq|
1388       <option value=proforma $form->{PD}{proforma}>|
1389       . $locale->text('Proforma Invoice') . qq|
1390       <option value=packing_list $form->{PD}{packing_list}>|
1391       . $locale->text('Packing List');
1392   }
1393
1394   if ($form->{type} eq 'invoice' && $form->{storno}) {
1395     $type = qq|<select name=formname>
1396             <option value=storno_invoice $form->{PD}{storno_invoice}>|
1397       . $locale->text('Storno Invoice') . qq|
1398       <option value=storno_packing_list $form->{PD}{storno_packing_list}>|
1399       . $locale->text('Storno Packing List');
1400   }
1401
1402   if ($form->{type} eq 'credit_note') {
1403     $type = qq|<select name=formname>
1404             <option value=credit_note $form->{PD}{credit_note}>|
1405       . $locale->text('Credit Note');
1406   }
1407
1408   if ($form->{type} eq 'ship_order') {
1409     $type = qq|<select name=formname>
1410             <option value=pick_list $form->{PD}{pick_list}>|
1411       . $locale->text('Pick List') . qq|
1412             <option value=packing_list $form->{PD}{packing_list}>|
1413       . $locale->text('Packing List');
1414   }
1415
1416   if ($form->{type} eq 'receive_order') {
1417     $type = qq|<select name=formname>
1418             <option value=bin_list $form->{PD}{bin_list}>|
1419       . $locale->text('Bin List');
1420   }
1421
1422   if ($form->{media} eq 'email') {
1423     $media = qq|<select name=sendmode>
1424             <option value=attachment $form->{SM}{attachment}>|
1425       . $locale->text('Attachment') . qq|
1426             <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
1427   } else {
1428     $media = qq|<select name=media>
1429             <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
1430     if (scalar(keys (%{ $form->{printers} })) !=0 && $latex_templates) {
1431       $media .= qq|
1432             <option value=printer $form->{OP}{printer}>|
1433         . $locale->text('Printer');
1434     }
1435     if ($latex_templates) {
1436       $media .= qq|
1437             <option value=queue $form->{OP}{queue}>| . $locale->text('Queue');
1438     }
1439   }
1440
1441   $format = qq|<select name=format>|;
1442   if ($opendocument_templates && $openofficeorg_writer_bin &&
1443       $xvfb_bin && (-x $openofficeorg_writer_bin) && (-x $xvfb_bin)) {
1444     $format .= qq|<option value=opendocument_pdf | .
1445       $form->{DF}{"opendocument_pdf"} . qq|>| .
1446       $locale->text("PDF (OpenDocument/OASIS)") . qq|</option>|;
1447   }
1448
1449   if ($latex_templates) {
1450     $format .= qq|<option value=pdf $form->{DF}{pdf}>| .
1451       $locale->text('PDF') . qq|</option>|;
1452   }
1453
1454   $format .= qq|<option value=html $form->{DF}{html}>HTML</option>|;
1455
1456   if ($latex) {
1457     $format .= qq|<option value=postscript $form->{DF}{postscript}>| .
1458       $locale->text('Postscript') . qq|</option>|;
1459   }
1460
1461   if ($opendocument_templates) {
1462     $format .= qq|<option value=opendocument $form->{DF}{opendocument}>| .
1463       $locale->text("OpenDocument/OASIS") . qq|</option>|;
1464   }
1465   $format .= qq|</select>|;
1466
1467   if (scalar(keys (%{ $form->{languages} })) !=0) {
1468     $language_select = qq|<select name=language_id>
1469                 <option value=""></option>}|;
1470     foreach $item (@{ $form->{languages} }) {
1471       if ($form->{language_id} eq $item->{id}) {
1472         $language_select .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
1473       } else {
1474         $language_select .= qq|<option value="$item->{id}">$item->{description}</option>|;
1475       }
1476     }
1477   }
1478
1479   if (scalar(keys (%{ $form->{printers} })) !=0) {
1480
1481     $printer_select = qq|<select name=printer_id>
1482                   <option value=""></option>|;
1483     foreach $item (@{ $form->{printers} }) {
1484       $printer_select .= qq|<option value="$item->{id}">$item->{printer_description}</option>|;
1485     }
1486   }
1487
1488
1489
1490   print qq|
1491 <table width=100% cellspacing=0 cellpadding=0>
1492   <tr>
1493     <td>
1494       <table>
1495         <tr>
1496           <td>$type</select></td>|;
1497   if (scalar(keys (%{ $form->{languages} })) !=0) {
1498   print qq|
1499           <td>${language_select}</select></td>|;
1500   }
1501   print qq|
1502           <td>$format</select></td>
1503           <td>$media</select></td>|;
1504   if (scalar(keys (%{ $form->{printers} })) !=0) {
1505   print qq|
1506           <td>$printer_select</select></td>
1507 |;
1508   }
1509
1510   if (scalar(keys (%{ $form->{printers} })) !=0 && $latex_templates && $form->{media} ne 'email') {
1511     print qq|
1512           <td>| . $locale->text('Copies') . qq|
1513           <input name=copies size=2 value=$form->{copies}></td>
1514 |;
1515   }
1516
1517   $form->{groupitems} = "checked" if $form->{groupitems};
1518
1519   print qq|
1520           <td>| . $locale->text('Group Items') . qq|</td>
1521           <td><input name=groupitems type=checkbox class=checkbox $form->{groupitems}></td>
1522         </tr>
1523       </table>
1524     </td>
1525     <td align=right>
1526       <table>
1527         <tr>
1528 |;
1529
1530   if ($form->{printed} =~ /$form->{formname}/) {
1531     print qq|
1532           <th>\|| . $locale->text('Printed') . qq|\|</th>
1533 |;
1534   }
1535
1536   if ($form->{emailed} =~ /$form->{formname}/) {
1537     print qq|
1538           <th>\|| . $locale->text('E-mailed') . qq|\|</th>
1539 |;
1540   }
1541
1542   if ($form->{queued} =~ /$form->{formname}/) {
1543     print qq|
1544           <th>\|| . $locale->text('Queued') . qq|\|</th>
1545 |;
1546   }
1547
1548   print qq|
1549         </tr>
1550       </table>
1551     </td>
1552   </tr>
1553 </table>
1554 |;
1555
1556   $lxdebug->leave_sub();
1557 }
1558
1559 sub print {
1560   $lxdebug->enter_sub();
1561
1562   # if this goes to the printer pass through
1563   if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1564     $form->error($locale->text('Select postscript or PDF!'))
1565       if ($form->{format} !~ /(postscript|pdf)/);
1566
1567     $old_form = new Form;
1568     map { $old_form->{$_} = $form->{$_} } keys %$form;
1569   }
1570
1571   if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1572     if ($form->{formname} eq "proforma") {
1573       $form->{proforma} = 1;
1574     }
1575     $form->{print_and_save} = 1;
1576     my $formname = $form->{formname};
1577     &save();
1578     $form->{formname} = $formname;
1579     &edit();
1580     exit;
1581   }
1582
1583   &print_form($old_form);
1584
1585   $lxdebug->leave_sub();
1586 }
1587
1588 sub print_form {
1589   $lxdebug->enter_sub();
1590   my ($old_form) = @_;
1591
1592   $inv       = "inv";
1593   $due       = "due";
1594   $numberfld = "invnumber";
1595
1596   $display_form =
1597     ($form->{display_form}) ? $form->{display_form} : "display_form";
1598
1599   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1600   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1601
1602   if ($form->{formname} eq "invoice") {
1603     $form->{label} = $locale->text('Invoice');
1604   }
1605   if ($form->{formname} eq "packing_list") {
1606
1607     # this is from an invoice
1608     $form->{label} = $locale->text('Packing List');
1609   }
1610   if ($form->{formname} eq 'sales_order') {
1611     $inv                  = "ord";
1612     $due                  = "req";
1613     $form->{"${inv}date"} = $form->{transdate};
1614     $form->{label}        = $locale->text('Sales Order');
1615     $numberfld            = "sonumber";
1616     $order                = 1;
1617   }
1618
1619   if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1620     $inv                  = "inv";
1621     $due                  = "due";
1622     $form->{"${inv}date"} = $form->{transdate};
1623     $form->{"invdate"}    = $form->{transdate};
1624     $form->{label}        = $locale->text('Proforma Invoice');
1625     $numberfld            = "sonumber";
1626     $order                = 0;
1627   }
1628
1629   if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1630     $inv                  = "inv";
1631     $due                  = "due";
1632     $form->{"${inv}date"} = $form->{transdate};
1633     $form->{"invdate"}    = $form->{transdate};
1634     $form->{invnumber}    = $form->{ordnumber};
1635     $form->{label}        = $locale->text('Proforma Invoice');
1636     $numberfld            = "sonumber";
1637     $order                = 1;
1638   }
1639
1640   if ($form->{formname} eq 'packing_list' && $form->{type} ne 'invoice') {
1641
1642     # we use the same packing list as from an invoice
1643     $inv = "ord";
1644     $due = "req";
1645     $form->{invdate} = $form->{"${inv}date"} = $form->{transdate};
1646     $form->{label} = $locale->text('Packing List');
1647     $order = 1;
1648     # set invnumber for template packing_list 
1649     $form->{invnumber}   = $form->{ordnumber};
1650   }
1651   if ($form->{formname} eq 'pick_list') {
1652     $inv                  = "ord";
1653     $due                  = "req";
1654     $form->{"${inv}date"} =
1655       ($form->{transdate}) ? $form->{transdate} : $form->{invdate};
1656     $form->{label} = $locale->text('Pick List');
1657     $order = 1 unless $form->{type} eq 'invoice';
1658   }
1659   if ($form->{formname} eq 'purchase_order') {
1660     $inv                  = "ord";
1661     $due                  = "req";
1662     $form->{"${inv}date"} = $form->{transdate};
1663     $form->{label}        = $locale->text('Purchase Order');
1664     $numberfld            = "ponumber";
1665     $order                = 1;
1666   }
1667   if ($form->{formname} eq 'bin_list') {
1668     $inv                  = "ord";
1669     $due                  = "req";
1670     $form->{"${inv}date"} = $form->{transdate};
1671     $form->{label}        = $locale->text('Bin List');
1672     $order                = 1;
1673   }
1674   if ($form->{formname} eq 'sales_quotation') {
1675     $inv                  = "quo";
1676     $due                  = "req";
1677     $form->{"${inv}date"} = $form->{transdate};
1678     $form->{label}        = $locale->text('Quotation');
1679     $numberfld            = "sqnumber";
1680     $order                = 1;
1681   }
1682
1683   if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1684     $inv                  = "quo";
1685     $due                  = "req";
1686     $form->{"${inv}date"} = $form->{transdate};
1687     $form->{"invdate"} = $form->{transdate};
1688     $form->{label}        = $locale->text('Proforma Invoice');
1689     $numberfld            = "sqnumber";
1690     $order                = 1;
1691   }
1692
1693   if ($form->{formname} eq 'request_quotation') {
1694     $inv                  = "quo";
1695     $due                  = "req";
1696     $form->{"${inv}date"} = $form->{transdate};
1697     $form->{label}        = $locale->text('Quotation');
1698     $numberfld            = "rfqnumber";
1699     $order                = 1;
1700   }
1701
1702   $form->isblank("email", $locale->text('E-mail address missing!'))
1703     if ($form->{media} eq 'email');
1704   $form->isblank("${inv}date",
1705                  $locale->text($form->{label} . ' Date missing!'));
1706
1707   # $locale->text('Invoice Number missing!')
1708   # $locale->text('Invoice Date missing!')
1709   # $locale->text('Packing List Number missing!')
1710   # $locale->text('Packing List Date missing!')
1711   # $locale->text('Order Number missing!')
1712   # $locale->text('Order Date missing!')
1713   # $locale->text('Quotation Number missing!')
1714   # $locale->text('Quotation Date missing!')
1715
1716   # assign number
1717   if (!$form->{"${inv}number"} && !$form->{preview}) {
1718     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1719     if ($form->{media} ne 'email') {
1720
1721       # get pricegroups for parts
1722       IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1723
1724       # build up html code for prices_$i
1725       set_pricegroup($form->{rowcount});
1726
1727       $form->{rowcount}--;
1728
1729       &{"$display_form"};
1730       exit;
1731     }
1732   }
1733
1734   &validate_items;
1735
1736   # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1737   my ($saved_email, $saved_cc, $saved_bcc) =
1738     ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1739
1740   $language_saved = $form->{language_id};
1741   $payment_id_saved = $form->{payment_id};
1742
1743   &{"$form->{vc}_details"};
1744
1745   $form->{language_id} = $language_saved;
1746   $form->{payment_id} = $payment_id_saved;
1747
1748   $form->{"email"} = $saved_email if ($saved_email);
1749   $form->{"cc"}    = $saved_cc    if ($saved_cc);
1750   $form->{"bcc"}   = $saved_bcc   if ($saved_bcc);
1751
1752   # format payment dates
1753   for $i (1 .. $form->{paidaccounts} - 1) {
1754     $form->{"datepaid_$i"} = $locale->date(\%myconfig, $form->{"datepaid_$i"});
1755   }
1756
1757   ($form->{employee}) = split /--/, $form->{employee};
1758   ($form->{warehouse}, $form->{warehouse_id}) = split /--/, $form->{warehouse};
1759
1760   # create the form variables
1761   if ($order) {
1762     OE->order_details(\%myconfig, \%$form);
1763   } else {
1764     IS->invoice_details(\%myconfig, \%$form, $locale);
1765   }
1766
1767   # format global dates
1768   map { $form->{$_} = $locale->date(\%myconfig, $form->{$_}, 1) }
1769     ("${inv}date", "${due}date", "shippingdate", "deliverydate");
1770
1771   # format item dates
1772   for my $field (qw(transdate_oe deliverydate_oe)) {
1773     map {
1774       $form->{$field}[$_] = $locale->date(\%myconfig, $form->{$field}[$_], 1);
1775     } 0 .. $#{ $form->{$field} };
1776   }
1777
1778   if ($form->{shipto_id}) {
1779     $form->get_shipto(\%myconfig);
1780   }
1781
1782   @a = qw(name street zipcode city country);
1783
1784   $shipto = 1;
1785
1786   # if there is no shipto fill it in from billto
1787   foreach $item (@a) {
1788     if ($form->{"shipto$item"}) {
1789       $shipto = 0;
1790       last;
1791     }
1792   }
1793
1794   if ($shipto) {
1795     if (   $form->{formname} eq 'purchase_order'
1796         || $form->{formname} eq 'request_quotation') {
1797       $form->{shiptoname}   = $myconfig{company};
1798       $form->{shiptostreet} = $myconfig{address};
1799         } else {
1800       map { $form->{"shipto$_"} = $form->{$_} } @a;
1801     }
1802   }
1803
1804   $form->{notes} =~ s/^\s+//g;
1805
1806   map({ $form->{$_} =~ s/\\n/\n/g; } qw(company address));
1807
1808   $form->{templates} = "$myconfig{templates}";
1809
1810   $form->{language} = $form->get_template_language(\%myconfig);
1811   $form->{printer_code} = $form->get_printer_code(\%myconfig);
1812
1813   if ($form->{language} ne "") {
1814     $form->{language} = "_" . $form->{language};
1815   }
1816
1817   if ($form->{printer_code} ne "") {
1818     $form->{printer_code} = "_" . $form->{printer_code};
1819   }
1820
1821   $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
1822   if ($form->{format} eq 'postscript') {
1823     $form->{postscript} = 1;
1824     $form->{IN} =~ s/html$/tex/;
1825   } elsif ($form->{"format"} =~ /pdf/) {
1826     $form->{pdf} = 1;
1827     if ($form->{"format"} =~ /opendocument/) {
1828       $form->{IN} =~ s/html$/odt/;
1829     } else {
1830       $form->{IN} =~ s/html$/tex/;
1831     }
1832   } elsif ($form->{"format"} =~ /opendocument/) {
1833     $form->{"opendocument"} = 1;
1834     $form->{"IN"} =~ s/html$/odt/;
1835   }
1836
1837   if ($form->{media} eq 'printer') {
1838     $form->{OUT} = "| $form->{printer_command} &>/dev/null";
1839     $form->{printed} .= " $form->{formname}";
1840     $form->{printed} =~ s/^ //;
1841   }
1842   $printed = $form->{printed};
1843
1844   if ($form->{media} eq 'email') {
1845     $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1846       unless $form->{subject};
1847
1848     $form->{OUT} = "$sendmail";
1849
1850     $form->{emailed} .= " $form->{formname}";
1851     $form->{emailed} =~ s/^ //;
1852   }
1853   $emailed = $form->{emailed};
1854
1855   if ($form->{media} eq 'queue') {
1856     %queued = split / /, $form->{queued};
1857
1858     if ($filename = $queued{ $form->{formname} }) {
1859       $form->{queued} =~ s/$form->{formname} $filename//;
1860       unlink "$spool/$filename";
1861       $filename =~ s/\..*$//g;
1862     } else {
1863       $filename = time;
1864       $filename .= $$;
1865     }
1866
1867     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
1868     $form->{OUT} = ">$spool/$filename";
1869
1870     # add type
1871     $form->{queued} .= " $form->{formname} $filename";
1872
1873     $form->{queued} =~ s/^ //;
1874   }
1875   $queued = $form->{queued};
1876
1877   $form->parse_template(\%myconfig, $userspath);
1878
1879   $form->{callback} = "";
1880
1881   if ($form->{media} eq 'email') {
1882     $form->{message} = $locale->text('sent') unless $form->{message};
1883   }
1884   $message = $form->{message};
1885
1886   # if we got back here restore the previous form
1887   if ($form->{media} =~ /(printer|email|queue)/) {
1888
1889     $form->update_status(\%myconfig)
1890       if ($form->{media} eq 'queue' && $form->{id});
1891
1892     if ($old_form) {
1893
1894       $old_form->{"${inv}number"} = $form->{"${inv}number"};
1895
1896       # restore and display form
1897       map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1898
1899       $form->{queued}  = $queued;
1900       $form->{printed} = $printed;
1901       $form->{emailed} = $emailed;
1902       $form->{message} = $message;
1903
1904       $form->{rowcount}--;
1905       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1906         qw(exchangerate creditlimit creditremaining);
1907
1908       for $i (1 .. $form->{paidaccounts}) {
1909         map {
1910           $form->{"${_}_$i"} =
1911             $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1912         } qw(paid exchangerate);
1913       }
1914
1915       &{"$display_form"};
1916       exit;
1917     }
1918
1919     $msg =
1920       ($form->{media} eq 'printer')
1921       ? $locale->text('sent to printer')
1922       : $locale->text('emailed to') . " $form->{email}";
1923     $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1924   }
1925   if ($form->{printing}) {
1926    &{"$display_form"};
1927    exit; 
1928   }
1929
1930   $lxdebug->leave_sub();
1931 }
1932
1933 sub customer_details {
1934   $lxdebug->enter_sub();
1935   IS->customer_details(\%myconfig, \%$form);
1936   $lxdebug->leave_sub();
1937 }
1938
1939 sub vendor_details {
1940   $lxdebug->enter_sub();
1941
1942   IR->vendor_details(\%myconfig, \%$form);
1943
1944   $lxdebug->leave_sub();
1945 }
1946
1947 sub post_as_new {
1948   $lxdebug->enter_sub();
1949
1950   $form->{postasnew} = 1;
1951   map { delete $form->{$_} } qw(printed emailed queued);
1952
1953   &post;
1954
1955   $lxdebug->leave_sub();
1956 }
1957
1958 sub ship_to {
1959   $lxdebug->enter_sub();
1960   if ($form->{second_run}) {
1961     $form->{print_and_post} = 0;
1962   }
1963
1964   $title = $form->{title};
1965   $form->{title} = $locale->text('Ship to');
1966
1967   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1968     qw(exchangerate creditlimit creditremaining);
1969
1970   # get details for name
1971   &{"$form->{vc}_details"};
1972
1973   $number =
1974     ($form->{vc} eq 'customer')
1975     ? $locale->text('Customer Number')
1976     : $locale->text('Vendor Number');
1977
1978   # get pricegroups for parts
1979   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1980
1981   # build up html code for prices_$i
1982   set_pricegroup($form->{rowcount});
1983
1984   $nextsub = ($form->{display_form}) ? $form->{display_form} : "display_form";
1985
1986   $form->{rowcount}--;
1987
1988   $form->header;
1989
1990   print qq|
1991 <body>
1992
1993 <form method=post action=$form->{script}>
1994
1995 <table width=100%>
1996   <tr>
1997     <td>
1998       <table>
1999         <tr class=listheading>
2000           <th class=listheading colspan=2 width=50%>|
2001     . $locale->text('Billing Address') . qq|</th>
2002           <th class=listheading width=50%>|
2003     . $locale->text('Shipping Address') . qq|</th>
2004         </tr>
2005         <tr height="5"></tr>
2006         <tr>
2007           <th align=right nowrap>$number</th>
2008           <td>$form->{"$form->{vc}number"}</td>
2009         </tr>
2010         <tr>
2011           <th align=right nowrap>| . $locale->text('Company Name') . qq|</th>
2012           <td>$form->{name}</td>
2013           <td><input name=shiptoname size=35 value="$form->{shiptoname}"></td>
2014         </tr>
2015         <tr>
2016           <th align=right nowrap>| . $locale->text('Street') . qq|</th>
2017           <td>$form->{street}</td>
2018           <td><input name=shiptostreet size=35 value="$form->{shiptostreet}"></td>
2019         </tr>
2020         <tr>
2021           <th align=right nowrap>| . $locale->text('Zipcode') . qq|</th>
2022           <td>$form->{zipcode}</td>
2023           <td><input name=shiptozipcode size=35 value="$form->{shiptozipcode}"></td>
2024         </tr>
2025         <tr>
2026           <th align=right nowrap>| . $locale->text('City') . qq|</th>
2027           <td>$form->{city}</td>
2028           <td><input name=shiptocity size=35 value="$form->{shiptocity}"></td>
2029         </tr>
2030         <tr>
2031           <th align=right nowrap>| . $locale->text('Country') . qq|</th>
2032           <td>$form->{country}</td>
2033           <td><input name=shiptocountry size=35 value="$form->{shiptocountry}"></td>
2034         </tr>
2035         <tr>
2036           <th align=right nowrap>| . $locale->text('Contact') . qq|</th>
2037           <td>$form->{contact}</td>
2038           <td><input name=shiptocontact size=35 value="$form->{shiptocontact}"></td>
2039         </tr>
2040         <tr>
2041           <th align=right nowrap>| . $locale->text('Phone') . qq|</th>
2042           <td>$form->{"$form->{vc}phone"}</td>
2043           <td><input name=shiptophone size=20 value="$form->{shiptophone}"></td>
2044         </tr>
2045         <tr>
2046           <th align=right nowrap>| . $locale->text('Fax') . qq|</th>
2047           <td>$form->{"$form->{vc}fax"}</td>
2048           <td><input name=shiptofax size=20 value="$form->{shiptofax}"></td>
2049         </tr>
2050         <tr>
2051           <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
2052           <td>$form->{email}</td>
2053           <td><input name=shiptoemail size=35 value="$form->{shiptoemail}"></td>
2054         </tr>
2055       </table>
2056     </td>
2057   </tr>
2058 </table>
2059
2060 <input type=hidden name=nextsub value=$nextsub>
2061 |;
2062
2063   # delete shipto
2064   map { delete $form->{$_} }
2065     qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail header);
2066   $form->{title} = $title;
2067
2068   foreach $key (keys %$form) {
2069     $form->{$key} =~ s/\"/&quot;/g;
2070     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2071   }
2072
2073   print qq|
2074
2075 <hr size=3 noshade>
2076
2077 <br>
2078 <input class=submit type=submit name=action value="|
2079     . $locale->text('Continue') . qq|">
2080 </form>
2081
2082 </body>
2083 </html>
2084 |;
2085
2086   $lxdebug->leave_sub();
2087 }
2088
2089 sub new_license {
2090   $lxdebug->enter_sub();
2091
2092   my $row = shift;
2093
2094   # change callback
2095   $form->{old_callback} = $form->escape($form->{callback}, 1);
2096   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
2097   $form->{old_callback} = $form->escape($form->{old_callback}, 1);
2098
2099   # delete action
2100   delete $form->{action};
2101   $customer = $form->{customer};
2102   map { $form->{"old_$_"} = $form->{"${_}_$row"} } qw(partnumber description);
2103
2104   # save all other form variables in a previousform variable
2105   $form->{row} = $row;
2106   foreach $key (keys %$form) {
2107
2108     # escape ampersands
2109     $form->{$key} =~ s/&/%26/g;
2110     $previousform .= qq|$key=$form->{$key}&|;
2111   }
2112   chop $previousform;
2113   $previousform = $form->escape($previousform, 1);
2114
2115   $form->{script} = "licenses.pl";
2116
2117   map { $form->{$_} = $form->{"old_$_"} } qw(partnumber description);
2118   map { $form->{$_} = $form->escape($form->{$_}, 1) }
2119     qw(partnumber description);
2120   $form->{callback} =
2121     qq|$form->{script}?login=$form->{login}&path=$form->{path}&password=$form->{password}&action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&customer=$customer&partnumber=$form->{partnumber}&description=$form->{description}&previousform="$previousform"&initial=1|;
2122   $form->redirect;
2123
2124   $lxdebug->leave_sub();
2125 }
2126
2127 sub relink_accounts {
2128   $lxdebug->enter_sub();
2129
2130   $form->{"taxaccounts"} =~ s/\s*$//;
2131   $form->{"taxaccounts"} =~ s/^\s*//;
2132   foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
2133     map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
2134   }
2135   $form->{"taxaccounts"} = "";
2136
2137   for ($i = 1; $i <= $form->{"rowcount"}; $i++) {
2138     if ($form->{"id_$i"}) {
2139       IC->retrieve_taxaccounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1);
2140     }
2141   }
2142
2143   $lxdebug->leave_sub();
2144 }