From 1f6609906a8a59c734f2a889619c7d3d31fca999 Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Mon, 28 Jul 2025 16:53:48 +0000 Subject: [PATCH] Bug 28530: Do not count onloan items towards the float limit --- Koha/Library/FloatLimits.pm | 10 +-- t/db_dependent/Koha/Library/FloatLimits.t | 75 ++++++++++++++++++++++- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/Koha/Library/FloatLimits.pm b/Koha/Library/FloatLimits.pm index 611b9eb4437..07b009e9a4d 100644 --- a/Koha/Library/FloatLimits.pm +++ b/Koha/Library/FloatLimits.pm @@ -68,16 +68,17 @@ sub lowest_ratio_library { $item_count = Koha::Items->search( { itype => $item->effective_itemtype, - holdingbranch => $branch - } + holdingbranch => $branch, + onloan => undef + }, )->count; } else { $item_count = Koha::Items->search( { holdingbranch => $branch, - 'biblioitem.itemtype' => $item->effective_itemtype + 'biblioitem.itemtype' => $item->effective_itemtype, + onloan => undef }, - { join => 'biblioitem' } )->count; } @@ -107,7 +108,6 @@ sub lowest_ratio_library { return Koha::Libraries->find( $candidate->{branchcode} ); } } - return; } diff --git a/t/db_dependent/Koha/Library/FloatLimits.t b/t/db_dependent/Koha/Library/FloatLimits.t index 3d646abe69d..bd54b110053 100755 --- a/t/db_dependent/Koha/Library/FloatLimits.t +++ b/t/db_dependent/Koha/Library/FloatLimits.t @@ -19,10 +19,11 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use Koha::Database; use C4::Circulation qw(CreateBranchTransferLimit); +use Koha::DateUtils qw( dt_from_string ); use t::lib::Mocks; use t::lib::TestBuilder; @@ -110,7 +111,8 @@ my $float_limit3 = Koha::Library::FloatLimit->new( )->store(); is( - Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} ), $library3->{branchcode}, + Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, + $library3->{branchcode}, "Correct library selected for float limit transfer" ); @@ -126,8 +128,75 @@ is( C4::Circulation::IsBranchTransferAllowed( $to, $from, $code ), 0, "Transfer say "C4::Circulation::IsBranchTransferAllowed( $to, $from, $code )"; is( - Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} ), $library2->{branchcode}, + Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, + $library2->{branchcode}, "Correct library selected for float limit transfer when best cannot be used" ); +subtest 'onloan items excluded from ratio calculation' => sub { + plan tests => 5; + + # Reset transfer limits for clean test + t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' ); + + # Verify initial item counts + my $library2_initial = Koha::Items->search( + { + holdingbranch => $library2->{branchcode}, + itype => $itemtype->itemtype, + onloan => undef + } + )->count; + + my $library3_initial = Koha::Items->search( + { + holdingbranch => $library3->{branchcode}, + itype => $itemtype->itemtype, + onloan => undef + } + )->count; + + is( $library2_initial, 10, "Library2 initially has 10 available items" ); + is( $library3_initial, 15, "Library3 initially has 15 available items" ); + + # Initial library selection based on ratios + is( + Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, + $library3->{branchcode}, + "Library3 initially selected (lowest ratio: 15/1000 = 0.015)" + ); + + # Check out enough items from library3 to change the selection + my @library3_items = Koha::Items->search( + { + holdingbranch => $library3->{branchcode}, + itype => $itemtype->itemtype, + onloan => undef + } + )->as_list; + + my $checkout_date = dt_from_string(); + for my $i ( 0 .. 9 ) { + $library3_items[$i]->onloan($checkout_date)->store; + } + + # Verify the count decreased + my $library3_after_checkout = Koha::Items->search( + { + holdingbranch => $library3->{branchcode}, + itype => $itemtype->itemtype, + onloan => undef + } + )->count; + + is( $library3_after_checkout, 5, "Library3 now has 5 available items after 10 checkouts" ); + + # Library3 should still be selected + is( + Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, + $library3->{branchcode}, + "Library3 still selected after checkouts (ratio now 5/1000 = 0.005, still better than library2's 10/100 = 0.1)" + ); +}; + $schema->storage->txn_rollback; -- 2.39.5