From 737500e3402998c3f062ddbc26c727d73d2392a2 Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Wed, 30 Jul 2025 22:03:44 +0000 Subject: [PATCH] Bug 28530: Subtract outgoing transfers from the branches float limit --- C4/Circulation.pm | 2 +- Koha/Library/FloatLimits.pm | 42 +++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 0084d4b1b80..145f7b222bd 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2281,7 +2281,7 @@ sub AddReturn { my $count = Koha::Items->count( { itype => $limit->itemtype, holdingbranch => $branch } ); if ( $count >= $limit->float_limit ) { my $transfer_library = Koha::Library::FloatLimits->lowest_ratio_library( $item, $branch ); - if ( $transfer_library->branchcode ne $branch ) { + if ( $transfer_library && $transfer_library->branchcode ne $branch ) { $returnbranch = $transfer_library->branchcode; $transfer_trigger = 'LibraryFloatLimit'; } diff --git a/Koha/Library/FloatLimits.pm b/Koha/Library/FloatLimits.pm index 066d88ca96d..db611314f18 100644 --- a/Koha/Library/FloatLimits.pm +++ b/Koha/Library/FloatLimits.pm @@ -60,6 +60,9 @@ sub lowest_ratio_library { my $branch = $limit->get_column('branchcode'); my $float_limit_val = $limit->get_column('float_limit'); + #don't try to transfer something to where it already is + next if $branch eq $branchcode; + my $item_count; if ($use_item_level) { @@ -72,7 +75,7 @@ sub lowest_ratio_library { )->count; # Count items in transit TO this branch - my $in_transit_count = Koha::Items->search( + my $in_transit_to_count = Koha::Items->search( { itype => $item->effective_itemtype, @@ -88,7 +91,20 @@ sub lowest_ratio_library { distinct => 1 } )->count; - $item_count = $at_branch_count + $in_transit_count; + + my $in_transit_from_count = Koha::Items->search( + { itype => $item->effective_itemtype }, + { + join => 'branchtransfers', + where => { + 'branchtransfers.frombranch' => $branch, + 'branchtransfers.datearrived' => undef, + 'branchtransfers.datecancelled' => undef, + }, + distinct => 1 + } + )->count; + $item_count = $at_branch_count + $in_transit_to_count - $in_transit_from_count; } else { my $at_branch_count = Koha::Items->search( { @@ -99,7 +115,7 @@ sub lowest_ratio_library { )->count; # Count items in transit TO this branch - my $in_transit_count = Koha::Items->search( + my $in_transit_to_count = Koha::Items->search( { itype => $item->effective_itemtype, }, @@ -113,9 +129,27 @@ sub lowest_ratio_library { distinct => 1 } )->count; - $item_count = $at_branch_count + $in_transit_count; + + my $in_transit_from_count = Koha::Items->search( + { + itype => $item->effective_itemtype, + }, + { + join => 'branchtransfers', + where => { + 'branchtransfers.frombranch' => $branch, + 'branchtransfers.datearrived' => undef, # Still in transit + 'branchtransfers.datecancelled' => undef, #Not cancelled + }, + distinct => 1 + } + )->count; + $item_count = $at_branch_count + $in_transit_to_count - $in_transit_from_count; } + #don't transfer to branches that are already at their float limit + next if $item_count >= $float_limit_val; + my $ratio = $item_count / $float_limit_val; push @candidates, { branchcode => $branch, -- 2.39.5