From 2283ca657583f772d2ee98f74df8dc98ee4b27f9 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Mon, 22 Feb 2021 17:54:58 +1300 Subject: [PATCH] Bug 15565: Koha::Biblio->allowed_holds and tests Signed-off-by: David Cook --- Koha/Biblio.pm | 29 +++++++++++++++++++++++++++++ t/db_dependent/Reserves/MultiplePerRecord.t | 16 +++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 1d84f01a7b..5d0d00b20e 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -850,6 +850,35 @@ sub get_marc_notes { return \@marcnotes; } +=head3 allowed_holds + + my $holds_allowed_on_record_today = $biblio->allowed_holds( $patron_obj ); + +Calculates and returns the number of item-level holds a borrower is allowed to place on the record on a single day. + +=cut + +sub allowed_holds { + + my ( $self, $patron ) = @_; + + my $controlbranch = C4::Context->preference('ReservesControlBranch'); + my $holds_allowed = 0; + foreach my $item ( $self->items()->as_list() ) { + my $holds_per_day = Koha::CirculationRules->get_effective_rule({ + rule_name => 'holds_per_day', + categorycode => $patron->categorycode, + itemtype => $item->effective_itemtype(), + branchcode => ( $controlbranch and $controlbranch eq 'PatronLibrary' ) ? $patron->branchcode : $item->homebranch, + }); + $holds_per_day = $holds_per_day ? $holds_per_day->rule_value : 0; + if ( $holds_per_day ) { + $holds_allowed = $holds_per_day if $holds_per_day > $holds_allowed; + } + } + return $holds_allowed; +} + =head3 to_api my $json = $biblio->to_api; diff --git a/t/db_dependent/Reserves/MultiplePerRecord.t b/t/db_dependent/Reserves/MultiplePerRecord.t index 6e88740702..7659f4d0b2 100755 --- a/t/db_dependent/Reserves/MultiplePerRecord.t +++ b/t/db_dependent/Reserves/MultiplePerRecord.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 16; +use Test::More tests => 18; use t::lib::TestBuilder; use t::lib::Mocks; @@ -98,6 +98,7 @@ Koha::CirculationRules->set_rules( rules => { reservesallowed => 1, holds_per_record => 1, + holds_per_day => 1, } } ); @@ -115,6 +116,7 @@ Koha::CirculationRules->set_rules( rules => { reservesallowed => 2, holds_per_record => 2, + holds_per_day => 2, } } ); @@ -130,6 +132,7 @@ Koha::CirculationRules->set_rules( rules => { reservesallowed => 3, holds_per_record => 3, + holds_per_day => 3, } } ); @@ -145,6 +148,7 @@ Koha::CirculationRules->set_rules( rules => { reservesallowed => 4, holds_per_record => 4, + holds_per_day => 4, } } ); @@ -152,6 +156,10 @@ Koha::CirculationRules->set_rules( $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber ); is( $max, 4, 'GetMaxPatronHoldsForRecord returns max of 4' ); +my $patron_obj = Koha::Patrons->find( $patron->{borrowernumber} ); +my $holds_per_day = $biblio->allowed_holds( $patron_obj ); +is( $holds_per_day, 4, "GetAllowedHoldsForPatronToday returns max of 4" ); + Koha::CirculationRules->set_rules( { categorycode => $category->{categorycode}, @@ -160,6 +168,7 @@ Koha::CirculationRules->set_rules( rules => { reservesallowed => 5, holds_per_record => 5, + holds_per_day => 5, } } ); @@ -167,6 +176,9 @@ Koha::CirculationRules->set_rules( $max = GetMaxPatronHoldsForRecord( $patron->{borrowernumber}, $biblio->biblionumber ); is( $max, 5, 'GetMaxPatronHoldsForRecord returns max of 5' ); +$holds_per_day = $biblio->allowed_holds( $patron_obj ); +is( $holds_per_day, 5, "GetAllowedHoldsForPatronToday returns max of 4" ); + Koha::CirculationRules->set_rules( { categorycode => undef, @@ -175,6 +187,7 @@ Koha::CirculationRules->set_rules( rules => { reservesallowed => 9, holds_per_record => 9, + holds_per_day => 9, } } ); @@ -216,6 +229,7 @@ Koha::CirculationRules->set_rules( rules => { reservesallowed => 3, holds_per_record => 2, + holds_per_day => 3, } } ); -- 2.11.0