da884bc782716402e6a3b5a824803ec6dfecc8c3
[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 CGI;
37 use CGI::Ajax;
38 use List::Util qw(max first);
39
40 use SL::CVar;
41 use SL::Common;
42 use SL::CT;
43 use SL::IC;
44 use SL::IO;
45
46 require "bin/mozilla/common.pl";
47
48 # any custom scripts for this one
49 if (-f "bin/mozilla/custom_io.pl") {
50   eval { require "bin/mozilla/custom_io.pl"; };
51 }
52 if (-f "bin/mozilla/$form->{login}_io.pl") {
53   eval { require "bin/mozilla/$form->{login}_io.pl"; };
54 }
55
56 1;
57
58 # end of main
59
60 # this is for our long dates
61 # $locale->text('January')
62 # $locale->text('February')
63 # $locale->text('March')
64 # $locale->text('April')
65 # $locale->text('May ')
66 # $locale->text('June')
67 # $locale->text('July')
68 # $locale->text('August')
69 # $locale->text('September')
70 # $locale->text('October')
71 # $locale->text('November')
72 # $locale->text('December')
73
74 # this is for our short month
75 # $locale->text('Jan')
76 # $locale->text('Feb')
77 # $locale->text('Mar')
78 # $locale->text('Apr')
79 # $locale->text('May')
80 # $locale->text('Jun')
81 # $locale->text('Jul')
82 # $locale->text('Aug')
83 # $locale->text('Sep')
84 # $locale->text('Oct')
85 # $locale->text('Nov')
86 # $locale->text('Dec')
87 use SL::IS;
88 use SL::PE;
89 use SL::AM;
90 use Data::Dumper;
91
92 sub _check_io_auth {
93   $auth->assert('part_service_assembly_edit   | vendor_invoice_edit       | sales_order_edit    | invoice_edit |' .
94                 'request_quotation_edit       | sales_quotation_edit      | purchase_order_edit | ' .
95                 'purchase_delivery_order_edit | sales_delivery_order_edit');
96 }
97
98 ########################################
99 # Eintrag fuer Version 2.2.0 geaendert #
100 # neue Optik im Rechnungsformular      #
101 ########################################
102 sub display_row {
103   $lxdebug->enter_sub();
104
105   _check_io_auth();
106
107   my $numrows = shift;
108
109   my ($readonly, $stock_in_out, $stock_in_out_title);
110
111   my $is_purchase        = (first { $_ eq $form->{type} } qw(request_quotation purchase_order purchase_delivery_order)) || ($form->{script} eq 'ir.pl');
112   my $show_min_order_qty =  first { $_ eq $form->{type} } qw(request_quotation purchase_order);
113   my $is_delivery_order  = $form->{type} =~ /_delivery_order$/;
114
115   if ($is_delivery_order) {
116     $readonly             = ' readonly' if ($form->{closed});
117
118     if ($form->{type} eq 'sales_delivery_order') {
119       $stock_in_out_title = $locale->text('Release From Stock');
120       $stock_in_out       = 'out';
121     } else {
122       $stock_in_out_title = $locale->text('Transfer To Stock');
123       $stock_in_out       = 'in';
124     }
125
126     retrieve_partunits();
127   }
128
129   # column_index
130   my @header_sort = qw(runningnumber partnumber description ship qty unit sellprice_pg sellprice discount linetotal);
131   my @HEADER = (
132     {  id => 'runningnumber', width => 5,     value => $locale->text('No.'),                  display => 1, },
133     {  id => 'partnumber',    width => 8,     value => $locale->text('Number'),               display => 1, },
134     {  id => 'description',   width => 30,    value => $locale->text('Part Description'),     display => 1, },
135     {  id => 'ship',          width => 5,     value => ($form->{type} eq 'purchase_order' ? $locale->text('Ship rcvd') : $locale->text('Ship')),                 
136        display => $form->{type} =~ /sales_order/ || ($form->{type} =~ /purchase_order/ && !($lizenzen && $form->{vc} eq "customer")) , },
137     {  id => 'qty',           width => 5,     value => $locale->text('Qty'),                  display => 1, },
138     {  id => 'price_factor',  width => 5,     value => $locale->text('Price Factor'),         display => !$is_delivery_order, },
139     {  id => 'unit',          width => 5,     value => $locale->text('Unit'),                 display => 1, },
140     {  id => 'license',       width => 10,    value => $locale->text('License'),              display => 0, },
141     {  id => 'serialnr',      width => 10,    value => $locale->text('Serial No.'),           display => 0, },
142     {  id => 'projectnr',     width => 10,    value => $locale->text('Project'),              display => 0, },
143     {  id => 'sellprice',     width => 15,    value => $locale->text('Price'),                display => !$is_delivery_order, },
144     {  id => 'sellprice_pg',  width => 8,    value => $locale->text('Pricegroup'),           display => ($form->{type} =~ /^sales_/) && !$is_delivery_order,  },
145     {  id => 'discount',      width => 5,     value => $locale->text('Discount'),             display => ($form->{vc} eq 'customer') && !$is_delivery_order, },
146     {  id => 'linetotal',     width => 10,    value => $locale->text('Extended'),             display => !$is_delivery_order, },
147     {  id => 'bin',           width => 10,    value => $locale->text('Bin'),                  display => 0, },
148     {  id => 'stock_in_out',  width => 10,    value => $stock_in_out_title,                   display => $is_delivery_order, },
149   ); 
150   my @column_index = map { $_->{id} } grep { $_->{display} } @HEADER;
151
152   # cache units
153   my $all_units       = AM->retrieve_units(\%myconfig, $form);
154
155   my %price_factors   = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
156
157   my $colspan = scalar @column_index;
158
159   $form->{invsubtotal} = 0;
160   map { $form->{"${_}_base"} = 0 } (split(/ /, $form->{taxaccounts}));
161
162   # about details 
163   $myconfig{show_form_details} = 1                            unless (defined($myconfig{show_form_details}));
164   $form->{show_details}        = $myconfig{show_form_details} unless (defined($form->{show_details}));
165   # /about details
166
167   # translations, unused commented out
168 #  $runningnumber = $locale->text('No.');
169   $deliverydate  = $locale->text('Delivery Date');
170   $serialnumber  = $locale->text('Serial No.');
171   $projectnumber = $locale->text('Project');
172 #  $partsgroup    = $locale->text('Group');
173   $reqdate       = $locale->text('Reqdate');
174   $deliverydate  = $locale->text('Required by');
175
176   # special alignings
177   my %align  = map { $_ => 'right' } qw(qty ship right sellprice_pg discount linetotal stock_in_out);
178   my %nowrap = map { $_ => 1 }       qw(description unit);
179
180   $form->{marge_total}           = 0;
181   $form->{sellprice_total}       = 0;
182   $form->{lastcost_total}        = 0;
183   my %projectnumber_labels = ();
184   my @projectnumber_values = ("");
185
186   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
187     push(@projectnumber_values, $item->{"id"});
188     $projectnumber_labels{$item->{"id"}} = $item->{"projectnumber"};
189   }
190
191   # rows
192   for $i (1 .. $numrows) {
193
194     # undo formatting
195     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(qty ship discount sellprice price_new price_old) unless ($form->{simple_save});
196
197 # unit begin
198     $form->{"unit_old_$i"}      ||= $form->{"unit_$i"};
199     $form->{"selected_unit_$i"} ||= $form->{"unit_$i"};
200
201     if (   !$all_units->{$form->{"selected_unit_$i"}}                                            # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
202         || !AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units)) { # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
203       $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};                 # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
204     }
205     # adjust prices by unit, ignore if pricegroup changed
206     if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
207         $form->{"sellprice_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
208         $form->{"unit_old_$i"}   = $form->{"selected_unit_$i"};
209     }
210     my $this_unit = $form->{"unit_$i"};
211     $this_unit    = $form->{"selected_unit_$i"} if AM->convert_unit($this_unit, $form->{"selected_unit_$i"}, $all_units);
212     $this_unit  ||= "kg";
213
214     if (0 < scalar @{ $form->{ALL_PRICE_FACTORS} }) {
215       my @values = ('', map { $_->{id}                      } @{ $form->{ALL_PRICE_FACTORS} });
216       my %labels =      map { $_->{id} => $_->{description} } @{ $form->{ALL_PRICE_FACTORS} };
217
218       $column_data{price_factor} =
219         NTI($cgi->popup_menu('-name'    => "price_factor_id_$i",
220                              '-default' => $form->{"price_factor_id_$i"},
221                              '-values'  => \@values,
222                              '-labels'  => \%labels,
223                              '-style'   => 'width:90px'));
224     } else {
225       $column_data{price_factor} = '&nbsp;';
226     }
227
228     $column_data{"unit"} = AM->unit_select_html($all_units, "unit_$i", $this_unit, $form->{"id_$i"} ? $form->{"unit_$i"} : undef);
229 # / unit ending
230
231     $form->{"sellprice_$i"} =~ /\.(\d+)/;
232     $decimalplaces = max 2, length $1;
233
234     $price_factor   = $price_factors{$form->{"price_factor_id_$i"}} || 1;
235     $discount       = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"} / 100, $decimalplaces);
236     $linetotal      = $form->round_amount(($form->{"sellprice_$i"} - $discount) / $price_factor, $decimalplaces);
237     $linetotal      = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
238
239     $column_data{runningnumber} = $cgi->textfield(-name => "runningnumber_$i", -size => 5,  -value => $i);    # HuT
240     $column_data{partnumber}    = $cgi->textfield(-name => "partnumber_$i",    -size => 12, -value => $form->{"partnumber_$i"});
241     $column_data{description} = ((($rows = $form->numtextrows($form->{"description_$i"}, 30, 6)) > 1) # if description is too large, use a textbox instead
242                                 ? $cgi->textarea( -name => "description_$i", -default => $form->{"description_$i"}, -rows => $rows, -columns => 30)
243                                 : $cgi->textfield(-name => "description_$i",   -size => 30, -value => $form->{"description_$i"}))
244                                 . $cgi->button(-value => $locale->text('L'), -onClick => "set_longdescription_window('longdescription_$i')");
245
246     $form->{"qty_$i"} =~ /\.(\d+)/;
247     my $qty_dec = length $1;
248
249     $column_data{qty}  = $cgi->textfield(-name => "qty_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec));
250     $column_data{qty} .= $cgi->button(-onclick => "calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)", -value => $locale->text('*/'))
251                        . $cgi->hidden(-name => "formel_$i", -value => $form->{"formel_$i"}) . $cgi->hidden("-name" => "alu_$i", "-value" => $form->{"alu_$i"})
252       if $form->{"formel_$i"};
253     $column_data{ship} = $cgi->textfield(-name => "ship_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"ship_$i"}));
254
255     # build in drop down list for pricesgroups
256     if ($form->{"prices_$i"}) {
257       $column_data{sellprice_pg} = qq|<select name="sellprice_pg_$i" style="width: 8em">$form->{"prices_$i"}</select>|;
258       $column_data{sellprice}    = $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => 'check_right_number_format(this)', -value =>
259                                    (($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})
260                                       ? $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces)
261                                       : $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces)));
262     } else {
263       # for last row and report
264       # set pricegroup drop down list from report menu
265       if ($form->{"sellprice_$i"} != 0) {
266         $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
267         my $default_option           = $form->{"sellprice_$i"}.'--'.$form->{"pricegroup_id_$i"};
268         $column_data{sellprice_pg}   = NTI($cgi->popup_menu("sellpricepg_$i", [ $default_option ], $default_option, { $default_option => $form->{"pricegroup_$i"} || '' }));
269       } else {
270         $column_data{sellprice_pg} = qq|&nbsp;|;
271       }
272       $column_data{sellprice} = $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value =>
273                                                 $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces));
274     }
275     $column_data{discount}    = $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
276     $column_data{linetotal}   = $form->format_amount(\%myconfig, $linetotal, 2);
277     $column_data{bin}         = $form->{"bin_$i"};
278
279     if ($is_delivery_order) {
280       $column_data{stock_in_out} = (!$form->{"assembly_$i"} && $form->{"inventory_accno_$i"}) ? calculate_stock_in_out($i) : '&nbsp;';
281     }
282
283     my @ROW1 = map { value => $column_data{$_}, align => $align{$_}, nowrap => $nowrap{$_} }, @column_index;
284
285     # second row
286     my @ROW2 = ();
287     push @ROW2, { value => qq|<b>$serialnumber</b> <input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">| } 
288       if $form->{type} !~ /_quotation/;
289     push @ROW2, { value => qq|<b>$projectnumber</b> | . NTI($cgi->popup_menu('-name'  => "project_id_$i",        '-values'  => \@projectnumber_values,
290                                                                              '-labels' => \%projectnumber_labels, '-default' => $form->{"project_id_$i"})) };
291     push @ROW2, { value => qq|<b>$reqdate</b> <input name="reqdate_$i" size="11" onBlur="check_right_date_format(this)" value="$form->{"reqdate_$i"}">| }
292       if $form->{type} =~ /order/;
293     push @ROW2, { value => sprintf qq|<b>%s</b>&nbsp;<input type="checkbox" name="subtotal_$i" value="1" %s>|, 
294                    $locale->text('Subtotal'), $form->{"subtotal_$i"} ? 'checked' : '' };
295
296 # begin marge calculations
297     my $marge_color;
298     my $real_sellprice     = ($form->{"sellprice_$i"} - $discount) / $price_factor;
299     my $marge_price_factor = $form->{"marge_price_factor_$i"} * 1 || 1;
300
301     $form->{"lastcost_$i"} *= 1;
302     $form->{"marge_percent_$i"} = 0;
303
304     if ($real_sellprice && ($form->{"qty_$i"} * 1)) {
305       $form->{"marge_percent_$i"}     = ($real_sellprice - $form->{"lastcost_$i"} / $marge_price_factor) * 100 / $real_sellprice;
306       $myconfig{marge_percent_warn} ||= 15;
307       $marge_color                    = 'color="#ff0000"' if $form->{"id_$i"} && ($form->{"marge_percent_$i"} < (1 * $myconfig{marge_percent_warn}));
308     }
309
310     my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
311     $form->{"marge_absolut_$i"}  = ($real_sellprice - $form->{"lastcost_$i"} / $marge_price_factor) * $form->{"qty_$i"} * $marge_adjust_credit_note;
312     $form->{"marge_total"}      += $form->{"marge_absolut_$i"};
313     $form->{"lastcost_total"}   += $form->{"lastcost_$i"} * $form->{"qty_$i"} / $marge_price_factor;
314     $form->{"sellprice_total"}  += $real_sellprice * $form->{"qty_$i"};
315
316     map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
317
318     push @ROW2, { value => sprintf qq|<font %s><b>%s</b> %s &nbsp;%s%% </font> &nbsp;<b>%s</b> %s &nbsp;<b>%s</b> %s|,
319                    $marge_color, $locale->text('Ertrag'),$form->{"marge_absolut_$i"}, $form->{"marge_percent_$i"},
320                    $locale->text('LP'), $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2),
321                    $locale->text('EK'), $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, 2) }
322       if $form->{"id_$i"} && $form->{type} =~ /^sales_/ && !$is_delivery_order;
323 # / marge calculations ending
324
325     my @hidden_vars;
326
327     if ($is_delivery_order) {
328       map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount);
329       push @hidden_vars, qw(sellprice discount price_factor_id);
330       push @hidden_vars, "stock_${stock_in_out}_sum_qty", "stock_${stock_in_out}";
331     }
332
333     my @HIDDENS = map { value => $_}, (
334           $cgi->hidden("-name" => "unit_old_$i", "-value" => $form->{"selected_unit_$i"}),
335           $cgi->hidden("-name" => "price_new_$i", "-value" => $form->format_amount(\%myconfig, $form->{"price_new_$i"})),
336           map { ($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } map { $_."_$i" } 
337             (qw(orderitems_id bo pricegroup_old price_old id inventory_accno bin partsgroup partnotes
338                 income_accno expense_accno listprice assembly taxaccounts ordnumber transdate cusordnumber
339                 longdescription basefactor marge_absolut marge_percent marge_price_factor lastcost), @hidden_vars)
340     );
341
342     map { $form->{"${_}_base"} += $linetotal } (split(/ /, $form->{"taxaccounts_$i"}));
343
344     $form->{invsubtotal} += $linetotal;
345
346     push @ROWS, { ROW1 => \@ROW1, ROW2 => \@ROW2, HIDDENS => \@HIDDENS, colspan => $colspan, error => $form->{"row_error_$i"}, };
347   }
348
349   print $form->parse_html_template('oe/sales_order', { ROWS   => \@ROWS,
350                                                        HEADER => \@HEADER,
351                                                      });
352
353   if (0 != ($form->{sellprice_total} * 1)) {
354     $form->{marge_percent} = ($form->{sellprice_total} - $form->{lastcost_total}) / $form->{sellprice_total} * 100;
355   }
356
357   $lxdebug->leave_sub();
358 }
359
360 ##################################################
361 # build html-code for pricegroups in variable $form->{prices_$j}
362
363 sub set_pricegroup {
364   $lxdebug->enter_sub();
365
366   _check_io_auth();
367
368   my $rowcount = shift;
369   for $j (1 .. $rowcount) {
370     next unless $form->{PRICES}{$j};
371     # build drop down list for pricegroups
372     my $option_tmpl = qq|<option value="%s--%s" %s>%s</option>|;
373     $form->{"prices_$j"}  = join '', map { sprintf $option_tmpl, @$_{qw(price pricegroup_id selected pricegroup)} } 
374                                          (+{ pricegroup => $locale->text("none (pricegroup)") }, @{ $form->{PRICES}{$j} });
375
376     foreach $item (@{ $form->{PRICES}{$j} }) {
377       # set new selectedpricegroup_id and prices for "Preis"
378       $form->{"pricegroup_old_$j"} = $item->{pricegroup_id}   if $item->{selected} &&  $item->{pricegroup_id};
379       $form->{"sellprice_$j"}      = $item->{price}           if $item->{selected} &&  $item->{pricegroup_id};
380       $form->{"price_new_$j"}      = $form->{"sellprice_$j"}  if $item->{selected} || !$item->{pricegroup_id};
381     }
382   }
383   $lxdebug->leave_sub();
384 }
385
386 sub select_item {
387   $lxdebug->enter_sub();
388
389   _check_io_auth();
390
391   @column_index = qw(ndx partnumber description onhand unit sellprice);
392
393   $column_data{ndx}        = qq|<th>&nbsp;</th>|;
394   $column_data{partnumber} =
395     qq|<th class="listheading">| . $locale->text('Number') . qq|</th>|;
396   $column_data{description} =
397     qq|<th class="listheading">| . $locale->text('Part Description') . qq|</th>|;
398   $column_data{sellprice} =
399     qq|<th class="listheading">| . $locale->text('Price') . qq|</th>|;
400   $column_data{onhand} =
401     qq|<th class="listheading">| . $locale->text('Qty') . qq|</th>|;
402   $column_data{unit} =
403     qq|<th class="listheading">| . $locale->text('Unit') . qq|</th>|;
404   # list items with radio button on a form
405   $form->header;
406
407   $title   = $locale->text('Select from one of the items below');
408   $colspan = $#column_index + 1;
409
410   print qq|
411   <body>
412
413 <form method="post" action="$form->{script}">
414
415 <table width="100%">
416   <tr>
417     <th class="listtop" colspan="$colspan">$title</th>
418   </tr>
419   <tr height="5"></tr>
420   <tr class="listheading">|;
421
422   map { print "\n$column_data{$_}" } @column_index;
423
424   print qq|</tr>|;
425
426   my $i = 0;
427   foreach $ref (@{ $form->{item_list} }) {
428     $checked = ($i++) ? "" : "checked";
429
430     if ($lizenzen) {
431       if ($ref->{inventory_accno} > 0) {
432         $ref->{"lizenzen"} = qq|<option></option>|;
433         foreach $item (@{ $form->{LIZENZEN}{ $ref->{"id"} } }) {
434           $ref->{"lizenzen"} .=
435             qq|<option value=\"$item->{"id"}\">$item->{"licensenumber"}</option>|;
436         }
437         $ref->{"lizenzen"} .= qq|<option value="-1">Neue Lizenz</option>|;
438         $ref->{"lizenzen"} =~ s/\"/&quot;/g;
439       }
440     }
441
442     map { $ref->{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
443
444     my $display_sellprice  = $ref->{sellprice} * (1 - $form->{tradediscount});
445     $display_sellprice    /= $ref->{price_factor} if ($ref->{price_factor});
446     $display_sellprice     = $form->format_amount(\%myconfig, $display_sellprice, 2);
447
448     $column_data{ndx} =
449       qq|<td><input name="ndx" class="radio" type="radio" value="$i" $checked></td>|;
450     $column_data{partnumber} =
451       qq|<td><input name="new_partnumber_$i" type="hidden" value="$ref->{partnumber}">$ref->{partnumber}</td>|;
452     $column_data{description} =
453       qq|<td><input name="new_description_$i" type="hidden" value="$ref->{description}">$ref->{description}</td>|;
454     $column_data{sellprice} =
455       qq|<td align="right"><input name="new_sellprice_$i" type="hidden" value="$ref->{sellprice}">|
456       . $display_sellprice
457       . qq|</td>|;
458     $column_data{onhand} =
459       qq|<td align="right"><input name="new_onhand_$i" type="hidden" value="$ref->{onhand}">|
460       . $form->format_amount(\%myconfig, $ref->{onhand}, '', "&nbsp;")
461       . qq|</td>|;
462     $column_data{unit} =
463       qq|<td>$ref->{unit}</td>|;
464     $j++;
465     $j %= 2;
466     print qq|
467 <tr class=listrow$j>|;
468
469     map { print "\n$column_data{$_}" } @column_index;
470
471     print("</tr>\n");
472
473     my @new_fields =
474       qw(bin listprice inventory_accno income_accno expense_accno unit weight
475          assembly taxaccounts partsgroup formel longdescription not_discountable
476          part_payment_id partnotes id lastcost price_factor_id price_factor);
477     push(@new_fields, "lizenzen") if ($lizenzen);
478
479     print join "\n", map { $cgi->hidden("-name" => "new_${_}_$i", "-value" => $ref->{$_}) } @new_fields;
480     print "\n";
481   }
482
483   print qq|
484 <tr><td colspan="8"><hr size="3" noshade></td></tr>
485 </table>
486
487 <input name="lastndx" type="hidden" value="$i">
488
489 |;
490
491   # delete action variable
492   map { delete $form->{$_} } qw(action item_list header);
493
494   # save all other form variables
495   foreach $key (keys %${form}) {
496     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
497     $form->{$key} =~ s/\"/&quot;/g;
498     print qq|<input name="$key" type="hidden" value="$form->{$key}">\n|;
499   }
500
501   print qq|
502 <input type="hidden" name="nextsub" value="item_selected">
503
504 <br>
505 <input class="submit" type="submit" name="action" value="|
506     . $locale->text('Continue') . qq|">
507 </form>
508
509 </body>
510 </html>
511 |;
512
513   $lxdebug->leave_sub();
514 }
515
516 sub item_selected {
517   $lxdebug->enter_sub();
518
519   _check_io_auth();
520
521   # replace the last row with the checked row
522   $i = $form->{rowcount};
523   $i = $form->{assembly_rows} if ($form->{item} eq 'assembly');
524
525   # index for new item
526   $j = $form->{ndx};
527
528   #sk
529   #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
530   #$form->{"sellprice_$i"} = $form->{"sellprice_$i"};
531
532   # if there was a price entered, override it
533   $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
534
535   my @new_fields =
536     qw(id partnumber description sellprice listprice inventory_accno
537        income_accno expense_accno bin unit weight assembly taxaccounts
538        partsgroup formel longdescription not_discountable partnotes lastcost
539        price_factor_id price_factor);
540
541   map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } @new_fields;
542
543   $form->{"marge_price_factor_$i"} = $form->{"new_price_factor_$j"};
544
545   if ($form->{"part_payment_id_$i"} ne "") {
546     $form->{payment_id} = $form->{"part_payment_id_$i"};
547   }
548
549   if ($lizenzen) {
550     map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } qw(lizenzen);
551   }
552
553   ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
554   $dec           = length $dec;
555   $decimalplaces = ($dec > 2) ? $dec : 2;
556
557   if ($sellprice) {
558     $form->{"sellprice_$i"} = $sellprice;
559   } else {
560
561     # if there is an exchange rate adjust sellprice
562     if (($form->{exchangerate} * 1) != 0) {
563       $form->{"sellprice_$i"} /= $form->{exchangerate};
564       $form->{"sellprice_$i"} =
565         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
566     }
567   }
568
569   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
570     qw(sellprice listprice weight);
571
572   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
573   $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
574   
575   if ($form->{"not_discountable_$i"}) {
576     $form->{"discount_$i"} = 0;
577   }
578
579   $amount =
580     $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
581     $form->{"qty_$i"};
582   map { $form->{"${_}_base"} += $amount }
583     (split / /, $form->{"taxaccounts_$i"});
584   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
585     $form->{"taxaccounts_$i"}
586     if !$form->{taxincluded};
587
588   $form->{creditremaining} -= $amount;
589
590   $form->{"runningnumber_$i"} = $i;
591
592   # delete all the new_ variables
593   for $i (1 .. $form->{lastndx}) {
594     map { delete $form->{"new_${_}_$i"} } @new_fields;
595   }
596
597   map { delete $form->{$_} } qw(ndx lastndx nextsub);
598
599   # format amounts
600   map {
601     $form->{"${_}_$i"} =
602       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
603   } qw(sellprice listprice) if $form->{item} ne 'assembly';
604
605   # get pricegroups for parts
606   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
607
608   # build up html code for prices_$i
609   set_pricegroup($form->{rowcount});
610
611   &display_form;
612
613   $lxdebug->leave_sub();
614 }
615
616 sub new_item {
617   $lxdebug->enter_sub();
618
619   _check_io_auth();
620
621   my $price_key = ($form->{type} =~ m/request_quotation|purchase_order/) || ($form->{script} eq 'ir.pl') ? 'lastcost' : 'sellprice';
622
623   # change callback
624   $form->{old_callback} = $form->escape($form->{callback}, 1);
625   $form->{callback}     = $form->escape("$form->{script}?action=display_form", 1);
626
627   # save all form variables except action in a previousform variable
628   my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
629
630   push @HIDDENS,      { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
631   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{$_} },                       qw(rowcount vc);
632   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{"${_}_$form->{rowcount}"} }, qw(partnumber description unit);
633   push @HIDDENS,      { 'name' => 'taxaccount2',  'value' => $form->{taxaccounts} };
634   push @HIDDENS,      { 'name' => $price_key,     'value' => $form->parse_amount(\%myconfig, $form->{"sellprice_$form->{rowcount}"}) };
635
636   $form->header();
637   print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
638
639   $lxdebug->leave_sub();
640 }
641
642 sub check_form {
643   $lxdebug->enter_sub();
644
645   _check_io_auth();
646
647   my @a     = ();
648   my $count = 0;
649
650   my @flds = qw(id partnumber description qty ship sellprice unit
651                 discount inventory_accno income_accno expense_accno listprice
652                 taxaccounts bin assembly weight projectnumber project_id
653                 oldprojectnumber runningnumber serialnumber partsgroup payment_id
654                 not_discountable shop ve gv buchungsgruppen_id language_values
655                 sellprice_pg pricegroup_old price_old price_new unit_old ordnumber
656                 transdate longdescription basefactor marge_total marge_percent
657                 marge_price_factor lastcost price_factor_id
658                 stock_out stock_in);
659
660   # remove any makes or model rows
661   if ($form->{item} eq 'part') {
662     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
663       qw(listprice sellprice lastcost weight rop);
664
665   } elsif ($form->{item} eq 'assembly') {
666
667     $form->{sellprice} = 0;
668     $form->{weight}    = 0;
669     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
670       qw(listprice rop stock);
671
672     @flds =
673       qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup);
674
675     for my $i (1 .. ($form->{assembly_rows} - 1)) {
676       if ($form->{"qty_$i"}) {
677         push @a, {};
678         my $j = $#a;
679
680         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
681
682         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
683
684         #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
685
686         $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"});
687         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"});
688         $count++;
689       }
690     }
691
692     $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
693
694     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
695     $form->{assembly_rows} = $count;
696
697   } elsif ($form->{item} eq 'service') {
698     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(listprice sellprice lastcost);
699
700   } else {
701
702     # this section applies to invoices and orders
703     # remove any empty numbers
704     if ($form->{rowcount}) {
705       for my $i (1 .. $form->{rowcount} - 1) {
706         if ($form->{"partnumber_$i"}) {
707           push @a, {};
708           my $j = $#a;
709
710           map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
711           $count++;
712           if ($lizenzen) {
713             if ($form->{"licensenumber_$i"} == -1) {
714               &new_license($i);
715               exit;
716             }
717           }
718         }
719       }
720
721       $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
722       $form->{rowcount} = $count;
723
724       $form->{creditremaining} -= &invoicetotal;
725
726     }
727   }
728
729   #sk
730   # if pricegroups
731   if (   $form->{type} =~ (/sales_quotation/)
732       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
733       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
734       or ($form->{type} =~ /sales_order/)) {
735
736     # get pricegroups for parts
737     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
738
739     # build up html code for prices_$i
740     set_pricegroup($form->{rowcount});
741
742   }
743
744   &display_form;
745
746   $lxdebug->leave_sub();
747 }
748
749 sub invoicetotal {
750   $lxdebug->enter_sub();
751
752   _check_io_auth();
753
754   $form->{oldinvtotal} = 0;
755
756   # add all parts and deduct paid
757   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
758
759   my ($amount, $sellprice, $discount, $qty);
760
761   for my $i (1 .. $form->{rowcount}) {
762     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
763     $discount  = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
764     $qty       = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
765
766     #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
767
768     $amount = $sellprice * (1 - $discount / 100) * $qty;
769     map { $form->{"${_}_base"} += $amount }
770       (split (/ /, $form->{"taxaccounts_$i"}));
771     $form->{oldinvtotal} += $amount;
772   }
773
774   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
775     split(/ /, $form->{taxaccounts})
776     if !$form->{taxincluded};
777
778   $form->{oldtotalpaid} = 0;
779   for $i (1 .. $form->{paidaccounts}) {
780     $form->{oldtotalpaid} += $form->{"paid_$i"};
781   }
782
783   $lxdebug->leave_sub();
784
785   # return total
786   return ($form->{oldinvtotal} - $form->{oldtotalpaid});
787 }
788
789 sub validate_items {
790   $lxdebug->enter_sub();
791
792   _check_io_auth();
793
794   # check if items are valid
795   if ($form->{rowcount} == 1) {
796     &update;
797     exit;
798   }
799
800   for $i (1 .. $form->{rowcount} - 1) {
801     $form->isblank("partnumber_$i",
802                    $locale->text('Number missing in Row') . " $i");
803   }
804
805   $lxdebug->leave_sub();
806 }
807
808 sub order {
809   $lxdebug->enter_sub();
810
811   _check_io_auth();
812
813   if ($form->{second_run}) {
814     $form->{print_and_post} = 0;
815   }
816   $form->{ordnumber} = $form->{invnumber};
817
818   $form->{old_employee_id} = $form->{employee_id};
819   $form->{old_salesman_id} = $form->{salesman_id};
820
821   map { delete $form->{$_} } qw(id printed emailed queued);
822   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
823     $form->{title} = $locale->text('Add Purchase Order');
824     $form->{vc}    = 'vendor';
825     $form->{type}  = 'purchase_order';
826     $buysell       = 'sell';
827   }
828   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
829     $form->{title} = $locale->text('Add Sales Order');
830     $form->{vc}    = 'customer';
831     $form->{type}  = 'sales_order';
832     $buysell       = 'buy';
833   }
834   $form->{script} = 'oe.pl';
835
836   $form->{shipto} = 1;
837
838   $form->{rowcount}--;
839
840   $form->{cp_id} *= 1;
841
842   require "bin/mozilla/$form->{script}";
843   my $script = $form->{"script"};
844   $script =~ s|.*/||;
845   $script =~ s|.pl$||;
846   $locale = new Locale($language, $script);
847
848   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
849
850   $currency = $form->{currency};
851
852   &order_links;
853
854   $form->{currency}     = $currency;
855   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
856   $form->{exchangerate} = $form->{forex} || '';
857
858   for $i (1 .. $form->{rowcount}) {
859     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"})
860             if ($form->{"${_}_${i}"}) }
861         qw(ship qty sellprice listprice basefactor));
862   }
863
864   &prepare_order;
865   &display_form;
866
867   $lxdebug->leave_sub();
868 }
869
870 sub quotation {
871   $lxdebug->enter_sub();
872
873   _check_io_auth();
874
875   if ($form->{second_run}) {
876     $form->{print_and_post} = 0;
877   }
878   map { delete $form->{$_} } qw(id printed emailed queued);
879
880   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
881     $form->{title} = $locale->text('Add Request for Quotation');
882     $form->{vc}    = 'vendor';
883     $form->{type}  = 'request_quotation';
884     $buysell       = 'sell';
885   }
886   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
887     $form->{title} = $locale->text('Add Quotation');
888     $form->{vc}    = 'customer';
889     $form->{type}  = 'sales_quotation';
890     $buysell       = 'buy';
891   }
892
893   $form->{cp_id} *= 1;
894
895   $form->{script} = 'oe.pl';
896
897   $form->{shipto} = 1;
898
899   $form->{rowcount}--;
900
901   require "bin/mozilla/$form->{script}";
902
903   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
904
905   $currency = $form->{currency};
906
907   &order_links;
908
909   $form->{currency}     = $currency;
910   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, $buysell);
911   $form->{exchangerate} = $form->{forex} || '';
912
913   for $i (1 .. $form->{rowcount}) {
914     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
915                                                      $form->{"${_}_${i}"})
916             if ($form->{"${_}_${i}"}) }
917         qw(ship qty sellprice listprice basefactor));
918   }
919
920   &prepare_order;
921   &display_form;
922
923   $lxdebug->leave_sub();
924 }
925
926 sub request_for_quotation {
927   quotation();
928 }
929
930 sub edit_e_mail {
931   $lxdebug->enter_sub();
932
933   _check_io_auth();
934
935   if ($form->{second_run}) {
936     $form->{print_and_post} = 0;
937     $form->{resubmit}       = 0;
938   }
939
940   $form->{email} = $form->{shiptoemail} if $form->{shiptoemail} && $form->{formname} =~ /(pick|packing|bin)_list/;
941
942   if ($form->{"cp_id"} && !$form->{"email"}) {
943     CT->get_contact(\%myconfig, $form);
944     $form->{"email"} = $form->{"cp_email"};
945   }
946
947   $title = $locale->text('E-mail') . " " . $form->get_formname_translation();
948
949   $form->{oldmedia} = $form->{media};
950   $form->{media}    = "email";
951
952   my $attachment_filename = $form->generate_attachment_filename();
953   my $subject             = $form->{subject} || $form->generate_email_subject();
954
955   $form->{"fokus"} = $form->{"email"} ? "Form.subject" : "Form.email";
956   $form->header;
957
958   my (@dont_hide_key_list, %dont_hide_key, @hidden_keys);
959   @dont_hide_key_list = qw(action email cc bcc subject message sendmode format header override login password);
960   @dont_hide_key{@dont_hide_key_list} = (1) x @dont_hide_key_list;
961   @hidden_keys = sort grep { !$dont_hide_key{$_} } grep { !ref $form->{$_} } keys %$form;
962
963   print $form->parse_html_template('generic/edit_email',
964                                    { title           => $title,
965                                      a_filename      => $attachment_filename,
966                                      subject         => $subject,
967                                      _print_options_ => print_options('inline' => 1),
968                                      HIDDEN          => [ map +{ name => $_, value => $form->{$_} }, @hidden_keys ],
969                                      SHOW_BCC        => $myconfig{role} eq 'admin' });
970
971   $lxdebug->leave_sub();
972 }
973
974 sub send_email {
975   $lxdebug->enter_sub();
976
977   _check_io_auth();
978
979   my $callback = $form->{script} . "?action=edit";
980   map({ $callback .= "\&${_}=" . E($form->{$_}); } qw(type id));
981
982   print_form("return");
983
984   Common->save_email_status(\%myconfig, $form);
985
986   $form->{callback} = $callback;
987   $form->redirect();
988
989   $lxdebug->leave_sub();
990 }
991
992 # generate the printing options displayed at the bottom of oe and is forms.
993 # this function will attempt to guess what type of form is displayed, and will generate according options
994 #
995 # about the coding: 
996 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
997 # the opthash function builds hashrefs which are then pieced together for the template arrays.
998 # unneeded options are "undef"ed out, and then grepped out. 
999 #
1000 # the inline options is untested, but intended to be used later in metatemplating
1001 sub print_options {
1002   $lxdebug->enter_sub();
1003
1004   _check_io_auth();
1005
1006   my %options = @_;
1007
1008   # names 3 parameters and returns a hashref, for use in templates
1009   sub opthash { +{ value => shift, selected => shift, oname => shift } }
1010   (@FORMNAME, @FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
1011
1012   # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
1013   $form->{sendmode}   = "attachment";
1014   $form->{format}     = $form->{format} || $myconfig{template_format} || "pdf";
1015   $form->{copies}     = $form->{copies} || $myconfig{copies}          || 3;
1016   $form->{media}      = $form->{media}  || $myconfig{default_media}   || "screen";
1017   $form->{printer_id} = defined $form->{printer_id}           ? $form->{printer_id} :
1018                         defined $myconfig{default_printer_id} ? $myconfig{default_printer_id} : "";
1019
1020   $form->{PD}{ $form->{formname} } = "selected";
1021   $form->{DF}{ $form->{format} }   = "selected";
1022   $form->{OP}{ $form->{media} }    = "selected";
1023   $form->{SM}{ $form->{formname} } = "selected";
1024
1025   push @FORMNAME, grep $_,
1026     ($form->{type} eq 'purchase_order') ? (
1027       opthash("purchase_order",      $form->{PD}{purchase_order},      $locale->text('Purchase Order')),
1028       opthash("bin_list",            $form->{PD}{bin_list},            $locale->text('Bin List')) 
1029     ) : undef,
1030     ($form->{type} eq 'credit_note') ?
1031       opthash("credit_note",         $form->{PD}{credit_note},         $locale->text('Credit Note')) : undef,
1032     ($form->{type} eq 'sales_order') ? (
1033       opthash("sales_order",         $form->{PD}{sales_order},         $locale->text('Confirmation')),
1034       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
1035     ) : undef,
1036     ($form->{type} =~ /sales_quotation$/) ?
1037       opthash('sales_quotation',     $form->{PD}{sales_quotation},     $locale->text('Quotation')) : undef,
1038     ($form->{type} =~ /request_quotation$/) ?
1039       opthash('request_quotation',   $form->{PD}{request_quotation},   $locale->text('Request for Quotation')) : undef,
1040     ($form->{type} eq 'invoice') ? (
1041       opthash("invoice",             $form->{PD}{invoice},             $locale->text('Invoice')),
1042       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
1043     ) : undef,
1044     ($form->{type} eq 'invoice' && $form->{storno}) ? (
1045       opthash("storno_invoice",      $form->{PD}{storno_invoice},      $locale->text('Storno Invoice')),
1046       opthash("storno_packing_list", $form->{PD}{storno_packing_list}, $locale->text('Storno Packing List')) 
1047     ) : undef,
1048     ($form->{type} =~ /_delivery_order$/) ? (
1049       opthash($form->{type},         $form->{PD}{$form->{type}},       $locale->text('Delivery Order')),
1050       opthash('pick_list',           $form->{PD}{pick_list},           $locale->text('Pick List')),
1051     ) : undef;
1052     ($form->{type} eq 'credit_note') ?
1053       opthash("credit_note",         $form->{PD}{credit_note},         $locale->text('Credit Note')) : undef;
1054
1055   push @SENDMODE, 
1056     opthash("attachment",            $form->{SM}{attachment},          $locale->text('Attachment')),
1057     opthash("inline",                $form->{SM}{inline},              $locale->text('In-line'))
1058       if ($form->{media} eq 'email');
1059
1060   push @MEDIA, grep $_,
1061       opthash("screen",              $form->{OP}{screen},              $locale->text('Screen')),
1062     (scalar @{ $form->{printers} } && $latex_templates) ?
1063       opthash("printer",             $form->{OP}{printer},             $locale->text('Printer')) : undef,
1064     ($latex_templates && !$options{no_queue}) ?
1065       opthash("queue",               $form->{OP}{queue},               $locale->text('Queue')) : undef
1066         if ($form->{media} ne 'email');
1067
1068   push @FORMAT, grep $_,
1069     ($opendocument_templates && $openofficeorg_writer_bin && $xvfb_bin && (-x $openofficeorg_writer_bin) && (-x $xvfb_bin)
1070      && !$options{no_opendocument_pdf}) ?
1071       opthash("opendocument_pdf",    $form->{DF}{"opendocument_pdf"},  $locale->text("PDF (OpenDocument/OASIS)")) : undef,
1072     ($latex_templates) ?
1073       opthash("pdf",                 $form->{DF}{pdf},                 $locale->text('PDF')) : undef,
1074     ($latex_templates && !$options{no_postscript}) ?
1075       opthash("postscript",          $form->{DF}{postscript},          $locale->text('Postscript')) : undef,
1076     (!$options{no_html}) ?
1077       opthash("html", $form->{DF}{html}, "HTML") : undef,
1078     ($opendocument_templates && !$options{no_opendocument}) ?
1079       opthash("opendocument",        $form->{DF}{opendocument},        $locale->text("OpenDocument/OASIS")) : undef;
1080
1081   push @LANGUAGE_ID, 
1082     map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
1083       if (ref $form->{languages} eq 'ARRAY');
1084
1085   push @PRINTER_ID, 
1086     map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
1087       if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
1088
1089   @SELECTS = map { sname => lc $_, DATA => \@$_, show => !$options{"hide_" . lc($_)} && scalar @$_ }, qw(FORMNAME LANGUAGE_ID FORMAT SENDMODE MEDIA PRINTER_ID);
1090
1091   my %dont_display_groupitems = (
1092     'dunning' => 1,
1093     );
1094
1095   %template_vars = (
1096     display_copies       => scalar @{ $form->{printers} } && $latex_templates && $form->{media} ne 'email',
1097     display_remove_draft => (!$form->{id} && $form->{draft_id}),
1098     display_groupitems   => !$dont_display_groupitems{$form->{type}},
1099     groupitems_checked   => $form->{groupitems} ? "checked" : '',
1100     remove_draft_checked => $form->{remove_draft} ? "checked" : ''
1101   );
1102
1103   my $print_options = $form->parse_html_template("generic/print_options", { SELECTS  => \@SELECTS, %template_vars } );
1104
1105   if ($options{inline}) {
1106     $lxdebug->leave_sub();
1107     return $print_options;
1108   }
1109
1110   print $print_options;
1111
1112   $lxdebug->leave_sub();
1113 }
1114
1115 sub print {
1116   $lxdebug->enter_sub();
1117
1118   _check_io_auth();
1119
1120   if ($form->{print_nextsub}) {
1121     call_sub($form->{print_nextsub});
1122     $lxdebug->leave_sub();
1123     return;
1124   }
1125
1126   # if this goes to the printer pass through
1127   if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1128     $form->error($locale->text('Select postscript or PDF!'))
1129       if ($form->{format} !~ /(postscript|pdf)/);
1130
1131     $old_form = new Form;
1132     map { $old_form->{$_} = $form->{$_} } keys %$form;
1133   }
1134
1135   if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1136     if ($form->{formname} eq "proforma") {
1137       $form->{proforma} = 1;
1138     }
1139     $form->{print_and_save} = 1;
1140     my $formname = $form->{formname};
1141     &save();
1142     $form->{formname} = $formname;
1143     &edit();
1144     exit;
1145   }
1146
1147   &print_form($old_form);
1148
1149   $lxdebug->leave_sub();
1150 }
1151
1152 sub print_form {
1153   $lxdebug->enter_sub();
1154
1155   _check_io_auth();
1156
1157   my ($old_form) = @_;
1158
1159   $inv       = "inv";
1160   $due       = "due";
1161   $numberfld = "invnumber";
1162
1163   $display_form =
1164     ($form->{display_form}) ? $form->{display_form} : "display_form";
1165
1166   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1167   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1168
1169   if ($form->{formname} eq "invoice") {
1170     $form->{label} = $locale->text('Invoice');
1171   }
1172   if ($form->{formname} eq "packing_list") {
1173
1174     # this is from an invoice
1175     $form->{label} = $locale->text('Packing List');
1176   }
1177   if ($form->{formname} eq 'sales_order') {
1178     $inv                  = "ord";
1179     $due                  = "req";
1180     $form->{"${inv}date"} = $form->{transdate};
1181     $form->{label}        = $locale->text('Confirmation');
1182     $numberfld            = "sonumber";
1183     $order                = 1;
1184   }
1185
1186   if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1187     $inv                  = "inv";
1188     $due                  = "due";
1189     $form->{"${inv}date"} = $form->{invdate};
1190     $form->{label}        = $locale->text('Proforma Invoice');
1191     $numberfld            = "sonumber";
1192     $order                = 0;
1193   }
1194
1195   if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1196     $inv                  = "inv";
1197     $due                  = "due";
1198     $form->{"${inv}date"} = $form->{transdate};
1199     $form->{"invdate"}    = $form->{transdate};
1200     $form->{invnumber}    = $form->{ordnumber};
1201     $form->{label}        = $locale->text('Proforma Invoice');
1202     $numberfld            = "sonumber";
1203     $order                = 1;
1204   }
1205
1206   if ($form->{formname} eq 'packing_list' && $form->{type} ne 'invoice') {
1207
1208     # we use the same packing list as from an invoice
1209     $inv = "ord";
1210     $due = "req";
1211     $form->{invdate} = $form->{"${inv}date"} = $form->{transdate};
1212     $form->{label} = $locale->text('Packing List');
1213     $order = 1;
1214     # set invnumber for template packing_list 
1215     $form->{invnumber}   = $form->{ordnumber};
1216   }
1217   if ($form->{formname} eq 'purchase_order') {
1218     $inv                  = "ord";
1219     $due                  = "req";
1220     $form->{"${inv}date"} = $form->{transdate};
1221     $form->{label}        = $locale->text('Purchase Order');
1222     $numberfld            = "ponumber";
1223     $order                = 1;
1224   }
1225   if ($form->{formname} eq 'bin_list') {
1226     $inv                  = "ord";
1227     $due                  = "req";
1228     $form->{"${inv}date"} = $form->{transdate};
1229     $form->{label}        = $locale->text('Bin List');
1230     $order                = 1;
1231   }
1232   if ($form->{formname} eq 'sales_quotation') {
1233     $inv                  = "quo";
1234     $due                  = "req";
1235     $form->{"${inv}date"} = $form->{transdate};
1236     $form->{label}        = $locale->text('Quotation');
1237     $numberfld            = "sqnumber";
1238     $order                = 1;
1239   }
1240
1241   if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1242     $inv                  = "quo";
1243     $due                  = "req";
1244     $form->{"${inv}date"} = $form->{transdate};
1245     $form->{"invdate"}    = $form->{transdate};
1246     $form->{label}        = $locale->text('Proforma Invoice');
1247     $numberfld            = "sqnumber";
1248     $order                = 1;
1249   }
1250
1251   if ($form->{formname} eq 'request_quotation') {
1252     $inv                  = "quo";
1253     $due                  = "req";
1254     $form->{"${inv}date"} = $form->{transdate};
1255     $form->{label}        = $locale->text('RFQ');
1256     $numberfld            = "rfqnumber";
1257     $order                = 1;
1258   }
1259
1260   if ($form->{type} =~ /_delivery_order$/) {
1261     undef $due;
1262     $inv                  = "do";
1263     $form->{"${inv}date"} = $form->{transdate};
1264     $numberfld            = $form->{type} =~ /^sales/ ? 'sdonumber' : 'pdonumber';
1265     $form->{label}        = $form->{formname} eq 'pick_list' ? $locale->text('Pick List') : $locale->text('Delivery Order');
1266   }
1267
1268   $form->isblank("email", $locale->text('E-mail address missing!'))
1269     if ($form->{media} eq 'email');
1270   $form->isblank("${inv}date",
1271            $locale->text($form->{label}) 
1272            . ": "
1273            . $locale->text(' Date missing!'));
1274
1275   # $locale->text('Invoice Number missing!')
1276   # $locale->text('Invoice Date missing!')
1277   # $locale->text('Packing List Number missing!')
1278   # $locale->text('Packing List Date missing!')
1279   # $locale->text('Order Number missing!')
1280   # $locale->text('Order Date missing!')
1281   # $locale->text('Quotation Number missing!')
1282   # $locale->text('Quotation Date missing!')
1283
1284   # assign number
1285   $form->{what_done} = $form->{formname};
1286   if (!$form->{"${inv}number"} && !$form->{preview} && !$form->{id}) {
1287     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1288     if ($form->{media} ne 'email') {
1289
1290       # get pricegroups for parts
1291       IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1292
1293       # build up html code for prices_$i
1294       set_pricegroup($form->{rowcount});
1295
1296       $form->{rowcount}--;
1297
1298       call_sub($display_form);
1299       # saving the history
1300           if(!exists $form->{addition}) {
1301         $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber}; 
1302             $form->{addition} = "PRINTED";
1303             $form->save_history($form->dbconnect(\%myconfig));
1304       }
1305       # /saving the history
1306       exit;
1307     }
1308   }
1309
1310   &validate_items;
1311
1312   # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1313   my ($saved_email, $saved_cc, $saved_bcc) =
1314     ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1315
1316   $language_saved = $form->{language_id};
1317   $payment_id_saved = $form->{payment_id};
1318   $salesman_id_saved = $form->{salesman_id};
1319   $cp_id_saved = $form->{cp_id};
1320
1321   call_sub("$form->{vc}_details") if ($form->{vc});
1322
1323   $form->{language_id} = $language_saved;
1324   $form->{payment_id} = $payment_id_saved;
1325
1326   $form->{"email"} = $saved_email if ($saved_email);
1327   $form->{"cc"}    = $saved_cc    if ($saved_cc);
1328   $form->{"bcc"}   = $saved_bcc   if ($saved_bcc);
1329
1330   if (!$cp_id_saved) {
1331     # No contact was selected. Delete all contact variables because
1332     # IS->customer_details() and IR->vendor_details() get the default
1333     # contact anyway.
1334     map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1335   }
1336
1337   my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
1338   if ($form->{"language_id"}) {
1339     ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
1340       AM->get_language_details(\%myconfig, $form, $form->{language_id});
1341   } else {
1342     $output_dateformat = $myconfig{"dateformat"};
1343     $output_numberformat = $myconfig{"numberformat"};
1344     $output_longdates = 1;
1345   }
1346
1347   ($form->{employee}) = split /--/, $form->{employee};
1348
1349   # create the form variables
1350   if ($form->{type} =~ /_delivery_order$/) {
1351     DO->order_details();
1352   } elsif ($order) {
1353     OE->order_details(\%myconfig, \%$form);
1354   } else {
1355     IS->invoice_details(\%myconfig, \%$form, $locale);
1356   }
1357
1358   $form->get_salesman(\%myconfig, $salesman_id_saved);
1359
1360   if ($form->{shipto_id}) {
1361     $form->get_shipto(\%myconfig);
1362   }
1363
1364   @a = qw(name street zipcode city country);
1365
1366   $shipto = 1;
1367
1368   # if there is no shipto fill it in from billto
1369   foreach $item (@a) {
1370     if ($form->{"shipto$item"}) {
1371       $shipto = 0;
1372       last;
1373     }
1374   }
1375
1376   if ($shipto) {
1377     if (   $form->{formname} eq 'purchase_order'
1378         || $form->{formname} eq 'request_quotation') {
1379       $form->{shiptoname}   = $myconfig{company};
1380       $form->{shiptostreet} = $myconfig{address};
1381     } else {
1382       map { $form->{"shipto$_"} = $form->{$_} } @a;
1383     }
1384   }
1385
1386   $form->{notes} =~ s/^\s+//g;
1387
1388   $form->{templates} = "$myconfig{templates}";
1389
1390   delete $form->{printer_command};
1391
1392   $form->{language} = $form->get_template_language(\%myconfig);
1393
1394   my $printer_code;
1395   if ($form->{media} ne 'email') {
1396     $printer_code = $form->get_printer_code(\%myconfig);
1397     if ($printer_code ne "") {
1398       $printer_code = "_" . $printer_code;
1399     }
1400   }
1401
1402   if ($form->{language} ne "") {
1403     map({ $form->{"unit"}->[$_] =
1404             AM->translate_units($form, $form->{"language"},
1405                                 $form->{"unit"}->[$_], $form->{"qty"}->[$_]); }
1406         (0..scalar(@{$form->{"unit"}}) - 1));
1407     $form->{language} = "_" . $form->{language};
1408   }
1409
1410   # Format dates.
1411   format_dates($output_dateformat, $output_longdates,
1412                qw(invdate orddate quodate pldate duedate reqdate transdate
1413                   shippingdate deliverydate validitydate paymentdate
1414                   datepaid transdate_oe deliverydate_oe
1415                   employee_startdate employee_enddate
1416                   ),
1417                grep({ /^datepaid_\d+$/ ||
1418                         /^transdate_oe_\d+$/ ||
1419                         /^deliverydate_oe_\d+$/ ||
1420                         /^reqdate_\d+$/ ||
1421                         /^deliverydate_\d+$/ ||
1422                         /^transdate_\d+$/
1423                     } keys(%{$form})));
1424
1425   reformat_numbers($output_numberformat, 2,
1426                    qw(invtotal ordtotal quototal subtotal linetotal
1427                       listprice sellprice netprice discount
1428                       tax taxbase total paid),
1429                    grep({ /^linetotal_\d+$/ ||
1430                             /^listprice_\d+$/ ||
1431                             /^sellprice_\d+$/ ||
1432                             /^netprice_\d+$/ ||
1433                             /^taxbase_\d+$/ ||
1434                             /^discount_\d+$/ ||
1435                             /^paid_\d+$/ ||
1436                             /^subtotal_\d+$/ ||
1437                             /^total_\d+$/ ||
1438                             /^tax_\d+$/
1439                         } keys(%{$form})));
1440
1441   reformat_numbers($output_numberformat, undef,
1442                    qw(qty price_factor),
1443                    grep({ /^qty_\d+$/
1444                         } keys(%{$form})));
1445
1446   my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
1447
1448   if (scalar @{ $cvar_date_fields }) {
1449     format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
1450   }
1451
1452   while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
1453     reformat_numbers($output_numberformat, $precision, @{ $field_list });
1454   }
1455
1456   $form->{IN} = "$form->{formname}$form->{language}${printer_code}.html";
1457   if ($form->{format} eq 'postscript') {
1458     $form->{postscript} = 1;
1459     $form->{IN} =~ s/html$/tex/;
1460   } elsif ($form->{"format"} =~ /pdf/) {
1461     $form->{pdf} = 1;
1462     if ($form->{"format"} =~ /opendocument/) {
1463       $form->{IN} =~ s/html$/odt/;
1464     } else {
1465       $form->{IN} =~ s/html$/tex/;
1466     }
1467   } elsif ($form->{"format"} =~ /opendocument/) {
1468     $form->{"opendocument"} = 1;
1469     $form->{"IN"} =~ s/html$/odt/;
1470   }
1471
1472   delete $form->{OUT};
1473
1474   if ($form->{media} eq 'printer') {
1475     $form->{OUT} = "| $form->{printer_command} &>/dev/null";
1476     $form->{printed} .= " $form->{formname}";
1477     $form->{printed} =~ s/^ //;
1478   }
1479   $printed = $form->{printed};
1480
1481   if ($form->{media} eq 'email') {
1482     $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1483       unless $form->{subject};
1484
1485     $form->{emailed} .= " $form->{formname}";
1486     $form->{emailed} =~ s/^ //;
1487   }
1488   $emailed = $form->{emailed};
1489
1490   if ($form->{media} eq 'queue') {
1491     %queued = map { s|.*/|| } split / /, $form->{queued};
1492
1493     if ($filename = $queued{ $form->{formname} }) {
1494       $form->{queued} =~ s/\Q$form->{formname} $filename\E//;
1495       unlink "$spool/$filename";
1496       $filename =~ s/\..*$//g;
1497     } else {
1498       $filename = time;
1499       $filename .= $$;
1500     }
1501
1502     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
1503     $form->{OUT} = ">$spool/$filename";
1504
1505     # add type
1506     $form->{queued} .= " $form->{formname} $filename";
1507
1508     $form->{queued} =~ s/^ //;
1509   }
1510   $queued = $form->{queued};
1511
1512 # saving the history
1513   if(!exists $form->{addition}) {
1514     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1515     if($form->{media} =~ /printer/) {
1516         $form->{addition} = "PRINTED";
1517     }
1518     elsif($form->{media} =~ /email/) {
1519         $form->{addition} = "MAILED";
1520     }
1521     elsif($form->{media} =~ /queue/) {
1522         $form->{addition} = "QUEUED";
1523     }
1524     elsif($form->{media} =~ /screen/) {
1525         $form->{addition} = "SCREENED";
1526     }
1527     $form->save_history($form->dbconnect(\%myconfig));
1528   }
1529   # /saving the history
1530
1531   $form->parse_template(\%myconfig, $userspath);
1532
1533   $form->{callback} = "";
1534
1535   if ($form->{media} eq 'email') {
1536     $form->{message} = $locale->text('sent') unless $form->{message};
1537   }
1538   $message = $form->{message};
1539
1540   # if we got back here restore the previous form
1541   if ($form->{media} =~ /(printer|email|queue)/) {
1542
1543     $form->update_status(\%myconfig)
1544       if ($form->{media} eq 'queue' && $form->{id});
1545
1546     return $lxdebug->leave_sub() if ($old_form eq "return");
1547
1548     if ($old_form) {
1549
1550       $old_form->{"${inv}number"} = $form->{"${inv}number"};
1551
1552       # restore and display form
1553       map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1554
1555       $form->{queued}  = $queued;
1556       $form->{printed} = $printed;
1557       $form->{emailed} = $emailed;
1558       $form->{message} = $message;
1559
1560       $form->{rowcount}--;
1561       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1562         qw(exchangerate creditlimit creditremaining);
1563
1564       for $i (1 .. $form->{paidaccounts}) {
1565         map {
1566           $form->{"${_}_$i"} =
1567             $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1568         } qw(paid exchangerate);
1569       }
1570
1571       call_sub($display_form);
1572       exit;
1573     }
1574
1575     $msg =
1576       ($form->{media} eq 'printer')
1577       ? $locale->text('sent to printer')
1578       : $locale->text('emailed to') . " $form->{email}";
1579     $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1580   }
1581   if ($form->{printing}) {
1582    call_sub($display_form);
1583    exit; 
1584   }
1585
1586   $lxdebug->leave_sub();
1587 }
1588
1589 sub customer_details {
1590   $lxdebug->enter_sub();
1591
1592   IS->customer_details(\%myconfig, \%$form, @_);
1593
1594   $lxdebug->leave_sub();
1595 }
1596
1597 sub vendor_details {
1598   $lxdebug->enter_sub();
1599
1600   IR->vendor_details(\%myconfig, \%$form, @_);
1601
1602   $lxdebug->leave_sub();
1603 }
1604
1605 sub post_as_new {
1606   $lxdebug->enter_sub();
1607
1608   _check_io_auth();
1609
1610   $form->{postasnew} = 1;
1611   map { delete $form->{$_} } qw(printed emailed queued);
1612
1613   &post;
1614
1615   $lxdebug->leave_sub();
1616 }
1617
1618 sub ship_to {
1619   $lxdebug->enter_sub();
1620
1621   _check_io_auth();
1622
1623   if ($form->{second_run}) {
1624     $form->{print_and_post} = 0;
1625   }
1626
1627   $title = $form->{title};
1628   $form->{title} = $locale->text('Ship to');
1629
1630   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1631     qw(exchangerate creditlimit creditremaining);
1632
1633   my @shipto_vars =
1634     qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry
1635        shiptocontact shiptophone shiptofax shiptoemail
1636        shiptodepartment_1 shiptodepartment_2);
1637
1638   my @addr_vars =
1639     (qw(name department_1 department_2 street zipcode city country
1640         contact email phone fax));
1641
1642   # get details for name
1643   call_sub("$form->{vc}_details", @addr_vars);
1644
1645   $number =
1646     ($form->{vc} eq 'customer')
1647     ? $locale->text('Customer Number')
1648     : $locale->text('Vendor Number');
1649
1650   # get pricegroups for parts
1651   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1652
1653   # build up html code for prices_$i
1654   set_pricegroup($form->{rowcount});
1655
1656   $nextsub = ($form->{display_form}) ? $form->{display_form} : "display_form";
1657
1658   $form->{rowcount}--;
1659
1660   $form->header;
1661
1662   print qq|
1663 <body>
1664
1665 <form method="post" action="$form->{script}">
1666
1667 <table width="100%">
1668   <tr>
1669     <td>
1670       <table>
1671         <tr class="listheading">
1672           <th class="listheading" colspan="2" width="50%">|
1673     . $locale->text('Billing Address') . qq|</th>
1674           <th class="listheading" width="50%">|
1675     . $locale->text('Shipping Address') . qq|</th>
1676         </tr>
1677         <tr height="5"></tr>
1678         <tr>
1679           <th align="right" nowrap>$number</th>
1680           <td>$form->{"$form->{vc}number"}</td>
1681         </tr>
1682         <tr>
1683           <th align="right" nowrap>| . $locale->text('Company Name') . qq|</th>
1684           <td>$form->{name}</td>
1685           <td><input name="shiptoname" size="35" value="$form->{shiptoname}"></td>
1686         </tr>
1687         <tr>
1688           <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
1689           <td>$form->{department_1}</td>
1690           <td><input name="shiptodepartment_1" size="35" value="$form->{shiptodepartment_1}"></td>
1691         </tr>
1692         <tr>
1693           <th align="right" nowrap>&nbsp;</th>
1694           <td>$form->{department_2}</td>
1695           <td><input name="shiptodepartment_2" size="35" value="$form->{shiptodepartment_2}"></td>
1696         </tr>
1697         <tr>
1698           <th align="right" nowrap>| . $locale->text('Street') . qq|</th>
1699           <td>$form->{street}</td>
1700           <td><input name="shiptostreet" size="35" value="$form->{shiptostreet}"></td>
1701         </tr>
1702         <tr>
1703           <th align="right" nowrap>| . $locale->text('Zipcode') . qq|</th>
1704           <td>$form->{zipcode}</td>
1705           <td><input name="shiptozipcode" size="35" value="$form->{shiptozipcode}"></td>
1706         </tr>
1707         <tr>
1708           <th align="right" nowrap>| . $locale->text('City') . qq|</th>
1709           <td>$form->{city}</td>
1710           <td><input name="shiptocity" size="35" value="$form->{shiptocity}"></td>
1711         </tr>
1712         <tr>
1713           <th align="right" nowrap>| . $locale->text('Country') . qq|</th>
1714           <td>$form->{country}</td>
1715           <td><input name="shiptocountry" size="35" value="$form->{shiptocountry}"></td>
1716         </tr>
1717         <tr>
1718           <th align="right" nowrap>| . $locale->text('Contact') . qq|</th>
1719           <td>$form->{contact}</td>
1720           <td><input name="shiptocontact" size="35" value="$form->{shiptocontact}"></td>
1721         </tr>
1722         <tr>
1723           <th align="right" nowrap>| . $locale->text('Phone') . qq|</th>
1724           <td>$form->{phone}</td>
1725           <td><input name="shiptophone" size="20" value="$form->{shiptophone}"></td>
1726         </tr>
1727         <tr>
1728           <th align="right" nowrap>| . $locale->text('Fax') . qq|</th>
1729           <td>$form->{fax}</td>
1730           <td><input name="shiptofax" size="20" value="$form->{shiptofax}"></td>
1731         </tr>
1732         <tr>
1733           <th align="right" nowrap>| . $locale->text('E-mail') . qq|</th>
1734           <td>$form->{email}</td>
1735           <td><input name="shiptoemail" size="35" value="$form->{shiptoemail}"></td>
1736         </tr>
1737       </table>
1738     </td>
1739   </tr>
1740 </table>
1741 | . $cgi->hidden("-name" => "nextsub", "-value" => $nextsub);
1742 ;
1743
1744
1745
1746   # delete shipto
1747   map({ delete $form->{$_} } (@shipto_vars, qw(header)));
1748   $form->{title} = $title;
1749
1750   foreach $key (keys %$form) {
1751     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1752     $form->{$key} =~ s/\"/&quot;/g;
1753     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1754   }
1755
1756   print qq|
1757
1758 <hr size="3" noshade>
1759
1760 <br>
1761 <input class="submit" type="submit" name="action" value="|
1762     . $locale->text('Continue') . qq|">
1763 </form>
1764
1765 </body>
1766 </html>
1767 |;
1768
1769   $lxdebug->leave_sub();
1770 }
1771
1772 sub new_license {
1773   $lxdebug->enter_sub();
1774
1775   _check_io_auth();
1776
1777   my $row = shift;
1778
1779   # change callback
1780   $form->{old_callback} = $form->escape($form->{callback}, 1);
1781   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
1782   $form->{old_callback} = $form->escape($form->{old_callback}, 1);
1783
1784   # delete action
1785   delete $form->{action};
1786   $customer = $form->{customer};
1787   map { $form->{"old_$_"} = $form->{"${_}_$row"} } qw(partnumber description);
1788
1789   # save all other form variables in a previousform variable
1790   $form->{row} = $row;
1791   foreach $key (keys %$form) {
1792     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1793
1794     # escape ampersands
1795     $form->{$key} =~ s/&/%26/g;
1796     $previousform .= qq|$key=$form->{$key}&|;
1797   }
1798   chop $previousform;
1799   $previousform = $form->escape($previousform, 1);
1800
1801   $form->{script} = "licenses.pl";
1802
1803   map { $form->{$_} = $form->{"old_$_"} } qw(partnumber description);
1804   map { $form->{$_} = $form->escape($form->{$_}, 1) }
1805     qw(partnumber description);
1806   $form->{callback} =
1807     qq|$form->{script}?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|;
1808   $form->redirect;
1809
1810   $lxdebug->leave_sub();
1811 }
1812
1813 sub relink_accounts {
1814   $lxdebug->enter_sub();
1815
1816   _check_io_auth();
1817
1818   $form->{"taxaccounts"} =~ s/\s*$//;
1819   $form->{"taxaccounts"} =~ s/^\s*//;
1820   foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1821     map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1822   }
1823   $form->{"taxaccounts"} = "";
1824
1825   for (my $i = 1; $i <= $form->{"rowcount"}; $i++) {
1826     if ($form->{"id_$i"}) {
1827       IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1);
1828     }
1829   }
1830
1831   $lxdebug->leave_sub();
1832 }
1833
1834 sub set_duedate {
1835   $lxdebug->enter_sub();
1836
1837   _check_io_auth();
1838
1839   $form->get_duedate(\%myconfig);
1840
1841   print $cgi->header() . $form->{duedate};
1842
1843   $lxdebug->leave_sub();
1844 }
1845