|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2023 ByWater Solutions |
| 4 |
# |
| 5 |
# This file is part of Koha |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 6; |
| 23 |
|
| 24 |
use Koha::Database; |
| 25 |
use C4::Circulation qw(CreateBranchTransferLimit); |
| 26 |
use Koha::DateUtils qw( dt_from_string ); |
| 27 |
|
| 28 |
use t::lib::Mocks; |
| 29 |
use t::lib::TestBuilder; |
| 30 |
|
| 31 |
BEGIN { |
| 32 |
use_ok('Koha::Library::FloatLimit'); |
| 33 |
use_ok('Koha::Library::FloatLimits'); |
| 34 |
} |
| 35 |
|
| 36 |
my $schema = Koha::Database->new->schema; |
| 37 |
$schema->storage->txn_begin; |
| 38 |
|
| 39 |
my $builder = t::lib::TestBuilder->new; |
| 40 |
my $library1 = $builder->build( { source => 'Branch' } ); |
| 41 |
my $library2 = $builder->build( { source => 'Branch' } ); |
| 42 |
my $library3 = $builder->build( { source => 'Branch' } ); |
| 43 |
my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } ); |
| 44 |
|
| 45 |
my $biblio = $builder->build_sample_biblio(); |
| 46 |
|
| 47 |
for ( 1 .. 5 ) { |
| 48 |
$builder->build_sample_item( |
| 49 |
{ |
| 50 |
biblionumber => $biblio->biblionumber, |
| 51 |
homebranch => $library1->{branchcode}, |
| 52 |
holdingbranch => $library1->{branchcode}, |
| 53 |
itype => $itemtype->itemtype, |
| 54 |
} |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
for ( 1 .. 10 ) { |
| 59 |
$builder->build_sample_item( |
| 60 |
{ |
| 61 |
biblionumber => $biblio->biblionumber, |
| 62 |
homebranch => $library2->{branchcode}, |
| 63 |
holdingbranch => $library2->{branchcode}, |
| 64 |
itype => $itemtype->itemtype, |
| 65 |
} |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
for ( 1 .. 15 ) { |
| 70 |
$builder->build_sample_item( |
| 71 |
{ |
| 72 |
biblionumber => $biblio->biblionumber, |
| 73 |
homebranch => $library3->{branchcode}, |
| 74 |
holdingbranch => $library3->{branchcode}, |
| 75 |
itype => $itemtype->itemtype, |
| 76 |
} |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
my $item = $builder->build_sample_item( |
| 81 |
{ |
| 82 |
biblionumber => $biblio->biblionumber, |
| 83 |
homebranch => $library1->{branchcode}, |
| 84 |
holdingbranch => $library1->{branchcode}, |
| 85 |
itype => $itemtype->itemtype, |
| 86 |
} |
| 87 |
); |
| 88 |
|
| 89 |
my $limit1 = Koha::Library::FloatLimit->new( |
| 90 |
{ |
| 91 |
branchcode => $library1->{branchcode}, |
| 92 |
itemtype => $itemtype->itemtype, |
| 93 |
float_limit => 1, |
| 94 |
} |
| 95 |
)->store(); |
| 96 |
|
| 97 |
my $float_limit2 = Koha::Library::FloatLimit->new( |
| 98 |
{ |
| 99 |
branchcode => $library2->{branchcode}, |
| 100 |
itemtype => $itemtype->itemtype, |
| 101 |
float_limit => 100, |
| 102 |
} |
| 103 |
)->store(); |
| 104 |
|
| 105 |
my $float_limit3 = Koha::Library::FloatLimit->new( |
| 106 |
{ |
| 107 |
branchcode => $library3->{branchcode}, |
| 108 |
itemtype => $itemtype->itemtype, |
| 109 |
float_limit => 1000, |
| 110 |
} |
| 111 |
)->store(); |
| 112 |
|
| 113 |
is( |
| 114 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
| 115 |
$library3->{branchcode}, |
| 116 |
"Correct library selected for float limit transfer" |
| 117 |
); |
| 118 |
|
| 119 |
t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' ); |
| 120 |
t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' ); |
| 121 |
|
| 122 |
my $to = $library3->{branchcode}; |
| 123 |
my $from = $item->holdingbranch; |
| 124 |
my $code = $itemtype->itemtype; |
| 125 |
CreateBranchTransferLimit( $to, $from, $code ); |
| 126 |
|
| 127 |
is( C4::Circulation::IsBranchTransferAllowed( $to, $from, $code ), 0, "Transfer to best library is no longer allowed" ); |
| 128 |
say "C4::Circulation::IsBranchTransferAllowed( $to, $from, $code )"; |
| 129 |
|
| 130 |
subtest 'onloan items excluded from ratio calculation' => sub { |
| 131 |
plan tests => 5; |
| 132 |
|
| 133 |
# Reset transfer limits for clean test |
| 134 |
t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' ); |
| 135 |
|
| 136 |
# Verify initial item counts |
| 137 |
my $library2_initial = Koha::Items->search( |
| 138 |
{ |
| 139 |
holdingbranch => $library2->{branchcode}, |
| 140 |
itype => $itemtype->itemtype, |
| 141 |
onloan => undef |
| 142 |
} |
| 143 |
)->count; |
| 144 |
|
| 145 |
my $library3_initial = Koha::Items->search( |
| 146 |
{ |
| 147 |
holdingbranch => $library3->{branchcode}, |
| 148 |
itype => $itemtype->itemtype, |
| 149 |
onloan => undef |
| 150 |
} |
| 151 |
)->count; |
| 152 |
|
| 153 |
is( $library2_initial, 10, "Library2 initially has 10 available items" ); |
| 154 |
is( $library3_initial, 15, "Library3 initially has 15 available items" ); |
| 155 |
|
| 156 |
# Initial library selection based on ratios |
| 157 |
is( |
| 158 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
| 159 |
$library3->{branchcode}, |
| 160 |
"Library3 initially selected (lowest ratio: 15/1000 = 0.015)" |
| 161 |
); |
| 162 |
|
| 163 |
# Check out enough items from library3 to change the selection |
| 164 |
my @library3_items = Koha::Items->search( |
| 165 |
{ |
| 166 |
holdingbranch => $library3->{branchcode}, |
| 167 |
itype => $itemtype->itemtype, |
| 168 |
onloan => undef |
| 169 |
} |
| 170 |
)->as_list; |
| 171 |
|
| 172 |
my $checkout_date = dt_from_string(); |
| 173 |
for my $i ( 0 .. 9 ) { |
| 174 |
$library3_items[$i]->onloan($checkout_date)->store; |
| 175 |
} |
| 176 |
|
| 177 |
# Verify the count decreased |
| 178 |
my $library3_after_checkout = Koha::Items->search( |
| 179 |
{ |
| 180 |
holdingbranch => $library3->{branchcode}, |
| 181 |
itype => $itemtype->itemtype, |
| 182 |
onloan => undef |
| 183 |
} |
| 184 |
)->count; |
| 185 |
|
| 186 |
is( $library3_after_checkout, 5, "Library3 now has 5 available items after 10 checkouts" ); |
| 187 |
|
| 188 |
# Library3 should still be selected |
| 189 |
is( |
| 190 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
| 191 |
$library3->{branchcode}, |
| 192 |
"Library3 still selected after checkouts (ratio now 5/1000 = 0.005, still better than library2's 10/100 = 0.1)" |
| 193 |
); |
| 194 |
}; |
| 195 |
|
| 196 |
subtest 'items in transit counted toward destination branch' => sub { |
| 197 |
plan tests => 6; |
| 198 |
|
| 199 |
# Reset transfer limits for clean test |
| 200 |
t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' ); |
| 201 |
|
| 202 |
Koha::Items->search( |
| 203 |
{ |
| 204 |
holdingbranch => $library3->{branchcode}, |
| 205 |
itype => $itemtype->itemtype, |
| 206 |
onloan => { '!=' => undef } |
| 207 |
} |
| 208 |
)->update( { onloan => undef } ); |
| 209 |
|
| 210 |
# Verify initial counts |
| 211 |
my $library2_initial = Koha::Items->search( |
| 212 |
{ |
| 213 |
holdingbranch => $library2->{branchcode}, |
| 214 |
itype => $itemtype->itemtype, |
| 215 |
onloan => undef |
| 216 |
} |
| 217 |
)->count; |
| 218 |
|
| 219 |
my $library3_initial = Koha::Items->search( |
| 220 |
{ |
| 221 |
holdingbranch => $library3->{branchcode}, |
| 222 |
itype => $itemtype->itemtype, |
| 223 |
onloan => undef |
| 224 |
} |
| 225 |
)->count; |
| 226 |
|
| 227 |
is( $library2_initial, 10, "Library2 initially has 10 available items" ); |
| 228 |
is( $library3_initial, 15, "Library3 initially has 15 available items" ); |
| 229 |
|
| 230 |
# Initial selection should be library3 |
| 231 |
is( |
| 232 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
| 233 |
$library3->{branchcode}, |
| 234 |
"Library3 initially selected (ratio: 15/1000 = 0.015)" |
| 235 |
); |
| 236 |
|
| 237 |
# Create items in transit TO library2 |
| 238 |
my @transit_items; |
| 239 |
for ( 1 .. 2 ) { |
| 240 |
my $transit_item = $builder->build_sample_item( |
| 241 |
{ |
| 242 |
biblionumber => $biblio->biblionumber, |
| 243 |
homebranch => $library1->{branchcode}, |
| 244 |
holdingbranch => $library1->{branchcode}, |
| 245 |
itype => $itemtype->itemtype, |
| 246 |
} |
| 247 |
); |
| 248 |
push @transit_items, $transit_item; |
| 249 |
} |
| 250 |
|
| 251 |
# Create active transfers to library2 |
| 252 |
my $transfer_date = dt_from_string(); |
| 253 |
for my $transit_item (@transit_items) { |
| 254 |
Koha::Item::Transfer->new( |
| 255 |
{ |
| 256 |
itemnumber => $transit_item->itemnumber, |
| 257 |
frombranch => $library1->{branchcode}, |
| 258 |
tobranch => $library2->{branchcode}, |
| 259 |
daterequested => $transfer_date, |
| 260 |
datesent => $transfer_date, |
| 261 |
reason => 'LibraryFloatLimit' |
| 262 |
} |
| 263 |
)->store; |
| 264 |
} |
| 265 |
|
| 266 |
# Verify that items in transit are counted toward destination |
| 267 |
my $in_transit_count = Koha::Items->search( |
| 268 |
{ |
| 269 |
itype => $itemtype->itemtype, |
| 270 |
}, |
| 271 |
{ |
| 272 |
join => 'branchtransfers', |
| 273 |
where => { |
| 274 |
'branchtransfers.tobranch' => $library2->{branchcode}, |
| 275 |
'branchtransfers.datearrived' => undef, |
| 276 |
'branchtransfers.datecancelled' => undef, |
| 277 |
}, |
| 278 |
distinct => 1 |
| 279 |
} |
| 280 |
)->count; |
| 281 |
|
| 282 |
is( $in_transit_count, 2, "2 items are in transit to library2" ); |
| 283 |
|
| 284 |
# Test 5: Library2's physical count should still be 10 |
| 285 |
my $library2_physical = Koha::Items->search( |
| 286 |
{ |
| 287 |
holdingbranch => $library2->{branchcode}, |
| 288 |
itype => $itemtype->itemtype, |
| 289 |
onloan => undef |
| 290 |
} |
| 291 |
)->count; |
| 292 |
|
| 293 |
is( $library2_physical, 10, "Library2 still has 10 physical items" ); |
| 294 |
|
| 295 |
# Library2's count should now be 10 + 2 = 12 |
| 296 |
is( |
| 297 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
| 298 |
$library3->{branchcode}, |
| 299 |
"Library3 still selected after items in transit to library2" |
| 300 |
); |
| 301 |
}; |
| 302 |
|
| 303 |
$schema->storage->txn_rollback; |