d03ddad336a869d67bec9be6f800a8a366ee45d0
[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_pg);
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_pg} =
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 drop down list 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_pg} =
341         qq|<td align=right><select name="sellprice_pg_$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 drop down list 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_pg} =
355           qq|<td align=right><select name="sellprice_pg_$i">$prices</select></td>|;
356
357       } else {
358
359         # for last row
360         $column_data{sellprice_pg} = qq|<td align=right>&nbsp;</td>|;
361       }
362
363       $column_data{sellprice} =
364         qq|<td><input name="sellprice_$i" size=10 value=|
365         . $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
366                                $decimalplaces)
367         . qq|></td>|;
368     }
369     $column_data{discount} =
370         qq|<td align=right><input name="discount_$i" size=3 value=|
371       . $form->format_amount(\%myconfig, $form->{"discount_$i"})
372       . qq|></td>|;
373     $column_data{linetotal} =
374         qq|<td align=right>|
375       . $form->format_amount(\%myconfig, $linetotal, 2)
376       . qq|</td>|;
377     $column_data{bin} = qq|<td>$form->{"bin_$i"}</td>|;
378
379 ########################################
380     # Eintrag fuer Version 2.2.0 geaendert #
381     # neue Optik im Rechnungsformular      #
382 ########################################
383     #     if ($lizenzen &&  $form->{type} eq "invoice" &&  $form->{vc} eq "customer") {
384     #     $column_data{license} = qq|<td><select name="licensenumber_$i">$form->{"lizenzen_$i"}></select></td>|;
385     #     }
386     #
387     #     if ($form->{type} !~ /_quotation/) {
388     #     $column_data{serialnr} = qq|<td><input name="serialnumber_$i" size=10 value="$form->{"serialnumber_$i"}"></td>|;
389     #     }
390     #
391     #     $column_data{projectnr} = qq|<td><input name="projectnumber_$i" size=10 value="$form->{"projectnumber_$i"}"></td>|;
392 ############## ENDE Neueintrag ##################
393     my $j = $i % 2;
394     print qq|
395
396         <tr valign=top class=listrow$j>|;
397
398     map { print "\n$column_data{$_}" } @column_index;
399
400     print qq|
401         </tr>
402
403 <input type=hidden name="orderitems_id_$i" value=$form->{"orderitems_id_$i"}>
404 <input type=hidden name="bo_$i" value=$form->{"bo_$i"}>
405
406 <input type=hidden name="pricegroup_old_$i" value=$form->{"pricegroup_old_$i"}>
407 <input type=hidden name="price_old_$i" value=$form->{"price_old_$i"}>
408 <input type=hidden name="unit_old_$i" value="$form->{"selected_unit_$i"}">
409 <input type=hidden name="price_new_$i" value=|
410       . $form->format_amount(\%myconfig, $form->{"price_new_$i"}) . qq|>
411
412 <input type=hidden name="id_$i" value=$form->{"id_$i"}>
413 <input type=hidden name="inventory_accno_$i" value=$form->{"inventory_accno_$i"}>
414 <input type=hidden name="bin_$i" value="$form->{"bin_$i"}">
415 <input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">
416 <input type=hidden name="partnotes_$i" value="$form->{"partnotes_$i"}">
417 <input type=hidden name="income_accno_$i" value=$form->{"income_accno_$i"}>
418 <input type=hidden name="expense_accno_$i" value=$form->{"expense_accno_$i"}>
419 <input type=hidden name="listprice_$i" value="$form->{"listprice_$i"}">
420 <input type=hidden name="assembly_$i" value="$form->{"assembly_$i"}">
421 <input type=hidden name="taxaccounts_$i" value="$form->{"taxaccounts_$i"}">
422 <input type=hidden name="ordnumber_$i" value="$form->{"ordnumber_$i"}">
423 <input type=hidden name="transdate_$i" value="$form->{"transdate_$i"}">
424 <input type=hidden name="cusordnumber_$i" value="$form->{"cusordnumber_$i"}">
425 <input type=hidden name="longdescription_$i" value="$form->{"longdescription_$i"}">
426 <input type=hidden name="basefactor_$i" value="$form->{"basefactor_$i"}">
427 <input type=hidden name="adr_description_$i" value="$form->{"adr_description_$i"}">
428
429 |;
430
431 ########################################
432     # Eintrag fuer Version 2.2.0 geaendert #
433     # neue Optik im Rechnungsformular      #
434 ########################################
435     # print second row
436     print qq|
437         <tr  class=listrow$j>
438           <td colspan=$colspan>
439 |;
440     if ($lizenzen && $form->{type} eq "invoice" && $form->{vc} eq "customer") {
441       my $selected = $form->{"licensenumber_$i"};
442       my $lizenzen_quoted;
443       $form->{"lizenzen_$i"} =~ s/ selected//g;
444       $form->{"lizenzen_$i"} =~
445         s/value="${selected}"\>/value="${selected}" selected\>/;
446       $lizenzen_quoted = $form->{"lizenzen_$i"};
447       $lizenzen_quoted =~ s/\"/&quot;/g;
448       print qq|
449         <b>Lizenz\#</b>&nbsp;<select name="licensenumber_$i" size=1>
450         $form->{"lizenzen_$i"}
451         </select>
452         <input type=hidden name="lizenzen_$i" value="${lizenzen_quoted}">
453 |;
454     }
455     if ($form->{type} !~ /_quotation/) {
456       print qq|
457           <b>$serialnumber</b>&nbsp;<input name="serialnumber_$i" size=15 value="$form->{"serialnumber_$i"}">|;
458     }
459
460     print qq|
461           <b>$projectnumber</b>&nbsp;<input name="projectnumber_$i" size=10 value="$form->{"projectnumber_$i"}">
462                   <input type=hidden name="oldprojectnumber_$i" value="$form->{"oldprojectnumber_$i"}">
463                   <input type=hidden name="project_id_$i" value="$form->{"project_id_$i"}">
464 |;
465     if ($form->{type} eq 'invoice' or $form->{type} =~ /order/) {
466       my $reqdate_term =
467         ($form->{type} eq 'invoice')
468         ? 'deliverydate'
469         : 'reqdate';    # invoice uses a different term for the same thing.
470       print qq|
471         <b>${$reqdate_term}</b>&nbsp;<input name="${reqdate_term}_$i" size=11 value="$form->{"${reqdate_term}_$i"}">
472 |;
473     }
474     my $subtotalchecked = ($form->{"subtotal_$i"}) ? "checked" : "";
475     print qq|
476           <b>|.$locale->text('Subtotal').qq|</b>&nbsp;<input type="checkbox" name="subtotal_$i" value="1" "$subtotalchecked">
477           </td>
478         </tr>
479
480 |;
481
482 ############## ENDE Neueintrag ##################
483
484     map { $form->{"${_}_base"} += $linetotal }
485       (split / /, $form->{"taxaccounts_$i"});
486
487     $form->{invsubtotal} += $linetotal;
488   }
489
490   print qq|
491       </table>
492     </td>
493   </tr>
494 |;
495
496   $lxdebug->leave_sub();
497 }
498
499 ##################################################
500 # build html-code for pricegroups in variable $form->{prices_$j}
501
502 sub set_pricegroup {
503   $lxdebug->enter_sub();
504   my $rowcount = shift;
505   $lxdebug->dump(0, "holy luja!", $form->{PRICES});
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_alu_$i" type=hidden value="$ref->{alu}">
641 <input name="new_longdescription_$i" type=hidden value="$ref->{longdescription}">
642 <input name="new_not_discountable_$i" type=hidden value="$ref->{not_discountable}">
643 <input name="new_part_payment_id_$i" type=hidden value="$ref->{part_payment_id}">
644 <input name="new_adr_description_$i" type=hidden value="$ref->{adr_description}">
645
646 <input name="new_id_$i" type=hidden value=$ref->{id}>
647
648 |;
649     if ($lizenzen) {
650       print qq|
651 <input name="new_lizenzen_$i" type=hidden value="$ref->{lizenzen}">
652 |;
653     }
654
655   }
656
657   print qq|
658 <tr><td colspan=8><hr size=3 noshade></td></tr>
659 </table>
660
661 <input name=lastndx type=hidden value=$i>
662
663 |;
664
665   # delete action variable
666   map { delete $form->{$_} } qw(action item_list header);
667
668   # save all other form variables
669   foreach $key (keys %${form}) {
670     $form->{$key} =~ s/\"/&quot;/g;
671     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
672   }
673
674   print qq|
675 <input type=hidden name=nextsub value=item_selected>
676
677 <br>
678 <input class=submit type=submit name=action value="|
679     . $locale->text('Continue') . qq|">
680 </form>
681
682 </body>
683 </html>
684 |;
685
686   $lxdebug->leave_sub();
687 }
688
689 sub item_selected {
690   $lxdebug->enter_sub();
691
692   # replace the last row with the checked row
693   $i = $form->{rowcount};
694   $i = $form->{assembly_rows} if ($form->{item} eq 'assembly');
695
696   # index for new item
697   $j = $form->{ndx};
698
699   #sk
700   #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
701   #$form->{"sellprice_$i"} = $form->{"sellprice_$i"};
702
703   # if there was a price entered, override it
704   $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
705
706   map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} }
707     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);
708   if ($form->{"part_payment_id_$i"} ne "") {
709     $form->{payment_id} = $form->{"part_payment_id_$i"};
710   }
711
712   if ($lizenzen) {
713     map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } qw(lizenzen);
714   }
715
716   ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
717   $dec           = length $dec;
718   $decimalplaces = ($dec > 2) ? $dec : 2;
719
720   if ($sellprice) {
721     $form->{"sellprice_$i"} = $sellprice;
722   } else {
723
724     # if there is an exchange rate adjust sellprice
725     if (($form->{exchangerate} * 1) != 0) {
726       $form->{"sellprice_$i"} /= $form->{exchangerate};
727       $form->{"sellprice_$i"} =
728         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
729     }
730   }
731
732   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
733     qw(sellprice listprice weight);
734
735   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
736   $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
737   
738   if ($form->{"not_discountable_$i"}) {
739     $form->{"discount_$i"} = 0;
740   }
741
742   $amount =
743     $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
744     $form->{"qty_$i"};
745   map { $form->{"${_}_base"} += $amount }
746     (split / /, $form->{"taxaccounts_$i"});
747   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
748     $form->{"taxaccounts_$i"}
749     if !$form->{taxincluded};
750
751   $form->{creditremaining} -= $amount;
752
753   $form->{"runningnumber_$i"} = $i;
754
755   # delete all the new_ variables
756   for $i (1 .. $form->{lastndx}) {
757     map { delete $form->{"new_${_}_$i"} }
758       qw(partnumber description sellprice bin listprice inventory_accno income_accno expense_accno unit assembly taxaccounts id);
759   }
760
761   map { delete $form->{$_} } qw(ndx lastndx nextsub);
762
763   # format amounts
764   map {
765     $form->{"${_}_$i"} =
766       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
767   } qw(sellprice listprice) if $form->{item} ne 'assembly';
768
769   # get pricegroups for parts
770   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
771
772   # build up html code for prices_$i
773   set_pricegroup($form->{rowcount});
774
775   &display_form;
776
777   $lxdebug->leave_sub();
778 }
779
780 sub new_item {
781   $lxdebug->enter_sub();
782
783   # change callback
784   $form->{old_callback} = $form->escape($form->{callback}, 1);
785   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
786
787   # delete action
788   delete $form->{action};
789
790   # save all other form variables in a previousform variable
791   foreach $key (keys %$form) {
792
793     # escape ampersands
794     $form->{$key} =~ s/&/%26/g;
795     $previousform .= qq|$key=$form->{$key}&|;
796   }
797   chop $previousform;
798   $previousform = $form->escape($previousform, 1);
799
800   $i = $form->{rowcount};
801   map { $form->{"${_}_$i"} =~ s/\"/&quot;/g } qw(partnumber description);
802
803   $form->header;
804
805   print qq|
806 <body>
807
808 <h4 class=error>| . $locale->text('Item not on file!') . qq|
809
810 <p>
811 | . $locale->text('What type of item is this?') . qq|</h4>
812
813 <form method=post action=ic.pl>
814
815 <p>
816
817   <input class=radio type=radio name=item value=part checked>&nbsp;|
818     . $locale->text('Part') . qq|<br>
819   <input class=radio type=radio name=item value=service>&nbsp;|
820     . $locale->text('Service')
821
822     . qq|
823 <input type=hidden name=previousform value="$previousform">
824 <input type=hidden name=partnumber value="$form->{"partnumber_$i"}">
825 <input type=hidden name=description value="$form->{"description_$i"}">
826 <input type=hidden name=rowcount value=$form->{rowcount}>
827 <input type=hidden name=taxaccount2 value=$form->{taxaccounts}>
828 <input type=hidden name=vc value=$form->{vc}>
829
830 <input type=hidden name=path value=$form->{path}>
831 <input type=hidden name=login value=$form->{login}>
832 <input type=hidden name=password value=$form->{password}>
833
834 <input type=hidden name=nextsub value=add>
835
836 <p>
837 <input class=submit type=submit name=action value="|
838     . $locale->text('Continue') . qq|">
839 </form>
840
841 </body>
842 </html>
843 |;
844
845   $lxdebug->leave_sub();
846 }
847
848 sub display_form {
849   $lxdebug->enter_sub();
850   $form->language_payment(\%myconfig);
851
852   # if we have a display_form
853   if ($form->{display_form}) {
854     &{"$form->{display_form}"};
855     exit;
856   }
857
858   #   if (   $form->{print_and_post}
859   #       && $form->{second_run}
860   #       && ($form->{action} eq "display_form")) {
861   #     for (keys %$form) { $old_form->{$_} = $form->{$_} }
862   #     $old_form->{rowcount}++;
863   #
864   #     #$form->{rowcount}--;
865   #     #$form->{rowcount}--;
866   #
867   #     $form->{print_and_post} = 0;
868   #
869   #     &print_form($old_form);
870   #     exit;
871   #   }
872   #
873   #   $form->{action}   = "";
874   #   $form->{resubmit} = 0;
875   #
876   #   if ($form->{print_and_post} && !$form->{second_run}) {
877   #     $form->{second_run} = 1;
878   #     $form->{action}     = "display_form";
879   #     $form->{rowcount}--;
880   #     my $rowcount = $form->{rowcount};
881   #
882   #     # get pricegroups for parts
883   #     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
884   #
885   #     # build up html code for prices_$i
886   #     set_pricegroup($rowcount);
887   #
888   #     $form->{resubmit} = 1;
889   #
890   #   }
891   &form_header;
892
893   $numrows    = ++$form->{rowcount};
894   $subroutine = "display_row";
895
896   if ($form->{item} eq 'part') {
897
898     #set preisgruppenanzahl
899     $numrows    = $form->{price_rows};
900     $subroutine = "price_row";
901
902     &{$subroutine}($numrows);
903
904     $numrows    = ++$form->{makemodel_rows};
905     $subroutine = "makemodel_row";
906   }
907   if ($form->{item} eq 'assembly') {
908     $numrows    = $form->{price_rows};
909     $subroutine = "price_row";
910
911     &{$subroutine}($numrows);
912
913     $numrows    = ++$form->{makemodel_rows};
914     $subroutine = "makemodel_row";
915
916     # create makemodel rows
917     &{$subroutine}($numrows);
918
919     $numrows    = ++$form->{assembly_rows};
920     $subroutine = "assembly_row";
921   }
922   if ($form->{item} eq 'service') {
923     $numrows    = $form->{price_rows};
924     $subroutine = "price_row";
925
926     &{$subroutine}($numrows);
927
928     $numrows = 0;
929   }
930
931   # create rows
932   &{$subroutine}($numrows) if $numrows;
933
934   &form_footer;
935
936   $lxdebug->leave_sub();
937 }
938
939 sub check_form {
940   $lxdebug->enter_sub();
941   my @a     = ();
942   my $count = 0;
943   my @flds  = (
944     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)
945   );
946
947
948   # remove any makes or model rows
949   if ($form->{item} eq 'part') {
950     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
951       qw(listprice sellprice lastcost weight rop);
952
953     @flds = (make, model);
954     for my $i (1 .. ($form->{makemodel_rows})) {
955       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
956         push @a, {};
957         my $j = $#a;
958
959         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
960         $count++;
961       }
962     }
963
964     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
965     $form->{makemodel_rows} = $count;
966
967   } elsif ($form->{item} eq 'assembly') {
968
969     $form->{sellprice} = 0;
970     $form->{weight}    = 0;
971     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
972       qw(listprice rop stock);
973
974     @flds =
975       qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup);
976
977     for my $i (1 .. ($form->{assembly_rows} - 1)) {
978       if ($form->{"qty_$i"}) {
979         push @a, {};
980         my $j = $#a;
981
982         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
983
984         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
985
986         #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
987
988         $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"});
989         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"});
990         $count++;
991       }
992     }
993
994     $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
995
996     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
997     $form->{assembly_rows} = $count;
998
999     $count = 0;
1000     @flds  = qw(make model);
1001     @a     = ();
1002
1003     for my $i (1 .. ($form->{makemodel_rows})) {
1004       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
1005         push @a, {};
1006         my $j = $#a;
1007
1008         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1009         $count++;
1010       }
1011     }
1012
1013     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
1014     $form->{makemodel_rows} = $count;
1015
1016   } else {
1017
1018     # this section applies to invoices and orders
1019     # remove any empty numbers
1020     if ($form->{rowcount}) {
1021       for my $i (1 .. $form->{rowcount} - 1) {
1022         if ($form->{"partnumber_$i"}) {
1023           push @a, {};
1024           my $j = $#a;
1025
1026           map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1027           $count++;
1028           if ($lizenzen) {
1029             if ($form->{"licensenumber_$i"} == -1) {
1030               &new_license($i);
1031               exit;
1032             }
1033           }
1034         }
1035       }
1036
1037       $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1038       $form->{rowcount} = $count;
1039
1040       $form->{creditremaining} -= &invoicetotal;
1041
1042     }
1043   }
1044
1045   #sk
1046   # if pricegroups
1047   if (   $form->{type} =~ (/sales_quotation/)
1048       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
1049       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
1050       or ($form->{type} =~ /sales_order/)) {
1051
1052     # get pricegroups for parts
1053     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1054
1055     # build up html code for prices_$i
1056     set_pricegroup($form->{rowcount});
1057
1058   }
1059
1060   &display_form;
1061
1062   $lxdebug->leave_sub();
1063 }
1064
1065 sub invoicetotal {
1066   $lxdebug->enter_sub();
1067
1068   $form->{oldinvtotal} = 0;
1069
1070   # add all parts and deduct paid
1071   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
1072
1073   my ($amount, $sellprice, $discount, $qty);
1074
1075   for my $i (1 .. $form->{rowcount}) {
1076     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1077     $discount  = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
1078     $qty       = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1079
1080     #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
1081
1082     $amount = $sellprice * (1 - $discount / 100) * $qty;
1083     map { $form->{"${_}_base"} += $amount }
1084       (split / /, $form->{"taxaccounts_$i"});
1085     $form->{oldinvtotal} += $amount;
1086   }
1087
1088   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1089     split / /, $form->{taxaccounts}
1090     if !$form->{taxincluded};
1091
1092   $form->{oldtotalpaid} = 0;
1093   for $i (1 .. $form->{paidaccounts}) {
1094     $form->{oldtotalpaid} += $form->{"paid_$i"};
1095   }
1096
1097   $lxdebug->leave_sub();
1098
1099   # return total
1100   return ($form->{oldinvtotal} - $form->{oldtotalpaid});
1101 }
1102
1103 sub validate_items {
1104   $lxdebug->enter_sub();
1105
1106   # check if items are valid
1107   if ($form->{rowcount} == 1) {
1108     &update;
1109     exit;
1110   }
1111
1112   for $i (1 .. $form->{rowcount} - 1) {
1113     $form->isblank("partnumber_$i",
1114                    $locale->text('Number missing in Row') . " $i");
1115   }
1116
1117   $lxdebug->leave_sub();
1118 }
1119
1120 sub order {
1121   $lxdebug->enter_sub();
1122   if ($form->{second_run}) {
1123     $form->{print_and_post} = 0;
1124   }
1125   $form->{ordnumber} = $form->{invnumber};
1126
1127   map { delete $form->{$_} } qw(id printed emailed queued);
1128   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
1129     $form->{title} = $locale->text('Add Purchase Order');
1130     $form->{vc}    = 'vendor';
1131     $form->{type}  = 'purchase_order';
1132     $buysell       = 'sell';
1133   }
1134   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
1135     $form->{title} = $locale->text('Add Sales Order');
1136     $form->{vc}    = 'customer';
1137     $form->{type}  = 'sales_order';
1138     $buysell       = 'buy';
1139   }
1140   $form->{script} = 'oe.pl';
1141
1142   $form->{shipto} = 1;
1143
1144   $form->{rowcount}--;
1145
1146   $form->{cp_id} *= 1;
1147
1148   require "$form->{path}/$form->{script}";
1149
1150   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1151
1152   $currency = $form->{currency};
1153
1154   &order_links;
1155
1156   $form->{currency}     = $currency;
1157   $form->{exchangerate} = "";
1158   $form->{forex}        = "";
1159   $form->{exchangerate} = $exchangerate
1160     if (
1161         $form->{forex} = (
1162                   $exchangerate =
1163                     $form->check_exchangerate(
1164                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1165                     )));
1166
1167   &prepare_order;
1168   &display_form;
1169
1170   $lxdebug->leave_sub();
1171 }
1172
1173 sub quotation {
1174   $lxdebug->enter_sub();
1175   if ($form->{second_run}) {
1176     $form->{print_and_post} = 0;
1177   }
1178   map { delete $form->{$_} } qw(id printed emailed queued);
1179
1180   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
1181     $form->{title} = $locale->text('Add Request for Quotation');
1182     $form->{vc}    = 'vendor';
1183     $form->{type}  = 'request_quotation';
1184     $buysell       = 'sell';
1185   }
1186   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
1187     $form->{title} = $locale->text('Add Quotation');
1188     $form->{vc}    = 'customer';
1189     $form->{type}  = 'sales_quotation';
1190     $buysell       = 'buy';
1191   }
1192
1193   $form->{cp_id} *= 1;
1194
1195   $form->{script} = 'oe.pl';
1196
1197   $form->{shipto} = 1;
1198
1199   $form->{rowcount}--;
1200
1201   require "$form->{path}/$form->{script}";
1202
1203   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1204
1205   $currency = $form->{currency};
1206
1207   &order_links;
1208
1209   $form->{currency}     = $currency;
1210   $form->{exchangerate} = "";
1211   $form->{forex}        = "";
1212   $form->{exchangerate} = $exchangerate
1213     if (
1214         $form->{forex} = (
1215                   $exchangerate =
1216                     $form->check_exchangerate(
1217                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1218                     )));
1219
1220   &prepare_order;
1221   &display_form;
1222
1223   $lxdebug->leave_sub();
1224 }
1225
1226 sub e_mail {
1227   $lxdebug->enter_sub();
1228   if ($form->{second_run}) {
1229     $form->{print_and_post} = 0;
1230     $form->{resubmit}       = 0;
1231   }
1232   if ($myconfig{role} eq 'admin') {
1233     $bcc = qq|
1234           <th align=right nowrap=true>| . $locale->text('Bcc') . qq|</th>
1235           <td><input name=bcc size=30 value="$form->{bcc}"></td>
1236 |;
1237   }
1238
1239   if ($form->{formname} =~ /(pick|packing|bin)_list/) {
1240     $form->{email} = $form->{shiptoemail} if $form->{shiptoemail};
1241   }
1242
1243   $name = $form->{ $form->{vc} };
1244   $name =~ s/--.*//g;
1245   $title = $locale->text('E-mail') . " $name";
1246
1247   $form->{oldmedia} = $form->{media};
1248   $form->{media}    = "email";
1249
1250   $form->header;
1251
1252   print qq|
1253 <body>
1254
1255 <form method=post action=$form->{script}>
1256
1257 <table width=100%>
1258   <tr class=listtop>
1259     <th class=listtop>$title</th>
1260   </tr>
1261   <tr height="5"></tr>
1262   <tr>
1263     <td>
1264       <table width=100%>
1265         <tr>
1266           <th align=right nowrap>| . $locale->text('To') . qq|</th>
1267           <td><input name=email size=30 value="$form->{email}"></td>
1268           <th align=right nowrap>| . $locale->text('Cc') . qq|</th>
1269           <td><input name=cc size=30 value="$form->{cc}"></td>
1270         </tr>
1271         <tr>
1272           <th align=right nowrap>| . $locale->text('Subject') . qq|</th>
1273           <td><input name=subject size=30 value="$form->{subject}"></td>
1274           $bcc
1275         </tr>
1276       </table>
1277     </td>
1278   </tr>
1279   <tr>
1280     <td>
1281       <table width=100%>
1282         <tr>
1283           <th align=left nowrap>| . $locale->text('Message') . qq|</th>
1284         </tr>
1285         <tr>
1286           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
1287         </tr>
1288       </table>
1289     </td>
1290   </tr>
1291   <tr>
1292     <td>
1293 |;
1294
1295   &print_options;
1296
1297   map { delete $form->{$_} }
1298     qw(action email cc bcc subject message formname sendmode format header override);
1299
1300   # save all other variables
1301   foreach $key (keys %$form) {
1302     $form->{$key} =~ s/\"/&quot;/g;
1303     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1304   }
1305
1306   print qq|
1307     </td>
1308   </tr>
1309   <tr>
1310     <td><hr size=3 noshade></td>
1311   </tr>
1312 </table>
1313
1314 <input type=hidden name=nextsub value=send_email>
1315
1316 <br>
1317 <input name=action class=submit type=submit value="|
1318     . $locale->text('Continue') . qq|">
1319 </form>
1320
1321 </body>
1322 </html>
1323 |;
1324
1325   $lxdebug->leave_sub();
1326 }
1327
1328 sub send_email {
1329   $lxdebug->enter_sub();
1330
1331   $old_form = new Form;
1332
1333   map { $old_form->{$_} = $form->{$_} } keys %$form;
1334   $old_form->{media} = $form->{oldmedia};
1335
1336   &print_form($old_form);
1337
1338   $lxdebug->leave_sub();
1339 }
1340
1341 sub print_options {
1342   $lxdebug->enter_sub();
1343   $form->{sendmode} = "attachment";
1344   $form->{copies}   = 3 unless $form->{copies};
1345
1346   $form->{PD}{ $form->{formname} } = "selected";
1347   $form->{DF}{ $form->{format} }   = "selected";
1348   $form->{OP}{ $form->{media} }    = "selected";
1349   $form->{SM}{ $form->{sendmode} } = "selected";
1350
1351   if ($form->{type} eq 'purchase_order') {
1352     $type = qq|<select name=formname>
1353             <option value=purchase_order $form->{PD}{purchase_order}>|
1354       . $locale->text('Purchase Order') . qq|
1355             <option value=bin_list $form->{PD}{bin_list}>|
1356       . $locale->text('Bin List');
1357   }
1358
1359   if ($form->{type} eq 'credit_note') {
1360     $type = qq|<select name=formname>
1361             <option value=credit_note $form->{PD}{credit_note}>|
1362       . $locale->text('Credit Note');
1363   }
1364
1365   if ($form->{type} eq 'sales_order') {
1366     $type = qq|<select name=formname>
1367             <option value=sales_order $form->{PD}{sales_order}>|
1368       . $locale->text('Confirmation') . qq|
1369       <option value=proforma $form->{PD}{proforma}>|
1370       . $locale->text('Proforma Invoice') . qq|
1371             <option value=pick_list $form->{PD}{pick_list}>|
1372       . $locale->text('Pick List') . qq|
1373             <option value=packing_list $form->{PD}{packing_list}>|
1374       . $locale->text('Packing List') . qq|
1375             <option value=agb $form->{PD}{agb}>|
1376       . $locale->text('AGB') . qq|
1377             <option value=bunker_receipt $form->{PD}{bunker_receipt}>|
1378       . $locale->text('Bunker Receipt');
1379   }
1380
1381   if ($form->{type} =~ /_quotation$/) {
1382     $type = qq|<select name=formname>
1383             <option value="$`_quotation" $form->{PD}{"$`_quotation"}>|
1384       . $locale->text('Quotation')
1385 ;
1386   }
1387
1388   if ($form->{type} eq 'invoice') {
1389     $type = qq|<select name=formname>
1390             <option value=invoice $form->{PD}{invoice}>|
1391       . $locale->text('Invoice') . qq|
1392       <option value=proforma $form->{PD}{proforma}>|
1393       . $locale->text('Proforma Invoice') . qq|
1394       <option value=packing_list $form->{PD}{packing_list}>|
1395       . $locale->text('Packing List');
1396   }
1397
1398   if ($form->{type} eq 'invoice' && $form->{storno}) {
1399     $type = qq|<select name=formname>
1400             <option value=storno_invoice $form->{PD}{storno_invoice}>|
1401       . $locale->text('Storno Invoice') . qq|
1402       <option value=storno_packing_list $form->{PD}{storno_packing_list}>|
1403       . $locale->text('Storno Packing List');
1404   }
1405
1406   if ($form->{type} eq 'credit_note') {
1407     $type = qq|<select name=formname>
1408             <option value=credit_note $form->{PD}{credit_note}>|
1409       . $locale->text('Credit Note');
1410   }
1411
1412   if ($form->{type} eq 'ship_order') {
1413     $type = qq|<select name=formname>
1414             <option value=pick_list $form->{PD}{pick_list}>|
1415       . $locale->text('Pick List') . qq|
1416             <option value=packing_list $form->{PD}{packing_list}>|
1417       . $locale->text('Packing List');
1418   }
1419
1420   if ($form->{type} eq 'receive_order') {
1421     $type = qq|<select name=formname>
1422             <option value=bin_list $form->{PD}{bin_list}>|
1423       . $locale->text('Bin List');
1424   }
1425
1426   if ($form->{media} eq 'email') {
1427     $media = qq|<select name=sendmode>
1428             <option value=attachment $form->{SM}{attachment}>|
1429       . $locale->text('Attachment') . qq|
1430             <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
1431   } else {
1432     $media = qq|<select name=media>
1433             <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
1434     if (scalar(keys (%{ $form->{printers} })) !=0 && $latex_templates) {
1435       $media .= qq|
1436             <option value=printer $form->{OP}{printer}>|
1437         . $locale->text('Printer');
1438     }
1439     if ($latex_templates) {
1440       $media .= qq|
1441             <option value=queue $form->{OP}{queue}>| . $locale->text('Queue');
1442     }
1443   }
1444
1445   $format = qq|<select name=format>|;
1446   if ($opendocument_templates && $openofficeorg_writer_bin &&
1447       $xvfb_bin && (-x $openofficeorg_writer_bin) && (-x $xvfb_bin)) {
1448     $format .= qq|<option value=opendocument_pdf | .
1449       $form->{DF}{"opendocument_pdf"} . qq|>| .
1450       $locale->text("PDF (OpenDocument/OASIS)") . qq|</option>|;
1451   }
1452
1453   if ($latex_templates) {
1454     $format .= qq|<option value=pdf $form->{DF}{pdf}>| .
1455       $locale->text('PDF') . qq|</option>|;
1456   }
1457
1458   $format .= qq|<option value=html $form->{DF}{html}>HTML</option>|;
1459
1460   if ($latex) {
1461     $format .= qq|<option value=postscript $form->{DF}{postscript}>| .
1462       $locale->text('Postscript') . qq|</option>|;
1463   }
1464
1465   if ($opendocument_templates) {
1466     $format .= qq|<option value=opendocument $form->{DF}{opendocument}>| .
1467       $locale->text("OpenDocument/OASIS") . qq|</option>|;
1468   }
1469   $format .= qq|</select>|;
1470
1471   if (scalar(keys (%{ $form->{languages} })) !=0) {
1472
1473     $language_select = qq|<select name=language_id>|;
1474     foreach $item (@{ $form->{languages} }) {
1475       if ($form->{language_id} eq $item->{id}) {
1476         $language_select .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
1477       } else {
1478         $language_select .= qq|<option value="$item->{id}">$item->{description}</option>|;
1479       }
1480     }
1481   }
1482
1483   if (scalar(keys (%{ $form->{printers} })) !=0) {
1484
1485     $printer_select = qq|<select name=printer_id>
1486                   <option value=""></option>|;
1487     foreach $item (@{ $form->{printers} }) {
1488       $printer_select .= qq|<option value="$item->{id}">$item->{printer_description}</option>|;
1489     }
1490   }
1491
1492
1493
1494   print qq|
1495 <table width=100% cellspacing=0 cellpadding=0>
1496   <tr>
1497     <td>
1498       <table>
1499         <tr>
1500           <td>$type</select></td>|;
1501   if (scalar(keys (%{ $form->{languages} })) !=0) {
1502   print qq|
1503           <td>${language_select}</select></td>|;
1504   }
1505   print qq|
1506           <td>$format</select></td>
1507           <td>$media</select></td>|;
1508   if (scalar(keys (%{ $form->{printers} })) !=0) {
1509   print qq|
1510           <td>$printer_select</select></td>
1511 |;
1512   }
1513
1514   if (scalar(keys (%{ $form->{printers} })) !=0 && $latex_templates && $form->{media} ne 'email') {
1515     print qq|
1516           <td>| . $locale->text('Copies') . qq|
1517           <input name=copies size=2 value=$form->{copies}></td>
1518 |;
1519   }
1520
1521   $form->{groupitems} = "checked" if $form->{groupitems};
1522
1523   print qq|
1524           <td>| . $locale->text('Group Items') . qq|</td>
1525           <td><input name=groupitems type=checkbox class=checkbox $form->{groupitems}></td>
1526         </tr>
1527       </table>
1528     </td>
1529     <td align=right>
1530       <table>
1531         <tr>
1532 |;
1533
1534   if ($form->{printed} =~ /$form->{formname}/) {
1535     print qq|
1536           <th>\|| . $locale->text('Printed') . qq|\|</th>
1537 |;
1538   }
1539
1540   if ($form->{emailed} =~ /$form->{formname}/) {
1541     print qq|
1542           <th>\|| . $locale->text('E-mailed') . qq|\|</th>
1543 |;
1544   }
1545
1546   if ($form->{queued} =~ /$form->{formname}/) {
1547     print qq|
1548           <th>\|| . $locale->text('Queued') . qq|\|</th>
1549 |;
1550   }
1551
1552   print qq|
1553         </tr>
1554       </table>
1555     </td>
1556   </tr>
1557 </table>
1558 |;
1559
1560   $lxdebug->leave_sub();
1561 }
1562
1563 sub print {
1564   $lxdebug->enter_sub();
1565
1566   # if this goes to the printer pass through
1567   if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1568     $form->error($locale->text('Select postscript or PDF!'))
1569       if ($form->{format} !~ /(postscript|pdf)/);
1570
1571     $old_form = new Form;
1572     map { $old_form->{$_} = $form->{$_} } keys %$form;
1573   }
1574
1575   if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1576     if ($form->{formname} eq "proforma") {
1577       $form->{proforma} = 1;
1578     }
1579     $form->{print_and_save} = 1;
1580     my $formname = $form->{formname};
1581     &save();
1582     $form->{formname} = $formname;
1583     &edit();
1584     exit;
1585   }
1586
1587   &print_form($old_form);
1588
1589   $lxdebug->leave_sub();
1590 }
1591
1592 sub print_form {
1593   $lxdebug->enter_sub();
1594   my ($old_form) = @_;
1595
1596   $inv       = "inv";
1597   $due       = "due";
1598   $numberfld = "invnumber";
1599
1600   $display_form =
1601     ($form->{display_form}) ? $form->{display_form} : "display_form";
1602
1603   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1604   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1605
1606   if ($form->{formname} eq "invoice") {
1607     $form->{label} = $locale->text('Invoice');
1608   }
1609   if ($form->{formname} eq "packing_list") {
1610
1611     # this is from an invoice
1612     $form->{label} = $locale->text('Packing List');
1613   }
1614   if (($form->{formname} eq 'sales_order') || ($form->{formname} eq 'agb') || ($form->{formname} eq 'bunker_receipt')) {
1615     $inv                  = "ord";
1616     $due                  = "req";
1617     $form->{"${inv}date"} = $form->{transdate};
1618     $form->{label}        = $locale->text('Sales Order');
1619     $numberfld            = "sonumber";
1620     $order                = 1;
1621   }
1622
1623   if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1624     $inv                  = "inv";
1625     $due                  = "due";
1626     $form->{"${inv}date"} = $form->{transdate};
1627     $form->{"invdate"}    = $form->{transdate};
1628     $form->{label}        = $locale->text('Proforma Invoice');
1629     $numberfld            = "sonumber";
1630     $order                = 0;
1631   }
1632
1633   if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1634     $inv                  = "inv";
1635     $due                  = "due";
1636     $form->{"${inv}date"} = $form->{transdate};
1637     $form->{"invdate"}    = $form->{transdate};
1638     $form->{invnumber}    = $form->{ordnumber};
1639     $form->{label}        = $locale->text('Proforma Invoice');
1640     $numberfld            = "sonumber";
1641     $order                = 1;
1642   }
1643
1644   if ($form->{formname} eq 'packing_list' && $form->{type} ne 'invoice') {
1645
1646     # we use the same packing list as from an invoice
1647     $inv = "ord";
1648     $due = "req";
1649     $form->{invdate} = $form->{"${inv}date"} = $form->{transdate};
1650     $form->{label} = $locale->text('Packing List');
1651     $order = 1;
1652     # set invnumber for template packing_list 
1653     $form->{invnumber}   = $form->{ordnumber};
1654   }
1655   if ($form->{formname} eq 'pick_list') {
1656     $inv                  = "ord";
1657     $due                  = "req";
1658     $form->{"${inv}date"} =
1659       ($form->{transdate}) ? $form->{transdate} : $form->{invdate};
1660     $form->{label} = $locale->text('Pick List');
1661     $order = 1 unless $form->{type} eq 'invoice';
1662   }
1663   if ($form->{formname} eq 'purchase_order') {
1664     $inv                  = "ord";
1665     $due                  = "req";
1666     $form->{"${inv}date"} = $form->{transdate};
1667     $form->{label}        = $locale->text('Purchase Order');
1668     $numberfld            = "ponumber";
1669     $order                = 1;
1670   }
1671   if ($form->{formname} eq 'bin_list') {
1672     $inv                  = "ord";
1673     $due                  = "req";
1674     $form->{"${inv}date"} = $form->{transdate};
1675     $form->{label}        = $locale->text('Bin List');
1676     $order                = 1;
1677   }
1678   if ($form->{formname} eq 'sales_quotation') {
1679     $inv                  = "quo";
1680     $due                  = "req";
1681     $form->{"${inv}date"} = $form->{transdate};
1682     $form->{label}        = $locale->text('Quotation');
1683     $numberfld            = "sqnumber";
1684     $order                = 1;
1685   }
1686
1687   if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1688     $inv                  = "quo";
1689     $due                  = "req";
1690     $form->{"${inv}date"} = $form->{transdate};
1691     $form->{"invdate"} = $form->{transdate};
1692     $form->{label}        = $locale->text('Proforma Invoice');
1693     $numberfld            = "sqnumber";
1694     $order                = 1;
1695   }
1696
1697   if ($form->{formname} eq 'request_quotation') {
1698     $inv                  = "quo";
1699     $due                  = "req";
1700     $form->{"${inv}date"} = $form->{transdate};
1701     $form->{label}        = $locale->text('Quotation');
1702     $numberfld            = "rfqnumber";
1703     $order                = 1;
1704   }
1705
1706   $form->isblank("email", $locale->text('E-mail address missing!'))
1707     if ($form->{media} eq 'email');
1708   $form->isblank("${inv}date",
1709                  $locale->text($form->{label} . ' Date missing!'));
1710
1711   # $locale->text('Invoice Number missing!')
1712   # $locale->text('Invoice Date missing!')
1713   # $locale->text('Packing List Number missing!')
1714   # $locale->text('Packing List Date missing!')
1715   # $locale->text('Order Number missing!')
1716   # $locale->text('Order Date missing!')
1717   # $locale->text('Quotation Number missing!')
1718   # $locale->text('Quotation Date missing!')
1719
1720   # assign number
1721   if (!$form->{"${inv}number"} && !$form->{preview}) {
1722     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1723     if ($form->{media} ne 'email') {
1724
1725       # get pricegroups for parts
1726       IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1727
1728       # build up html code for prices_$i
1729       set_pricegroup($form->{rowcount});
1730
1731       $form->{rowcount}--;
1732
1733       &{"$display_form"};
1734       exit;
1735     }
1736   }
1737
1738   &validate_items;
1739
1740   # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1741   my ($saved_email, $saved_cc, $saved_bcc) =
1742     ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1743
1744   $language_saved = $form->{language_id};
1745   &{"$form->{vc}_details"};
1746   $form->{language_id} = $language_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