X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/2536b717d4c7ce36e0bd2a85a6fba9e81d980955..99d8aaf4fd6df466ee540770cc7e80ae724dd4ad:/SL/Form.pm diff --git a/SL/Form.pm b/SL/Form.pm index 7030454d6..1368df738 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -948,24 +948,32 @@ sub parse_amount { } sub round_amount { - $main::lxdebug->enter_sub(2); - my ($self, $amount, $places) = @_; - my $round_amount; # Rounding like "Kaufmannsrunden" (see http://de.wikipedia.org/wiki/Rundung ) # Round amounts to eight places before rounding to the requested # number of places. This gets rid of errors due to internal floating # point representation. - $amount = $self->round_amount($amount, 8) if $places < 8; - $amount = $amount * (10**($places)); - $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places)); + $amount = $self->round_amount($amount, 8) if $places < 8; - $main::lxdebug->leave_sub(2); + # Remember the amount's sign but calculate in positive values only. + my $sign = $amount <=> 0; + $amount = abs $amount; + + # Shift the amount left by $places+1 decimal places and truncate it + # to integer. Then to the integer equivalent of rounding to the next + # multiple of 10: first add half of it (5). Then truncate it back to + # the lower multiple of 10 by subtracting $amount modulo 10. + my $shift = 10 ** ($places + 1); + $amount = int($amount * $shift) + 5; + $amount -= $amount % 10; - return $round_amount; + # Lastly shift the amount back right by $places+1 decimal places and + # restore its sign. Then we're done. + $amount = ($amount / $shift) * $sign; + return $amount; } sub parse_template { @@ -1026,7 +1034,7 @@ sub parse_template { %{ $self->{TEMPLATE_DRIVER_OPTIONS} || {} }); # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be. - $self->{"notes"} = $self->{ $self->{"formname"} . "notes" }; + $self->{"notes"} = $self->{ $self->{"formname"} . "notes" } if exists $self->{ $self->{"formname"} . "notes" }; if (!$self->{employee_id}) { $self->{"employee_${_}"} = $myconfig->{$_} for qw(email tel fax name signature); @@ -2137,8 +2145,10 @@ sub _get_taxzones { my ($self, $dbh, $key) = @_; $key = "all_taxzones" unless ($key); + my $tzfilter = ""; + $tzfilter = "WHERE obsolete is FALSE" if $key eq 'ALL_ACTIVE_TAXZONES'; - my $query = qq|SELECT * FROM tax_zones ORDER BY id|; + my $query = qq|SELECT * FROM tax_zones $tzfilter ORDER BY sortkey|; $self->{$key} = selectall_hashref_query($self, $dbh, $query); @@ -3344,7 +3354,7 @@ sub prepare_for_printing { # Load shipping address from database if shipto_id is set. if ($self->{shipto_id}) { - my $shipto = SL::DB::Shipto->new(id => $self->{shipto_id})->load; + my $shipto = SL::DB::Shipto->new(shipto_id => $self->{shipto_id})->load; $self->{$_} = $shipto->$_ for grep { m{^shipto} } map { $_->name } @{ $shipto->meta->columns }; } @@ -3353,15 +3363,15 @@ sub prepare_for_printing { my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates); if ($self->{language_id}) { ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) = AM->get_language_details(\%::myconfig, $self, $self->{language_id}); - } else { - $output_dateformat = $::myconfig{dateformat}; - $output_numberformat = $::myconfig{numberformat}; - $output_longdates = 1; } - $self->{myconfig_output_dateformat} = $output_dateformat; - $self->{myconfig_output_longdates} = $output_longdates; - $self->{myconfig_output_numberformat} = $output_numberformat; + $output_dateformat ||= $::myconfig{dateformat}; + $output_numberformat ||= $::myconfig{numberformat}; + $output_longdates //= 1; + + $self->{myconfig_output_dateformat} = $output_dateformat // $::myconfig{dateformat}; + $self->{myconfig_output_longdates} = $output_longdates // 1; + $self->{myconfig_output_numberformat} = $output_numberformat // $::myconfig{numberformat}; # Retrieve accounts for tax calculation. IC->retrieve_accounts(\%::myconfig, $self, map { $_ => $self->{"id_$_"} } 1 .. $self->{rowcount});