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