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