From aa86b0c68b33dbce7aa2e53e87a3c3d04bdd711f Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Wed, 6 Aug 2025 15:17:15 +0000 Subject: [PATCH] Bug 40529: (QA follow-up): Remove hold as hold group target when reverted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a hold that is a hold group target, i.e. it's 'waiting' or 'in transit', is reverted it needs to remove itself as its own hold group target Tests added here as well: prove t/db_dependent/Koha/Hold.t Signed-off-by: Anneli Österman Signed-off-by: Andrew Fuerste Henry --- Koha/Hold.pm | 25 +++++++++++++++++++ .../bug_40529_-_hold_group_table_target.pl | 1 + installer/data/mysql/kohastructure.sql | 1 + t/db_dependent/Koha/Hold.t | 11 ++++++-- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Koha/Hold.pm b/Koha/Hold.pm index 253988b99d2..2c4aeea684b 100644 --- a/Koha/Hold.pm +++ b/Koha/Hold.pm @@ -290,6 +290,29 @@ sub set_as_hold_group_target { } } +=head3 remove_as_hold_group_target + +$self->remove_as_hold_group_target; + +Removes this hold as the target of its hold group if it is currently set as such, +and if the DisplayAddHoldGroups system preference is enabled. + +=cut + +sub remove_as_hold_group_target { + my ($self) = @_; + + if ( $self->hold_group + && $self->hold_group->target_hold_id eq $self->reserve_id + && C4::Context->preference("DisplayAddHoldGroups") ) + { + $self->_result->find_related( + 'hold_group_target_hold', + { hold_group_id => $self->hold_group->hold_group_id, reserve_id => $self->reserve_id } + )->delete(); + } +} + =head3 is_hold_group_target $self->is_hold_group_target; @@ -1083,6 +1106,8 @@ sub revert_found { C4::Reserves::_FixPriority( { biblionumber => $self->biblionumber } ); + $self->remove_as_hold_group_target(); + Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( { biblio_ids => [ $self->biblionumber ] } ) if C4::Context->preference('RealTimeHoldsQueue'); } diff --git a/installer/data/mysql/atomicupdate/bug_40529_-_hold_group_table_target.pl b/installer/data/mysql/atomicupdate/bug_40529_-_hold_group_table_target.pl index 4d064351362..bff6bd7504b 100755 --- a/installer/data/mysql/atomicupdate/bug_40529_-_hold_group_table_target.pl +++ b/installer/data/mysql/atomicupdate/bug_40529_-_hold_group_table_target.pl @@ -14,6 +14,7 @@ return { CREATE TABLE hold_group_target_holds ( `hold_group_id` int unsigned NOT NULL UNIQUE COMMENT 'foreign key, linking this to the hold_groups table', `reserve_id` int(11) DEFAULT NULL UNIQUE COMMENT 'foreign key, linking this to the reserves table', + PRIMARY KEY (`hold_group_id`, `reserve_id`), CONSTRAINT `hold_group_target_holds_ibfk_1` FOREIGN KEY (`hold_group_id`) REFERENCES `hold_groups` (`hold_group_id`) ON DELETE CASCADE, CONSTRAINT `hold_group_target_holds_ibfk_2` FOREIGN KEY (`reserve_id`) REFERENCES `reserves` (`reserve_id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index f6904a300f7..9fc9e867a34 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -5122,6 +5122,7 @@ DROP TABLE IF EXISTS `hold_group_target_holds`; CREATE TABLE hold_group_target_holds ( `hold_group_id` int unsigned NOT NULL UNIQUE COMMENT 'foreign key, linking this to the hold_groups table', `reserve_id` int(11) DEFAULT NULL UNIQUE COMMENT 'foreign key, linking this to the reserves table', + PRIMARY KEY (`hold_group_id`, `reserve_id`), CONSTRAINT `hold_group_target_holds_ibfk_1` FOREIGN KEY (`hold_group_id`) REFERENCES `hold_groups` (`hold_group_id`) ON DELETE CASCADE, CONSTRAINT `hold_group_target_holds_ibfk_2` FOREIGN KEY (`reserve_id`) REFERENCES `reserves` (`reserve_id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; diff --git a/t/db_dependent/Koha/Hold.t b/t/db_dependent/Koha/Hold.t index 19f1738e5fd..d00d6b69e07 100755 --- a/t/db_dependent/Koha/Hold.t +++ b/t/db_dependent/Koha/Hold.t @@ -36,7 +36,7 @@ use Koha::DateUtils qw(dt_from_string); use Koha::Holds; use Koha::Libraries; use Koha::Old::Holds; -use C4::Reserves qw( AddReserve ModReserveAffect ); +use C4::Reserves qw( AddReserve ModReserveAffect RevertWaitingStatus); use C4::Circulation qw( AddReturn ); my $schema = Koha::Database->new->schema; @@ -1977,7 +1977,7 @@ subtest 'is_hold_group_target, cleanup_hold_group and set_as_hold_group_target t }; subtest '_Findgroupreserve in the context of hold groups' => sub { - plan tests => 17; + plan tests => 19; $schema->storage->txn_begin; @@ -2078,6 +2078,13 @@ subtest '_Findgroupreserve in the context of hold groups' => sub { is( $hold->is_hold_group_target, 1, 'First hold is the hold group target' ); + C4::Reserves::RevertWaitingStatus( { itemnumber => $item->itemnumber } ); + is( $hold->is_hold_group_target, 0, 'First hold is no longer the hold group target' ); + is( $hold->hold_group->target_hold_id, undef, 'Hold group no longer has a target' ); + + AddReturn( $item->barcode, $library_1->{branchcode} ); + ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id ); + my @reserves_with_target_hold = C4::Reserves::_Findgroupreserve( $item->biblionumber, $item->id, 0, [] ); is( -- 2.39.5