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