Bug 18736: (follow-up) Fix missing rounding and bad formatting
authorNick Clemens <nick@bywatersolutions.com>
Fri, 24 Aug 2018 01:22:35 +0000 (01:22 +0000)
committerNick Clemens <nick@bywatersolutions.com>
Thu, 21 Mar 2019 16:29:06 +0000 (16:29 +0000)
This patch:
Adds a missing use
Uses 'Koha::Number::Price->round()' instead of 'format()' to ensure numeric
returns
Ensures too big numbers don't crash round()
Uses syspref in 'GetBudgetHierarchy'

To test:
Follow previous test plan
Check values on admin/aqbudgets.pl are affected by syspref
Ensure values throughout acquisitions are correctly calculated/displayed
(even when greater than 1,000)

Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

C4/Acquisition.pm
C4/Budgets.pm
Koha/Number/Price.pm
acqui/spent.pl

index 9f892e9..4d33a68 100644 (file)
@@ -2026,7 +2026,7 @@ returns a price rounded as specified in OrderPriceRounding system preference.
 sub get_rounded_price {
     my ( $price ) =  @_;
     my $rounding_pref = C4::Context->preference('OrderPriceRounding');
-    if( $rounding_pref eq 'nearest_cent' ) { return Koha::Number::Price->new( $price )->format(); }
+    if( $rounding_pref eq 'nearest_cent' ) { return Koha::Number::Price->new( $price )->round(); }
     else                                   { return $price; }
 }
 
index e1ef185..e43b44f 100644 (file)
@@ -561,14 +561,14 @@ sub GetBudgetHierarchy {
     # Get all the budgets totals in as few queries as possible
     my $hr_budget_spent = $dbh->selectall_hashref(q|
         SELECT aqorders.budget_id, aqbudgets.budget_parent_id,
-               SUM( COALESCE(unitprice_tax_included, ecost_tax_included) * quantity ) AS budget_spent
+               SUM( | . _get_rounding_sql(qq|COALESCE(unitprice_tax_included, ecost_tax_included)|) . q| * quantity ) AS budget_spent
         FROM aqorders JOIN aqbudgets USING (budget_id)
         WHERE quantityreceived > 0 AND datecancellationprinted IS NULL
         GROUP BY budget_id, budget_parent_id
         |, 'budget_id');
     my $hr_budget_ordered = $dbh->selectall_hashref(q|
         SELECT aqorders.budget_id, aqbudgets.budget_parent_id,
-               SUM(ecost_tax_included *  quantity) AS budget_ordered
+               SUM( | . _get_rounding_sql(qq|ecost_tax_included|) . q| *  quantity) AS budget_ordered
         FROM aqorders JOIN aqbudgets USING (budget_id)
         WHERE quantityreceived = 0 AND datecancellationprinted IS NULL
         GROUP BY budget_id, budget_parent_id
index 64c9dfe..7b0b826 100644 (file)
@@ -19,7 +19,7 @@ package Koha::Number::Price;
 
 use Modern::Perl;
 
-use Number::Format qw( format_price );
+use Number::Format;
 use C4::Context;
 use Koha::Acquisition::Currencies;
 
@@ -84,6 +84,11 @@ sub round {
 
     my $format_params = $self->_format_params;
 
+    # To avoid the system to crash, we will not format big number
+    # We divide per 100 because we want to keep the default DECIMAL_DIGITS (2)
+    # error - round() overflow. Try smaller precision or use Math::BigFloat
+    return $self->value if $self->value > Number::Format::MAX_INT/100;
+
     return Number::Format->new(%$format_params)->round($self->value);
 }
 
index 3801b44..80d9361 100755 (executable)
@@ -34,6 +34,7 @@ use C4::Auth;
 use C4::Output;
 use Modern::Perl;
 use CGI qw ( -utf8 );
+use C4::Acquisition;
 use Koha::Acquisition::Invoice::Adjustments;
 
 my $dbh      = C4::Context->dbh;