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