bbdbeafbafa8d706147bb1421c1c6262ff0a7471
[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     $lxdebug->message(0, "klaus1 for j $j");
509     if ($form->{PRICES}{$j}) {
510       $lxdebug->message(0, "klaus2");
511       $len    = 0;
512       $prices = '<option value="--">' . $locale->text("none (pricegroup)") . '</option>';
513       $price  = 0;
514       foreach $item (@{ $form->{PRICES}{$j} }) {
515
516         #$price = $form->round_amount($myconfig,  $item->{price}, 5);
517         #$price = $form->format_amount($myconfig, $item->{price}, 2);
518         $price         = $item->{price};
519         $pricegroup_id = $item->{pricegroup_id};
520         $pricegroup    = $item->{pricegroup};
521
522         # build drop down list for pricegroups
523         $prices .=
524           qq|<option value="$price--$pricegroup_id"$item->{selected}>$pricegroup</option>\n|;
525
526         $len += 1;
527
528         #        map {
529         #               $form->{"${_}_$j"} =
530         #               $form->format_amount(\%myconfig, $form->{"${_}_$j"})
531         #              } qw(sellprice price_new price_old);
532
533         # set new selectedpricegroup_id and prices for "Preis"
534         if ($item->{selected} && ($pricegroup_id != 0)) {
535           $form->{"pricegroup_old_$j"} = $pricegroup_id;
536           $form->{"price_new_$j"}      = $price;
537           $form->{"sellprice_$j"}      = $price;
538         }
539         if ($pricegroup_id == 0) {
540           $form->{"price_new_$j"} = $form->{"sellprice_$j"};
541         }
542       }
543       $form->{"prices_$j"} = $prices;
544     }
545   }
546   $lxdebug->leave_sub();
547 }
548
549 sub select_item {
550   $lxdebug->enter_sub();
551   @column_index = qw(ndx partnumber description onhand sellprice);
552
553   $column_data{ndx}        = qq|<th>&nbsp;</th>|;
554   $column_data{partnumber} =
555     qq|<th class=listheading>| . $locale->text('Number') . qq|</th>|;
556   $column_data{description} =
557     qq|<th class=listheading>| . $locale->text('Part Description') . qq|</th>|;
558   $column_data{sellprice} =
559     qq|<th class=listheading>| . $locale->text('Price') . qq|</th>|;
560   $column_data{onhand} =
561     qq|<th class=listheading>| . $locale->text('Qty') . qq|</th>|;
562
563   # list items with radio button on a form
564   $form->header;
565
566   $title   = $locale->text('Select from one of the items below');
567   $colspan = $#column_index + 1;
568
569   print qq|
570 <body>
571
572 <form method=post action=$form->{script}>
573
574 <table width=100%>
575   <tr>
576     <th class=listtop colspan=$colspan>$title</th>
577   </tr>
578   <tr height="5"></tr>
579   <tr class=listheading>|;
580
581   map { print "\n$column_data{$_}" } @column_index;
582
583   print qq|</tr>|;
584
585   my $i = 0;
586   foreach $ref (@{ $form->{item_list} }) {
587     $checked = ($i++) ? "" : "checked";
588
589     if ($lizenzen) {
590       if ($ref->{inventory_accno} > 0) {
591         $ref->{"lizenzen"} = qq|<option></option>|;
592         foreach $item (@{ $form->{LIZENZEN}{ $ref->{"id"} } }) {
593           $ref->{"lizenzen"} .=
594             qq|<option value=\"$item->{"id"}\">$item->{"licensenumber"}</option>|;
595         }
596         $ref->{"lizenzen"} .= qq|<option value=-1>Neue Lizenz</option>|;
597         $ref->{"lizenzen"} =~ s/\"/&quot;/g;
598       }
599     }
600
601     map { $ref->{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
602
603     #sk tradediscount
604     $ref->{sellprice} =
605       $form->round_amount($ref->{sellprice} * (1 - $form->{tradediscount}), 2);
606     $column_data{ndx} =
607       qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
608     $column_data{partnumber} =
609       qq|<td><input name="new_partnumber_$i" type=hidden value="$ref->{partnumber}">$ref->{partnumber}</td>|;
610     $column_data{description} =
611       qq|<td><input name="new_description_$i" type=hidden value="$ref->{description}">$ref->{description}</td>|;
612     $column_data{sellprice} =
613       qq|<td align=right><input name="new_sellprice_$i" type=hidden value=$ref->{sellprice}>|
614       . $form->format_amount(\%myconfig, $ref->{sellprice}, 2, "&nbsp;")
615       . qq|</td>|;
616     $column_data{onhand} =
617       qq|<td align=right><input name="new_onhand_$i" type=hidden value=$ref->{onhand}>|
618       . $form->format_amount(\%myconfig, $ref->{onhand}, '', "&nbsp;")
619       . qq|</td>|;
620
621     $j++;
622     $j %= 2;
623     print qq|
624 <tr class=listrow$j>|;
625
626     map { print "\n$column_data{$_}" } @column_index;
627
628     print qq|
629 </tr>
630
631 <input name="new_bin_$i" type=hidden value="$ref->{bin}">
632 <input name="new_listprice_$i" type=hidden value=$ref->{listprice}>
633 <input name="new_inventory_accno_$i" type=hidden value=$ref->{inventory_accno}>
634 <input name="new_income_accno_$i" type=hidden value=$ref->{income_accno}>
635 <input name="new_expense_accno_$i" type=hidden value=$ref->{expense_accno}>
636 <input name="new_unit_$i" type=hidden value="$ref->{unit}">
637 <input name="new_weight_$i" type=hidden value="$ref->{weight}">
638 <input name="new_assembly_$i" type=hidden value="$ref->{assembly}">
639 <input name="new_taxaccounts_$i" type=hidden value="$ref->{taxaccounts}">
640 <input name="new_partsgroup_$i" type=hidden value="$ref->{partsgroup}">
641 <input name="new_formel_$i" type=hidden value="$ref->{formel}">
642 <input name="new_alu_$i" type=hidden value="$ref->{alu}">
643 <input name="new_longdescription_$i" type=hidden value="$ref->{longdescription}">
644 <input name="new_not_discountable_$i" type=hidden value="$ref->{not_discountable}">
645 <input name="new_part_payment_id_$i" type=hidden value="$ref->{part_payment_id}">
646 <input name="new_adr_description_$i" type=hidden value="$ref->{adr_description}">
647
648 <input name="new_id_$i" type=hidden value=$ref->{id}>
649
650 |;
651     if ($lizenzen) {
652       print qq|
653 <input name="new_lizenzen_$i" type=hidden value="$ref->{lizenzen}">
654 |;
655     }
656
657   }
658
659   print qq|
660 <tr><td colspan=8><hr size=3 noshade></td></tr>
661 </table>
662
663 <input name=lastndx type=hidden value=$i>
664
665 |;
666
667   # delete action variable
668   map { delete $form->{$_} } qw(action item_list header);
669
670   # save all other form variables
671   foreach $key (keys %${form}) {
672     $form->{$key} =~ s/\"/&quot;/g;
673     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
674   }
675
676   print qq|
677 <input type=hidden name=nextsub value=item_selected>
678
679 <br>
680 <input class=submit type=submit name=action value="|
681     . $locale->text('Continue') . qq|">
682 </form>
683
684 </body>
685 </html>
686 |;
687
688   $lxdebug->leave_sub();
689 }
690
691 sub item_selected {
692   $lxdebug->enter_sub();
693
694   # replace the last row with the checked row
695   $i = $form->{rowcount};
696   $i = $form->{assembly_rows} if ($form->{item} eq 'assembly');
697
698   # index for new item
699   $j = $form->{ndx};
700
701   #sk
702   #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
703   #$form->{"sellprice_$i"} = $form->{"sellprice_$i"};
704
705   # if there was a price entered, override it
706   $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
707
708   map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} }
709     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);
710   if ($form->{"part_payment_id_$i"} ne "") {
711     $form->{payment_id} = $form->{"part_payment_id_$i"};
712   }
713
714   if ($lizenzen) {
715     map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } qw(lizenzen);
716   }
717
718   ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
719   $dec           = length $dec;
720   $decimalplaces = ($dec > 2) ? $dec : 2;
721
722   if ($sellprice) {
723     $form->{"sellprice_$i"} = $sellprice;
724   } else {
725
726     # if there is an exchange rate adjust sellprice
727     if (($form->{exchangerate} * 1) != 0) {
728       $form->{"sellprice_$i"} /= $form->{exchangerate};
729       $form->{"sellprice_$i"} =
730         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
731     }
732   }
733
734   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
735     qw(sellprice listprice weight);
736
737   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
738   $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
739   
740   if ($form->{"not_discountable_$i"}) {
741     $form->{"discount_$i"} = 0;
742   }
743
744   $amount =
745     $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
746     $form->{"qty_$i"};
747   map { $form->{"${_}_base"} += $amount }
748     (split / /, $form->{"taxaccounts_$i"});
749   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
750     $form->{"taxaccounts_$i"}
751     if !$form->{taxincluded};
752
753   $form->{creditremaining} -= $amount;
754
755   $form->{"runningnumber_$i"} = $i;
756
757   # delete all the new_ variables
758   for $i (1 .. $form->{lastndx}) {
759     map { delete $form->{"new_${_}_$i"} }
760       qw(partnumber description sellprice bin listprice inventory_accno income_accno expense_accno unit assembly taxaccounts id);
761   }
762
763   map { delete $form->{$_} } qw(ndx lastndx nextsub);
764
765   # format amounts
766   map {
767     $form->{"${_}_$i"} =
768       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
769   } qw(sellprice listprice) if $form->{item} ne 'assembly';
770
771   # get pricegroups for parts
772   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
773
774   # build up html code for prices_$i
775   set_pricegroup($form->{rowcount});
776
777   &display_form;
778
779   $lxdebug->leave_sub();
780 }
781
782 sub new_item {
783   $lxdebug->enter_sub();
784
785   # change callback
786   $form->{old_callback} = $form->escape($form->{callback}, 1);
787   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
788
789   # delete action
790   delete $form->{action};
791
792   # save all other form variables in a previousform variable
793   foreach $key (keys %$form) {
794
795     # escape ampersands
796     $form->{$key} =~ s/&/%26/g;
797     $previousform .= qq|$key=$form->{$key}&|;
798   }
799   chop $previousform;
800   $previousform = $form->escape($previousform, 1);
801
802   $i = $form->{rowcount};
803   map { $form->{"${_}_$i"} =~ s/\"/&quot;/g } qw(partnumber description);
804
805   $form->header;
806
807   print qq|
808 <body>
809
810 <h4 class=error>| . $locale->text('Item not on file!') . qq|
811
812 <p>
813 | . $locale->text('What type of item is this?') . qq|</h4>
814
815 <form method=post action=ic.pl>
816
817 <p>
818
819   <input class=radio type=radio name=item value=part checked>&nbsp;|
820     . $locale->text('Part') . qq|<br>
821   <input class=radio type=radio name=item value=service>&nbsp;|
822     . $locale->text('Service')
823
824     . qq|
825 <input type=hidden name=previousform value="$previousform">
826 <input type=hidden name=partnumber value="$form->{"partnumber_$i"}">
827 <input type=hidden name=description value="$form->{"description_$i"}">
828 <input type=hidden name=rowcount value=$form->{rowcount}>
829 <input type=hidden name=taxaccount2 value=$form->{taxaccounts}>
830 <input type=hidden name=vc value=$form->{vc}>
831
832 <input type=hidden name=path value=$form->{path}>
833 <input type=hidden name=login value=$form->{login}>
834 <input type=hidden name=password value=$form->{password}>
835
836 <input type=hidden name=nextsub value=add>
837
838 <p>
839 <input class=submit type=submit name=action value="|
840     . $locale->text('Continue') . qq|">
841 </form>
842
843 </body>
844 </html>
845 |;
846
847   $lxdebug->leave_sub();
848 }
849
850 sub display_form {
851   $lxdebug->enter_sub();
852   $form->language_payment(\%myconfig);
853
854   # if we have a display_form
855   if ($form->{display_form}) {
856     &{"$form->{display_form}"};
857     exit;
858   }
859
860   #   if (   $form->{print_and_post}
861   #       && $form->{second_run}
862   #       && ($form->{action} eq "display_form")) {
863   #     for (keys %$form) { $old_form->{$_} = $form->{$_} }
864   #     $old_form->{rowcount}++;
865   #
866   #     #$form->{rowcount}--;
867   #     #$form->{rowcount}--;
868   #
869   #     $form->{print_and_post} = 0;
870   #
871   #     &print_form($old_form);
872   #     exit;
873   #   }
874   #
875   #   $form->{action}   = "";
876   #   $form->{resubmit} = 0;
877   #
878   #   if ($form->{print_and_post} && !$form->{second_run}) {
879   #     $form->{second_run} = 1;
880   #     $form->{action}     = "display_form";
881   #     $form->{rowcount}--;
882   #     my $rowcount = $form->{rowcount};
883   #
884   #     # get pricegroups for parts
885   #     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
886   #
887   #     # build up html code for prices_$i
888   #     set_pricegroup($rowcount);
889   #
890   #     $form->{resubmit} = 1;
891   #
892   #   }
893   &form_header;
894
895   $numrows    = ++$form->{rowcount};
896   $subroutine = "display_row";
897
898   if ($form->{item} eq 'part') {
899
900     #set preisgruppenanzahl
901     $numrows    = $form->{price_rows};
902     $subroutine = "price_row";
903
904     &{$subroutine}($numrows);
905
906     $numrows    = ++$form->{makemodel_rows};
907     $subroutine = "makemodel_row";
908   }
909   if ($form->{item} eq 'assembly') {
910     $numrows    = $form->{price_rows};
911     $subroutine = "price_row";
912
913     &{$subroutine}($numrows);
914
915     $numrows    = ++$form->{makemodel_rows};
916     $subroutine = "makemodel_row";
917
918     # create makemodel rows
919     &{$subroutine}($numrows);
920
921     $numrows    = ++$form->{assembly_rows};
922     $subroutine = "assembly_row";
923   }
924   if ($form->{item} eq 'service') {
925     $numrows    = $form->{price_rows};
926     $subroutine = "price_row";
927
928     &{$subroutine}($numrows);
929
930     $numrows = 0;
931   }
932
933   # create rows
934   &{$subroutine}($numrows) if $numrows;
935
936   &form_footer;
937
938   $lxdebug->leave_sub();
939 }
940
941 sub check_form {
942   $lxdebug->enter_sub();
943   my @a     = ();
944   my $count = 0;
945   my @flds  = (
946     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)
947   );
948
949
950   # remove any makes or model rows
951   if ($form->{item} eq 'part') {
952     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
953       qw(listprice sellprice lastcost weight rop);
954
955     @flds = (make, model);
956     for my $i (1 .. ($form->{makemodel_rows})) {
957       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
958         push @a, {};
959         my $j = $#a;
960
961         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
962         $count++;
963       }
964     }
965
966     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
967     $form->{makemodel_rows} = $count;
968
969   } elsif ($form->{item} eq 'assembly') {
970
971     $form->{sellprice} = 0;
972     $form->{weight}    = 0;
973     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
974       qw(listprice rop stock);
975
976     @flds =
977       qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup);
978
979     for my $i (1 .. ($form->{assembly_rows} - 1)) {
980       if ($form->{"qty_$i"}) {
981         push @a, {};
982         my $j = $#a;
983
984         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
985
986         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
987
988         #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
989
990         $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"});
991         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"});
992         $count++;
993       }
994     }
995
996     $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
997
998     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
999     $form->{assembly_rows} = $count;
1000
1001     $count = 0;
1002     @flds  = qw(make model);
1003     @a     = ();
1004
1005     for my $i (1 .. ($form->{makemodel_rows})) {
1006       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
1007         push @a, {};
1008         my $j = $#a;
1009
1010         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1011         $count++;
1012       }
1013     }
1014
1015     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
1016     $form->{makemodel_rows} = $count;
1017
1018   } else {
1019
1020     # this section applies to invoices and orders
1021     # remove any empty numbers
1022     if ($form->{rowcount}) {
1023       for my $i (1 .. $form->{rowcount} - 1) {
1024         if ($form->{"partnumber_$i"}) {
1025           push @a, {};
1026           my $j = $#a;
1027
1028           map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1029           $count++;
1030           if ($lizenzen) {
1031             if ($form->{"licensenumber_$i"} == -1) {
1032               &new_license($i);
1033               exit;
1034             }
1035           }
1036         }
1037       }
1038
1039       $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1040       $form->{rowcount} = $count;
1041
1042       $form->{creditremaining} -= &invoicetotal;
1043
1044     }
1045   }
1046
1047   #sk
1048   # if pricegroups
1049   if (   $form->{type} =~ (/sales_quotation/)
1050       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
1051       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
1052       or ($form->{type} =~ /sales_order/)) {
1053
1054     # get pricegroups for parts
1055     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1056
1057     # build up html code for prices_$i
1058     set_pricegroup($form->{rowcount});
1059
1060   }
1061
1062   &display_form;
1063
1064   $lxdebug->leave_sub();
1065 }
1066
1067 sub invoicetotal {
1068   $lxdebug->enter_sub();
1069
1070   $form->{oldinvtotal} = 0;
1071
1072   # add all parts and deduct paid
1073   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
1074
1075   my ($amount, $sellprice, $discount, $qty);
1076
1077   for my $i (1 .. $form->{rowcount}) {
1078     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1079     $discount  = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
1080     $qty       = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1081
1082     #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
1083
1084     $amount = $sellprice * (1 - $discount / 100) * $qty;
1085     map { $form->{"${_}_base"} += $amount }
1086       (split / /, $form->{"taxaccounts_$i"});
1087     $form->{oldinvtotal} += $amount;
1088   }
1089
1090   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1091     split / /, $form->{taxaccounts}
1092     if !$form->{taxincluded};
1093
1094   $form->{oldtotalpaid} = 0;
1095   for $i (1 .. $form->{paidaccounts}) {
1096     $form->{oldtotalpaid} += $form->{"paid_$i"};
1097   }
1098
1099   $lxdebug->leave_sub();
1100
1101   # return total
1102   return ($form->{oldinvtotal} - $form->{oldtotalpaid});
1103 }
1104
1105 sub validate_items {
1106   $lxdebug->enter_sub();
1107
1108   # check if items are valid
1109   if ($form->{rowcount} == 1) {
1110     &update;
1111     exit;
1112   }
1113
1114   for $i (1 .. $form->{rowcount} - 1) {
1115     $form->isblank("partnumber_$i",
1116                    $locale->text('Number missing in Row') . " $i");
1117   }
1118
1119   $lxdebug->leave_sub();
1120 }
1121
1122 sub order {
1123   $lxdebug->enter_sub();
1124   if ($form->{second_run}) {
1125     $form->{print_and_post} = 0;
1126   }
1127   $form->{ordnumber} = $form->{invnumber};
1128
1129   map { delete $form->{$_} } qw(id printed emailed queued);
1130   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
1131     $form->{title} = $locale->text('Add Purchase Order');
1132     $form->{vc}    = 'vendor';
1133     $form->{type}  = 'purchase_order';
1134     $buysell       = 'sell';
1135   }
1136   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
1137     $form->{title} = $locale->text('Add Sales Order');
1138     $form->{vc}    = 'customer';
1139     $form->{type}  = 'sales_order';
1140     $buysell       = 'buy';
1141   }
1142   $form->{script} = 'oe.pl';
1143
1144   $form->{shipto} = 1;
1145
1146   $form->{rowcount}--;
1147
1148   $form->{cp_id} *= 1;
1149
1150   require "$form->{path}/$form->{script}";
1151
1152   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1153
1154   $currency = $form->{currency};
1155
1156   &order_links;
1157
1158   $form->{currency}     = $currency;
1159   $form->{exchangerate} = "";
1160   $form->{forex}        = "";
1161   $form->{exchangerate} = $exchangerate
1162     if (
1163         $form->{forex} = (
1164                   $exchangerate =
1165                     $form->check_exchangerate(
1166                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1167                     )));
1168
1169   &prepare_order;
1170   &display_form;
1171
1172   $lxdebug->leave_sub();
1173 }
1174
1175 sub quotation {
1176   $lxdebug->enter_sub();
1177   if ($form->{second_run}) {
1178     $form->{print_and_post} = 0;
1179   }
1180   map { delete $form->{$_} } qw(id printed emailed queued);
1181
1182   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
1183     $form->{title} = $locale->text('Add Request for Quotation');
1184     $form->{vc}    = 'vendor';
1185     $form->{type}  = 'request_quotation';
1186     $buysell       = 'sell';
1187   }
1188   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
1189     $form->{title} = $locale->text('Add Quotation');
1190     $form->{vc}    = 'customer';
1191     $form->{type}  = 'sales_quotation';
1192     $buysell       = 'buy';
1193   }
1194
1195   $form->{cp_id} *= 1;
1196
1197   $form->{script} = 'oe.pl';
1198
1199   $form->{shipto} = 1;
1200
1201   $form->{rowcount}--;
1202
1203   require "$form->{path}/$form->{script}";
1204
1205   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1206
1207   $currency = $form->{currency};
1208
1209   &order_links;
1210
1211   $form->{currency}     = $currency;
1212   $form->{exchangerate} = "";
1213   $form->{forex}        = "";
1214   $form->{exchangerate} = $exchangerate
1215     if (
1216         $form->{forex} = (
1217                   $exchangerate =
1218                     $form->check_exchangerate(
1219                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1220                     )));
1221
1222   &prepare_order;
1223   &display_form;
1224
1225   $lxdebug->leave_sub();
1226 }
1227
1228 sub e_mail {
1229   $lxdebug->enter_sub();
1230   if ($form->{second_run}) {
1231     $form->{print_and_post} = 0;
1232     $form->{resubmit}       = 0;
1233   }
1234   if ($myconfig{role} eq 'admin') {
1235     $bcc = qq|
1236           <th align=right nowrap=true>| . $locale->text('Bcc') . qq|</th>
1237           <td><input name=bcc size=30 value="$form->{bcc}"></td>
1238 |;
1239   }
1240
1241   if ($form->{formname} =~ /(pick|packing|bin)_list/) {
1242     $form->{email} = $form->{shiptoemail} if $form->{shiptoemail};
1243   }
1244
1245   $name = $form->{ $form->{vc} };
1246   $name =~ s/--.*//g;
1247   $title = $locale->text('E-mail') . " $name";
1248
1249   $form->{oldmedia} = $form->{media};
1250   $form->{media}    = "email";
1251
1252   $form->header;
1253
1254   print qq|
1255 <body>
1256
1257 <form method=post action=$form->{script}>
1258
1259 <table width=100%>
1260   <tr class=listtop>
1261     <th class=listtop>$title</th>
1262   </tr>
1263   <tr height="5"></tr>
1264   <tr>
1265     <td>
1266       <table width=100%>
1267         <tr>
1268           <th align=right nowrap>| . $locale->text('To') . qq|</th>
1269           <td><input name=email size=30 value="$form->{email}"></td>
1270           <th align=right nowrap>| . $locale->text('Cc') . qq|</th>
1271           <td><input name=cc size=30 value="$form->{cc}"></td>
1272         </tr>
1273         <tr>
1274           <th align=right nowrap>| . $locale->text('Subject') . qq|</th>
1275           <td><input name=subject size=30 value="$form->{subject}"></td>
1276           $bcc
1277         </tr>
1278       </table>
1279     </td>
1280   </tr>
1281   <tr>
1282     <td>
1283       <table width=100%>
1284         <tr>
1285           <th align=left nowrap>| . $locale->text('Message') . qq|</th>
1286         </tr>
1287         <tr>
1288           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
1289         </tr>
1290       </table>
1291     </td>
1292   </tr>
1293   <tr>
1294     <td>
1295 |;
1296
1297   &print_options;
1298
1299   map { delete $form->{$_} }
1300     qw(action email cc bcc subject message formname sendmode format header override);
1301
1302   # save all other variables
1303   foreach $key (keys %$form) {
1304     $form->{$key} =~ s/\"/&quot;/g;
1305     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1306   }
1307
1308   print qq|
1309     </td>
1310   </tr>
1311   <tr>
1312     <td><hr size=3 noshade></td>
1313   </tr>
1314 </table>
1315
1316 <input type=hidden name=nextsub value=send_email>
1317
1318 <br>
1319 <input name=action class=submit type=submit value="|
1320     . $locale->text('Continue') . qq|">
1321 </form>
1322
1323 </body>
1324 </html>
1325 |;
1326
1327   $lxdebug->leave_sub();
1328 }
1329
1330 sub send_email {
1331   $lxdebug->enter_sub();
1332
1333   $old_form = new Form;
1334
1335   map { $old_form->{$_} = $form->{$_} } keys %$form;
1336   $old_form->{media} = $form->{oldmedia};
1337
1338   &print_form($old_form);
1339
1340   $lxdebug->leave_sub();
1341 }
1342
1343 sub print_options {
1344   $lxdebug->enter_sub();
1345   $form->{sendmode} = "attachment";
1346   $form->{copies}   = 3 unless $form->{copies};
1347
1348   $form->{PD}{ $form->{formname} } = "selected";
1349   $form->{DF}{ $form->{format} }   = "selected";
1350   $form->{OP}{ $form->{media} }    = "selected";
1351   $form->{SM}{ $form->{sendmode} } = "selected";
1352
1353   if ($form->{type} eq 'purchase_order') {
1354     $type = qq|<select name=formname>
1355             <option value=purchase_order $form->{PD}{purchase_order}>|
1356       . $locale->text('Purchase Order') . qq|
1357             <option value=bin_list $form->{PD}{bin_list}>|
1358       . $locale->text('Bin List');
1359   }
1360
1361   if ($form->{type} eq 'credit_note') {
1362     $type = qq|<select name=formname>
1363             <option value=credit_note $form->{PD}{credit_note}>|
1364       . $locale->text('Credit Note');
1365   }
1366
1367   if ($form->{type} eq 'sales_order') {
1368     $type = qq|<select name=formname>
1369             <option value=sales_order $form->{PD}{sales_order}>|
1370       . $locale->text('Confirmation') . qq|
1371       <option value=proforma $form->{PD}{proforma}>|
1372       . $locale->text('Proforma Invoice') . qq|
1373             <option value=pick_list $form->{PD}{pick_list}>|
1374       . $locale->text('Pick List') . qq|
1375             <option value=packing_list $form->{PD}{packing_list}>|
1376       . $locale->text('Packing List') . qq|
1377             <option value=agb $form->{PD}{agb}>|
1378       . $locale->text('AGB') . qq|
1379             <option value=bunker_receipt $form->{PD}{bunker_receipt}>|
1380       . $locale->text('Bunker Receipt');
1381   }
1382
1383   if ($form->{type} =~ /_quotation$/) {
1384     $type = qq|<select name=formname>
1385             <option value="$`_quotation" $form->{PD}{"$`_quotation"}>|
1386       . $locale->text('Quotation')
1387 ;
1388   }
1389
1390   if ($form->{type} eq 'invoice') {
1391     $type = qq|<select name=formname>
1392             <option value=invoice $form->{PD}{invoice}>|
1393       . $locale->text('Invoice') . qq|
1394       <option value=proforma $form->{PD}{proforma}>|
1395       . $locale->text('Proforma Invoice') . qq|
1396       <option value=packing_list $form->{PD}{packing_list}>|
1397       . $locale->text('Packing List');
1398   }
1399
1400   if ($form->{type} eq 'invoice' && $form->{storno}) {
1401     $type = qq|<select name=formname>
1402             <option value=storno_invoice $form->{PD}{storno_invoice}>|
1403       . $locale->text('Storno Invoice') . qq|
1404       <option value=storno_packing_list $form->{PD}{storno_packing_list}>|
1405       . $locale->text('Storno Packing List');
1406   }
1407
1408   if ($form->{type} eq 'credit_note') {
1409     $type = qq|<select name=formname>
1410             <option value=credit_note $form->{PD}{credit_note}>|
1411       . $locale->text('Credit Note');
1412   }
1413
1414   if ($form->{type} eq 'ship_order') {
1415     $type = qq|<select name=formname>
1416             <option value=pick_list $form->{PD}{pick_list}>|
1417       . $locale->text('Pick List') . qq|
1418             <option value=packing_list $form->{PD}{packing_list}>|
1419       . $locale->text('Packing List');
1420   }
1421
1422   if ($form->{type} eq 'receive_order') {
1423     $type = qq|<select name=formname>
1424             <option value=bin_list $form->{PD}{bin_list}>|
1425       . $locale->text('Bin List');
1426   }
1427
1428   if ($form->{media} eq 'email') {
1429     $media = qq|<select name=sendmode>
1430             <option value=attachment $form->{SM}{attachment}>|
1431       . $locale->text('Attachment') . qq|
1432             <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
1433   } else {
1434     $media = qq|<select name=media>
1435             <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
1436     if (scalar(keys (%{ $form->{printers} })) !=0 && $latex_templates) {
1437       $media .= qq|
1438             <option value=printer $form->{OP}{printer}>|
1439         . $locale->text('Printer');
1440     }
1441     if ($latex_templates) {
1442       $media .= qq|
1443             <option value=queue $form->{OP}{queue}>| . $locale->text('Queue');
1444     }
1445   }
1446
1447   $format = qq|<select name=format>|;
1448   if ($opendocument_templates && $openofficeorg_writer_bin &&
1449       $xvfb_bin && (-x $openofficeorg_writer_bin) && (-x $xvfb_bin)) {
1450     $format .= qq|<option value=opendocument_pdf | .
1451       $form->{DF}{"opendocument_pdf"} . qq|>| .
1452       $locale->text("PDF (OpenDocument/OASIS)") . qq|</option>|;
1453   }
1454
1455   if ($latex_templates) {
1456     $format .= qq|<option value=pdf $form->{DF}{pdf}>| .
1457       $locale->text('PDF') . qq|</option>|;
1458   }
1459
1460   $format .= qq|<option value=html $form->{DF}{html}>HTML</option>|;
1461
1462   if ($latex) {
1463     $format .= qq|<option value=postscript $form->{DF}{postscript}>| .
1464       $locale->text('Postscript') . qq|</option>|;
1465   }
1466
1467   if ($opendocument_templates) {
1468     $format .= qq|<option value=opendocument $form->{DF}{opendocument}>| .
1469       $locale->text("OpenDocument/OASIS") . qq|</option>|;
1470   }
1471   $format .= qq|</select>|;
1472
1473   if (scalar(keys (%{ $form->{languages} })) !=0) {
1474
1475     $language_select = qq|<select name=language_id>|;
1476     foreach $item (@{ $form->{languages} }) {
1477       if ($form->{language_id} eq $item->{id}) {
1478         $language_select .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
1479       } else {
1480         $language_select .= qq|<option value="$item->{id}">$item->{description}</option>|;
1481       }
1482     }
1483   }
1484
1485   if (scalar(keys (%{ $form->{printers} })) !=0) {
1486
1487     $printer_select = qq|<select name=printer_id>
1488                   <option value=""></option>|;
1489     foreach $item (@{ $form->{printers} }) {
1490       $printer_select .= qq|<option value="$item->{id}">$item->{printer_description}</option>|;
1491     }
1492   }
1493
1494
1495
1496   print qq|
1497 <table width=100% cellspacing=0 cellpadding=0>
1498   <tr>
1499     <td>
1500       <table>
1501         <tr>
1502           <td>$type</select></td>|;
1503   if (scalar(keys (%{ $form->{languages} })) !=0) {
1504   print qq|
1505           <td>${language_select}</select></td>|;
1506   }
1507   print qq|
1508           <td>$format</select></td>
1509           <td>$media</select></td>|;
1510   if (scalar(keys (%{ $form->{printers} })) !=0) {
1511   print qq|
1512           <td>$printer_select</select></td>
1513 |;
1514   }
1515
1516   if (scalar(keys (%{ $form->{printers} })) !=0 && $latex_templates && $form->{media} ne 'email') {
1517     print qq|
1518           <td>| . $locale->text('Copies') . qq|
1519           <input name=copies size=2 value=$form->{copies}></td>
1520 |;
1521   }
1522
1523   $form->{groupitems} = "checked" if $form->{groupitems};
1524
1525   print qq|
1526           <td>| . $locale->text('Group Items') . qq|</td>
1527           <td><input name=groupitems type=checkbox class=checkbox $form->{groupitems}></td>
1528         </tr>
1529       </table>
1530     </td>
1531     <td align=right>
1532       <table>
1533         <tr>
1534 |;
1535
1536   if ($form->{printed} =~ /$form->{formname}/) {
1537     print qq|
1538           <th>\|| . $locale->text('Printed') . qq|\|</th>
1539 |;
1540   }
1541
1542   if ($form->{emailed} =~ /$form->{formname}/) {
1543     print qq|
1544           <th>\|| . $locale->text('E-mailed') . qq|\|</th>
1545 |;
1546   }
1547
1548   if ($form->{queued} =~ /$form->{formname}/) {
1549     print qq|
1550           <th>\|| . $locale->text('Queued') . qq|\|</th>
1551 |;
1552   }
1553
1554   print qq|
1555         </tr>
1556       </table>
1557     </td>
1558   </tr>
1559 </table>
1560 |;
1561
1562   $lxdebug->leave_sub();
1563 }
1564
1565 sub print {
1566   $lxdebug->enter_sub();
1567
1568   # if this goes to the printer pass through
1569   if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1570     $form->error($locale->text('Select postscript or PDF!'))
1571       if ($form->{format} !~ /(postscript|pdf)/);
1572
1573     $old_form = new Form;
1574     map { $old_form->{$_} = $form->{$_} } keys %$form;
1575   }
1576
1577   if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1578     if ($form->{formname} eq "proforma") {
1579       $form->{proforma} = 1;
1580     }
1581     $form->{print_and_save} = 1;
1582     my $formname = $form->{formname};
1583     &save();
1584     $form->{formname} = $formname;
1585     &edit();
1586     exit;
1587   }
1588
1589   &print_form($old_form);
1590
1591   $lxdebug->leave_sub();
1592 }
1593
1594 sub print_form {
1595   $lxdebug->enter_sub();
1596   my ($old_form) = @_;
1597
1598   $inv       = "inv";
1599   $due       = "due";
1600   $numberfld = "invnumber";
1601
1602   $display_form =
1603     ($form->{display_form}) ? $form->{display_form} : "display_form";
1604
1605   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1606   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1607
1608   if ($form->{formname} eq "invoice") {
1609     $form->{label} = $locale->text('Invoice');
1610   }
1611   if ($form->{formname} eq "packing_list") {
1612
1613     # this is from an invoice
1614     $form->{label} = $locale->text('Packing List');
1615   }
1616   if (($form->{formname} eq 'sales_order') || ($form->{formname} eq 'agb') || ($form->{formname} eq 'bunker_receipt')) {
1617     $inv                  = "ord";
1618     $due                  = "req";
1619     $form->{"${inv}date"} = $form->{transdate};
1620     $form->{label}        = $locale->text('Sales Order');
1621     $numberfld            = "sonumber";
1622     $order                = 1;
1623   }
1624
1625   if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1626     $inv                  = "inv";
1627     $due                  = "due";
1628     $form->{"${inv}date"} = $form->{transdate};
1629     $form->{"invdate"}    = $form->{transdate};
1630     $form->{label}        = $locale->text('Proforma Invoice');
1631     $numberfld            = "sonumber";
1632     $order                = 0;
1633   }
1634
1635   if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1636     $inv                  = "inv";
1637     $due                  = "due";
1638     $form->{"${inv}date"} = $form->{transdate};
1639     $form->{"invdate"}    = $form->{transdate};
1640     $form->{invnumber}    = $form->{ordnumber};
1641     $form->{label}        = $locale->text('Proforma Invoice');
1642     $numberfld            = "sonumber";
1643     $order                = 1;
1644   }
1645
1646   if ($form->{formname} eq 'packing_list' && $form->{type} ne 'invoice') {
1647
1648     # we use the same packing list as from an invoice
1649     $inv = "ord";
1650     $due = "req";
1651     $form->{invdate} = $form->{"${inv}date"} = $form->{transdate};
1652     $form->{label} = $locale->text('Packing List');
1653     $order = 1;
1654     # set invnumber for template packing_list 
1655     $form->{invnumber}   = $form->{ordnumber};
1656   }
1657   if ($form->{formname} eq 'pick_list') {
1658     $inv                  = "ord";
1659     $due                  = "req";
1660     $form->{"${inv}date"} =
1661       ($form->{transdate}) ? $form->{transdate} : $form->{invdate};
1662     $form->{label} = $locale->text('Pick List');
1663     $order = 1 unless $form->{type} eq 'invoice';
1664   }
1665   if ($form->{formname} eq 'purchase_order') {
1666     $inv                  = "ord";
1667     $due                  = "req";
1668     $form->{"${inv}date"} = $form->{transdate};
1669     $form->{label}        = $locale->text('Purchase Order');
1670     $numberfld            = "ponumber";
1671     $order                = 1;
1672   }
1673   if ($form->{formname} eq 'bin_list') {
1674     $inv                  = "ord";
1675     $due                  = "req";
1676     $form->{"${inv}date"} = $form->{transdate};
1677     $form->{label}        = $locale->text('Bin List');
1678     $order                = 1;
1679   }
1680   if ($form->{formname} eq 'sales_quotation') {
1681     $inv                  = "quo";
1682     $due                  = "req";
1683     $form->{"${inv}date"} = $form->{transdate};
1684     $form->{label}        = $locale->text('Quotation');
1685     $numberfld            = "sqnumber";
1686     $order                = 1;
1687   }
1688
1689   if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1690     $inv                  = "quo";
1691     $due                  = "req";
1692     $form->{"${inv}date"} = $form->{transdate};
1693     $form->{"invdate"} = $form->{transdate};
1694     $form->{label}        = $locale->text('Proforma Invoice');
1695     $numberfld            = "sqnumber";
1696     $order                = 1;
1697   }
1698
1699   if ($form->{formname} eq 'request_quotation') {
1700     $inv                  = "quo";
1701     $due                  = "req";
1702     $form->{"${inv}date"} = $form->{transdate};
1703     $form->{label}        = $locale->text('Quotation');
1704     $numberfld            = "rfqnumber";
1705     $order                = 1;
1706   }
1707
1708   $form->isblank("email", $locale->text('E-mail address missing!'))
1709     if ($form->{media} eq 'email');
1710   $form->isblank("${inv}date",
1711                  $locale->text($form->{label} . ' Date missing!'));
1712
1713   # $locale->text('Invoice Number missing!')
1714   # $locale->text('Invoice Date missing!')
1715   # $locale->text('Packing List Number missing!')
1716   # $locale->text('Packing List Date missing!')
1717   # $locale->text('Order Number missing!')
1718   # $locale->text('Order Date missing!')
1719   # $locale->text('Quotation Number missing!')
1720   # $locale->text('Quotation Date missing!')
1721
1722   # assign number
1723   if (!$form->{"${inv}number"} && !$form->{preview}) {
1724     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1725     if ($form->{media} ne 'email') {
1726
1727       # get pricegroups for parts
1728       IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1729
1730       # build up html code for prices_$i
1731       set_pricegroup($form->{rowcount});
1732
1733       $form->{rowcount}--;
1734
1735       &{"$display_form"};
1736       exit;
1737     }
1738   }
1739
1740   &validate_items;
1741
1742   # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1743   my ($saved_email, $saved_cc, $saved_bcc) =
1744     ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1745
1746   $language_saved = $form->{language_id};
1747   &{"$form->{vc}_details"};
1748   $form->{language_id} = $language_saved;
1749
1750   $form->{"email"} = $saved_email if ($saved_email);
1751   $form->{"cc"}    = $saved_cc    if ($saved_cc);
1752   $form->{"bcc"}   = $saved_bcc   if ($saved_bcc);
1753
1754   # format payment dates
1755   for $i (1 .. $form->{paidaccounts} - 1) {
1756     $form->{"datepaid_$i"} = $locale->date(\%myconfig, $form->{"datepaid_$i"});
1757   }
1758
1759   ($form->{employee}) = split /--/, $form->{employee};
1760   ($form->{warehouse}, $form->{warehouse_id}) = split /--/, $form->{warehouse};
1761
1762   # create the form variables
1763   if ($order) {
1764     OE->order_details(\%myconfig, \%$form);
1765   } else {
1766     IS->invoice_details(\%myconfig, \%$form, $locale);
1767   }
1768
1769   # format global dates
1770   map { $form->{$_} = $locale->date(\%myconfig, $form->{$_}, 1) }
1771     ("${inv}date", "${due}date", "shippingdate", "deliverydate");
1772
1773   # format item dates
1774   for my $field (qw(transdate_oe deliverydate_oe)) {
1775     map {
1776       $form->{$field}[$_] = $locale->date(\%myconfig, $form->{$field}[$_], 1);
1777     } 0 .. $#{ $form->{$field} };
1778   }
1779
1780   if ($form->{shipto_id}) {
1781     $form->get_shipto(\%myconfig);
1782   }
1783
1784   @a = qw(name street zipcode city country);
1785
1786   $shipto = 1;
1787
1788   # if there is no shipto fill it in from billto
1789   foreach $item (@a) {
1790     if ($form->{"shipto$item"}) {
1791       $shipto = 0;
1792       last;
1793     }
1794   }
1795
1796   if ($shipto) {
1797     if (   $form->{formname} eq 'purchase_order'
1798         || $form->{formname} eq 'request_quotation') {
1799       $form->{shiptoname}   = $myconfig{company};
1800       $form->{shiptostreet} = $myconfig{address};
1801         } else {
1802       map { $form->{"shipto$_"} = $form->{$_} } @a;
1803     }
1804   }
1805
1806   $form->{notes} =~ s/^\s+//g;
1807
1808   map({ $form->{$_} =~ s/\\n/\n/g; } qw(company address));
1809
1810   $form->{templates} = "$myconfig{templates}";
1811
1812   $form->{language} = $form->get_template_language(\%myconfig);
1813   $form->{printer_code} = $form->get_printer_code(\%myconfig);
1814
1815   if ($form->{language} ne "") {
1816     $form->{language} = "_" . $form->{language};
1817   }
1818
1819   if ($form->{printer_code} ne "") {
1820     $form->{printer_code} = "_" . $form->{printer_code};
1821   }
1822
1823   $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
1824   if ($form->{format} eq 'postscript') {
1825     $form->{postscript} = 1;
1826     $form->{IN} =~ s/html$/tex/;
1827   } elsif ($form->{"format"} =~ /pdf/) {
1828     $form->{pdf} = 1;
1829     if ($form->{"format"} =~ /opendocument/) {
1830       $form->{IN} =~ s/html$/odt/;
1831     } else {
1832       $form->{IN} =~ s/html$/tex/;
1833     }
1834   } elsif ($form->{"format"} =~ /opendocument/) {
1835     $form->{"opendocument"} = 1;
1836     $form->{"IN"} =~ s/html$/odt/;
1837   }
1838
1839   if ($form->{media} eq 'printer') {
1840     $form->{OUT} = "| $form->{printer_command} &>/dev/null";
1841     $form->{printed} .= " $form->{formname}";
1842     $form->{printed} =~ s/^ //;
1843   }
1844   $printed = $form->{printed};
1845
1846   if ($form->{media} eq 'email') {
1847     $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1848       unless $form->{subject};
1849
1850     $form->{OUT} = "$sendmail";
1851
1852     $form->{emailed} .= " $form->{formname}";
1853     $form->{emailed} =~ s/^ //;
1854   }
1855   $emailed = $form->{emailed};
1856
1857   if ($form->{media} eq 'queue') {
1858     %queued = split / /, $form->{queued};
1859
1860     if ($filename = $queued{ $form->{formname} }) {
1861       $form->{queued} =~ s/$form->{formname} $filename//;
1862       unlink "$spool/$filename";
1863       $filename =~ s/\..*$//g;
1864     } else {
1865       $filename = time;
1866       $filename .= $$;
1867     }
1868
1869     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
1870     $form->{OUT} = ">$spool/$filename";
1871
1872     # add type
1873     $form->{queued} .= " $form->{formname} $filename";
1874
1875     $form->{queued} =~ s/^ //;
1876   }
1877   $queued = $form->{queued};
1878
1879   $form->parse_template(\%myconfig, $userspath);
1880
1881   $form->{callback} = "";
1882
1883   if ($form->{media} eq 'email') {
1884     $form->{message} = $locale->text('sent') unless $form->{message};
1885   }
1886   $message = $form->{message};
1887
1888   # if we got back here restore the previous form
1889   if ($form->{media} =~ /(printer|email|queue)/) {
1890
1891     $form->update_status(\%myconfig)
1892       if ($form->{media} eq 'queue' && $form->{id});
1893
1894     if ($old_form) {
1895
1896       $old_form->{"${inv}number"} = $form->{"${inv}number"};
1897
1898       # restore and display form
1899       map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1900
1901       $form->{queued}  = $queued;
1902       $form->{printed} = $printed;
1903       $form->{emailed} = $emailed;
1904       $form->{message} = $message;
1905
1906       $form->{rowcount}--;
1907       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1908         qw(exchangerate creditlimit creditremaining);
1909
1910       for $i (1 .. $form->{paidaccounts}) {
1911         map {
1912           $form->{"${_}_$i"} =
1913             $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1914         } qw(paid exchangerate);
1915       }
1916
1917       &{"$display_form"};
1918       exit;
1919     }
1920
1921     $msg =
1922       ($form->{media} eq 'printer')
1923       ? $locale->text('sent to printer')
1924       : $locale->text('emailed to') . " $form->{email}";
1925     $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1926   }
1927   if ($form->{printing}) {
1928    &{"$display_form"};
1929    exit; 
1930   }
1931
1932   $lxdebug->leave_sub();
1933 }
1934
1935 sub customer_details {
1936   $lxdebug->enter_sub();
1937   IS->customer_details(\%myconfig, \%$form);
1938   $lxdebug->leave_sub();
1939 }
1940
1941 sub vendor_details {
1942   $lxdebug->enter_sub();
1943
1944   IR->vendor_details(\%myconfig, \%$form);
1945
1946   $lxdebug->leave_sub();
1947 }
1948
1949 sub post_as_new {
1950   $lxdebug->enter_sub();
1951
1952   $form->{postasnew} = 1;
1953   map { delete $form->{$_} } qw(printed emailed queued);
1954
1955   &post;
1956
1957   $lxdebug->leave_sub();
1958 }
1959
1960 sub ship_to {
1961   $lxdebug->enter_sub();
1962   if ($form->{second_run}) {
1963     $form->{print_and_post} = 0;
1964   }
1965
1966   $title = $form->{title};
1967   $form->{title} = $locale->text('Ship to');
1968
1969   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1970     qw(exchangerate creditlimit creditremaining);
1971
1972   # get details for name
1973   &{"$form->{vc}_details"};
1974
1975   $number =
1976     ($form->{vc} eq 'customer')
1977     ? $locale->text('Customer Number')
1978     : $locale->text('Vendor Number');
1979
1980   # get pricegroups for parts
1981   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1982
1983   # build up html code for prices_$i
1984   set_pricegroup($form->{rowcount});
1985
1986   $nextsub = ($form->{display_form}) ? $form->{display_form} : "display_form";
1987
1988   $form->{rowcount}--;
1989
1990   $form->header;
1991
1992   print qq|
1993 <body>
1994
1995 <form method=post action=$form->{script}>
1996
1997 <table width=100%>
1998   <tr>
1999     <td>
2000       <table>
2001         <tr class=listheading>
2002           <th class=listheading colspan=2 width=50%>|
2003     . $locale->text('Billing Address') . qq|</th>
2004           <th class=listheading width=50%>|
2005     . $locale->text('Shipping Address') . qq|</th>
2006         </tr>
2007         <tr height="5"></tr>
2008         <tr>
2009           <th align=right nowrap>$number</th>
2010           <td>$form->{"$form->{vc}number"}</td>
2011         </tr>
2012         <tr>
2013           <th align=right nowrap>| . $locale->text('Company Name') . qq|</th>
2014           <td>$form->{name}</td>
2015           <td><input name=shiptoname size=35 value="$form->{shiptoname}"></td>
2016         </tr>
2017         <tr>
2018           <th align=right nowrap>| . $locale->text('Street') . qq|</th>
2019           <td>$form->{street}</td>
2020           <td><input name=shiptostreet size=35 value="$form->{shiptostreet}"></td>
2021         </tr>
2022         <tr>
2023           <th align=right nowrap>| . $locale->text('Zipcode') . qq|</th>
2024           <td>$form->{zipcode}</td>
2025           <td><input name=shiptozipcode size=35 value="$form->{shiptozipcode}"></td>
2026         </tr>
2027         <tr>
2028           <th align=right nowrap>| . $locale->text('City') . qq|</th>
2029           <td>$form->{city}</td>
2030           <td><input name=shiptocity size=35 value="$form->{shiptocity}"></td>
2031         </tr>
2032         <tr>
2033           <th align=right nowrap>| . $locale->text('Country') . qq|</th>
2034           <td>$form->{country}</td>
2035           <td><input name=shiptocountry size=35 value="$form->{shiptocountry}"></td>
2036         </tr>
2037         <tr>
2038           <th align=right nowrap>| . $locale->text('Contact') . qq|</th>
2039           <td>$form->{contact}</td>
2040           <td><input name=shiptocontact size=35 value="$form->{shiptocontact}"></td>
2041         </tr>
2042         <tr>
2043           <th align=right nowrap>| . $locale->text('Phone') . qq|</th>
2044           <td>$form->{"$form->{vc}phone"}</td>
2045           <td><input name=shiptophone size=20 value="$form->{shiptophone}"></td>
2046         </tr>
2047         <tr>
2048           <th align=right nowrap>| . $locale->text('Fax') . qq|</th>
2049           <td>$form->{"$form->{vc}fax"}</td>
2050           <td><input name=shiptofax size=20 value="$form->{shiptofax}"></td>
2051         </tr>
2052         <tr>
2053           <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
2054           <td>$form->{email}</td>
2055           <td><input name=shiptoemail size=35 value="$form->{shiptoemail}"></td>
2056         </tr>
2057       </table>
2058     </td>
2059   </tr>
2060 </table>
2061
2062 <input type=hidden name=nextsub value=$nextsub>
2063 |;
2064
2065   # delete shipto
2066   map { delete $form->{$_} }
2067     qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail header);
2068   $form->{title} = $title;
2069
2070   foreach $key (keys %$form) {
2071     $form->{$key} =~ s/\"/&quot;/g;
2072     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2073   }
2074
2075   print qq|
2076
2077 <hr size=3 noshade>
2078
2079 <br>
2080 <input class=submit type=submit name=action value="|
2081     . $locale->text('Continue') . qq|">
2082 </form>
2083
2084 </body>
2085 </html>
2086 |;
2087
2088   $lxdebug->leave_sub();
2089 }
2090
2091 sub new_license {
2092   $lxdebug->enter_sub();
2093
2094   my $row = shift;
2095
2096   # change callback
2097   $form->{old_callback} = $form->escape($form->{callback}, 1);
2098   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
2099   $form->{old_callback} = $form->escape($form->{old_callback}, 1);
2100
2101   # delete action
2102   delete $form->{action};
2103   $customer = $form->{customer};
2104   map { $form->{"old_$_"} = $form->{"${_}_$row"} } qw(partnumber description);
2105
2106   # save all other form variables in a previousform variable
2107   $form->{row} = $row;
2108   foreach $key (keys %$form) {
2109
2110     # escape ampersands
2111     $form->{$key} =~ s/&/%26/g;
2112     $previousform .= qq|$key=$form->{$key}&|;
2113   }
2114   chop $previousform;
2115   $previousform = $form->escape($previousform, 1);
2116
2117   $form->{script} = "licenses.pl";
2118
2119   map { $form->{$_} = $form->{"old_$_"} } qw(partnumber description);
2120   map { $form->{$_} = $form->escape($form->{$_}, 1) }
2121     qw(partnumber description);
2122   $form->{callback} =
2123     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|;
2124   $form->redirect;
2125
2126   $lxdebug->leave_sub();
2127 }
2128