|
Lines 24-38
use Test::More tests => 6;
Link Here
|
| 24 |
use Koha::Database; |
24 |
use Koha::Database; |
| 25 |
use C4::Circulation qw(CreateBranchTransferLimit); |
25 |
use C4::Circulation qw(CreateBranchTransferLimit); |
| 26 |
use Koha::DateUtils qw( dt_from_string ); |
26 |
use Koha::DateUtils qw( dt_from_string ); |
|
|
27 |
use Koha::Library::FloatLimit; |
| 28 |
use Koha::Library::FloatLimits; |
| 27 |
|
29 |
|
| 28 |
use t::lib::Mocks; |
30 |
use t::lib::Mocks; |
| 29 |
use t::lib::TestBuilder; |
31 |
use t::lib::TestBuilder; |
| 30 |
|
32 |
|
| 31 |
BEGIN { |
|
|
| 32 |
use_ok('Koha::Library::FloatLimit'); |
| 33 |
use_ok('Koha::Library::FloatLimits'); |
| 34 |
} |
| 35 |
|
| 36 |
my $schema = Koha::Database->new->schema; |
33 |
my $schema = Koha::Database->new->schema; |
| 37 |
$schema->storage->txn_begin; |
34 |
$schema->storage->txn_begin; |
| 38 |
|
35 |
|
|
Lines 44-49
my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
Link Here
|
| 44 |
|
41 |
|
| 45 |
my $biblio = $builder->build_sample_biblio(); |
42 |
my $biblio = $builder->build_sample_biblio(); |
| 46 |
|
43 |
|
|
|
44 |
# Create initial items for testing |
| 47 |
for ( 1 .. 5 ) { |
45 |
for ( 1 .. 5 ) { |
| 48 |
$builder->build_sample_item( |
46 |
$builder->build_sample_item( |
| 49 |
{ |
47 |
{ |
|
Lines 125-133
my $code = $itemtype->itemtype;
Link Here
|
| 125 |
CreateBranchTransferLimit( $to, $from, $code ); |
123 |
CreateBranchTransferLimit( $to, $from, $code ); |
| 126 |
|
124 |
|
| 127 |
is( C4::Circulation::IsBranchTransferAllowed( $to, $from, $code ), 0, "Transfer to best library is no longer allowed" ); |
125 |
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 |
|
126 |
|
| 130 |
subtest 'onloan items excluded from ratio calculation' => sub { |
127 |
subtest 'FloatLimits: General tests' => sub { |
|
|
128 |
$schema->storage->txn_begin; |
| 129 |
plan tests => 2; |
| 130 |
|
| 131 |
# Test with no float limits defined |
| 132 |
my $no_limits_item = $builder->build_sample_item( |
| 133 |
{ |
| 134 |
biblionumber => $biblio->biblionumber, |
| 135 |
homebranch => $library1->{branchcode}, |
| 136 |
holdingbranch => $library1->{branchcode}, |
| 137 |
itype => $builder->build_object( { class => 'Koha::ItemTypes' } )->itemtype, |
| 138 |
} |
| 139 |
); |
| 140 |
|
| 141 |
my $no_limits_result = Koha::Library::FloatLimits->lowest_ratio_library( $no_limits_item, $library1->{branchcode} ); |
| 142 |
is( $no_limits_result, undef, "Returns undef when no float limits defined" ); |
| 143 |
|
| 144 |
# Test with only zero float limits |
| 145 |
my $zero_lib = $builder->build( { source => 'Branch' } ); |
| 146 |
my $zero_itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } ); |
| 147 |
|
| 148 |
Koha::Library::FloatLimit->new( |
| 149 |
{ |
| 150 |
branchcode => $zero_lib->{branchcode}, |
| 151 |
itemtype => $zero_itemtype->itemtype, |
| 152 |
float_limit => 0, # Zero limit should be excluded |
| 153 |
} |
| 154 |
)->store(); |
| 155 |
|
| 156 |
# Test with item type not in any float limits |
| 157 |
my $unknown_itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } ); |
| 158 |
my $unknown_item = $builder->build_sample_item( |
| 159 |
{ |
| 160 |
biblionumber => $biblio->biblionumber, |
| 161 |
homebranch => $library1->{branchcode}, |
| 162 |
holdingbranch => $library1->{branchcode}, |
| 163 |
itype => $unknown_itemtype->itemtype, |
| 164 |
} |
| 165 |
); |
| 166 |
|
| 167 |
my $unknown_result = Koha::Library::FloatLimits->lowest_ratio_library( $unknown_item, $library1->{branchcode} ); |
| 168 |
is( $unknown_result, undef, "Returns undef for item type not in float limits" ); |
| 169 |
|
| 170 |
$schema->storage->txn_rollback; |
| 171 |
|
| 172 |
}; |
| 173 |
|
| 174 |
subtest 'FloatLimits: Count on loan items against total' => sub { |
| 175 |
$schema->storage->txn_begin; |
| 131 |
plan tests => 5; |
176 |
plan tests => 5; |
| 132 |
|
177 |
|
| 133 |
# Reset transfer limits for clean test |
178 |
# Reset transfer limits for clean test |
|
Lines 191-199
subtest 'onloan items excluded from ratio calculation' => sub {
Link Here
|
| 191 |
$library3->{branchcode}, |
236 |
$library3->{branchcode}, |
| 192 |
"Library3 still selected after checkouts (ratio now 5/1000 = 0.005, still better than library2's 10/100 = 0.1)" |
237 |
"Library3 still selected after checkouts (ratio now 5/1000 = 0.005, still better than library2's 10/100 = 0.1)" |
| 193 |
); |
238 |
); |
|
|
239 |
$schema->storage->txn_rollback; |
| 194 |
}; |
240 |
}; |
| 195 |
|
241 |
|
| 196 |
subtest 'items in transit counted toward destination branch' => sub { |
242 |
subtest 'FloatLimits: Count in transit TO items against total' => sub { |
|
|
243 |
$schema->storage->txn_begin; |
| 197 |
plan tests => 6; |
244 |
plan tests => 6; |
| 198 |
|
245 |
|
| 199 |
# Reset transfer limits for clean test |
246 |
# Reset transfer limits for clean test |
|
Lines 281-287
subtest 'items in transit counted toward destination branch' => sub {
Link Here
|
| 281 |
|
328 |
|
| 282 |
is( $in_transit_count, 2, "2 items are in transit to library2" ); |
329 |
is( $in_transit_count, 2, "2 items are in transit to library2" ); |
| 283 |
|
330 |
|
| 284 |
# Test 5: Library2's physical count should still be 10 |
|
|
| 285 |
my $library2_physical = Koha::Items->search( |
331 |
my $library2_physical = Koha::Items->search( |
| 286 |
{ |
332 |
{ |
| 287 |
holdingbranch => $library2->{branchcode}, |
333 |
holdingbranch => $library2->{branchcode}, |
|
Lines 292-303
subtest 'items in transit counted toward destination branch' => sub {
Link Here
|
| 292 |
|
338 |
|
| 293 |
is( $library2_physical, 10, "Library2 still has 10 physical items" ); |
339 |
is( $library2_physical, 10, "Library2 still has 10 physical items" ); |
| 294 |
|
340 |
|
| 295 |
# Library2's count should now be 10 + 2 = 12 |
|
|
| 296 |
is( |
341 |
is( |
| 297 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
342 |
Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, |
| 298 |
$library3->{branchcode}, |
343 |
$library3->{branchcode}, |
| 299 |
"Library3 still selected after items in transit to library2" |
344 |
"Library3 still selected after items in transit to library2" |
| 300 |
); |
345 |
); |
|
|
346 |
|
| 347 |
$schema->storage->txn_rollback; |
| 348 |
|
| 349 |
}; |
| 350 |
|
| 351 |
subtest 'FloatLimits: Do not count in transit FROM items against total' => sub { |
| 352 |
$schema->storage->txn_begin; |
| 353 |
plan tests => 2; |
| 354 |
|
| 355 |
# Reset transfer limits |
| 356 |
t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' ); |
| 357 |
|
| 358 |
# Create unique itemtype for this test |
| 359 |
my $transit_itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } ); |
| 360 |
|
| 361 |
# Create test libraries |
| 362 |
my $from_lib = $builder->build( { source => 'Branch' } ); |
| 363 |
my $to_lib = $builder->build( { source => 'Branch' } ); |
| 364 |
|
| 365 |
# Create items at from_lib |
| 366 |
my @from_items; |
| 367 |
for ( 1 .. 8 ) { |
| 368 |
my $from_item = $builder->build_sample_item( |
| 369 |
{ |
| 370 |
biblionumber => $biblio->biblionumber, |
| 371 |
homebranch => $from_lib->{branchcode}, |
| 372 |
holdingbranch => $from_lib->{branchcode}, |
| 373 |
itype => $transit_itemtype->itemtype, # Use unique itemtype |
| 374 |
} |
| 375 |
); |
| 376 |
push @from_items, $from_item; |
| 377 |
} |
| 378 |
|
| 379 |
# Create items at to_lib |
| 380 |
for ( 1 .. 5 ) { |
| 381 |
$builder->build_sample_item( |
| 382 |
{ |
| 383 |
biblionumber => $biblio->biblionumber, |
| 384 |
homebranch => $to_lib->{branchcode}, |
| 385 |
holdingbranch => $to_lib->{branchcode}, |
| 386 |
itype => $transit_itemtype->itemtype, # Use unique itemtype |
| 387 |
} |
| 388 |
); |
| 389 |
} |
| 390 |
|
| 391 |
# Set float limits |
| 392 |
Koha::Library::FloatLimit->new( |
| 393 |
{ |
| 394 |
branchcode => $from_lib->{branchcode}, |
| 395 |
itemtype => $transit_itemtype->itemtype, # Use unique itemtype |
| 396 |
float_limit => 10, |
| 397 |
} |
| 398 |
)->store(); |
| 399 |
|
| 400 |
Koha::Library::FloatLimit->new( |
| 401 |
{ |
| 402 |
branchcode => $to_lib->{branchcode}, |
| 403 |
itemtype => $transit_itemtype->itemtype, # Use unique itemtype |
| 404 |
float_limit => 10, |
| 405 |
} |
| 406 |
)->store(); |
| 407 |
|
| 408 |
# Create test item with unique itemtype |
| 409 |
my $transit_item = $builder->build_sample_item( |
| 410 |
{ |
| 411 |
biblionumber => $biblio->biblionumber, |
| 412 |
homebranch => $from_lib->{branchcode}, |
| 413 |
holdingbranch => $from_lib->{branchcode}, |
| 414 |
itype => $transit_itemtype->itemtype, |
| 415 |
} |
| 416 |
); |
| 417 |
|
| 418 |
# Initial state: from_lib has 8 items, to_lib has 5 items |
| 419 |
# Ratios: from_lib = 8/10 = 0.8, to_lib = 5/10 = 0.5 |
| 420 |
my $initial_result = Koha::Library::FloatLimits->lowest_ratio_library( $transit_item, $from_lib->{branchcode} ); |
| 421 |
is( $initial_result->branchcode, $to_lib->{branchcode}, "to_lib selected initially (better ratio)" ); |
| 422 |
|
| 423 |
# Create transfers FROM from_lib TO to_lib (3 items) |
| 424 |
my $transfer_date = dt_from_string(); |
| 425 |
for my $i ( 0 .. 2 ) { |
| 426 |
Koha::Item::Transfer->new( |
| 427 |
{ |
| 428 |
itemnumber => $from_items[$i]->itemnumber, |
| 429 |
frombranch => $from_lib->{branchcode}, |
| 430 |
tobranch => $to_lib->{branchcode}, |
| 431 |
daterequested => $transfer_date, |
| 432 |
datesent => $transfer_date, |
| 433 |
reason => 'LibraryFloatLimit' |
| 434 |
} |
| 435 |
)->store; |
| 436 |
} |
| 437 |
|
| 438 |
# After transfers: |
| 439 |
# from_lib effective count = 8 - 3 = 5 (ratio = 5/10 = 0.5) |
| 440 |
# to_lib effective count = 5 + 3 = 8 (ratio = 8/10 = 0.8) |
| 441 |
# Now from_lib should be better choice |
| 442 |
my $after_transfer_result = |
| 443 |
Koha::Library::FloatLimits->lowest_ratio_library( $transit_item, $to_lib->{branchcode} ); |
| 444 |
is( |
| 445 |
$after_transfer_result->branchcode, $from_lib->{branchcode}, |
| 446 |
"from_lib selected after transfers (items subtracted)" |
| 447 |
); |
| 448 |
|
| 449 |
$schema->storage->txn_rollback; |
| 450 |
|
| 301 |
}; |
451 |
}; |
| 302 |
|
452 |
|
| 303 |
$schema->storage->txn_rollback; |
453 |
$schema->storage->txn_rollback; |
| 304 |
- |
|
|