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