1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   6 #############################################################################
 
   7 # Changelog: Wann - Wer - Was
 
   8 # Veraendert 2005-01-05 - Marco Welter <mawe@linux-studio.de> - Neue Optik
 
   9 # 08.11.2008 - information@richardson-bueren.de jb  - Backport von Revision 7339 xplace - E-Mail-Vorlage automatisch auswählen
 
  10 # 02.02.2009 - information@richardson-bueren.de jb - Backport von Revision 8535 xplace - Erweiterung der Waren bei Lieferantenauftrag um den Eintrag Mindestlagerbestand. Offen: Auswahlliste auf Lieferantenaufträge einschränken -> Erledigt 2.2.09 Prüfung wie das Skript heisst (oe.pl) -> das ist nur die halbe Miete, nochmal mb fragen -> mb gefragt und es gibt die variable is_purchase
 
  11 #############################################################################
 
  12 # SQL-Ledger, Accounting
 
  13 # Copyright (c) 1998-2002
 
  15 #  Author: Dieter Simader
 
  16 #   Email: dsimader@sql-ledger.org
 
  17 #     Web: http://www.sql-ledger.org
 
  20 # This program is free software; you can redistribute it and/or modify
 
  21 # it under the terms of the GNU General Public License as published by
 
  22 # the Free Software Foundation; either version 2 of the License, or
 
  23 # (at your option) any later version.
 
  25 # This program is distributed in the hope that it will be useful,
 
  26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  28 # GNU General Public License for more details.
 
  29 # You should have received a copy of the GNU General Public License
 
  30 # along with this program; if not, write to the Free Software
 
  31 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  33 #######################################################################
 
  35 # common routines used in is, ir, oe
 
  37 #######################################################################
 
  41 use List::MoreUtils qw(uniq);
 
  42 use List::Util qw(min max first);
 
  53 require "bin/mozilla/common.pl";
 
  57 # any custom scripts for this one
 
  58 if (-f "bin/mozilla/custom_io.pl") {
 
  59   eval { require "bin/mozilla/custom_io.pl"; };
 
  61 if (-f "bin/mozilla/$::form->{login}_io.pl") {
 
  62   eval { require "bin/mozilla/$::form->{login}_io.pl"; };
 
  69 # this is for our long dates
 
  70 # $locale->text('January')
 
  71 # $locale->text('February')
 
  72 # $locale->text('March')
 
  73 # $locale->text('April')
 
  74 # $locale->text('May ')
 
  75 # $locale->text('June')
 
  76 # $locale->text('July')
 
  77 # $locale->text('August')
 
  78 # $locale->text('September')
 
  79 # $locale->text('October')
 
  80 # $locale->text('November')
 
  81 # $locale->text('December')
 
  83 # this is for our short month
 
  84 # $locale->text('Jan')
 
  85 # $locale->text('Feb')
 
  86 # $locale->text('Mar')
 
  87 # $locale->text('Apr')
 
  88 # $locale->text('May')
 
  89 # $locale->text('Jun')
 
  90 # $locale->text('Jul')
 
  91 # $locale->text('Aug')
 
  92 # $locale->text('Sep')
 
  93 # $locale->text('Oct')
 
  94 # $locale->text('Nov')
 
  95 # $locale->text('Dec')
 
 102   $main::auth->assert('part_service_assembly_edit   | vendor_invoice_edit       | sales_order_edit    | invoice_edit |' .
 
 103                 'request_quotation_edit       | sales_quotation_edit      | purchase_order_edit | ' .
 
 104                 'purchase_delivery_order_edit | sales_delivery_order_edit');
 
 107 ########################################
 
 108 # Eintrag fuer Version 2.2.0 geaendert #
 
 109 # neue Optik im Rechnungsformular      #
 
 110 ########################################
 
 112   $main::lxdebug->enter_sub();
 
 116   my $form     = $main::form;
 
 117   my %myconfig = %main::myconfig;
 
 118   my $locale   = $main::locale;
 
 119   my $cgi      = $::request->{cgi};
 
 123   my ($stock_in_out, $stock_in_out_title);
 
 125   my $is_purchase        = (first { $_ eq $form->{type} } qw(request_quotation purchase_order purchase_delivery_order)) || ($form->{script} eq 'ir.pl');
 
 126   my $show_min_order_qty =  first { $_ eq $form->{type} } qw(request_quotation purchase_order);
 
 127   my $is_delivery_order  = $form->{type} =~ /_delivery_order$/;
 
 128   my $is_s_p_order       = (first { $_ eq $form->{type} } qw(sales_order purchase_order));
 
 130   if ($is_delivery_order) {
 
 131     if ($form->{type} eq 'sales_delivery_order') {
 
 132       $stock_in_out_title = $locale->text('Release From Stock');
 
 133       $stock_in_out       = 'out';
 
 135       $stock_in_out_title = $locale->text('Transfer To Stock');
 
 136       $stock_in_out       = 'in';
 
 139     retrieve_partunits();
 
 143   my @header_sort = qw(runningnumber partnumber description ship qty unit sellprice_pg sellprice discount linetotal);
 
 145     {  id => 'runningnumber', width => 5,     value => $locale->text('No.'),                  display => 1, },
 
 146     {  id => 'partnumber',    width => 8,     value => $locale->text('Number'),               display => 1, },
 
 147     {  id => 'description',   width => 30,    value => $locale->text('Part Description'),     display => 1, },
 
 148     {  id => 'ship',          width => 5,     value => $locale->text('Delivered'),            display => $is_s_p_order, },
 
 149     {  id => 'qty',           width => 5,     value => $locale->text('Qty'),                  display => 1, },
 
 150     {  id => 'price_factor',  width => 5,     value => $locale->text('Price Factor'),         display => !$is_delivery_order, },
 
 151     {  id => 'unit',          width => 5,     value => $locale->text('Unit'),                 display => 1, },
 
 152     {  id => 'serialnr',      width => 10,    value => $locale->text('Serial No.'),           display => 0, },
 
 153     {  id => 'projectnr',     width => 10,    value => $locale->text('Project'),              display => 0, },
 
 154     {  id => 'sellprice',     width => 15,    value => $locale->text('Price'),                display => !$is_delivery_order, },
 
 155     {  id => 'sellprice_pg',  width => 8,     value => $locale->text('Pricegroup'),           display => !$is_delivery_order && !$is_purchase, },
 
 156     {  id => 'discount',      width => 5,     value => $locale->text('Discount'),             display => !$is_delivery_order, },
 
 157     {  id => 'linetotal',     width => 10,    value => $locale->text('Extended'),             display => !$is_delivery_order, },
 
 158     {  id => 'bin',           width => 10,    value => $locale->text('Bin'),                  display => 0, },
 
 159     {  id => 'stock_in_out',  width => 10,    value => $stock_in_out_title,                   display => $is_delivery_order, },
 
 161   my @column_index = map { $_->{id} } grep { $_->{display} } @HEADER;
 
 165   my $all_units       = AM->retrieve_units(\%myconfig, $form);
 
 167   my %price_factors   = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
 
 169   my $colspan = scalar @column_index;
 
 171   $form->{invsubtotal} = 0;
 
 172   map { $form->{"${_}_base"} = 0 } (split(/ /, $form->{taxaccounts}));
 
 175   $myconfig{show_form_details} = 1                            unless (defined($myconfig{show_form_details}));
 
 176   $form->{show_details}        = $myconfig{show_form_details} unless (defined($form->{show_details}));
 
 179   # translations, unused commented out
 
 180 #  $runningnumber = $locale->text('No.');
 
 181 #  my $deliverydate  = $locale->text('Delivery Date');
 
 182   my $serialnumber  = $locale->text('Serial No.');
 
 183   my $projectnumber = $locale->text('Project');
 
 184 #  $partsgroup    = $locale->text('Group');
 
 185   my $reqdate       = $locale->text('Reqdate');
 
 186   my $deliverydate  = $locale->text('Required by');
 
 189   my %align  = map { $_ => 'right' } qw(qty ship right sellprice_pg discount linetotal stock_in_out);
 
 190   my %nowrap = map { $_ => 1 }       qw(description unit);
 
 192   $form->{marge_total}           = 0;
 
 193   $form->{sellprice_total}       = 0;
 
 194   $form->{lastcost_total}        = 0;
 
 195   my %projectnumber_labels = ();
 
 196   my @projectnumber_values = ("");
 
 198   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
 
 199     push(@projectnumber_values, $item->{"id"});
 
 200     $projectnumber_labels{$item->{"id"}} = $item->{"projectnumber"};
 
 203   _update_part_information();
 
 204   _update_ship() if ($is_s_p_order);
 
 205   _update_custom_variables();
 
 210   for my $i (1 .. $numrows) {
 
 211     my %column_data = ();
 
 214     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
 
 215       qw(qty discount sellprice lastcost price_new price_old)
 
 216         unless ($form->{simple_save});
 
 219     $form->{"unit_old_$i"}      ||= $form->{"unit_$i"};
 
 220     $form->{"selected_unit_$i"} ||= $form->{"unit_$i"};
 
 222     if (   !$all_units->{$form->{"selected_unit_$i"}}                                            # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
 
 223         || !AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units)) { # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
 
 224       $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};                 # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
 
 226     # adjust prices by unit, ignore if pricegroup changed
 
 227     if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
 
 228         $form->{"sellprice_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
 
 229         $form->{"lastcost_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
 
 230         $form->{"unit_old_$i"}   = $form->{"selected_unit_$i"};
 
 232     my $this_unit = $form->{"unit_$i"};
 
 233     $this_unit    = $form->{"selected_unit_$i"} if AM->convert_unit($this_unit, $form->{"selected_unit_$i"}, $all_units);
 
 235     if (0 < scalar @{ $form->{ALL_PRICE_FACTORS} }) {
 
 236       my @values = ('', map { $_->{id}                      } @{ $form->{ALL_PRICE_FACTORS} });
 
 237       my %labels =      map { $_->{id} => $_->{description} } @{ $form->{ALL_PRICE_FACTORS} };
 
 239       $column_data{price_factor} =
 
 240         NTI($cgi->popup_menu('-name'    => "price_factor_id_$i",
 
 241                              '-default' => $form->{"price_factor_id_$i"},
 
 242                              '-values'  => \@values,
 
 243                              '-labels'  => \%labels,
 
 244                              '-style'   => 'width:90px'));
 
 246       $column_data{price_factor} = ' ';
 
 249     $column_data{"unit"} = AM->unit_select_html($all_units, "unit_$i", $this_unit, $form->{"id_$i"} ? $form->{"unit_$i"} : undef);
 
 252 #count the max of decimalplaces of sellprice and lastcost, so the same number of decimalplaces
 
 253 #is shown for lastcost and sellprice.
 
 254     my $decimalplaces = ($form->{"sellprice_$i"} =~ /\.(\d+)/) ? max 2, length $1 : 2;
 
 255     $decimalplaces = ($form->{"lastcost_$i"} =~ /\.(\d+)/) ? max $decimalplaces, length $1 : $decimalplaces;
 
 257     my $price_factor   = $price_factors{$form->{"price_factor_id_$i"}} || 1;
 
 258     my $discount       = $form->round_amount($form->{"qty_$i"} * $form->{"sellprice_$i"} *        $form->{"discount_$i"}  / 100 / $price_factor, 2);
 
 259     my $linetotal      = $form->round_amount($form->{"qty_$i"} * $form->{"sellprice_$i"} * (100 - $form->{"discount_$i"}) / 100 / $price_factor, 2);
 
 260     my $rows            = $form->numtextrows($form->{"description_$i"}, 30, 6);
 
 262     $column_data{runningnumber} = $cgi->textfield(-name => "runningnumber_$i", -size => 5,  -value => $i);    # HuT
 
 263     $column_data{partnumber}    = $cgi->textfield(-name => "partnumber_$i",    -size => 12, -value => $form->{"partnumber_$i"});
 
 264     $column_data{description} = (($rows > 1) # if description is too large, use a textbox instead
 
 265                                 ? $cgi->textarea( -name => "description_$i", -default => $form->{"description_$i"}, -rows => $rows, -columns => 30)
 
 266                                 : $cgi->textfield(-name => "description_$i",   -size => 30, -value => $form->{"description_$i"}))
 
 267                                 . $cgi->button(-value => $locale->text('L'), -onClick => "set_longdescription_window('longdescription_$i')");
 
 269     my $qty_dec = ($form->{"qty_$i"} =~ /\.(\d+)/) ? length $1 : 2;
 
 271     $column_data{qty}  = $cgi->textfield(-name => "qty_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec));
 
 272     $column_data{qty} .= $cgi->button(-onclick => "calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)", -value => $locale->text('*/'))
 
 273                        . $cgi->hidden(-name => "formel_$i", -value => $form->{"formel_$i"}) . $cgi->hidden("-name" => "alu_$i", "-value" => $form->{"alu_$i"})
 
 274       if $form->{"formel_$i"};
 
 276     $column_data{ship} = '';
 
 277     if ($form->{"id_$i"}) {
 
 278       my $ship_qty        = $form->{"ship_$i"} * 1;
 
 279       $ship_qty          *= $all_units->{$form->{"partunit_$i"}}->{factor};
 
 280       $ship_qty          /= ( $all_units->{$form->{"unit_$i"}}->{factor} || 1 );
 
 282       $column_data{ship}  = $form->format_amount(\%myconfig, $form->round_amount($ship_qty, 2) * 1) . ' ' . $form->{"unit_$i"};
 
 285     # build in drop down list for pricesgroups
 
 286     # $sellprice_value setzt den Wert etwas unabhängiger von der Darstellung.
 
 287     # Hintergrund: Preisgruppen werden hier überprüft und neu berechnet.
 
 288     # Vorher wurde der ganze cgi->textfield Block zweimal identisch eingebaut, dass passiert
 
 289     # jetzt nach der Abfrage.
 
 291     if ($form->{"prices_$i"}) {
 
 292       $column_data{sellprice_pg} = qq|<select name="sellprice_pg_$i" style="width: 8em">$form->{"prices_$i"}</select>|;
 
 293       $sellprice_value           =($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})
 
 294                                       ? $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces)
 
 295                                       : $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 297       # for last row and report
 
 298       # set pricegroup drop down list from report menu
 
 299       if ($form->{"sellprice_$i"} != 0) {
 
 300         # remember the pricegroup_id in pricegroup_old
 
 301         # but don't overwrite it
 
 302         $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
 
 303         my $default_option           = $form->{"sellprice_$i"}.'--'.$form->{"pricegroup_id_$i"};
 
 304         $column_data{sellprice_pg}   = NTI($cgi->popup_menu("sellprice_pg_$i", [ $default_option ], $default_option, { $default_option => $form->{"pricegroup_$i"} || '' }));
 
 306         $column_data{sellprice_pg} = qq| |;
 
 308       $sellprice_value = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 311     # Falls der Benutzer die Preise nicht anpassen sollte, wird das entsprechende
 
 312     # Textfield auf readonly gesetzt. Anm. von Sven: Manipulation der Preise ist
 
 313     # immer noch möglich, konsequenterweise sollten diese NUR aus der Datenbank
 
 315     my $edit_prices = $main::auth->assert('edit_prices', 1);
 
 316     $column_data{sellprice} = (!$edit_prices)
 
 317                                 ? $cgi->textfield(-readonly => "readonly",
 
 318                                                   -name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value)
 
 319                                 : $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value);
 
 320     $column_data{discount}    = (!$edit_prices)
 
 321                                   ? $cgi->textfield(-readonly => "readonly",
 
 322                                                     -name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}))
 
 323                                   : $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
 
 324     $column_data{linetotal}   = $form->format_amount(\%myconfig, $linetotal, 2);
 
 325     $column_data{bin}         = $form->{"bin_$i"};
 
 327     if ($is_delivery_order) {
 
 328       $column_data{stock_in_out} =  calculate_stock_in_out($i);
 
 331     my @ROW1 = map { value => $column_data{$_}, align => $align{$_}, nowrap => $nowrap{$_} }, @column_index;
 
 335     push @ROW2, { value => qq|<b>$serialnumber</b> <input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">| }
 
 336       if $form->{type} !~ /_quotation/;
 
 337     push @ROW2, { value => qq|<b>$projectnumber</b> | . NTI($cgi->popup_menu('-name'  => "project_id_$i",        '-values'  => \@projectnumber_values,
 
 338                                                                              '-labels' => \%projectnumber_labels, '-default' => $form->{"project_id_$i"})) };
 
 339     push @ROW2, { value => qq|<b>$reqdate</b> <input name="reqdate_$i" size="11" onBlur="check_right_date_format(this)" value="$form->{"reqdate_$i"}">| }
 
 340       if ($form->{type} =~ /order/ ||  $form->{type} =~ /invoice/);
 
 341     push @ROW2, { value => sprintf qq|<b>%s</b> <input type="checkbox" name="subtotal_$i" value="1" %s>|,
 
 342                    $locale->text('Subtotal'), $form->{"subtotal_$i"} ? 'checked' : '' };
 
 344 # begin marge calculations
 
 345     $form->{"lastcost_$i"}     *= 1;
 
 346     $form->{"marge_percent_$i"} = 0;
 
 350     if ( $form->{taxincluded} and $form->{"qty_$i"} * 1  and $form->{$form->{"taxaccounts_$i"} . "_rate"} * 1) {
 
 351       # if we use taxincluded we need to calculate the marge from the net_value
 
 352       # all the marge calculations are based on linetotal which we need to
 
 353       # convert to net first
 
 355       # there is no direct form value for the tax_rate of the item, but
 
 356       # form->{taxaccounts_$i} gives the tax account (e.g. 3806) and 3806_rate
 
 357       # gives the tax percentage (e.g. 0.19)
 
 358       $real_sellprice = $linetotal / (1 + $form->{$form->{"taxaccounts_$i"} . "_rate"});
 
 360       $real_sellprice            = $linetotal;
 
 362     my $real_lastcost            = $form->round_amount($form->{"lastcost_$i"} * $form->{"qty_$i"} / $price_factor, 2);
 
 363     my $marge_percent_warn       = $myconfig{marge_percent_warn} * 1 || 15;
 
 364     my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
 
 366     if ($real_sellprice * 1 && ($form->{"qty_$i"} * 1)) {
 
 367       $form->{"marge_percent_$i"} = ($real_sellprice - $real_lastcost) * 100 / $real_sellprice;
 
 368       $marge_color                = 'color="#ff0000"' if $form->{"id_$i"} && $form->{"marge_percent_$i"} < $marge_percent_warn;
 
 371     $form->{"marge_absolut_$i"}  = ($real_sellprice - $real_lastcost) * $marge_adjust_credit_note;
 
 372     $form->{"marge_total"}      += $form->{"marge_absolut_$i"};
 
 373     $form->{"lastcost_total"}   += $real_lastcost;
 
 374     $form->{"sellprice_total"}  += $real_sellprice;
 
 376     map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
 
 378     push @ROW2, { value => sprintf qq|
 
 379          <font %s><b>%s</b> %s  %s%% </font>
 
 381          <b>%s</b> <input size="5" name="lastcost_$i" value="%s">|,
 
 382                    $marge_color, $locale->text('Ertrag'),$form->{"marge_absolut_$i"}, $form->{"marge_percent_$i"},
 
 383                    $locale->text('LP'), $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2),
 
 384                    $locale->text('EK'), $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces) }
 
 385       if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ ||  $form->{type} =~ /invoice/ || $form->{type} =~ /^credit_note$/ ) && !$is_delivery_order;
 
 387     $form->{"listprice_$i"} = $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2)
 
 388       if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ ||  $form->{type} =~ /invoice/) ;
 
 389 # / marge calculations ending
 
 392     if ($form->{"id_$i"}) {
 
 393       my $part         = IC->get_basic_part_info(id => $form->{"id_$i"});
 
 394       my $onhand_color = $part->{onhand} < $part->{rop} ? 'color="#ff0000"' : '';
 
 395       push @ROW2, { value => sprintf "<b>%s</b> <font %s>%s %s</font>",
 
 396                       $locale->text('On Hand'),
 
 398                       $form->format_amount(\%myconfig, $part->{onhand}, 2),
 
 406     if ($is_delivery_order) {
 
 407       map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount lastcost);
 
 408       push @hidden_vars, qw(sellprice discount not_discountable price_factor_id lastcost pricegroup_id);
 
 409       push @hidden_vars, "stock_${stock_in_out}_sum_qty", "stock_${stock_in_out}";
 
 412     my @HIDDENS = map { value => $_}, (
 
 413           $cgi->hidden("-name" => "unit_old_$i", "-value" => $form->{"selected_unit_$i"}),
 
 414           $cgi->hidden("-name" => "price_new_$i", "-value" => $form->format_amount(\%myconfig, $form->{"price_new_$i"})),
 
 415           map { ($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } map { $_."_$i" }
 
 416             (qw(orderitems_id bo pricegroup_old price_old id inventory_accno bin partsgroup partnotes
 
 417                 income_accno expense_accno listprice assembly taxaccounts ordnumber transdate cusordnumber
 
 418                 longdescription basefactor marge_absolut marge_percent marge_price_factor), @hidden_vars)
 
 421     map { $form->{"${_}_base"} += $linetotal } (split(/ /, $form->{"taxaccounts_$i"}));
 
 423     $form->{invsubtotal} += $linetotal;
 
 425     # Benutzerdefinierte Variablen für Waren/Dienstleistungen/Erzeugnisse
 
 426     _render_custom_variables_inputs(ROW2 => \@ROW2, row => $i, part_id => $form->{"id_$i"});
 
 428     push @ROWS, { ROW1 => \@ROW1, ROW2 => \@ROW2, HIDDENS => \@HIDDENS, colspan => $colspan, error => $form->{"row_error_$i"}, };
 
 431   print $form->parse_html_template('oe/sales_order', { ROWS   => \@ROWS,
 
 435   if (0 != ($form->{sellprice_total} * 1)) {
 
 436     $form->{marge_percent} = ($form->{sellprice_total} - $form->{lastcost_total}) / $form->{sellprice_total} * 100;
 
 439   $main::lxdebug->leave_sub();
 
 442 ##################################################
 
 443 # build html-code for pricegroups in variable $form->{prices_$j}
 
 446   $main::lxdebug->enter_sub();
 
 448   my $form     = $main::form;
 
 449   my $locale   = $main::locale;
 
 450   my $cgi      = $::request->{cgi};
 
 454   my $rowcount = shift;
 
 455   for my $j (1 .. $rowcount) {
 
 456     next unless $form->{PRICES}{$j};
 
 457     # build drop down list for pricegroups
 
 458     my $option_tmpl = qq|<option value="%s--%s" %s>%s</option>|;
 
 459     $form->{"prices_$j"}  = join '', map { sprintf $option_tmpl, @$_{qw(price pricegroup_id selected pricegroup)} }
 
 460                                          (+{ pricegroup => $locale->text("none (pricegroup)") }, @{ $form->{PRICES}{$j} });
 
 462     foreach my $item (@{ $form->{PRICES}{$j} }) {
 
 463       # set new selectedpricegroup_id and prices for "Preis"
 
 464       $form->{"pricegroup_old_$j"} = $item->{pricegroup_id}   if $item->{selected} &&  $item->{pricegroup_id};
 
 465       $form->{"sellprice_$j"}      = $item->{price}           if $item->{selected} &&  $item->{pricegroup_id};
 
 466       $form->{"price_new_$j"}      = $form->{"sellprice_$j"}  if $item->{selected} || !$item->{pricegroup_id};
 
 469   $main::lxdebug->leave_sub();
 
 473   $main::lxdebug->enter_sub();
 
 476   my $mode   = $params{mode} || croak "Missing parameter 'mode'";
 
 480   my $previous_form = $::auth->save_form_in_session(form => $::form);
 
 481   $::form->{title}  = $::locale->text('Select from one of the items below');
 
 484   my @item_list = map {
 
 485     $_->{display_sellprice}  = $_->{sellprice} * (1 - $::form->{tradediscount});
 
 486     $_->{display_sellprice} /= $_->{price_factor} if ($_->{price_factor});
 
 488   } @{ $::form->{item_list} };
 
 490   # delete action variable
 
 491   delete @{$::form}{qw(action item_list header)};
 
 493   print $::form->parse_html_template('io/select_item', { PREVIOUS_FORM => $previous_form,
 
 495                                                          ITEM_LIST     => \@item_list,
 
 496                                                          IS_PURCHASE   => $mode eq 'IS' });
 
 498   $main::lxdebug->leave_sub();
 
 502   $main::lxdebug->enter_sub();
 
 504   my $form     = $main::form;
 
 505   my %myconfig = %main::myconfig;
 
 509   $::auth->restore_form_from_session($form->{select_item_previous_form} || croak('Missing previous form ID'), form => $form);
 
 511   my $mode = delete($form->{select_item_mode}) || croak 'Missing item selection mode';
 
 512   my $id   = delete($form->{select_item_id})   || croak 'Missing item selection ID';
 
 513   my $i    = $form->{ $mode eq 'IC' ? 'assembly_rows' : 'rowcount' };
 
 515   $form->{"id_${i}"} = $id;
 
 518     IS->retrieve_item(\%myconfig, \%$form);
 
 519   } elsif ($mode eq 'IR') {
 
 520     IR->retrieve_item(\%myconfig, \%$form);
 
 521   } elsif ($mode eq 'IC') {
 
 522     IC->assembly_item(\%myconfig, \%$form);
 
 524     croak "Invalid item selection mode '${mode}'";
 
 527   my $new_item = $form->{item_list}->[0] || croak "No item found for mode '${mode}' and ID '${id}'";
 
 529   # if there was a price entered, override it
 
 530   my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 533     qw(id partnumber description sellprice listprice inventory_accno
 
 534        income_accno expense_accno bin unit weight assembly taxaccounts
 
 535        partsgroup formel longdescription not_discountable partnotes lastcost
 
 536        price_factor_id price_factor);
 
 538   my $ic_cvar_configs = CVar->get_configs(module => 'IC');
 
 539   push @new_fields, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
 
 541   map { $form->{"${_}_$i"} = $new_item->{$_} } @new_fields;
 
 543   $form->{"marge_price_factor_$i"} = $new_item->{price_factor};
 
 545   if ($form->{"part_payment_id_$i"} ne "") {
 
 546     $form->{payment_id} = $form->{"part_payment_id_$i"};
 
 549   my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
 551   my $decimalplaces = ($dec > 2) ? $dec : 2;
 
 554     $form->{"sellprice_$i"} = $sellprice;
 
 557     # if there is an exchange rate adjust sellprice
 
 558     if (($form->{exchangerate} * 1) != 0) {
 
 559       $form->{"sellprice_$i"} /= $form->{exchangerate};
 
 560       $form->{"sellprice_$i"} =
 
 561         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
 
 565     if ($::form->{tradediscount}) {
 
 566       $::form->{"sellprice_$i"} *= 1 - $::form->{tradediscount};
 
 570   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
 571     qw(sellprice listprice weight);
 
 573   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
 
 574   $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
 
 576   if ($form->{"not_discountable_$i"}) {
 
 577     $form->{"discount_$i"} = 0;
 
 581     $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
 
 583   map { $form->{"${_}_base"} += $amount }
 
 584     (split / /, $form->{"taxaccounts_$i"});
 
 585   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
 
 586     $form->{"taxaccounts_$i"}
 
 587     if !$form->{taxincluded};
 
 589   $form->{creditremaining} -= $amount;
 
 591   $form->{"runningnumber_$i"} = $i;
 
 593   delete $form->{nextsub};
 
 598       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
 
 599   } qw(sellprice listprice lastcost) if $form->{item} ne 'assembly';
 
 601   # get pricegroups for parts
 
 602   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 604   # build up html code for prices_$i
 
 605   set_pricegroup($form->{rowcount});
 
 609   $main::lxdebug->leave_sub();
 
 613   $main::lxdebug->enter_sub();
 
 615   my $form     = $main::form;
 
 616   my %myconfig = %main::myconfig;
 
 620   my $price_key = ($form->{type} =~ m/request_quotation|purchase_order/) || ($form->{script} eq 'ir.pl') ? 'lastcost' : 'sellprice';
 
 623   $form->{old_callback} = $form->escape($form->{callback}, 1);
 
 624   $form->{callback}     = $form->escape("$form->{script}?action=display_form", 1);
 
 626   # save all form variables except action in the session and keep the key in the previousform variable
 
 627   my $previousform = $::auth->save_form_in_session(skip_keys => [ qw(action) ]);
 
 630   push @HIDDENS,      { 'name' => 'previousform', 'value' => $previousform };
 
 631   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{$_} },                       qw(rowcount vc);
 
 632   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{"${_}_$form->{rowcount}"} }, qw(partnumber description unit);
 
 633   push @HIDDENS,      { 'name' => 'taxaccount2',  'value' => $form->{taxaccounts} };
 
 634   push @HIDDENS,      { 'name' => $price_key,     'value' => $form->parse_amount(\%myconfig, $form->{"sellprice_$form->{rowcount}"}) };
 
 635   push @HIDDENS,      { 'name' => 'notes',        'value' => $form->{"longdescription_$form->{rowcount}"} };
 
 638   print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
 
 640   $main::lxdebug->leave_sub();
 
 644   $main::lxdebug->enter_sub();
 
 646   my $form     = $main::form;
 
 647   my %myconfig = %main::myconfig;
 
 654   # remove any makes or model rows
 
 655   if ($form->{item} eq 'part') {
 
 656     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
 657       qw(listprice sellprice lastcost weight rop);
 
 659   } elsif ($form->{item} eq 'assembly') {
 
 661     # fuer assemblies auskommentiert. seiteneffekte? ;-) wird die woanders benoetigt?
 
 662     #$form->{sellprice} = 0;
 
 664     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
 665       qw(listprice sellprice rop stock);
 
 667     my @flds = qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup lastcost);
 
 669     for my $i (1 .. ($form->{assembly_rows} - 1)) {
 
 670       if ($form->{"qty_$i"}) {
 
 674         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
 
 676         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
 
 678         #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
 
 680         # fuer assemblies auskommentiert. siehe oben
 
 681         #    $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"} / ($form->{"price_factor_$i"} || 1));
 
 682         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"} / ($form->{"price_factor_$i"} || 1));
 
 686     # kann das hier auch weg? s.o. jb
 
 687     $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
 
 689     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
 
 690     $form->{assembly_rows} = $count;
 
 692   } elsif ($form->{item} eq 'service') {
 
 693     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(listprice sellprice lastcost);
 
 696     remove_emptied_rows(1);
 
 698     $form->{creditremaining} -= &invoicetotal;
 
 703   if (   $form->{type} =~ (/sales_quotation/)
 
 704       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
 
 705       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
 
 706       or ($form->{type} =~ /sales_order/)) {
 
 708     # get pricegroups for parts
 
 709     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 711     # build up html code for prices_$i
 
 712     set_pricegroup($form->{rowcount});
 
 718   $main::lxdebug->leave_sub();
 
 721 sub remove_emptied_rows {
 
 722   my $dont_add_empty = shift;
 
 725   return unless $form->{rowcount};
 
 727   my @flds = qw(id partnumber description qty ship sellprice unit
 
 728                 discount inventory_accno income_accno expense_accno listprice
 
 729                 taxaccounts bin assembly weight projectnumber project_id
 
 730                 oldprojectnumber runningnumber serialnumber partsgroup payment_id
 
 731                 not_discountable shop ve gv buchungsgruppen_id language_values
 
 732                 sellprice_pg pricegroup_old price_old price_new unit_old ordnumber
 
 733                 transdate longdescription basefactor marge_total marge_percent
 
 734                 marge_price_factor lastcost price_factor_id partnotes
 
 735                 stock_out stock_in has_sernumber reqdate);
 
 737   my $ic_cvar_configs = CVar->get_configs(module => 'IC');
 
 738   push @flds, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
 
 741   for my $i (1 .. $form->{rowcount} - 1) {
 
 742     next unless $form->{"partnumber_$i"};
 
 744     push @new_rows, { map { $_ => $form->{"${_}_$i" } } @flds };
 
 747   my $new_rowcount = scalar @new_rows;
 
 748   $form->redo_rows(\@flds, \@new_rows, $new_rowcount, $form->{rowcount});
 
 749   $form->{rowcount} = $new_rowcount + ($dont_add_empty ? 0 : 1);
 
 753   $main::lxdebug->enter_sub();
 
 755   my $form     = $main::form;
 
 756   my %myconfig = %main::myconfig;
 
 760   $form->{oldinvtotal} = 0;
 
 762   # add all parts and deduct paid
 
 763   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
 
 765   my ($amount, $sellprice, $discount, $qty);
 
 767   for my $i (1 .. $form->{rowcount}) {
 
 768     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 769     $discount  = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
 
 770     $qty       = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
 
 772     #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
 
 774     $amount = $sellprice * (1 - $discount / 100) * $qty;
 
 775     map { $form->{"${_}_base"} += $amount }
 
 776       (split (/ /, $form->{"taxaccounts_$i"}));
 
 777     $form->{oldinvtotal} += $amount;
 
 780   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
 
 781     split(/ /, $form->{taxaccounts})
 
 782     if !$form->{taxincluded};
 
 784   $form->{oldtotalpaid} = 0;
 
 785   for my $i (1 .. $form->{paidaccounts}) {
 
 786     $form->{oldtotalpaid} += $form->{"paid_$i"};
 
 789   $main::lxdebug->leave_sub();
 
 792   return ($form->{oldinvtotal} - $form->{oldtotalpaid});
 
 796   $main::lxdebug->enter_sub();
 
 798   my $form     = $main::form;
 
 799   my $locale   = $main::locale;
 
 803   # check if items are valid
 
 804   if ($form->{rowcount} == 1) {
 
 809   for my $i (1 .. $form->{rowcount} - 1) {
 
 810     $form->isblank("partnumber_$i",
 
 811                    $locale->text('Number missing in Row') . " $i");
 
 814   $main::lxdebug->leave_sub();
 
 818   $main::lxdebug->enter_sub();
 
 820   my $form     = $main::form;
 
 821   my %myconfig = %main::myconfig;
 
 822   my $locale   = $main::locale;
 
 826   if ($form->{second_run}) {
 
 827     $form->{print_and_post} = 0;
 
 829   $form->{ordnumber} = $form->{invnumber};
 
 831   $form->{old_employee_id} = $form->{employee_id};
 
 832   $form->{old_salesman_id} = $form->{salesman_id};
 
 834   map { delete $form->{$_} } qw(id printed emailed queued);
 
 836   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
 
 837     $form->{title} = $locale->text('Add Purchase Order');
 
 838     $form->{vc}    = 'vendor';
 
 839     $form->{type}  = 'purchase_order';
 
 842   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
 
 843     $form->{title} = $locale->text('Add Sales Order');
 
 844     $form->{vc}    = 'customer';
 
 845     $form->{type}  = 'sales_order';
 
 848   $form->{script} = 'oe.pl';
 
 856   require "bin/mozilla/$form->{script}";
 
 857   my $script = $form->{"script"};
 
 860   $locale = new Locale($::lx_office_conf{system}->{language}, $script);
 
 862   map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
 
 864   my $currency = $form->{currency};
 
 868   $form->{currency}     = $currency;
 
 869   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
 
 870   $form->{exchangerate} = $form->{forex} || '';
 
 872   for my $i (1 .. $form->{rowcount}) {
 
 873     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"})
 
 874             if ($form->{"${_}_${i}"}) }
 
 875         qw(ship qty sellprice listprice basefactor discount));
 
 881   $main::lxdebug->leave_sub();
 
 885   $main::lxdebug->enter_sub();
 
 887   my $form     = $main::form;
 
 888   my %myconfig = %main::myconfig;
 
 889   my $locale   = $main::locale;
 
 893   if ($form->{second_run}) {
 
 894     $form->{print_and_post} = 0;
 
 896   map { delete $form->{$_} } qw(id printed emailed queued);
 
 899   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
 
 900     $form->{title} = $locale->text('Add Request for Quotation');
 
 901     $form->{vc}    = 'vendor';
 
 902     $form->{type}  = 'request_quotation';
 
 905   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
 
 906     $form->{title} = $locale->text('Add Quotation');
 
 907     $form->{vc}    = 'customer';
 
 908     $form->{type}  = 'sales_quotation';
 
 914   $form->{script} = 'oe.pl';
 
 920   require "bin/mozilla/$form->{script}";
 
 922   map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
 
 924   my $currency = $form->{currency};
 
 928   $form->{currency}     = $currency;
 
 929   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, $buysell);
 
 930   $form->{exchangerate} = $form->{forex} || '';
 
 932   for my $i (1 .. $form->{rowcount}) {
 
 933     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
 
 934                                                      $form->{"${_}_${i}"})
 
 935             if ($form->{"${_}_${i}"}) }
 
 936         qw(ship qty sellprice listprice basefactor discount));
 
 942   $main::lxdebug->leave_sub();
 
 945 sub request_for_quotation {
 
 950   $main::lxdebug->enter_sub();
 
 952   my $form     = $main::form;
 
 953   my %myconfig = %main::myconfig;
 
 954   my $locale   = $main::locale;
 
 958   if ($form->{second_run}) {
 
 959     $form->{print_and_post} = 0;
 
 960     $form->{resubmit}       = 0;
 
 963   $form->{email} = $form->{shiptoemail} if $form->{shiptoemail} && $form->{formname} =~ /(pick|packing|bin)_list/;
 
 965   if ($form->{"cp_id"}) {
 
 966     CT->get_contact(\%myconfig, $form);
 
 967     $form->{"email"} = $form->{"cp_email"} if $form->{"cp_email"};
 
 970   $form->{language} = $form->get_template_language(\%myconfig);
 
 971   $form->{language} = "_" . $form->{language} if $form->{language};
 
 973   my $title = $locale->text('E-mail') . " " . $form->get_formname_translation();
 
 975   $form->{oldmedia} = $form->{media};
 
 976   $form->{media}    = "email";
 
 978   my $attachment_filename = $form->generate_attachment_filename();
 
 979   my $subject             = $form->{subject} || $form->generate_email_subject();
 
 981   $form->{"fokus"} = $form->{"email"} ? "Form.subject" : "Form.email";
 
 984   my (@dont_hide_key_list, %dont_hide_key, @hidden_keys);
 
 985   @dont_hide_key_list = qw(action email cc bcc subject message sendmode format header override login password);
 
 986   @dont_hide_key{@dont_hide_key_list} = (1) x @dont_hide_key_list;
 
 987   @hidden_keys = sort grep { !$dont_hide_key{$_} } grep { !ref $form->{$_} } keys %$form;
 
 989   print $form->parse_html_template('generic/edit_email',
 
 991                                      a_filename    => $attachment_filename,
 
 993                                      print_options => print_options('inline' => 1),
 
 994                                      HIDDEN        => [ map +{ name => $_, value => $form->{$_} }, @hidden_keys ],
 
 995                                      SHOW_BCC      => $::auth->assert('email_bcc', 'may fail') });
 
 997   $main::lxdebug->leave_sub();
 
1001   $main::lxdebug->enter_sub();
 
1003   my $form     = $main::form;
 
1004   my %myconfig = %main::myconfig;
 
1008   my $callback = $form->{script} . "?action=edit";
 
1009   map({ $callback .= "\&${_}=" . E($form->{$_}); } qw(type id));
 
1011   print_form("return");
 
1013   Common->save_email_status(\%myconfig, $form);
 
1015   $form->{callback} = $callback;
 
1018   $main::lxdebug->leave_sub();
 
1021 # generate the printing options displayed at the bottom of oe and is forms.
 
1022 # this function will attempt to guess what type of form is displayed, and will generate according options
 
1025 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
 
1026 # the opthash function builds hashrefs which are then pieced together for the template arrays.
 
1027 # unneeded options are "undef"ed out, and then grepped out.
 
1029 # the inline options is untested, but intended to be used later in metatemplating
 
1031   $main::lxdebug->enter_sub();
 
1033   my $form     = $main::form;
 
1034   my %myconfig = %main::myconfig;
 
1035   my $locale   = $main::locale;
 
1041   # names 3 parameters and returns a hashref, for use in templates
 
1042   sub opthash { +{ value => shift, selected => shift, oname => shift } }
 
1043   my (@FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
 
1045   # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
 
1046   $form->{sendmode}   = "attachment";
 
1047   $form->{format}     = $form->{format} || $myconfig{template_format} || "pdf";
 
1048   $form->{copies}     = $form->{copies} || $myconfig{copies}          || 3;
 
1049   $form->{media}      = $form->{media}  || $myconfig{default_media}   || "screen";
 
1050   $form->{printer_id} = defined $form->{printer_id}           ? $form->{printer_id} :
 
1051                         defined $myconfig{default_printer_id} ? $myconfig{default_printer_id} : "";
 
1053   $form->{PD}{ $form->{formname} } = "selected";
 
1054   $form->{DF}{ $form->{format} }   = "selected";
 
1055   $form->{OP}{ $form->{media} }    = "selected";
 
1056   $form->{SM}{ $form->{formname} } = "selected";
 
1058   push @FORMNAME, grep $_,
 
1059     ($form->{type} eq 'purchase_order') ? (
 
1060       opthash("purchase_order",      $form->{PD}{purchase_order},      $locale->text('Purchase Order')),
 
1061       opthash("bin_list",            $form->{PD}{bin_list},            $locale->text('Bin List'))
 
1063     ($form->{type} eq 'credit_note') ?
 
1064       opthash("credit_note",         $form->{PD}{credit_note},         $locale->text('Credit Note')) : undef,
 
1065     ($form->{type} eq 'sales_order') ? (
 
1066       opthash("sales_order",         $form->{PD}{sales_order},         $locale->text('Confirmation')),
 
1067       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
 
1069     ($form->{type} =~ /sales_quotation$/) ?
 
1070       opthash('sales_quotation',     $form->{PD}{sales_quotation},     $locale->text('Quotation')) : undef,
 
1071     ($form->{type} =~ /request_quotation$/) ?
 
1072       opthash('request_quotation',   $form->{PD}{request_quotation},   $locale->text('Request for Quotation')) : undef,
 
1073     ($form->{type} eq 'invoice') ? (
 
1074       opthash("invoice",             $form->{PD}{invoice},             $locale->text('Invoice')),
 
1075       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
 
1077     ($form->{type} eq 'invoice' && $form->{storno}) ? (
 
1078       opthash("storno_invoice",      $form->{PD}{storno_invoice},      $locale->text('Storno Invoice')),
 
1080     ($form->{type} =~ /_delivery_order$/) ? (
 
1081       opthash($form->{type},         $form->{PD}{$form->{type}},       $locale->text('Delivery Order')),
 
1082       opthash('pick_list',           $form->{PD}{pick_list},           $locale->text('Pick List')),
 
1086     opthash("attachment",            $form->{SM}{attachment},          $locale->text('Attachment')),
 
1087     opthash("inline",                $form->{SM}{inline},              $locale->text('In-line'))
 
1088       if ($form->{media} eq 'email');
 
1090   push @MEDIA, grep $_,
 
1091       opthash("screen",              $form->{OP}{screen},              $locale->text('Screen')),
 
1092     ($form->{printers} && scalar @{ $form->{printers} } && $::lx_office_conf{print_templates}->{latex}) ?
 
1093       opthash("printer",             $form->{OP}{printer},             $locale->text('Printer')) : undef,
 
1094     ($::lx_office_conf{print_templates}->{latex} && !$options{no_queue}) ?
 
1095       opthash("queue",               $form->{OP}{queue},               $locale->text('Queue')) : undef
 
1096         if ($form->{media} ne 'email');
 
1098   push @FORMAT, grep $_,
 
1099     ($::lx_office_conf{print_templates}->{opendocument} &&     $::lx_office_conf{applications}->{openofficeorg_writer}  &&     $::lx_office_conf{applications}->{xvfb}
 
1100                                                         && (-x $::lx_office_conf{applications}->{openofficeorg_writer}) && (-x $::lx_office_conf{applications}->{xvfb})
 
1101      && !$options{no_opendocument_pdf}) ?
 
1102       opthash("opendocument_pdf",    $form->{DF}{"opendocument_pdf"},  $locale->text("PDF (OpenDocument/OASIS)")) : undef,
 
1103     ($::lx_office_conf{print_templates}->{latex}) ?
 
1104       opthash("pdf",                 $form->{DF}{pdf},                 $locale->text('PDF')) : undef,
 
1105     ($::lx_office_conf{print_templates}->{latex} && !$options{no_postscript}) ?
 
1106       opthash("postscript",          $form->{DF}{postscript},          $locale->text('Postscript')) : undef,
 
1107     (!$options{no_html}) ?
 
1108       opthash("html", $form->{DF}{html}, "HTML") : undef,
 
1109     ($::lx_office_conf{print_templates}->{opendocument} && !$options{no_opendocument}) ?
 
1110       opthash("opendocument",        $form->{DF}{opendocument},        $locale->text("OpenDocument/OASIS")) : undef,
 
1111     ($::lx_office_conf{print_templates}->{excel} && !$options{no_excel}) ?
 
1112       opthash("excel",               $form->{DF}{excel},               $locale->text("Excel")) : undef;
 
1115     map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
 
1116       if (ref $form->{languages} eq 'ARRAY');
 
1119     map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
 
1120       if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
 
1125     show  => !$options{"hide_" . $_->[1]} && scalar @{ $_->[0] }
 
1127   [ \@FORMNAME,    'formname',    ],
 
1128   [ \@LANGUAGE_ID, 'language_id', ],
 
1129   [ \@FORMAT,      'format',      ],
 
1130   [ \@SENDMODE,    'sendmode',    ],
 
1131   [ \@MEDIA,       'media',       ],
 
1132   [ \@PRINTER_ID,  'printer_id',  ];
 
1134   my %dont_display_groupitems = (
 
1138   my %template_vars = (
 
1139     display_copies       => scalar @{ $form->{printers} || [] } && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email',
 
1140     display_remove_draft => (!$form->{id} && $form->{draft_id}),
 
1141     display_groupitems   => !$dont_display_groupitems{$form->{type}},
 
1142     groupitems_checked   => $form->{groupitems} ? "checked" : '',
 
1143     remove_draft_checked => $form->{remove_draft} ? "checked" : ''
 
1146   my $print_options = $form->parse_html_template("generic/print_options", { SELECTS  => \@SELECTS, %template_vars } );
 
1148   if ($options{inline}) {
 
1149     $main::lxdebug->leave_sub();
 
1150     return $print_options;
 
1153   print $print_options;
 
1155   $main::lxdebug->leave_sub();
 
1159   $main::lxdebug->enter_sub();
 
1161   my $form     = $main::form;
 
1162   my $locale   = $main::locale;
 
1166   if ($form->{print_nextsub}) {
 
1167     call_sub($form->{print_nextsub});
 
1168     $main::lxdebug->leave_sub();
 
1172   # if this goes to the printer pass through
 
1174   if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
 
1175     $form->error($locale->text('Select postscript or PDF!'))
 
1176       if ($form->{format} !~ /(postscript|pdf)/);
 
1178     $old_form = new Form;
 
1179     map { $old_form->{$_} = $form->{$_} } keys %$form;
 
1182   if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
 
1183     if ($form->{formname} eq "proforma") {
 
1184       $form->{proforma} = 1;
 
1186     $form->{print_and_save} = 1;
 
1187     my $formname = $form->{formname};
 
1189     $form->{formname} = $formname;
 
1191     $::lxdebug->leave_sub();
 
1195   &print_form($old_form);
 
1197   $main::lxdebug->leave_sub();
 
1201   $main::lxdebug->enter_sub();
 
1203   my $form     = $main::form;
 
1204   my %myconfig = %main::myconfig;
 
1205   my $locale   = $main::locale;
 
1209   my ($old_form) = @_;
 
1213   my $numberfld = "invnumber";
 
1217     ($form->{display_form}) ? $form->{display_form} : "display_form";
 
1219   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
 
1220   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
 
1222   if ($form->{formname} eq "invoice") {
 
1223     $form->{label} = $locale->text('Invoice');
 
1225   if ($form->{formname} eq 'sales_order') {
 
1228     $form->{"${inv}date"} = $form->{transdate};
 
1229     $form->{label}        = $locale->text('Confirmation');
 
1230     $numberfld            = "sonumber";
 
1234   if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
 
1237     $form->{"${inv}date"} = $form->{invdate};
 
1238     $form->{label}        = $locale->text('Proforma Invoice');
 
1239     $numberfld            = "sonumber";
 
1243   if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
 
1246     $form->{"${inv}date"} = $form->{transdate};
 
1247     $form->{"invdate"}    = $form->{transdate};
 
1248     $form->{invnumber}    = $form->{ordnumber};
 
1249     $form->{label}        = $locale->text('Proforma Invoice');
 
1250     $numberfld            = "sonumber";
 
1254   if ($form->{formname} eq 'purchase_order') {
 
1257     $form->{"${inv}date"} = $form->{transdate};
 
1258     $form->{label}        = $locale->text('Purchase Order');
 
1259     $numberfld            = "ponumber";
 
1262   if ($form->{formname} eq 'bin_list') {
 
1265     $form->{"${inv}date"} = $form->{transdate};
 
1266     $form->{label}        = $locale->text('Bin List');
 
1269   if ($form->{formname} eq 'sales_quotation') {
 
1272     $form->{"${inv}date"} = $form->{transdate};
 
1273     $form->{label}        = $locale->text('Quotation');
 
1274     $numberfld            = "sqnumber";
 
1278   if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
 
1281     $form->{"${inv}date"} = $form->{transdate};
 
1282     $form->{"invdate"}    = $form->{transdate};
 
1283     $form->{label}        = $locale->text('Proforma Invoice');
 
1284     $numberfld            = "sqnumber";
 
1288   if ($form->{formname} eq 'request_quotation') {
 
1291     $form->{"${inv}date"} = $form->{transdate};
 
1292     $form->{label}        = $locale->text('RFQ');
 
1293     $numberfld            = "rfqnumber";
 
1297   if ($form->{type} =~ /_delivery_order$/) {
 
1300     $form->{"${inv}date"} = $form->{transdate};
 
1301     $numberfld            = $form->{type} =~ /^sales/ ? 'sdonumber' : 'pdonumber';
 
1302     $form->{label}        = $form->{formname} eq 'pick_list' ? $locale->text('Pick List') : $locale->text('Delivery Order');
 
1305   $form->isblank("email", $locale->text('E-mail address missing!'))
 
1306     if ($form->{media} eq 'email');
 
1307   $form->isblank("${inv}date",
 
1308            $locale->text($form->{label})
 
1310            . $locale->text(' Date missing!'));
 
1312   # $locale->text('Invoice Number missing!')
 
1313   # $locale->text('Invoice Date missing!')
 
1314   # $locale->text('Order Number missing!')
 
1315   # $locale->text('Order Date missing!')
 
1316   # $locale->text('Quotation Number missing!')
 
1317   # $locale->text('Quotation Date missing!')
 
1320   $form->{what_done} = $form->{formname};
 
1321   if (!$form->{"${inv}number"} && !$form->{preview} && !$form->{id}) {
 
1322     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
 
1323     if ($form->{media} ne 'email') {
 
1325       # get pricegroups for parts
 
1326       IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
1328       # build up html code for prices_$i
 
1329       set_pricegroup($form->{rowcount});
 
1331       $form->{rowcount}--;
 
1333       call_sub($display_form);
 
1334       # saving the history
 
1335       if(!exists $form->{addition}) {
 
1336         $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1337         $form->{addition} = "PRINTED";
 
1338         $form->save_history;
 
1340       # /saving the history
 
1347   # Save the email address given in the form because it should override the setting saved for the customer/vendor.
 
1348   my ($saved_email, $saved_cc, $saved_bcc) =
 
1349     ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
 
1351   my $language_saved = $form->{language_id};
 
1352   my $payment_id_saved = $form->{payment_id};
 
1353   my $salesman_id_saved = $form->{salesman_id};
 
1354   my $cp_id_saved = $form->{cp_id};
 
1355   my $taxzone_id_saved = $form->{taxzone_id};
 
1356   my $currency_saved = $form->{currency};
 
1358   call_sub("$form->{vc}_details") if ($form->{vc});
 
1360   $form->{language_id} = $language_saved;
 
1361   $form->{payment_id} = $payment_id_saved;
 
1362   $form->{taxzone_id} = $taxzone_id_saved;
 
1363   $form->{currency} = $currency_saved;
 
1365   $form->{"email"} = $saved_email if ($saved_email);
 
1366   $form->{"cc"}    = $saved_cc    if ($saved_cc);
 
1367   $form->{"bcc"}   = $saved_bcc   if ($saved_bcc);
 
1369   if (!$cp_id_saved) {
 
1370     # No contact was selected. Delete all contact variables because
 
1371     # IS->customer_details() and IR->vendor_details() get the default
 
1373     map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
 
1376   my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
 
1377   if ($form->{"language_id"}) {
 
1378     ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
 
1379       AM->get_language_details(\%myconfig, $form, $form->{language_id});
 
1381     $output_dateformat = $myconfig{"dateformat"};
 
1382     $output_numberformat = $myconfig{"numberformat"};
 
1383     $output_longdates = 1;
 
1386   # Store the output number format so that the template modules know
 
1387   # how to parse the amounts back if requested.
 
1388   $myconfig{output_numberformat} = $output_numberformat || $myconfig{numberformat};
 
1390   ($form->{employee}) = split /--/, $form->{employee};
 
1392   # create the form variables
 
1393   if ($form->{type} =~ /_delivery_order$/) {
 
1394     DO->order_details();
 
1396     OE->order_details(\%myconfig, \%$form);
 
1398     IS->invoice_details(\%myconfig, \%$form, $locale);
 
1401   $form->get_employee_data('prefix' => 'employee', 'id' => $form->{employee_id});
 
1402   $form->get_employee_data('prefix' => 'salesman', 'id' => $salesman_id_saved);
 
1404   if ($form->{shipto_id}) {
 
1405     $form->get_shipto(\%myconfig);
 
1408   my @a = qw(name department_1 department_2 street zipcode city country contact phone fax email);
 
1412   # if there is no shipto fill it in from billto
 
1413   foreach my $item (@a) {
 
1414     if ($form->{"shipto$item"}) {
 
1421     if (   $form->{formname} eq 'purchase_order'
 
1422         || $form->{formname} eq 'request_quotation') {
 
1423       $form->{shiptoname}   = $myconfig{company};
 
1424       $form->{shiptostreet} = $myconfig{address};
 
1426       map { $form->{"shipto$_"} = $form->{$_} } @a;
 
1430   $form->{notes} =~ s/^\s+//g;
 
1432   $form->{templates} = "$myconfig{templates}";
 
1434   delete $form->{printer_command};
 
1436   $form->{language} = $form->get_template_language(\%myconfig);
 
1439   if ($form->{media} ne 'email') {
 
1440     $printer_code = $form->get_printer_code(\%myconfig);
 
1441     if ($printer_code ne "") {
 
1442       $printer_code = "_" . $printer_code;
 
1446   if ($form->{language} ne "") {
 
1447     my $template_arrays = $form->{TEMPLATE_ARRAYS} || $form;
 
1448     map { $template_arrays->{unit}->[$_] = AM->translate_units($form, $form->{language}, $template_arrays->{unit}->[$_], $template_arrays->{qty}->[$_]); } (0..scalar(@{ $template_arrays->{unit} }) - 1);
 
1450     $form->{language} = "_" . $form->{language};
 
1454   format_dates($output_dateformat, $output_longdates,
 
1455                qw(invdate orddate quodate pldate duedate reqdate transdate
 
1456                   shippingdate deliverydate validitydate paymentdate
 
1457                   datepaid transdate_oe deliverydate_oe dodate
 
1458                   employee_startdate employee_enddate
 
1460                grep({ /^datepaid_\d+$/ ||
 
1461                         /^transdate_oe_\d+$/ ||
 
1462                         /^deliverydate_oe_\d+$/ ||
 
1464                         /^deliverydate_\d+$/ ||
 
1468   reformat_numbers($output_numberformat, 2,
 
1469                    qw(invtotal ordtotal quototal subtotal linetotal
 
1470                       listprice sellprice netprice discount
 
1471                       tax taxbase total paid),
 
1472                    grep({ /^(?:linetotal|nodiscount_linetotal|listprice|sellprice|netprice|taxbase|discount|p_discount|discount_sub|nodiscount_sub|paid|subtotal|total|tax)_\d+$/ } keys(%{$form})));
 
1474   reformat_numbers($output_numberformat, undef,
 
1475                    qw(qty price_factor),
 
1479   my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
 
1481   if (scalar @{ $cvar_date_fields }) {
 
1482     format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
 
1485   while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
 
1486     reformat_numbers($output_numberformat, $precision, @{ $field_list });
 
1489   my $extension = 'html';
 
1490   if ($form->{format} eq 'postscript') {
 
1491     $form->{postscript}   = 1;
 
1494   } elsif ($form->{"format"} =~ /pdf/) {
 
1496     $extension            = $form->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
 
1498   } elsif ($form->{"format"} =~ /opendocument/) {
 
1499     $form->{opendocument} = 1;
 
1501   } elsif ($form->{"format"} =~ /excel/) {
 
1506   # search for the template
 
1508   push @template_files, "$form->{formname}_email$form->{language}$printer_code.$extension" if $form->{media} eq 'email';
 
1509   push @template_files, "$form->{formname}$form->{language}$printer_code.$extension";
 
1510   push @template_files, "$form->{formname}.$extension";
 
1511   push @template_files, "default.$extension";
 
1512   @template_files = uniq @template_files;
 
1513   $form->{IN}     = first { -f "$myconfig{templates}/$_" } @template_files;
 
1515   if (!defined $form->{IN}) {
 
1516     $::form->error($::locale->text('Cannot find matching template for this print request. Please contact your template maintainer. I tried these: #1.', join ', ', map { "'$_'"} @template_files));
 
1519   delete $form->{OUT};
 
1521   if ($form->{media} eq 'printer') {
 
1522     $form->{OUT}      = $form->{printer_command};
 
1523     $form->{OUT_MODE} = '|-';
 
1524     $form->{printed} .= " $form->{formname}";
 
1525     $form->{printed}  =~ s/^ //;
 
1527   my $printed = $form->{printed};
 
1529   if ($form->{media} eq 'email') {
 
1530     $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
 
1531       unless $form->{subject};
 
1533     $form->{emailed} .= " $form->{formname}";
 
1534     $form->{emailed} =~ s/^ //;
 
1536   my $emailed = $form->{emailed};
 
1538   if ($form->{media} eq 'queue') {
 
1539     my %queued = map { s|.*/|| } split / /, $form->{queued};
 
1542     my $suffix = ($form->{postscript}) ? '.ps' : '.pdf';
 
1543     if ($filename = $queued{ $form->{formname} }) {
 
1544       $form->{queued} =~ s/\Q$form->{formname} $filename\E//;
 
1545       unlink $::lx_office_conf{paths}->{spool} . "/$filename";
 
1546       $filename =~ s/\..*$//g;
 
1547       $filename .= $suffix;
 
1548       $form->{OUT} = $::lx_office_conf{paths}->{spool} . "/$filename";
 
1549       $form->{OUT_MODE} = '>';
 
1552       ($temp_fh, $filename) = File::Temp::tempfile(
 
1553         'kivitendo-spoolXXXXXX',
 
1554         SUFFIX => "$suffix",
 
1555         DIR => $::lx_office_conf{paths}->{spool},
 
1558       $form->{OUT} = "$filename";
 
1559       # use >> for OUT_MODE because file is already created by File::Temp
 
1560       $form->{OUT_MODE} = '>>';
 
1561       # strip directory so that only filename is stored in table status
 
1562       ($filename) = $filename =~ /^$::lx_office_conf{paths}->{spool}\/(.*)/;
 
1566     $form->{queued} .= " $form->{formname} $filename";
 
1567     $form->{queued} =~ s/^ //;
 
1569   my $queued = $form->{queued};
 
1571 # saving the history
 
1572   if(!exists $form->{addition}) {
 
1573     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1574     if($form->{media} =~ /printer/) {
 
1575       $form->{addition} = "PRINTED";
 
1577     elsif($form->{media} =~ /email/) {
 
1578       $form->{addition} = "MAILED";
 
1580     elsif($form->{media} =~ /queue/) {
 
1581       $form->{addition} = "QUEUED";
 
1583     elsif($form->{media} =~ /screen/) {
 
1584       $form->{addition} = "SCREENED";
 
1586     $form->save_history;
 
1588   # /saving the history
 
1590   # prepare meta information for template introspection
 
1591   $form->{template_meta} = {
 
1592     formname  => $form->{formname},
 
1593     language  => SL::DB::Manager::Language->find_by_or_create(id => $form->{language_id}),
 
1594     format    => $form->{format},
 
1595     media     => $form->{media},
 
1596     extension => $extension,
 
1597     printer   => SL::DB::Manager::Printer->find_by_or_create(id => $form->{printer_id}),
 
1598     today     => DateTime->today,
 
1601   $form->parse_template(\%myconfig);
 
1603   $form->{callback} = "";
 
1605   if ($form->{media} eq 'email') {
 
1606     $form->{message} = $locale->text('sent') unless $form->{message};
 
1608   my $message = $form->{message};
 
1610   # if we got back here restore the previous form
 
1611   if ($form->{media} =~ /(printer|email|queue)/) {
 
1613     $form->update_status(\%myconfig)
 
1614       if ($form->{media} eq 'queue' && $form->{id});
 
1616     return $main::lxdebug->leave_sub() if ($old_form eq "return");
 
1620       $old_form->{"${inv}number"} = $form->{"${inv}number"};
 
1622       # restore and display form
 
1623       map { $form->{$_} = $old_form->{$_} } keys %$old_form;
 
1625       $form->{queued}  = $queued;
 
1626       $form->{printed} = $printed;
 
1627       $form->{emailed} = $emailed;
 
1628       $form->{message} = $message;
 
1630       $form->{rowcount}--;
 
1631       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
1632         qw(exchangerate creditlimit creditremaining);
 
1634       for my $i (1 .. $form->{paidaccounts}) {
 
1636           $form->{"${_}_$i"} =
 
1637             $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
 
1638         } qw(paid exchangerate);
 
1641       call_sub($display_form);
 
1646       ($form->{media} eq 'printer')
 
1647       ? $locale->text('sent to printer')
 
1648       : $locale->text('emailed to') . " $form->{email}";
 
1649     $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
 
1651   if ($form->{printing}) {
 
1652    call_sub($display_form);
 
1656   $main::lxdebug->leave_sub();
 
1659 sub customer_details {
 
1660   $main::lxdebug->enter_sub();
 
1662   my $form     = $main::form;
 
1663   my %myconfig = %main::myconfig;
 
1665   IS->customer_details(\%myconfig, \%$form, @_);
 
1667   $main::lxdebug->leave_sub();
 
1670 sub vendor_details {
 
1671   $main::lxdebug->enter_sub();
 
1673   my $form     = $main::form;
 
1674   my %myconfig = %main::myconfig;
 
1676   IR->vendor_details(\%myconfig, \%$form, @_);
 
1678   $main::lxdebug->leave_sub();
 
1682   $main::lxdebug->enter_sub();
 
1684   my $form     = $main::form;
 
1688   $form->{postasnew} = 1;
 
1689   map { delete $form->{$_} } qw(printed emailed queued);
 
1693   $main::lxdebug->leave_sub();
 
1697   $main::lxdebug->enter_sub();
 
1701   $::form->{print_and_post} = 0 if $::form->{second_run};
 
1703   map { $::form->{$_} = $::form->parse_amount(\%::myconfig, $::form->{$_}) } qw(exchangerate creditlimit creditremaining);
 
1705   # get details for customer/vendor
 
1706   call_sub($::form->{vc} . "_details", qw(name department_1 department_2 street zipcode city country contact email phone fax), $::form->{vc} . "number");
 
1708   # get pricegroups for parts
 
1709   IS->get_pricegroups_for_parts(\%::myconfig, \%$::form);
 
1711   # build up html code for prices_$i
 
1712   set_pricegroup($::form->{rowcount});
 
1714   $::form->{rowcount}--;
 
1716   my @shipto_vars   = qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry
 
1717                          shiptocontact shiptocp_gender shiptophone shiptofax shiptoemail
 
1718                          shiptodepartment_1 shiptodepartment_2);
 
1719   my $previous_form = $::auth->save_form_in_session(skip_keys => [ @shipto_vars, qw(header shipto_id) ]);
 
1720   $::form->{title}  = $::locale->text('Ship to');
 
1723   print $::form->parse_html_template('io/ship_to', { previousform => $previous_form,
 
1724                                                      nextsub      => $::form->{display_form} || 'display_form',
 
1727   $main::lxdebug->leave_sub();
 
1730 sub ship_to_entered {
 
1731   $::auth->restore_form_from_session(delete $::form->{previousform});
 
1732   call_sub($::form->{nextsub});
 
1735 sub relink_accounts {
 
1736   $main::lxdebug->enter_sub();
 
1738   my $form     = $main::form;
 
1739   my %myconfig = %main::myconfig;
 
1743   $form->{"taxaccounts"} =~ s/\s*$//;
 
1744   $form->{"taxaccounts"} =~ s/^\s*//;
 
1745   foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
 
1746     map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
 
1748   $form->{"taxaccounts"} = "";
 
1750   IC->retrieve_accounts(\%myconfig, $form, map { $_ => $form->{"id_$_"} } 1 .. $form->{rowcount});
 
1752   $main::lxdebug->leave_sub();
 
1756   $main::lxdebug->enter_sub();
 
1758   my $form     = $main::form;
 
1759   my %myconfig = %main::myconfig;
 
1763   my $invdate = $form->{invdate} eq 'undefined' ? undef : $form->{invdate};
 
1764   my $duedate = $form->get_duedate(\%myconfig, $invdate);
 
1766   print $form->ajax_response_header() . $duedate;
 
1768   $main::lxdebug->leave_sub();
 
1771 sub _update_part_information {
 
1772   $main::lxdebug->enter_sub();
 
1774   my $form     = $main::form;
 
1776   my %part_information = IC->get_basic_part_info('id'        => [ grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount}) ],
 
1777                                                  'vendor_id' => $form->{vendor_id});
 
1779   $form->{PART_INFORMATION} = \%part_information;
 
1781   foreach my $i (1..$form->{rowcount}) {
 
1782     next unless ($form->{"id_${i}"});
 
1784     my $info                 = $form->{PART_INFORMATION}->{$form->{"id_${i}"}} || { };
 
1785     $form->{"partunit_${i}"} = $info->{unit};
 
1788   $main::lxdebug->leave_sub();
 
1792   $main::lxdebug->enter_sub();
 
1794   my $form     = $main::form;
 
1795   my %myconfig = %main::myconfig;
 
1797   if (!$form->{ordnumber} || !$form->{id}) {
 
1798     map { $form->{"ship_$_"} = 0 } (1..$form->{rowcount});
 
1799     $main::lxdebug->leave_sub();
 
1803   my $all_units = AM->retrieve_all_units();
 
1805   my %ship = DO->get_shipped_qty('type'  => ($form->{type} eq 'purchase_order') ? 'purchase' : 'sales',
 
1806                                  'oe_id' => $form->{id},);
 
1808   foreach my $i (1..$form->{rowcount}) {
 
1809     next unless ($form->{"id_${i}"});
 
1811     $form->{"ship_$i"} = 0;
 
1813     my $ship_entry = $ship{$form->{"id_$i"}};
 
1815     next if (!$ship_entry || ($ship_entry->{qty} <= 0));
 
1818       ($form->{simple_save} ? $form->{"qty_$i"} : $form->parse_amount(\%myconfig, $form->{"qty_$i"}))
 
1819       * $all_units->{$form->{"unit_$i"}}->{factor}
 
1820       / $all_units->{$form->{"partunit_$i"}}->{factor};
 
1822     $form->{"ship_$i"}  = min($rowqty, $ship_entry->{qty});
 
1823     $ship_entry->{qty} -= $form->{"ship_$i"};
 
1826   foreach my $i (1..$form->{rowcount}) {
 
1827     next unless ($form->{"id_${i}"});
 
1829     my $ship_entry = $ship{$form->{"id_$i"}};
 
1831     next if (!$ship_entry || ($ship_entry->{qty} <= 0.01));
 
1833     $form->{"ship_$i"} += $ship_entry->{qty};
 
1834     $ship_entry->{qty}  = 0;
 
1837   $main::lxdebug->leave_sub();
 
1840 sub _update_custom_variables {
 
1841   $main::lxdebug->enter_sub();
 
1843   my $form     = $main::form;
 
1845   $form->{CVAR_CONFIGS}         = { } unless ref $form->{CVAR_CONFIGS} eq 'HASH';
 
1846   $form->{CVAR_CONFIGS}->{IC} ||= CVar->get_configs(module => 'IC');
 
1848   $main::lxdebug->leave_sub();
 
1851 sub _render_custom_variables_inputs {
 
1852   $main::lxdebug->enter_sub(2);
 
1854   my $form     = $main::form;
 
1858   if (!$form->{CVAR_CONFIGS}->{IC}) {
 
1859     $main::lxdebug->leave_sub();
 
1863   my $valid = CVar->custom_variables_validity_by_trans_id(trans_id => $params{part_id});
 
1865   my $num_visible_cvars = 0;
 
1866   foreach my $cvar (@{ $form->{CVAR_CONFIGS}->{IC} }) {
 
1867     $cvar->{valid} = $params{part_id} && $valid->($cvar->{id});
 
1869     my $description = '';
 
1870     if ($cvar->{flag_editable} && $cvar->{valid}) {
 
1871       $num_visible_cvars++;
 
1872       $description = $cvar->{description} . ' ';
 
1875     my $form_key = "ic_cvar_" . $cvar->{name} . "_$params{row}";
 
1877     push @{ $params{ROW2} }, {
 
1878       line_break     => $num_visible_cvars == 1,
 
1879       description    => $description,
 
1882          hide_non_editable => 1,
 
1884          name_prefix       => 'ic_',
 
1885          name_postfix      => "_$params{row}",
 
1886          valid             => $cvar->{valid},
 
1887          value             => CVar->parse($::form->{$form_key}, $cvar),
 
1892   $main::lxdebug->leave_sub(2);