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