From f601941b50eae7e1a09829313ed020afbf915c0f Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 29 Aug 2019 18:42:24 +0000 Subject: [PATCH] Bug 23523: unitprice tax column values are not populated if entered upon ordering To test: 1 - Add an item to an acquisitions basket 2 - Make sure to enter 'Actual cost' 3 - Check the db: SELECT * FROM aqorders WHERE ordernumber={your ordernumber} 4 - Note that unitprice_tax_included and unitprice_tax_excluded are not populated 5 - Apply patch 6 - Edit that order 7 - Check DB 8 - Values should be populated 9 - Place another order, ensude values populated on creation 10 - QA people: prove -v t/db_dependent/Acquisition/populate_order_with_prices.t Signed-off-by: Claire Gravely Signed-off-by: Martin Renvoize --- C4/Acquisition.pm | 22 ++++++--- .../Acquisition/populate_order_with_prices.t | 46 ++++++++++++++++--- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 75429e7e22..ba0028a37c 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -3062,10 +3062,13 @@ sub populate_order_with_prices { if ($ordering) { $order->{tax_rate_on_ordering} //= $order->{tax_rate}; if ( $bookseller->listincgst ) { - # The user entered the rrp tax included + + # The user entered the prices tax included + $order->{unitprice_tax_included} = $order->{unitprice}; $order->{rrp_tax_included} = $order->{rrp}; - # rrp tax excluded = rrp tax included / ( 1 + tax rate ) + # price tax excluded = price tax included / ( 1 + tax rate ) + $order->{unitprice_tax_excluded} = $order->{unitprice_tax_included} / ( 1 + $order->{tax_rate_on_ordering} ); $order->{rrp_tax_excluded} = $order->{rrp_tax_included} / ( 1 + $order->{tax_rate_on_ordering} ); # ecost tax included = rrp tax included ( 1 - discount ) @@ -3075,14 +3078,19 @@ sub populate_order_with_prices { $order->{ecost_tax_excluded} = $order->{rrp_tax_excluded} * ( 1 - $discount ); # tax value = quantity * ecost tax excluded * tax rate - $order->{tax_value_on_ordering} = ( get_rounded_price($order->{ecost_tax_included}) - get_rounded_price($order->{ecost_tax_excluded}) ) * $order->{quantity}; + # we should use the unitprice if included + my $cost_tax_included = $order->{unitprice_tax_included} || $order->{ecost_tax_included}; + my $cost_tax_excluded = $order->{unitprice_tax_excluded} || $order->{ecost_tax_excluded}; + $order->{tax_value_on_ordering} = ( get_rounded_price($cost_tax_included) - get_rounded_price($cost_tax_excluded) ) * $order->{quantity}; } else { - # The user entered the rrp tax excluded + # The user entered the prices tax excluded + $order->{unitprice_tax_excluded} = $order->{unitprice}; $order->{rrp_tax_excluded} = $order->{rrp}; - # rrp tax included = rrp tax excluded * ( 1 - tax rate ) + # price tax included = price tax excluded * ( 1 - tax rate ) + $order->{unitprice_tax_included} = $order->{unitprice_tax_excluded} * ( 1 + $order->{tax_rate_on_ordering} ); $order->{rrp_tax_included} = $order->{rrp_tax_excluded} * ( 1 + $order->{tax_rate_on_ordering} ); # ecost tax excluded = rrp tax excluded * ( 1 - discount ) @@ -3092,7 +3100,9 @@ sub populate_order_with_prices { $order->{ecost_tax_included} = $order->{ecost_tax_excluded} * ( 1 + $order->{tax_rate_on_ordering} ); # tax value = quantity * ecost tax included * tax rate - $order->{tax_value_on_ordering} = $order->{quantity} * get_rounded_price($order->{ecost_tax_excluded}) * $order->{tax_rate_on_ordering}; + # we should use the unitprice if included + my $cost_tax_excluded = $order->{unitprice_tax_excluded} || $order->{ecost_tax_excluded}; + $order->{tax_value_on_ordering} = $order->{quantity} * get_rounded_price($cost_tax_excluded) * $order->{tax_rate_on_ordering}; } } diff --git a/t/db_dependent/Acquisition/populate_order_with_prices.t b/t/db_dependent/Acquisition/populate_order_with_prices.t index 16772e9eb8..874ffebec6 100644 --- a/t/db_dependent/Acquisition/populate_order_with_prices.t +++ b/t/db_dependent/Acquisition/populate_order_with_prices.t @@ -2,7 +2,7 @@ use Modern::Perl; -use Test::More tests => 34; +use Test::More tests => 44; use C4::Acquisition; use C4::Context; use Koha::Database; @@ -44,7 +44,7 @@ my $order_exc_tax = { tax_rate => .1965, discount => .42, rrp => 16.99, - unitprice => 9.85, + unitprice => 0.00, quantity => 8, }; @@ -60,7 +60,19 @@ is( $order_with_prices->{rrp_tax_excluded}+0 ,16.99 ,"Ordering tax exc is( $order_with_prices->{rrp_tax_included}+0 ,20.328535 ,"Ordering tax excluded, no round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)"); is( $order_with_prices->{ecost_tax_excluded}+0 ,9.8542 ,"Ordering tax excluded, no round: ecost tax excluded is rrp * ( 1 - discount )"); is( $order_with_prices->{ecost_tax_included}+0 ,11.7905503 ,"Ordering tax excluded, no round: ecost tax included is ecost tax excluded * (1 + tax rate on ordering)"); -is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4908024 ,"Ordering tax excluded, no round: tax value on ordering is quantity * ecost_tax_excluded * tax rate on ordering"); +is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4908024 ,"Ordering tax excluded, no round: tax value on ordering is quantity * ecost_tax_excluded * tax rate on ordering if no unitprice"); + +$order_exc_tax->{unitprice} = 9.85; + +$order_with_prices = C4::Acquisition::populate_order_with_prices({ + ordering => 1, + booksellerid => $bookseller_exc_tax->id, + order => $order_exc_tax, +}); + +is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85 ,"Ordering tax excluded, no round: rrp tax excluded is rrp"); +is( $order_with_prices->{unitprice_tax_included}+0 ,11.785525 ,"Ordering tax excluded, no round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)"); +is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4842 ,"Ordering tax excluded, no round: tax value on ordering is quantity * unitprice_tax_excluded * tax rate on ordering if unitprice"); #Vendor prices exclude tax, no rounding, receiving $order_with_prices = C4::Acquisition::populate_order_with_prices({ @@ -72,7 +84,12 @@ $order_with_prices = C4::Acquisition::populate_order_with_prices({ is( $order_with_prices->{unitprice}+0 ,9.8542 ,"Receiving tax excluded, no round, rounded ecost tax excluded = rounded unitprice : unitprice is ecost tax excluded"); is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.8542 ,"Receiving tax excluded, no round, rounded ecost tax excluded = rounded unitprice : unitprice tax excluded is ecost tax excluded"); is( $order_with_prices->{unitprice_tax_included}+0 ,11.7905503 ,"Receiving tax excluded, no round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)"); -is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4908024 ,"Receiving tax excluded, no round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving"); +is( $order_with_prices->{tax_value_on_receiving}+0 ,15.4908024 ,"Receiving tax excluded, no round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving"); + + +$order_exc_tax->{unitprice} = 9.85; +#populate order with prices updates the passed in order hashref +#we need to reset after additional tests and changes #Vendor prices exclude tax, rounding to nearest cent, ordering t::lib::Mocks::mock_preference('OrderPriceRounding', 'nearest_cent'); @@ -82,6 +99,8 @@ $order_with_prices = C4::Acquisition::populate_order_with_prices({ order => $order_exc_tax, }); +is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85 ,"Ordering tax excluded, round: unitprice tax excluded is unitprice"); +is( $order_with_prices->{unitprice_tax_included}+0 ,11.785525 ,"Ordering tax excluded, round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)"); is( $order_with_prices->{rrp_tax_excluded}+0 ,16.99 ,"Ordering tax excluded, round: rrp tax excluded is rrp"); is( $order_with_prices->{rrp_tax_included}+0 ,20.328535 ,"Ordering tax excluded, round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)"); is( $order_with_prices->{ecost_tax_excluded}+0 ,9.8542 ,"Ordering tax excluded, round: ecost tax excluded is rrp * ( 1 - discount )"); @@ -105,7 +124,7 @@ my $order_inc_tax = { tax_rate => .1965, discount => .42, rrp => 20.33, - unitprice => 11.79, + unitprice => 0.00, quantity => 8, }; @@ -121,8 +140,18 @@ is( $order_with_prices->{rrp_tax_included}+0 ,20.33 ,"Ordering t is( $order_with_prices->{rrp_tax_excluded}+0 ,16.9912244045132 ,"Ordering tax included, no round: rrp tax excluded is rrp tax included / (1 + tax rate on ordering)"); is( $order_with_prices->{ecost_tax_included}+0 ,11.7914 ,"Ordering tax included, no round: ecost tax included is rrp tax included * (1 - discount)"); is( $order_with_prices->{ecost_tax_excluded}+0 ,9.85491015461764 ,"Ordering tax included, no round: ecost tax excluded is rrp tax excluded * ( 1 - discount )"); -is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4919187630589 ,"Ordering tax included, no round: tax value on ordering is ( ecost tax included - ecost tax excluded ) * quantity"); +is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4919187630589 ,"Ordering tax included, no round: tax value on ordering is ( ecost tax included - ecost tax excluded ) * quantity if no unitprice"); + +$order_inc_tax->{unitprice} = 11.79; +$order_with_prices = C4::Acquisition::populate_order_with_prices({ + ordering => 1, + booksellerid => $bookseller_inc_tax->id, + order => $order_inc_tax, +}); +is( $order_with_prices->{unitprice_tax_included}+0 ,11.79 ,"Ordering tax included, no round: unitprice tax included is unitprice"); +is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85374007521939 ,"Ordering tax included, no round: unitprice tax excluded is unitprice tax included / (1 + tax_rate_on_ordering "); +is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4900793982449 ,"Ordering tax included, no round: tax value on ordering is ( unitprice tax included - unitprice tax excluded ) * quantity if unitprice"); #Vendor prices include tax, no rounding, receiving $order_with_prices = C4::Acquisition::populate_order_with_prices({ @@ -134,16 +163,19 @@ $order_with_prices = C4::Acquisition::populate_order_with_prices({ is( $order_with_prices->{unitprice}+0 ,11.7914 ,"Receiving tax included, no round, rounded ecost tax excluded = rounded unitprice : unitprice is ecost tax excluded"); is( $order_with_prices->{unitprice_tax_included}+0 ,11.7914 ,"Receiving tax included, no round: unitprice tax included is unitprice"); is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85491015461764 ,"Receiving tax included, no round: unitprice tax excluded is unitprice tax included / (1 + tax rate on receiving)"); -is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4919187630589 ,"Receiving tax included, no round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving"); +is( $order_with_prices->{tax_value_on_receiving}+0 ,15.4919187630589 ,"Receiving tax included, no round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving"); #Vendor prices include tax, rounding to nearest cent, ordering t::lib::Mocks::mock_preference('OrderPriceRounding', 'nearest_cent'); +$order_inc_tax->{unitprice} = 11.79; $order_with_prices = C4::Acquisition::populate_order_with_prices({ ordering => 1, booksellerid => $bookseller_inc_tax->id, order => $order_inc_tax, }); +is( $order_with_prices->{unitprice_tax_included}+0 ,11.79 ,"Ordering tax included, round: unitprice tax included is unitprice"); +is( $order_with_prices->{unitprice_tax_excluded}+0,9.85374007521939 ,"Ordering tax included, round: unitprice tax excluded is unitprice tax included / (1 + tax_rate_on_ordering "); is( $order_with_prices->{rrp_tax_included}+0 ,20.33 ,"Ordering tax included, round: rrp tax included is rrp"); is( $order_with_prices->{rrp_tax_excluded}+0 ,16.9912244045132 ,"Ordering tax included, round: rrp tax excluded is rounded rrp tax included * (1 + tax rate on ordering)"); is( $order_with_prices->{ecost_tax_included}+0 ,11.7914 ,"Ordering tax included, round: ecost tax included is rounded rrp * ( 1 - discount )"); -- 2.39.5