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