6 use C4::Circulation qw( AddIssue AddReturn );
9 use Koha::Cache::Memory::Lite;
10 use Koha::CirculationRules;
12 use Test::More tests => 13;
14 use t::lib::TestBuilder;
18 use_ok( 'C4::Reserves', qw( ItemsAnyAvailableAndNotRestricted IsAvailableForItemLevelRequest ) );
21 my $schema = Koha::Database->schema;
22 $schema->storage->txn_begin;
23 my $dbh = C4::Context->dbh;
24 my $cache = Koha::Cache::Memory::Lite->get_instance();
26 my $builder = t::lib::TestBuilder->new;
28 my $library1 = $builder->build( { source => 'Branch', } );
29 my $library2 = $builder->build( { source => 'Branch', } );
30 my $itemtype = $builder->build(
33 value => { notforloan => 0 }
37 t::lib::Mocks::mock_userenv( { branchcode => $library1->{branchcode} } );
39 my $patron1 = $builder->build_object(
41 class => 'Koha::Patrons',
43 branchcode => $library1->{branchcode},
44 dateexpiry => '3000-01-01',
48 my $borrower1 = $patron1->unblessed;
50 my $patron2 = $builder->build_object(
52 class => 'Koha::Patrons',
54 branchcode => $library1->{branchcode},
55 dateexpiry => '3000-01-01',
60 my $patron3 = $builder->build_object(
62 class => 'Koha::Patrons',
64 branchcode => $library2->{branchcode},
65 dateexpiry => '3000-01-01',
70 my $library_A = $library1->{branchcode};
71 my $library_B = $library2->{branchcode};
73 my $biblio = $builder->build_sample_biblio( { itemtype => $itemtype } );
74 my $biblionumber = $biblio->biblionumber;
75 my $item1 = $builder->build_sample_item(
77 biblionumber => $biblionumber,
79 homebranch => $library_A,
80 holdingbranch => $library_A
83 my $item2 = $builder->build_sample_item(
85 biblionumber => $biblionumber,
87 homebranch => $library_A,
88 holdingbranch => $library_A
92 # Test hold_fulfillment_policy
93 $dbh->do("DELETE FROM circulation_rules");
94 Koha::CirculationRules->set_rules(
96 categorycode => undef,
97 itemtype => $itemtype,
102 reservesallowed => 99,
110 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } );
111 is( $is, 1, "Items availability: both of 2 items are available" );
113 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
114 is( $is, 0, "Item cannot be held, 2 items available" );
116 my $issue1 = AddIssue( $patron2->unblessed, $item1->barcode );
118 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } );
119 is( $is, 1, "Items availability: one item is available" );
121 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
122 is( $is, 0, "Item cannot be held, 1 item available" );
124 AddIssue( $patron2->unblessed, $item2->barcode );
127 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } );
128 is( $is, 0, "Items availability: none of items are available" );
130 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
131 is( $is, 1, "Item can be held, no items available" );
133 AddReturn( $item1->barcode );
135 { # Remove the issue for the first patron, and modify the branch for item1
136 subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
139 my $hold_allowed_from_home_library = 'from_home_library';
140 my $hold_allowed_from_any_libraries = 'from_any_library';
142 subtest 'Item is available at a different library' => sub {
145 $item1->set( { homebranch => $library_B, holdingbranch => $library_B } )->store;
148 #One shelf holds is 'If all unavailable'/2
149 #Item 1 homebranch library B is available
150 #Item 2 homebranch library A is checked out
151 #Borrower1 is from library A
154 set_holdallowed_rule($hold_allowed_from_home_library);
156 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
158 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
159 ; # patron1 in library A, library A 0 items, library B 1 item
162 "Items availability: hold allowed from home + ReservesControlBranch=ItemHomeLibrary + one item is available at different library"
165 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
168 "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, "
169 . "One item is available at different library, not holdable = none available => the hold is allowed at item level"
171 $is = IsAvailableForItemLevelRequest( $item1, $patron2 );
174 "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, "
175 . "One item is available at home library, holdable = one available => the hold is not allowed at item level"
177 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_B );
179 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
182 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
183 ; # patron1 in library A, library A 0 items, library B 1 item
186 "Items availability: hold allowed from any library for library B + ReservesControlBranch=ItemHomeLibrary + one item is available at different library"
189 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
192 "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, "
193 . "One item is available at different library, holdable = one available => the hold is not allowed at item level"
196 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
199 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
200 ; # patron1 in library A, library A 0 items, library B 1 item
203 "Items availability: hold allowed from any library for library B + ReservesControlBranch=PatronLibrary + one item is available at different library"
206 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
209 "Hold allowed from home library + ReservesControlBranch=PatronLibrary, "
210 . "One item is available at different library, not holdable = none available => the hold is allowed at item level"
213 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
214 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_A );
217 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
218 ; # patron1 in library A, library A 0 items, library B 1 item
221 "Items availability: hold allowed from any library for library A + ReservesControlBranch=PatronLibrary + one item is available at different library"
224 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
227 "Hold allowed from home library + ReservesControlBranch=PatronLibrary, "
228 . "One item is available at different library, holdable = one available => the hold is not allowed at item level"
233 set_holdallowed_rule($hold_allowed_from_any_libraries);
235 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
238 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
239 ; # patron1 in library A, library A 0 items, library B 1 item
242 "Items availability: hold allowed from any library + ReservesControlBranch=ItemHomeLibrary + one item is available at different library"
245 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
248 "Hold allowed from any library + ReservesControlBranch=ItemHomeLibrary, "
249 . "One item is available at the diff library, holdable = 1 available => the hold is not allowed at item level"
252 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
255 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
256 ; # patron1 in library A, library A 0 items, library B 1 item
259 "Items availability: hold allowed from any library + ReservesControlBranch=PatronLibrary + one item is available at different library"
262 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
265 "Hold allowed from any library + ReservesControlBranch=PatronLibrary, "
266 . "One item is available at the diff library, holdable = 1 available => the hold is not allowed at item level"
271 subtest 'Item is available at the same library' => sub {
274 $item1->set( { homebranch => $library_A, holdingbranch => $library_A } )->store;
277 #One shelf holds is 'If all unavailable'/2
278 #Item 1 homebranch library A is available
279 #Item 2 homebranch library A is checked out
280 #Borrower1 is from library A
281 #CircControl has no effect - same rule for all branches as set at line 96
282 #ReservesControlBranch is not checked in these subs we are testing?
285 set_holdallowed_rule($hold_allowed_from_home_library);
287 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
289 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
290 ; # patron1 in library A, library A 1 item
293 "Items availability: hold allowed from home library + ReservesControlBranch=ItemHomeLibrary + one item is available at home library"
296 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
299 "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, "
300 . "One item is available at the same library, holdable = 1 available => the hold is not allowed at item level"
303 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
305 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
306 ; # patron1 in library A, library A 1 item
309 "Items availability: hold allowed from home library + ReservesControlBranch=PatronLibrary + one item is available at home library"
312 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
315 "Hold allowed from home library + ReservesControlBranch=PatronLibrary, "
316 . "One item is available at the same library, holdable = 1 available => the hold is not allowed at item level"
321 set_holdallowed_rule($hold_allowed_from_any_libraries);
323 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
325 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
326 ; # patron1 in library A, library A 1 item
329 "Items availability: hold allowed from any library + ReservesControlBranch=ItemHomeLibrary + one item is available at home library"
332 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
335 "Hold allowed from any library + ReservesControlBranch=ItemHomeLibrary, "
336 . "One item is available at the same library, holdable = 1 available => the hold is not allowed at item level"
339 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
341 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
342 ; # patron1 in library A, library A 1 item
345 "Items availability: hold allowed from any library + ReservesControlBranch=PatronLibrary + one item is available at home library"
348 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
351 "Hold allowed from any library + ReservesControlBranch=PatronLibrary, "
352 . "One item is available at the same library, holdable = 1 available => the hold is not allowed at item level"
359 my $itemtype2 = $builder->build(
361 source => 'Itemtype',
362 value => { notforloan => 0 }
365 my $item3 = $builder->build_sample_item( { itype => $itemtype2 } );
367 my $hold = $builder->build(
371 itemnumber => $item3->itemnumber,
377 Koha::CirculationRules->set_rules(
379 categorycode => undef,
380 itemtype => $itemtype2,
389 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
390 ; # patron1 in library A, library A 1 item
391 is( $is, 1, "Items availability: 1 item is available, 1 item held in T" );
393 $is = IsAvailableForItemLevelRequest( $item3, $patron1 );
394 is( $is, 1, "Item can be held, items in transit are not available" );
396 subtest 'Check holds availability with different item types' => sub {
399 # Check for holds availability when different item types have different
400 # smart rules assigned both with "if all unavailable" set,
401 # and $itemtype rule allows holds, $itemtype2 rule disallows holds.
402 # So, $item should be available for hold when checked out even if $item2
403 # is not checked out, because anyway $item2 unavailable for holds by rule
406 my $biblio2 = $builder->build_sample_biblio( { itemtype => $itemtype } );
407 my $item4 = $builder->build_sample_item(
409 biblionumber => $biblio2->biblionumber,
411 homebranch => $library_A,
412 holdingbranch => $library_A
415 my $item5 = $builder->build_sample_item(
417 biblionumber => $biblio2->biblionumber,
419 homebranch => $library_A,
420 holdingbranch => $library_A
424 # Test hold_fulfillment_policy
425 $dbh->do("DELETE FROM circulation_rules");
426 Koha::CirculationRules->set_rules(
428 categorycode => undef,
429 itemtype => $itemtype,
434 reservesallowed => 99,
435 holds_per_record => 99,
440 Koha::CirculationRules->set_rules(
442 categorycode => undef,
443 itemtype => $itemtype2,
448 reservesallowed => 0,
449 holds_per_record => 0,
455 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblio2->biblionumber, patron => $patron1 } );
458 "Items availability: 2 items, one allowed by smart rule but not checked out, another one not allowed to be held by smart rule"
461 $is = IsAvailableForItemLevelRequest( $item4, $patron1 );
462 is( $is, 0, "Item4 cannot be requested to hold: 2 items, Item4 available, Item5 restricted" );
464 $is = IsAvailableForItemLevelRequest( $item5, $patron1 );
465 is( $is, 0, "Item5 cannot be requested to hold: 2 items, Item4 available, Item5 restricted" );
467 AddIssue( $patron2->unblessed, $item4->barcode );
470 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblio2->biblionumber, patron => $patron1 } );
473 "Items availability: 2 items, one allowed by smart rule and checked out, another one not allowed to be held by smart rule"
476 $is = IsAvailableForItemLevelRequest( $item4, $patron1 );
477 is( $is, 1, "Item4 can be requested to hold, 2 items, Item4 checked out, Item5 restricted" );
479 $is = IsAvailableForItemLevelRequest( $item5, $patron1 );
481 # Note: read IsAvailableForItemLevelRequest sub description about CanItemBeReserved/CanBookBeReserved:
482 is( $is, 1, "Item5 can be requested to hold, 2 items, Item4 checked out, Item5 restricted" );
485 subtest 'Check item checkout availability with ordered item' => sub {
488 my $biblio2 = $builder->build_sample_biblio( { itemtype => $itemtype } );
489 my $item1 = $builder->build_sample_item(
491 biblionumber => $biblio2->biblionumber,
493 homebranch => $library_A,
494 holdingbranch => $library_A,
499 $dbh->do("DELETE FROM circulation_rules");
500 Koha::CirculationRules->set_rules(
502 categorycode => undef,
503 itemtype => $itemtype2,
508 reservesallowed => 99,
509 holds_per_record => 99,
516 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblio2->biblionumber, patron => $patron1 } );
517 is( $is, 0, "Ordered item cannot be checked out" );
520 subtest 'Check item availability for hold with ordered item' => sub {
523 my $biblio2 = $builder->build_sample_biblio( { itemtype => $itemtype } );
524 my $item1 = $builder->build_sample_item(
526 biblionumber => $biblio2->biblionumber,
528 homebranch => $library_A,
529 holdingbranch => $library_A,
534 $dbh->do("DELETE FROM circulation_rules");
535 Koha::CirculationRules->set_rules(
537 categorycode => undef,
538 itemtype => $itemtype2,
543 reservesallowed => 99,
544 holds_per_record => 99,
551 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
552 is( $is, 1, "Ordered items are available for hold" );
556 $schema->storage->txn_rollback;
558 sub set_holdallowed_rule {
559 my ( $holdallowed, $branchcode ) = @_;
560 Koha::CirculationRules->set_rules(
562 branchcode => $branchcode || undef,
565 holdallowed => $holdallowed,
566 hold_fulfillment_policy => 'any',
567 returnbranch => 'homebranch',