From 000d56214a3bd5d876209c9c3ba9fc8a547036d3 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Thu, 27 Jan 2022 13:50:52 +0100 Subject: [PATCH] Bug 29959: Populate prices when creating an order with the API Price columns (aqorders.*_tax_included, aqorders.*_tax_excluded, aqorders.tax_value_*) are automatically calculated when creating an order with the staff interface. They should also be automatically calculated when the order is created with the API. Test plan: 1. Run tests in t/db_dependent/api/v1/acquisitions_orders.t Signed-off-by: David Nind --- Koha/REST/V1/Acquisitions/Orders.pm | 19 +++++++++++++++ t/db_dependent/api/v1/acquisitions_orders.t | 27 ++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Koha/REST/V1/Acquisitions/Orders.pm b/Koha/REST/V1/Acquisitions/Orders.pm index 39120dda3f..dd39437a0b 100644 --- a/Koha/REST/V1/Acquisitions/Orders.pm +++ b/Koha/REST/V1/Acquisitions/Orders.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Mojo::Base 'Mojolicious::Controller'; use Koha::Acquisition::Orders; +use Koha::Exceptions; use Clone qw( clone ); use JSON; @@ -120,6 +121,24 @@ sub add { return try { my $order = Koha::Acquisition::Order->new_from_api( $c->req->json ); + + my $basket = $order->basket; + unless ($basket) { + Koha::Exceptions::ObjectNotFound->throw(); + } + + my $bookseller = $basket->bookseller; + + unless (defined $order->tax_rate_on_ordering) { + $order->tax_rate_on_ordering($bookseller->tax_rate || 0); + } + $order->populate_with_prices_for_ordering(); + + unless (defined $order->tax_rate_on_receiving) { + $order->tax_rate_on_receiving($bookseller->tax_rate || 0); + } + $order->populate_with_prices_for_receiving(); + $order->store->discard_changes; $c->res->headers->location( $c->req->url->to_string . '/' . $order->ordernumber ); diff --git a/t/db_dependent/api/v1/acquisitions_orders.t b/t/db_dependent/api/v1/acquisitions_orders.t index 9ecc657bb0..6d59ab119c 100755 --- a/t/db_dependent/api/v1/acquisitions_orders.t +++ b/t/db_dependent/api/v1/acquisitions_orders.t @@ -286,7 +286,7 @@ subtest 'get() tests' => sub { subtest 'add() tests' => sub { - plan tests => 17; + plan tests => 27; $schema->storage->txn_begin; @@ -369,6 +369,31 @@ subtest 'add() tests' => sub { } qr/DBD::mysql::st execute failed: Duplicate entry '(.*)' for key '(aqorders\.)?PRIMARY'/; + # Check that prices are correctly populated + my $order_without_prices = { + basket_id => $order->{basket_id}, + biblio_id => $order->{biblio_id}, + fund_id => $order->{fund_id}, + quantity => 1, + tax_rate_on_ordering => '0.5', + tax_rate_on_receiving => '0.5', + unit_price => 4.20, + rrp => 4.20, + discount_rate => 0.10, + }; + + $order_obj->basket->bookseller->listincgst(0)->invoiceincgst(0)->store(); + $t->post_ok("//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order_without_prices) + ->status_is(201) + ->json_is('/tax_value_on_ordering', '2.1') + ->json_is('/tax_value_on_receiving', '2.1') + ->json_is('/rrp_tax_included', '6.3') + ->json_is('/rrp_tax_excluded', '4.2') + ->json_is('/unit_price_tax_included', '6.3') + ->json_is('/unit_price_tax_excluded', '4.2') + ->json_is('/ecost_tax_included', '5.67') + ->json_is('/ecost_tax_excluded', '3.78'); + $schema->storage->txn_rollback; }; -- 2.39.5