From 7f605f29626e398422f629343f6d0f49473dfcf0 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Wed, 22 Apr 2020 20:06:54 +0000 Subject: [PATCH] Bug 19532: Other objects used in recalls feature * biblio->recalls * biblio->can_be_recalled * item->recall * item->can_be_recalled * item->can_set_waiting_recall * patron->recalls * tests --- Koha/Biblio.pm | 107 ++++++++++++++++++++++++++++++ Koha/Item.pm | 150 +++++++++++++++++++++++++++++++++++++++++++ Koha/Patron.pm | 21 ++++++ t/db_dependent/Koha/Biblio.t | 57 +++++++++++++++- 4 files changed, 334 insertions(+), 1 deletion(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 4fe30113e3b..3082cc1bc11 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -815,6 +815,113 @@ sub to_api_mapping { }; } +=head3 recalls + + my $recalls = $biblio->recalls; + +Return all active recalls attached to this biblio + +=cut + +sub recalls { + my ( $self ) = @_; + $self->{_recalls} ||= Koha::Recalls->search({ biblionumber => $self->biblionumber, old => undef }); + return $self->{_recalls}; +} + +=head3 can_be_recalled + + my @items_for_recall = $biblio->can_be_recalled({ borrower => $borrower_object }); + +Does biblio-level checks and returns the items attached to this biblio that are available for recall + +=cut + +sub can_be_recalled { + my ( $self, $params ) = @_; + + return 0 if !( C4::Context->preference('UseRecalls') ); + + my $borrower = $params->{borrower}; + + my $branchcode = C4::Context->userenv->{'branch'}; + if ( C4::Context->preference('CircControl') eq 'PatronLibrary' and $borrower ) { + $branchcode = $borrower->branchcode; + } + + # items are unavailable for recall if they are lost, withdrawn or notforloan + my @items = Koha::Items->search({ biblionumber => $self->biblionumber, itemlost => 0, withdrawn => 0, notforloan => 0 }); + + # if there are no available items at all, no recall can be placed + return 0 if ( scalar @items == 0 ); + + my @itemtypes; + my @itemnumbers; + foreach my $item ( @items ) { + push( @itemtypes, $item->effective_itemtype ); + push( @itemnumbers, $item->itemnumber ); + } + + # Check the circulation rule for each relevant itemtype for this biblio + my ( @recalls_allowed, @recalls_per_record, @on_shelf_recalls ); + foreach my $itemtype ( @itemtypes ) { + my $rule = Koha::CirculationRules->get_effective_rules({ + branchcode => $branchcode, + categorycode => $borrower ? $borrower->categorycode : undef, + itemtype => $itemtype, + rules => [ + 'recalls_allowed', + 'recalls_per_record', + 'on_shelf_recalls', + ], + }); + push( @recalls_allowed, $rule->{recalls_allowed} ) if $rule; + push( @recalls_per_record, $rule->{recalls_per_record} ) if $rule; + push( @on_shelf_recalls, $rule->{on_shelf_recalls} ) if $rule; + } + my $recalls_allowed = max(@recalls_allowed); # take highest + my $recalls_per_record = max(@recalls_per_record); # take highest + my %on_shelf_recalls_count = (); + foreach my $count ( @on_shelf_recalls ) { + $on_shelf_recalls_count{$count}++; + } + my $on_shelf_recalls = (sort {$on_shelf_recalls_count{$b} <=> $on_shelf_recalls_count{$a}} @on_shelf_recalls)[0]; # take most common + + # check recalls allowed has been set and is not zero + return 0 if ( $recalls_allowed == 0 || !defined($recalls_allowed) ); + + if ( $borrower ) { + # check borrower has not reached open recalls allowed limit + return 0 if ( $borrower->recalls->count >= $recalls_allowed ); + + # check borrower has not reach open recalls allowed per record limit + return 0 if ( $borrower->recalls({ biblionumber => $self->biblionumber })->count >= $recalls_per_record ); + + # check if any of the items under this biblio are already checked out by this borrower + return 0 if ( Koha::Checkouts->search({ itemnumber => @itemnumbers, borrowernumber => $borrower->borrowernumber })->count > 0 ); + } + + # check item availability + my $checked_out_count = 0; + foreach (@items) { + if ( Koha::Checkouts->search({ itemnumber => $_->itemnumber })->count > 0 ){ $checked_out_count++; } + } + + # can't recall if on shelf recalls only allowed when all unavailable, but items are still available for checkout + return 0 if ( $on_shelf_recalls == 2 && $checked_out_count < scalar @items ); + + # check if on shelf recalls allowed when any unavailable, but no items have been checked out + # can't recall if no items have been checked out + return 0 if ( $on_shelf_recalls == 0 && $checked_out_count == 0 ); + + # check if on shelf recalls allowed always, but no items have been checked out + # can't recall if no items have been checked out + return 0 if ( $on_shelf_recalls == 1 && $checked_out_count == 0 ); + + # can recall + return @items; +} + =head2 Internal methods =head3 type diff --git a/Koha/Item.pm b/Koha/Item.pm index 9e1bca4ef5f..f1c50db711a 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -842,6 +842,156 @@ sub _after_item_action_hooks { } } +=head3 recall + + my $recall = $item->recall; + +Return the relevant recall for this item + +=cut + +sub recall { + my ( $self ) = @_; + my $recalls = Koha::Recalls->search({ biblionumber => $self->biblionumber, old => undef }, { order_by => { -asc => 'recalldate' } }); + foreach my $recall (@$recall_rs) { + if ( $recall->item_level_recall and $recall->itemnumber == $self->itemnumber ){ + return $recall; + } + } + # no item-level recall to return, so return earliest biblio-level + # FIXME: eventually this will be based on priority + return $recalls->first; +} + +=head3 can_be_recalled + + if ( $item->can_be_recalled({ borrower => $borrower_object }) ) # do recall + +Does item-level checks and returns if items can be recalled by this borrower + +=cut + +sub can_be_recalled { + my ( $self, $params ) = @_; + + return 0 if !( C4::Context->preference('UseRecalls') ); + + my $borrower = $params->{borrower}; + + my $branchcode = C4::Context->userenv->{'branch'}; + if ( $borrower ) { + $branchcode = C4::Circulation::_GetCircControlBranch( $self->unblessed, $borrower->unblessed ); + } + + # Check the circulation rule for each relevant itemtype for this item + my $rule = Koha::CirculationRules->get_effective_rules({ + branchcode => $branchcode, + categorycode => $borrower ? $borrower->categorycode : undef, + itemtype => $self->effective_itemtype, + rules => [ + 'recalls_allowed', + 'recalls_per_record', + 'on_shelf_recalls', + ], + }); + + # check recalls allowed has been set and is not zero + return 0 if ( $rule->{recalls_allowed} == 0 || !defined($rule->{recalls_allowed}) ); + + if ( $borrower ) { + # check borrower has not reached open recalls allowed limit + return 0 if ( $borrower->recalls->count >= $rule->{recalls_allowed} ); + + # check borrower has not reach open recalls allowed per record limit + return 0 if ( $borrower->recalls({ biblionumber => $self->biblionumber })->count >= $rule->{recalls_per_record} ); + } + + # check item availability + # items are unavailable for recall if they are lost, withdrawn or notforloan + my @items = Koha::Items->search({ biblionumber => $self->biblionumber, itemlost => 0, withdrawn => 0, notforloan => 0 }); + + # if there are no available items at all, no recall can be placed + return 0 if ( scalar @items == 0 ); + + my $checked_out_count = 0; + foreach (@items) { + if ( Koha::Checkouts->search({ itemnumber => $_->itemnumber })->count > 0 ){ $checked_out_count++; } + } + + # can't recall if on shelf recalls only allowed when all unavailable, but items are still available for checkout + return 0 if ( $rule->{on_shelf_recalls} == 2 && $checked_out_count < scalar @items ); + + # check if on shelf recalls allowed when any unavailable, but no items have been checked out + # can't recall if no items have been checked out + return 0 if ( $rule->{on_shelf_recalls} == 0 && $checked_out_count == 0 ); + + # check if on shelf recalls allowed always, but no items have been checked out + # can't recall if no items have been checked out + return 0 if ( $rule->{on_shelf_recalls} == 1 && $checked_out_count == 0 ); + + # check if this patron has already recalled this item + return 0 if ( Koha::Recalls->search({ itemnumber => $self->itemnumber, borrowernumber => $borrower->borrowernumber, old => undef })->count > 0 ); + + # check if this patron has already checked out this item + return 0 if ( Koha::Checkouts->search({ itemnumber => $self->itemnumber, borrowernumber => $borrower->borrowernumber })->count > 0 ); + + # check if this patron has already reserved this item + return 0 if ( Koha::Holds->search({ itemnumber => $self->itemnumber, borrowernumber => $borrower->borrowernumber })->count > 0 ); + + # check if this item is not checked out - if not checked out, can't be recalled + return 0 if ( !defined( $self->checkout ) ); + + # check if this item is not for loan, withdrawn or lost + return 0 if ( $self->notforloan != 0 ); + return 0 if ( $self->itemlost != 0 ); + return 0 if ( $self->withdrawn != 0 ); + + # can recall + return 1; +} + +=head3 can_set_waiting_recall + + if ( $item->can_set_waiting_recall ) { # allocate item as waiting for recall + +Checks item type and branch of circ rules to return whether this item can be used to fill a recall. +At this point the item has already been recalled. We are now at the checkin and set waiting stage. + +=cut + +sub can_set_waiting_recall { + my ( $self ) = @_; + + return 0 if !( C4::Context->preference('UseRecalls') ); + + my $branchcode = $self->holdingbranch; + if ( C4::Context->preference('CircControl') eq 'PickupLibrary' and C4::Context->userenv and C4::Context->userenv->{'branch'} ) { + $branchcode = C4::Context->userenv->{'branch'}; + } else { + $branchcode = ( C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ) ? $self->homebranch : $self->holdingbranch; + } + + # Check the circulation rule for each relevant itemtype for this item + my $rule = Koha::CirculationRules->get_effective_rules({ + branchcode => $branchcode, + categorycode => undef, + itemtype => $self->effective_itemtype, + rules => [ + 'recalls_allowed', + ], + }); + + # check recalls allowed has been set and is not zero + return 0 if ( $rule->{recalls_allowed} == 0 || !defined($rule->{recalls_allowed}) ); + + # check if this item is not for loan, withdrawn or lost + return 0 if ( $self->notforloan != 0 ); + return 0 if ( $self->itemlost != 0 ); + return 0 if ( $self->withdrawn != 0 ); + + # can recall + return 1; +} =head3 _type =cut diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 5436ccc4485..5abd55485e2 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1712,6 +1712,27 @@ sub to_api_mapping { }; } +=head3 recalls + + my $recalls = $patron->recalls; + + my $recalls = $patron->recalls({ biblionumber => $biblionumber }); + +Return the patron's active recalls - total, or on a specific biblio + +=cut + +sub recalls { + my ( $self, $params ) = @_; + + my $biblionumber = $params->{biblionumber}; + + if ( $biblionumber ) { + return Koha::Recalls->search({ borrowernumber => $self->borrowernumber, old => undef, biblionumber => $biblionumber }); + } + return Koha::Recalls->search({ borrowernumber => $self->borrowernumber, old => undef }); +} + =head2 Internal methods =head3 _type diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 790b623aade..7bedde4afa2 100644 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 12; +use Test::More tests => 13; use C4::Biblio; use Koha::Database; @@ -569,3 +569,58 @@ subtest 'subscriptions() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'Recalls tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + my $item = $builder->build_sample_item; + my $biblio = $item->biblio; + my $branchcode = $item->holdingbranch; + my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } }); + my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } }); + my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } }); + + my $recall1 = Koha::Recall->new({ + borrowernumber => $patron1->borrowernumber, + recalldate => Koha::DateUtils::dt_from_string, + biblionumber => $biblio->biblionumber, + branchcode => $branchcode, + status => 'R', + itemnumber => $item->itemnumber, + expirationdate => undef, + item_level_recall => 1 + })->store; + my $recall2 = Koha::Recall->new({ + borrowernumber => $patron2->borrowernumber, + recalldate => Koha::DateUtils::dt_from_string, + biblionumber => $biblio->biblionumber, + branchcode => $branchcode, + status => 'R', + itemnumber => undef, + expirationdate => undef, + item_level_recall => 0 + })->store; + my $recall3 = Koha::Recall->new({ + borrowernumber => $patron3->borrowernumber, + recalldate => Koha::DateUtils::dt_from_string, + biblionumber => $biblio->biblionumber, + branchcode => $branchcode, + status => 'R', + itemnumber => $item->itemnumber, + expirationdate => undef, + item_level_recall => 1 + })->store; + + my $recalls_count = $biblio->recalls->count; + is( $recalls_count, 3, 'Correctly get number of active recalls for biblio' ); + + $recall1->set_cancelled; + $recall2->set_expired({ location => 'COMMANDLINE' }); + + $recalls_count = $biblio->recalls->count; + is( $recalls_count, 1, 'Correctly get number of active recalls for biblio' ); + + $schema->storage->txn_rollback; +}; -- 2.11.0