From e8414baf5b69a239b7c2df43b88c027be84794a4 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 2 Dec 2019 16:32:13 +0100 Subject: [PATCH] Bug 8132: Adding a new message 'last_item_for_hold' blocking item deletion If an item is the last one of a biblio that have biblio-level hold placed on it, we should block the deletion. It takes effect if the hold is found (W or T), to follow existing behavior for item-level holds. If we want to block deletion for any holds we should deal with it on a separate bug report. Test plan: 0/ Setup Apply the patches Create Biblio B1 with 1 item Create Biblio B2 with 2 items Create Biblio B3 with 1+ item Create Biblio B4 with 1+ item Create Biblio B5 with 1+ item Place a biblio-level hold on B1 and B2 Place an item-level hold on B3 and B4 Confirm the item-level hold for the items of B3 to mark it waiting. 1/ Delete those 6 items in a batch => delete of item from B1 is blocked on first screen - only 1 item left and there is a biblio-level hold on the record => delete of items from B2 is *not* blocked on first screen - One of them will block the deletion, but so far we are not aware of that situation => delete of item from B3 is blocked on first screen - there is a waiting item-level hold placed on the item => delete of item from B4 is *not* blocked - there is a hold but it is not found => delete of item from B5 is *not* - there is no reason to block its deletion Note that you can only select items from B2, B4 and B5 2/ Select them and confirm the deletion => Nothing happened and you get a message saying that one of the 2 items from B2 is blocking the whole deletion process 3/ Remove the biblio-level hold from B2 4/ Repeat 1 => The deletion has been effective! => Note that there is something a bit weird as we are blocking items from a biblio that has biblio-level holds on it (not found), but we do not blocking the deletion of an item with a waiting item-level hold Signed-off-by: Kelly McElligott Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Marcel de Rooy Signed-off-by: Martin Renvoize --- Koha/Item.pm | 10 ++++++++++ t/db_dependent/Koha/Item.t | 10 +++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index 9e1bca4ef5..57fe8f1b20 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -234,6 +234,8 @@ returns 1 if the item is safe to delete, "linked_analytics" if the item has linked analytic records. +"last_item_for_hold" if the item is the last one on a record on which a biblio-level hold is placed + =cut sub safe_to_delete { @@ -254,6 +256,14 @@ sub safe_to_delete { return "linked_analytics" if C4::Items::GetAnalyticsCount( $self->itemnumber ) > 0; + return "last_item_for_hold" + if $self->biblio->items->count == 1 + && $self->biblio->holds->search( + { + itemnumber => undef, + } + )->count; + return 1; } diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index 55709643b7..517126130c 100644 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -342,7 +342,7 @@ subtest 'pickup_locations' => sub { }; subtest 'deletion' => sub { - plan tests => 11; + plan tests => 12; $schema->storage->txn_begin; @@ -430,6 +430,14 @@ subtest 'deletion' => sub { } + { # last_item_for_hold + C4::Reserves::AddReserve($patron->branchcode, $patron->borrowernumber, $item->biblionumber ); + is( $item->safe_to_delete, 'last_item_for_hold', 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio' ); + + # With another item attached to the biblio, the item can be deleted + $builder->build_sample_item({ biblionumber => $item->biblionumber }); + } + is( $item->safe_to_delete, 1, -- 2.39.5