From 8c663b9df37863d3170e89e2309dee3d600d1901 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Wed, 1 Mar 2023 01:02:01 +0000 Subject: [PATCH] Bug 33098: Revert suggestion status when orders are cancelled This enhancement will revert the status of a suggestion from ORDERED to ACCEPTED when an order made from a suggestion is cancelled. To test: 1. Log into the OPAC and go to purchase suggestions 2. Add a new purchase suggestion 3. Log into the staff interface. The suggestion will show on the home page 4. Select the suggestion and mark it as Accepted 5. Go to Acquisitions. Find or create a basket 6. Add an order to the basket from a suggestion 7. You'll be redirected to the suggestions page. Click the order button next to your suggestion 8. Fill out the order details as normal and confirm the order. 9. In a new tab, go to the suggestions management page and confirm the suggestion now shows as ORDERED. 10. In the other tab with your basket, cancel the order you just made from the suggestion. 11. Refresh the suggestions management tab. Confirm the suggestion now shows as ACCEPTED. 12. Confirm tests pass t/db_dependent/Koha/Acquisition/Order.t Sponsored-by: Waikato Institute of Technology Signed-off-by: Laura Escamilla Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Tomas Cohen Arazi --- Koha/Acquisition/Order.pm | 13 ++++++++ t/db_dependent/Koha/Acquisition/Order.t | 43 ++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Koha/Acquisition/Order.pm b/Koha/Acquisition/Order.pm index 5c2ef810ff..73f3a9fdcd 100644 --- a/Koha/Acquisition/Order.pm +++ b/Koha/Acquisition/Order.pm @@ -21,6 +21,7 @@ use Carp qw( croak ); use C4::Biblio qw( DelBiblio ); use C4::Acquisition; +use C4::Suggestions qw( ModSuggestion ); use Koha::Acquisition::Baskets; use Koha::Acquisition::Funds; @@ -144,6 +145,18 @@ sub cancel { } } + # If ordered from a suggestion, revert the suggestion status to ACCEPTED + my $suggestion = Koha::Suggestions->find({ biblionumber => $self->biblionumber, status => "ORDERED" }); + if ( $suggestion and $suggestion->id ) { + ModSuggestion( + { + suggestionid => $suggestion->id, + biblionumber => $self->biblionumber, + STATUS => 'ACCEPTED', + } + ); + } + my $biblio = $self->biblio; if ( $biblio and $delete_biblio ) { diff --git a/t/db_dependent/Koha/Acquisition/Order.t b/t/db_dependent/Koha/Acquisition/Order.t index 1d280915ba..22e4af6225 100755 --- a/t/db_dependent/Koha/Acquisition/Order.t +++ b/t/db_dependent/Koha/Acquisition/Order.t @@ -655,7 +655,7 @@ subtest 'creator ()' => sub { subtest 'cancel() tests' => sub { - plan tests => 54; + plan tests => 56; $schema->storage->txn_begin; @@ -891,6 +891,47 @@ subtest 'cancel() tests' => sub { @messages = @{ $order->object_messages }; is( scalar @messages, 0, 'No errors' ); + # Scenario: + # * order made from a suggestion with same biblionumber + # => order is cancelled + # => suggestion status is changed to ACCEPTED + + $item = $builder->build_sample_item; + $biblio_id = $item->biblionumber; + + # Add the suggestion + my $suggestion = $builder->build_object( + { + class => 'Koha::Suggestions', + value => { + biblionumber => $biblio_id, + suggesteddate => dt_from_string, + STATUS => 'ORDERED', + archived => 0, + } + } + ); + + $order = $builder->build_object( + { + class => 'Koha::Acquisition::Orders', + value => { + orderstatus => 'new', + biblionumber => $biblio_id, + datecancellationprinted => undef, + cancellationreason => undef, + } + } + ); + + $order->cancel({ reason => $reason }) + ->discard_changes; + + $suggestion = Koha::Suggestions->find( $suggestion->id ); + + is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' ); + is( $suggestion->STATUS, 'ACCEPTED', 'Suggestion status is correctly reverted after order is cancelled' ); + # Scenario: # * order with two items attached # * one of the items is on loan -- 2.39.5