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