@@ -, +, @@ CanItemBeReserved - Allow holds only on items that are currently checked out or otherwise unavailable. - If set to "Yes", patrons can place holds on items currently checked in. - If set to "If any unavailable", patrons can only place holds on items that are not unavailable. - If set to "If all unavailable", patrons can only place holds on items where *all* items on the record are unavailable. --------- fines rules. allowed" for all libraries/patron categories/item types: "Yes", "If any unavailable", and "If all unavailable" loan, the rules should work as expected before and after the patch is applied: "Yes": information column in the item table displays "Not on hold", the hold is placed, cancel the hold "If any unavailable" and "If all unavailable": the hold is not placed, message is "Cannot place hold. No items are available to be placed on hold.", red "X" in the hold column and the information column displays "Not on hold" (note: without the patch, holds can be placed): - Query to place a hold using ILS-DI on a title that have all its items available, example query: http://127.0.0.1:8080/cgi-bin/koha/ilsdi.pl?service=HoldTitle&patron_id=1&bib_id=1&request_location=127.0.0.1 Without the patch the hold is placed but it shouldn't be allowed, cancel the hold - Query to place a hold using ILS-DI on an available item, example query: http://127.0.0.1:8080/cgi-bin/koha/ilsdi.pl?service=HoldItem&patron_id=1&bib_id=1&item_id=1) Without the patch the hold is placed but it shouldn't be allowed, cancel the hold koha-testing-docker, run flush_memcached and restart_all) unavailable" and "If all unavailable" options are used: For the staff interface: there should be no change in behavour and should work as expected, for the red "X" in the items table additional text is added "onShelfHoldsNotAllowed". For ILS-DI: these should now work as expected, with holds not placed, and this message in the results returned onShelfHoldsNotAllowed (check to confirm no holds place for either the patron or the item) Tests: should still pass, in particular the following tests: - t/db_dependent/Reserves.t - t/db_dependent/Holds.t - t/db_dependent/Holds/DisallowHoldIfItemsAvailable.t - t/db_dependent/api/v1/holds.t - t/db_dependent/ILSDI_Services.t - t/db_dependent/Reserves/MultiplePerRecord.t - t/db_dependent/Reserves/IsOnShelfHoldsPolicySatisfied.t (new test file) --- C4/Reserves.pm | 93 +++++++++++---- .../prog/en/modules/reserve/request.tt | 1 + t/db_dependent/Holds.t | 30 +++++ t/db_dependent/ILSDI_Services.t | 28 +++++ t/db_dependent/Reserves.t | 76 +++++++++++- .../Reserves/IsOnShelfHoldsPolicySatisfied.t | 111 ++++++++++++++++++ t/db_dependent/Reserves/MultiplePerRecord.t | 1 + t/db_dependent/api/v1/holds.t | 2 + 8 files changed, 317 insertions(+), 25 deletions(-) create mode 100755 t/db_dependent/Reserves/IsOnShelfHoldsPolicySatisfied.t --- a/C4/Reserves.pm +++ a/C4/Reserves.pm @@ -404,8 +404,12 @@ sub CanBookBeReserved{ current params are: 'ignore_hold_counts' - we use this routine to check if an item can fill a hold - on this case we should not check if there are too many holds as we only care about reservability + 'ignore_onshelfholds_policy' - if set to a true value, do not check if the + "on shelf holds" policy is satisfied; useful to avoid infinite recursion with + IsOnShelfHoldsPolicySatisfied and ItemsAnyAvailableAndNotRestricted @RETURNS { status => OK }, if the Item can be reserved. + { status => onShelfHoldsNotAllowed }, if onShelfHoldsAllowed parameter and item availability combination doesn't allow holds. { status => ageRestricted }, if the Item is age restricted for this borrower. { status => damaged }, if the Item is damaged. { status => cannotReserveFromOtherBranches }, if syspref 'canreservefromotherbranches' is OK. @@ -657,6 +661,12 @@ sub CanItemBeReserved { } } + unless ( $params->{ignore_onshelfholds_policy} ) { + unless ( IsOnShelfHoldsPolicySatisfied( { item => $item, patron => $patron } ) ) { + return _cache { status => 'onShelfHoldsNotAllowed' }; + } + } + return _cache { status => 'OK' }; } @@ -1367,29 +1377,7 @@ sub IsAvailableForItemLevelRequest { return 0 unless $branchitemrule->{hold_fulfillment_policy} ne 'holdgroup' || $home_library->validate_hold_sibling( {branchcode => $pickup_branchcode} ); } - my $on_shelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } ); - - if ( $on_shelf_holds == 1 ) { - return 1; - } elsif ( $on_shelf_holds == 2 ) { - - # These calculations work at the biblio level, and can be expensive - # we use the in-memory cache to avoid calling once per item when looping items on a biblio - - my $memory_cache = Koha::Cache::Memory::Lite->get_instance(); - my $cache_key = sprintf "ItemsAnyAvailableAndNotRestricted:%s:%s", $patron->id, $item->biblionumber; - - my $any_available = $memory_cache->get_from_cache($cache_key); - return $any_available ? 0 : 1 if defined($any_available); - - $any_available = - ItemsAnyAvailableAndNotRestricted( { biblionumber => $item->biblionumber, patron => $patron } ); - $memory_cache->set_in_cache( $cache_key, $any_available ); - return $any_available ? 0 : 1; - - } else { # on_shelf_holds == 0 "If any unavailable" (the description is rather cryptic and could still be improved) - return $item->notforloan < 0 || $item->onloan || IsItemOnHoldAndFound( $item->itemnumber ); - } + return IsOnShelfHoldsPolicySatisfied({ item => $item, patron => $patron }); } =head2 ItemsAnyAvailableAndNotRestricted @@ -1427,12 +1415,69 @@ sub ItemsAnyAvailableAndNotRestricted { || Koha::ItemTypes->find( $i->effective_itemtype() )->notforloan || $branchitemrule->{holdallowed} eq 'from_home_library' && $param->{patron}->branchcode ne $i->homebranch || $branchitemrule->{holdallowed} eq 'from_local_hold_group' && ! $item_library->validate_hold_sibling( { branchcode => $param->{patron}->branchcode } ) - || CanItemBeReserved( $param->{patron}, $i )->{status} ne 'OK'; + || CanItemBeReserved( $param->{patron}, $i, undef, { ignore_onshelfholds_policy => 1 } )->{status} ne 'OK'; } return 0; } +=head2 IsOnShelfHoldsPolicySatisfied + +Verifies if a patron can place a hold on an item according to the "on shelf +holds" policy. + +Uses in-memory cache. + + $policy_ok = IsOnShelfHoldsPolicySatisfied({ item => $item, patron => $patron }) + +=head3 Parameters + +=over + +=item C<$item> - a Koha::Item object + +=item C<$patron> - a Koha::Patron object + +=back + +=head3 Return value + +a true value if the hold is possible, a false value otherwise + +=cut + +sub IsOnShelfHoldsPolicySatisfied { + my ($args) = @_; + my ( $item, $patron ) = @$args{qw(item patron)}; + + my $on_shelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } ); + + if ( $on_shelf_holds == 1 ) { + return 1; + } + + if ( $on_shelf_holds == 2 ) { + + # These calculations work at the biblio level, and can be expensive + # we use the in-memory cache to avoid calling once per item when looping items on a biblio + + my $memory_cache = Koha::Cache::Memory::Lite->get_instance(); + my $cache_key = sprintf "ItemsAnyAvailableAndNotRestricted:%s:%s", $patron->id, $item->biblionumber; + + my $any_available = $memory_cache->get_from_cache($cache_key); + unless ( defined $any_available ) { + $any_available = + ItemsAnyAvailableAndNotRestricted( { biblionumber => $item->biblionumber, patron => $patron } ); + $memory_cache->set_in_cache( $cache_key, $any_available ); + } + + return $any_available ? 0 : 1; + } + + # on_shelf_holds == 0 "If any unavailable" (the description is rather cryptic and could still be improved) + return $item->notforloan < 0 || $item->onloan || IsItemOnHoldAndFound( $item->itemnumber ); +} + =head2 AlterPriority AlterPriority( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority ); --- a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt @@ -1314,6 +1314,7 @@ tooManyReserves: _("Too many holds"), notReservable: _("Not holdable"), noReservesAllowed: _("No reserves allowed"), + onShelfHoldsNotAllowed: _("Not holdable"), cannotReserveFromOtherBranches: _("Patron is from different library"), itemAlreadyOnHold: _("Patron already has hold for this item"), cannotBeTransferred: _("Cannot be transferred to pickup library"), --- a/t/db_dependent/Holds.t +++ a/t/db_dependent/Holds.t @@ -251,6 +251,7 @@ Koha::CirculationRules->set_rules( branchcode => undef, itemtype => undef, rules => { + onshelfholds => 1, reservesallowed => 25, holds_per_record => 99, } @@ -262,6 +263,7 @@ Koha::CirculationRules->set_rules( branchcode => undef, itemtype => 'CANNOT', rules => { + onshelfholds => 1, reservesallowed => 0, holds_per_record => 99, } @@ -430,6 +432,7 @@ subtest 'CanItemBeReserved' => sub { branchcode => undef, itemtype => $itemtype_cant, rules => { + onshelfholds => 1, reservesallowed => 0, holds_per_record => 99, } @@ -441,6 +444,7 @@ subtest 'CanItemBeReserved' => sub { branchcode => undef, itemtype => $itemtype_can, rules => { + onshelfholds => 1, reservesallowed => 2, holds_per_record => 2, } @@ -452,6 +456,7 @@ subtest 'CanItemBeReserved' => sub { branchcode => undef, itemtype => $itemtype_cant_record, rules => { + onshelfholds => 1, reservesallowed => 0, holds_per_record => 0, } @@ -561,6 +566,7 @@ subtest 'CanItemBeReserved' => sub { branchcode => undef, itemtype => undef, rules => { + onshelfholds => 1, reservesallowed => 5, holds_per_record => 1, } @@ -639,6 +645,7 @@ Koha::CirculationRules->set_rules( branchcode => undef, itemtype => undef, rules => { + onshelfholds => 1, reservesallowed => 25, holds_per_record => 99, } @@ -701,6 +708,7 @@ Koha::CirculationRules->set_rules( branchcode => undef, itemtype => 'ONLY1', rules => { + onshelfholds => 1, reservesallowed => 1, holds_per_record => 99, } @@ -743,6 +751,7 @@ subtest 'Test max_holds per library/patron category' => sub { branchcode => undef, itemtype => $biblio->itemtype, rules => { + onshelfholds => 1, reservesallowed => 99, holds_per_record => 99, } @@ -822,6 +831,7 @@ subtest 'Pickup location availability tests' => sub { categorycode => undef, itemtype => undef, rules => { + onshelfholds => 1, reservesallowed => 25, holds_per_record => 99, } @@ -882,6 +892,7 @@ subtest 'CanItemBeReserved / holds_per_day tests' => sub { itemtype => $itemtype->itemtype, rules => { reservesallowed => 1, + onshelfholds => 1, holds_per_record => 99, holds_per_day => 2 } @@ -1051,6 +1062,15 @@ subtest 'CanItemBeReserved / branch_not_in_hold_group' => sub { rule_value => 25, } ); + Koha::CirculationRules->set_rule( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rule_name => 'onshelfholds', + rule_value => 1, + } + ); # Create item types my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } ); @@ -1265,6 +1285,15 @@ subtest 'CanItemBeReserved / pickup_not_in_hold_group' => sub { rule_value => 25, } ); + Koha::CirculationRules->set_rule( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rule_name => 'onshelfholds', + rule_value => 1, + } + ); # Create item types my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } ); @@ -1613,6 +1642,7 @@ subtest 'CanItemBeReserved rule precedence tests' => sub { categorycode => $patron->categorycode, itemtype => $item->itype, rules => { + onshelfholds => 1, reservesallowed => 1, } } --- a/t/db_dependent/ILSDI_Services.t +++ a/t/db_dependent/ILSDI_Services.t @@ -371,6 +371,16 @@ subtest 'Holds test' => sub { } ); + Koha::CirculationRules->set_rule( + { + categorycode => undef, + itemtype => undef, + branchcode => undef, + rule_name => 'onshelfholds', + rule_value => 1, + } + ); + t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' ); Koha::CirculationRules->set_rule( { @@ -547,6 +557,15 @@ subtest 'Holds test for branch transfer limits' => sub { rule_value => 99, } ); + Koha::CirculationRules->set_rule( + { + categorycode => undef, + itemtype => undef, + branchcode => undef, + rule_name => 'onshelfholds', + rule_value => 1, + } + ); my $limit = Koha::Item::Transfer::Limit->new({ toBranch => $pickup_branch->{branchcode}, @@ -613,6 +632,15 @@ subtest 'Holds test with start_date and end_date' => sub { rule_value => 99, } ); + Koha::CirculationRules->set_rule( + { + categorycode => undef, + itemtype => undef, + branchcode => undef, + rule_name => 'onshelfholds', + rule_value => 1, + } + ); my $query = CGI->new; $query->param( 'pickup_location', $pickup_library->branchcode ); --- a/t/db_dependent/Reserves.t +++ a/t/db_dependent/Reserves.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 77; +use Test::More tests => 78; use Test::MockModule; use Test::Warn; @@ -185,6 +185,7 @@ Koha::CirculationRules->set_rules( rules => { reservesallowed => 25, holds_per_record => 1, + onshelfholds => 1, } } ); @@ -1079,7 +1080,67 @@ subtest 'reserves.item_level_hold' => sub { $hold->delete; }; +}; + +subtest 'OnShelfHoldAllowed test' => sub { + plan tests => 3; + $dbh->do('DELETE FROM circulation_rules'); + my $biblio = $builder->build_sample_biblio({frameworkcode => $frameworkcode})->biblionumber; + + # Create a helper item instance for testing + my $item = $builder->build_sample_item({ biblionumber => $biblio, library => $branch_1, itype => $itemtype }); + + Koha::CirculationRules->set_rules( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + opacitemholds => 'Y', + onshelfholds => 1, + } + } + ); + + my $canreserve = C4::Reserves::CanItemBeReserved($patron, $item); + is( $canreserve->{status}, 'OK', + 'item-level holds should be possible with onshelfholdallowed set to "Yes"' ); + + Koha::CirculationRules->set_rules( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + opacitemholds => 'Y', + onshelfholds => '0', + } + }); + + $canreserve = C4::Reserves::CanItemBeReserved($patron, $item); + + is( $canreserve->{status}, 'onShelfHoldsNotAllowed', + 'item-level holds should not be possible with onshelfholdallowed set to "If any unavailable"' ); + + Koha::CirculationRules->set_rules( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + opacitemholds => 'Y', + onshelfholds => '2', + } + }); + + $canreserve = C4::Reserves::CanItemBeReserved($patron, $item); + + is( $canreserve->{status}, 'onShelfHoldsNotAllowed', + 'item-level holds should not be possible with onshelfholdallowed set to "If all unavailable"' ); }; subtest 'MoveReserve additional test' => sub { @@ -1319,6 +1380,17 @@ subtest 'AllowHoldOnPatronPossession test' => sub { my $patron = $builder->build_object({ class => "Koha::Patrons", value => { branchcode => $item->homebranch }}); + Koha::CirculationRules->set_rules( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + onshelfholds => 1, + } + } + ); + C4::Circulation::AddIssue($patron->unblessed, $item->barcode); t::lib::Mocks::mock_preference('AllowHoldsOnPatronsPossessions', 0); @@ -1639,6 +1711,7 @@ subtest 'CanBookBeReserved() tests' => sub { itemtype => $itype->id, rules => { reservesallowed => 2, + onshelfholds => 1, } } ); @@ -1710,6 +1783,7 @@ subtest 'CanItemBeReserved() tests' => sub { itemtype => $itype->id, rules => { reservesallowed => 2, + onshelfholds => 1, } } ); --- a/t/db_dependent/Reserves/IsOnShelfHoldsPolicySatisfied.t +++ a/t/db_dependent/Reserves/IsOnShelfHoldsPolicySatisfied.t @@ -0,0 +1,111 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Test::More; + +use t::lib::TestBuilder; +use Koha::Database; +use Koha::Cache::Memory::Lite; + +use C4::Reserves; + +plan tests => 1; + +my $schema = Koha::Database->schema; +my $circulationrule_rs = $schema->resultset('CirculationRule'); +my $builder = t::lib::TestBuilder->new; + +subtest '2 items, 1 itemtype, 1 library' => sub { + plan tests => 3; + $schema->txn_begin; + + my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } )->itemtype; + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $branchcode = $library->branchcode; + my $biblio = $builder->build_sample_biblio( { itemtype => $itemtype } ); + my $item1 = + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, library => $branchcode } ); + my $item2 = + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, library => $branchcode } ); + my $patron = + $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $branchcode } } ); + my $categorycode = $patron->categorycode; + + $circulationrule_rs->populate( + [ + [ 'branchcode', 'categorycode', 'itemtype', 'rule_name', 'rule_value' ], + [ $branchcode, undef, $itemtype, 'holdallowed', 'from_any_library' ], + [ $branchcode, undef, $itemtype, 'hold_fulfillment_policy', 'any' ], + [ $branchcode, $categorycode, undef, 'max_holds', '99' ], + [ $branchcode, $categorycode, $itemtype, 'holds_per_day', '99' ], + [ $branchcode, $categorycode, $itemtype, 'holds_per_record', '99' ], + [ $branchcode, $categorycode, $itemtype, 'reservesallowed', '99' ], + ] + ); + my $onshelfholds_rule = $circulationrule_rs->create( + { + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => 'onshelfholds', + rule_value => '0', + } + ); + + subtest 'when all items are available' => sub { + plan tests => 3; + + $onshelfholds_rule->update({ rule_value => '0' }); + Koha::Cache::Memory::Lite->flush(); + ok(!C4::Reserves::IsOnShelfHoldsPolicySatisfied({ item => $item1, patron => $patron }), 'on shelf hold is not allowed if onshelfholds == 0'); + + $onshelfholds_rule->update({ rule_value => '1' }); + Koha::Cache::Memory::Lite->flush(); + ok(C4::Reserves::IsOnShelfHoldsPolicySatisfied({ item => $item1, patron => $patron }), 'on shelf hold is always allowed if onshelfholds == 1'); + + $onshelfholds_rule->update({ rule_value => '2' }); + Koha::Cache::Memory::Lite->flush(); + ok(!C4::Reserves::IsOnShelfHoldsPolicySatisfied({ item => $item1, patron => $patron }), 'on shelf hold is not allowed if onshelfholds == 2'); + }; + + subtest 'when checked item is not available but others are available' => sub { + plan tests => 3; + + $item1->onloan(DateTime->now)->store; + + $onshelfholds_rule->update({ rule_value => '0' }); + Koha::Cache::Memory::Lite->flush(); + ok(C4::Reserves::IsOnShelfHoldsPolicySatisfied({ item => $item1, patron => $patron }), 'on shelf hold is allowed if onshelfholds == 0'); + + $onshelfholds_rule->update({ rule_value => '1' }); + Koha::Cache::Memory::Lite->flush(); + ok(C4::Reserves::IsOnShelfHoldsPolicySatisfied({ item => $item1, patron => $patron }), 'on shelf hold is always allowed if onshelfholds == 1'); + + $onshelfholds_rule->update({ rule_value => '2' }); + Koha::Cache::Memory::Lite->flush(); + ok(!C4::Reserves::IsOnShelfHoldsPolicySatisfied({ item => $item1, patron => $patron }), 'on shelf hold is not allowed if onshelfholds == 2'); + }; + + subtest 'when all items are unavailable' => sub { + plan tests => 3; + + $item1->onloan(DateTime->now)->store; + $item2->onloan(DateTime->now)->store; + + $onshelfholds_rule->update({ rule_value => '0' }); + Koha::Cache::Memory::Lite->flush(); + ok(C4::Reserves::IsOnShelfHoldsPolicySatisfied({ item => $item1, patron => $patron }), 'on shelf hold is allowed if onshelfholds == 0'); + + $onshelfholds_rule->update({ rule_value => '1' }); + Koha::Cache::Memory::Lite->flush(); + ok(C4::Reserves::IsOnShelfHoldsPolicySatisfied({ item => $item1, patron => $patron }), 'on shelf hold is always allowed if onshelfholds == 1'); + + $onshelfholds_rule->update({ rule_value => '2' }); + Koha::Cache::Memory::Lite->flush(); + ok(C4::Reserves::IsOnShelfHoldsPolicySatisfied({ item => $item1, patron => $patron }), 'on shelf hold is allowed if onshelfholds == 2'); + }; + + $schema->txn_rollback; +}; + --- a/t/db_dependent/Reserves/MultiplePerRecord.t +++ a/t/db_dependent/Reserves/MultiplePerRecord.t @@ -222,6 +222,7 @@ Koha::CirculationRules->set_rules( itemtype => undef, branchcode => undef, rules => { + onshelfholds => 1, reservesallowed => 3, holds_per_record => 2, } --- a/t/db_dependent/api/v1/holds.t +++ a/t/db_dependent/api/v1/holds.t @@ -124,6 +124,7 @@ Koha::CirculationRules->set_rules( branchcode => undef, itemtype => undef, rules => { + onshelfholds => 1, reservesallowed => 1, holds_per_record => 99 } @@ -683,6 +684,7 @@ subtest 'add() tests (maxreserves behaviour)' => sub { branchcode => undef, categorycode => undef, rules => { + onshelfholds => 1, reservesallowed => 3 } } --