From fb793c48a94d911373c2578bedd8f91a35d3b214 Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Thu, 31 Jul 2025 22:46:36 +0000 Subject: [PATCH] Bug 28530: If return policy is set to float in library group, remove non group branches --- Koha/Library/FloatLimits.pm | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Koha/Library/FloatLimits.pm b/Koha/Library/FloatLimits.pm index 189c67d6cfe..650496d14e9 100644 --- a/Koha/Library/FloatLimits.pm +++ b/Koha/Library/FloatLimits.pm @@ -41,7 +41,6 @@ Koha::Library::FloatLimits - Koha Library::FloatLimit object set class sub lowest_ratio_library { my ( $self, $item, $branchcode ) = @_; - my $schema = Koha::Database->new->schema; my @float_limits = $schema->resultset('LibraryFloatLimit')->search( @@ -51,6 +50,21 @@ sub lowest_ratio_library { } )->all; + # check the items return policy + my $hbr = Koha::CirculationRules->get_return_branch_policy($item); + + # if item only floats in the library group we must eliminate all other candidates + if ( $hbr && $hbr eq 'returnbylibrarygroup' ) { + my $current_library = Koha::Libraries->find($branchcode); + my $float_libraries = $current_library->get_float_libraries; + + if ( $float_libraries->count > 0 ) { + my @allowed_branchcodes = $float_libraries->get_column('branchcode'); + my %allowed_branches = map { $_ => 1 } @allowed_branchcodes; + @float_limits = grep { $allowed_branches{ $_->get_column('branchcode') } } @float_limits; + } + } + return unless @float_limits; my @candidates; @@ -154,7 +168,12 @@ sub lowest_ratio_library { } # sort the branches by lowest ratio in the event of a tie choose a random branch - @candidates = sort { $a->{ratio} <=> $b->{ratio} || rand() <=> rand() } @candidates; + @candidates = sort { + $a->{ratio} <=> $b->{ratio} || # Primary: lowest ratio + ( $a->{branchcode} eq $branchcode ? -1 : 0 ) - ( $b->{branchcode} eq $branchcode ? -1 : 0 ) + || # Prefer current branch if tied + rand() <=> rand() # Random if tied and neither is current branch + } @candidates; my $UseBranchTransferLimits = C4::Context->preference("UseBranchTransferLimits"); my $BranchTransferLimitsType = @@ -162,12 +181,14 @@ sub lowest_ratio_library { my $transfer_branch; for my $candidate (@candidates) { + if ($UseBranchTransferLimits) { my $allowed = C4::Circulation::IsBranchTransferAllowed( $candidate->{branchcode}, $branchcode, $item->$BranchTransferLimitsType ); + $transfer_branch = Koha::Libraries->find( $candidate->{branchcode} ) if $allowed; last; } else { -- 2.39.5