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