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