From 74a0b4e21e3e8ed08483a1ca4b00c76d7f0dffa9 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. Depends on bug 29955 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 c91353a070..5d08866352 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; @@ -241,6 +242,24 @@ sub add { return try { my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') ); + + 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( diff --git a/t/db_dependent/api/v1/acquisitions_orders.t b/t/db_dependent/api/v1/acquisitions_orders.t index cee1aecb5e..702d3c00bb 100755 --- a/t/db_dependent/api/v1/acquisitions_orders.t +++ b/t/db_dependent/api/v1/acquisitions_orders.t @@ -272,7 +272,7 @@ subtest 'get() tests' => sub { subtest 'add() tests' => sub { - plan tests => 17; + plan tests => 27; $schema->storage->txn_begin; @@ -354,6 +354,31 @@ subtest 'add() tests' => sub { ->json_like( '/conflict' => qr/(aqorders\.)?PRIMARY/ ); } 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.30.2