From 089f8be6f321717ff3fe6ff2359e7d7c32f35339 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 --- C4/Circulation.pm | 101 ++++++++++++++++++++++- Koha/Biblio.pm | 102 +++++++++++++++++++++++ Koha/Item.pm | 181 +++++++++++++++++++++++++++++++++++++++++ Koha/Patron.pm | 24 ++++++ Koha/Template/Plugin/Biblio.pm | 9 ++ t/db_dependent/Koha/Biblio.t | 118 ++++++++++++++++++++++++++- t/db_dependent/Koha/Item.t | 147 ++++++++++++++++++++++++++++++++- 7 files changed, 678 insertions(+), 4 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 8db887db2e7..b89f445efa4 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -299,6 +299,14 @@ The item was reserved. The value is a reference-to-hash whose keys are fields fr The item was eligible to be transferred. Barring problems communicating with the database, the transfer should indeed have succeeded. The value should be ignored. +=item C + +A recall for this item was found, and the transfer has already been completed as the item's branch matches the recall's pickup branch. + +=item C + +A recall for this item was found, and the item needs to be transferred to the recall's pickup branch. + =back =back @@ -360,6 +368,19 @@ sub transferbook { $dotransfer = 1; } + # find recall + my $recall = Koha::Recalls->find({ itemnumber => $itemnumber, status => 'T' }); + if ( defined $recall ) { + # do a transfer if the recall branch is different to the item holding branch + if ( $recall->branchcode eq $fbr ) { + $dotransfer = 0; + $messages->{'RecallPlacedAtHoldingBranch'} = 1; + } else { + $dotransfer = 1; + $messages->{'RecallFound'} = $recall; + } + } + #actually do the transfer.... if ($dotransfer) { ModItemTransfer( $itemnumber, $fbr, $tbr, $trigger ); @@ -669,6 +690,10 @@ sticky due date is invalid or due date in the past if the borrower borrows to much things +=head3 RECALLED + +recalled by someone else + =cut sub CanBookBeIssued { @@ -1020,7 +1045,35 @@ sub CanBookBeIssued { } } - unless ( $ignore_reserves ) { + my $recall; + # CHECK IF ITEM HAS BEEN RECALLED BY ANOTHER PATRON + # Only bother doing this if UseRecalls is enabled and the item is recallable + # Don't look at recalls that are in transit + if ( C4::Context->preference('UseRecalls') and $item_object->can_be_recalled({ patron => $patron }) ) { + my @recalls = $item_object->biblio->recalls; + + foreach my $r ( @recalls ) { + if ( $r->item_level_recall and + $r->itemnumber == $item_object->itemnumber and + $r->borrowernumber != $patron->borrowernumber and + !$r->in_transit ) { + # this specific item has been recalled by a different patron + $needsconfirmation{RECALLED} = $r; + $recall = $r; + last; + } + elsif ( !$r->item_level_recall and + $r->borrowernumber != $patron->borrowernumber and + !$r->in_transit ) { + # a different patron has placed a biblio-level recall and this item is eligible to fill it + $needsconfirmation{RECALLED} = $r; + $recall = $r; + last; + } + } + } + + unless ( $ignore_reserves and defined $recall ) { # See if the item is on reserve. my ( $restype, $res ) = C4::Reserves::CheckReserves( $item_object->itemnumber ); if ($restype) { @@ -1290,6 +1343,10 @@ AddIssue does the following things : * RESERVE PLACED ? - fill reserve if reserve to this patron - cancel reserve or not, otherwise + * RECALL PLACED ? + - fill recall if recall to this patron + - cancel recall or not + - revert recall's waiting status or not * TRANSFERT PENDING ? - complete the transfert * ISSUE THE BOOK @@ -1304,6 +1361,8 @@ sub AddIssue { my $onsite_checkout = $params && $params->{onsite_checkout} ? 1 : 0; my $switch_onsite_checkout = $params && $params->{switch_onsite_checkout}; my $auto_renew = $params && $params->{auto_renew}; + my $cancel_recall = $params && $params->{cancel_recall}; + my $recall_id = $params && $params->{recall_id}; my $dbh = C4::Context->dbh; my $barcodecheck = CheckValidBarcode($barcode); @@ -1375,6 +1434,8 @@ sub AddIssue { AddReturn( $item_object->barcode, C4::Context->userenv->{'branch'} ); } + Koha::Recalls->move_recall({ action => $cancel_recall, recall_id => $recall_id, itemnumber => $item_object->itemnumber, borrowernumber => $borrower->{borrowernumber} }); + C4::Reserves::MoveReserve( $item_object->itemnumber, $borrower->{'borrowernumber'}, $cancelreserve ); # Starting process for transfer job (checking transfert and validate it if we have one) @@ -1836,6 +1897,16 @@ Value 1 if return is successful. If AutomaticItemReturn is disabled, return branch is given as value of NeedsTransfer. +=item C + +This item can fill a recall. The recall object is returned. If the recall pickup branch differs from +the branch this item is being returned at, C is also returned which contains this +branchcode. + +=item C + +This item has been transferred to this branch to fill a recall. The recall object is returned. + =back C<$iteminformation> is a reference-to-hash, giving information about the @@ -2076,6 +2147,16 @@ sub AddReturn { } } + # find recalls... + # check if this item is recallable first, which includes checking if UseRecalls syspref is enabled + my $recall = $item->check_recalls if $item->can_be_waiting_recall; + if ( defined $recall ) { + $messages->{RecallFound} = $recall; + if ( $recall->branchcode ne $branch ) { + $messages->{RecallNeedsTransfer} = $branch; + } + } + # find reserves..... # launch the Checkreserves routine to find any holds my ($resfound, $resrec); @@ -2143,8 +2224,13 @@ sub AddReturn { $request->status('RET') if $request; } + my $transfer_recall = Koha::Recalls->find({ itemnumber => $item->itemnumber, status => 'T' }); # all recalls that have triggered a transfer will have an allocated itemnumber + if ( $transfer_recall && $transfer_recall->branchcode eq $branch ) { + $messages->{TransferredRecall} = $transfer_recall; + } + # Transfer to returnbranch if Automatic transfer set or append message NeedsTransfer - if (!$is_in_rotating_collection && ($doreturn or $messages->{'NotIssued'}) and !$resfound and ($branch ne $returnbranch) and not $messages->{'WrongTransfer'}){ + if (!$is_in_rotating_collection && ($doreturn or $messages->{'NotIssued'}) and !$resfound and ($branch ne $returnbranch) and not $messages->{'WrongTransfer'} and not $messages->{TransferredRecall} and $recall->branchcode ne $branch){ my $BranchTransferLimitsType = C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ? 'effective_itemtype' : 'ccode'; if (C4::Context->preference("AutomaticItemReturn" ) or (C4::Context->preference("UseBranchTransferLimits") and @@ -2835,6 +2921,17 @@ sub CanBookBeRenewed { } } + my $recall = $item->check_recalls if $item->can_be_waiting_recall; + if ( defined $recall ) { + if ( $recall->item_level_recall ) { + # item-level recall. check if this item is the recalled item, otherwise renewal will be allowed + return ( 0, 'recalled' ) if ( $recall->itemnumber == $item->itemnumber ); + } + } else { + # biblio-level recall, so only disallow renewal if the biblio-level recall has been fulfilled by a different item + return ( 0, 'recalled' ) unless ( $recall->waiting ); + } + my ( $resfound, $resrec, undef ) = C4::Reserves::CheckReserves($itemnumber); # This item can fill one or more unfilled reserve, can those unfilled reserves diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 4fe30113e3b..84ed8ebc361 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -815,6 +815,108 @@ sub to_api_mapping { }; } +=head3 recalls + + my $recalls = $biblio->recalls; + +Return all active recalls attached to this biblio, sorted by oldest first + +=cut + +sub recalls { + my ( $self ) = @_; + $self->{_recalls} ||= Koha::Recalls->search({ biblionumber => $self->biblionumber, old => undef }, { order_by => { -asc => 'recalldate' } } ); + return $self->{_recalls}; +} + +=head3 can_be_recalled + + my @items_for_recall = $biblio->can_be_recalled({ patron => $patron_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 $patron = $params->{patron}; + + my $branchcode = C4::Context->userenv->{'branch'}; + if ( C4::Context->preference('CircControl') eq 'PatronLibrary' and $patron ) { + $branchcode = $patron->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 => $patron ? $patron->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 = (sort {$b <=> $a} @recalls_allowed)[0]; # take highest + my $recalls_per_record = (sort {$b <=> $a} @recalls_per_record)[0]; # 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 ( !defined($recalls_allowed) || $recalls_allowed == 0 ); + + if ( $patron ) { + # check borrower has not reached open recalls allowed limit + return 0 if ( $patron->recalls->count >= $recalls_allowed ); + + # check borrower has not reached open recalls allowed per record limit + return 0 if ( $patron->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 => $patron->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 eq 'all' && $checked_out_count < scalar @items ); + + # can't recall if no items have been checked out + return 0 if ( $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..0cdec4afe0f 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -842,6 +842,187 @@ 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 (@recalls) { + 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[0]; +} + +=head3 can_be_recalled + + if ( $item->can_be_recalled({ patron => $patron_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') ); + + # 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 ); + + # check if this item is not checked out - if not checked out, can't be recalled + return 0 if ( !defined( $self->checkout ) ); + + my $patron = $params->{patron}; + + my $branchcode = C4::Context->userenv->{'branch'}; + if ( $patron ) { + $branchcode = C4::Circulation::_GetCircControlBranch( $self->unblessed, $patron->unblessed ); + } + + # Check the circulation rule for each relevant itemtype for this item + my $rule = Koha::CirculationRules->get_effective_rules({ + branchcode => $branchcode, + categorycode => $patron ? $patron->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 ( !defined($rule->{recalls_allowed}) || $rule->{recalls_allowed} == 0 ); + + if ( $patron ) { + # check borrower has not reached open recalls allowed limit + return 0 if ( $patron->recalls->count >= $rule->{recalls_allowed} ); + + # check borrower has not reach open recalls allowed per record limit + return 0 if ( $patron->recalls({ biblionumber => $self->biblionumber })->count >= $rule->{recalls_per_record} ); + + # check if this patron has already recalled this item + return 0 if ( Koha::Recalls->search({ itemnumber => $self->itemnumber, borrowernumber => $patron->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 => $patron->borrowernumber })->count > 0 ); + + # check if this patron has already reserved this item + return 0 if ( Koha::Holds->search({ itemnumber => $self->itemnumber, borrowernumber => $patron->borrowernumber })->count > 0 ); + } + + # 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} eq 'all' && $checked_out_count < scalar @items ); + + # can't recall if no items have been checked out + return 0 if ( $checked_out_count == 0 ); + + # can recall + return 1; +} + +=head3 can_be_waiting_recall + + if ( $item->can_be_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_be_waiting_recall { + my ( $self ) = @_; + + return 0 if !( C4::Context->preference('UseRecalls') ); + + # 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 ); + + 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 ( !defined($rule->{recalls_allowed}) || $rule->{recalls_allowed} == 0 ); + + # can recall + return 1; +} + +=head3 check_recalls + + my $recall = $item->check_recalls; + +Get the most relevant recall for this item. + +=cut + +sub check_recalls { + my ( $self ) = @_; + + my @recalls = Koha::Recalls->search({ itemnumber => $self->itemnumber, status => [ 'R','O','W' ] }, { order_by => { -asc => 'recalldate' } }); + if ( scalar @recalls == 0 ) { + # no item-level recalls or recalls waiting for this specific item + # look for biblio-level recalls + @recalls = Koha::Recalls->search({ biblionumber => $self->biblionumber, itemnumber => [ $self->itemnumber, undef ], status => [ 'R','O','W' ] }, { order_by => { -asc => 'recalldate' } }); + } + + my $recall; + # iterate through relevant recalls to find the best one. + # if we come across a waiting recall, use this one. + # if we have iterated through all recalls and not found a waiting recall, use the first recall in the array, which should be the oldest recall. + foreach my $r ( @recalls ) { + if ( $r->waiting ) { + $recall = $r; + last; + } + } + unless ( defined $recall ) { + $recall = @recalls[0]; + } + + return $recall; +} + =head3 _type =cut diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 5436ccc4485..6e2db596545 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1712,6 +1712,30 @@ 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}; + + my $recalls_rs = Koha::Recalls->search({ borrowernumber => $self->borrowernumber, old => undef }); + + if ( $biblionumber ) { + $recalls_rs = Koha::Recalls->search({ borrowernumber => $self->borrowernumber, old => undef, biblionumber => $biblionumber }); + } + + return $recalls_rs; +} + =head2 Internal methods =head3 _type diff --git a/Koha/Template/Plugin/Biblio.pm b/Koha/Template/Plugin/Biblio.pm index e9d7a730098..a4e6aab3417 100644 --- a/Koha/Template/Plugin/Biblio.pm +++ b/Koha/Template/Plugin/Biblio.pm @@ -27,6 +27,7 @@ use Koha::Biblios; use Koha::Patrons; use Koha::ArticleRequests; use Koha::ArticleRequest::Status; +use Koha::Recalls; sub HoldsCount { my ( $self, $biblionumber ) = @_; @@ -63,4 +64,12 @@ sub CanArticleRequest { return $biblio ? $biblio->can_article_request( $borrower ) : 0; } +sub RecallsCount { + my ( $self, $biblionumber ) = @_; + + my $recalls = Koha::Recalls->search({ biblionumber => $biblionumber, old => undef }); + + return $recalls->count; +} + 1; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 790b623aade..42c918a2389 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,119 @@ subtest 'subscriptions() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'Recalls tests' => sub { + + plan tests => 12; + + $schema->storage->txn_begin; + my $item1 = $builder->build_sample_item; + my $biblio = $item1->biblio; + my $branchcode = $item1->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 $item2 = $builder->build_object({ class => 'Koha::Items', value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype } }); + t::lib::Mocks::mock_userenv({ patron => $patron1 }); + + my $recall1 = Koha::Recall->new({ + borrowernumber => $patron1->borrowernumber, + recalldate => Koha::DateUtils::dt_from_string, + biblionumber => $biblio->biblionumber, + branchcode => $branchcode, + status => 'R', + itemnumber => $item1->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 => $item1->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({ interface => 'COMMANDLINE' }); + + $recalls_count = $biblio->recalls->count; + is( $recalls_count, 1, 'Correctly get number of active recalls for biblio' ); + + t::lib::Mocks::mock_preference('UseRecalls', 0); + is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" ); + + t::lib::Mocks::mock_preference("UseRecalls", 1); + $item1->update({ notforloan => 1 }); + is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with no available items" ); + + $item1->update({ notforloan => 0 }); + Koha::CirculationRules->set_rules({ + branchcode => $branchcode, + categorycode => $patron1->categorycode, + itemtype => $item1->effective_itemtype, + rules => { + recalls_allowed => 0, + recalls_per_record => 1, + on_shelf_recalls => 'all', + }, + }); + is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" ); + + Koha::CirculationRules->set_rules({ + branchcode => $branchcode, + categorycode => $patron1->categorycode, + itemtype => $item1->effective_itemtype, + rules => { + recalls_allowed => 1, + recalls_per_record => 1, + on_shelf_recalls => 'all', + }, + }); + is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" ); + is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" ); + + $recall1->set_cancelled; + C4::Circulation::AddIssue( $patron1->unblessed, $item2->barcode ); + is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" ); + + is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" ); + + Koha::CirculationRules->set_rules({ + branchcode => $branchcode, + categorycode => $patron1->categorycode, + itemtype => $item1->effective_itemtype, + rules => { + recalls_allowed => 1, + recalls_per_record => 1, + on_shelf_recalls => 'any', + }, + }); + C4::Circulation::AddReturn( $item2->barcode, $branchcode ); + is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" ); + + $recall2->set_cancelled; + C4::Circulation::AddIssue( $patron2->unblessed, $item2->barcode ); + is( $biblio->can_be_recalled({ patron => $patron1 }), 2, "Can recall two items" ); + + $item1->update({ withdrawn => 1 }); + is( $biblio->can_be_recalled({ patron => $patron1 }), 1, "Can recall one item" ); + + $schema->storage->txn_rollback; +}; diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index 55709643b78..6461364b4a2 100644 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 7; use C4::Biblio; use C4::Circulation; @@ -493,3 +493,148 @@ subtest 'renewal_branchcode' => sub { is( $item->renewal_branchcode({branch=>'MANATEE'}), $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item even if branch passed"); }; + +subtest 'Recalls tests' => sub { + + plan tests => 17; + + $schema->storage->txn_begin; + + my $item1 = $builder->build_sample_item; + my $biblio = $item1->biblio; + my $branchcode = $item1->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 $item2 = $builder->build_object({ class => 'Koha::Items', value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype } }); + t::lib::Mocks::mock_userenv({ patron => $patron1 }); + + my $recall1 = Koha::Recall->new({ + borrowernumber => $patron1->borrowernumber, + recalldate => Koha::DateUtils::dt_from_string, + biblionumber => $biblio->biblionumber, + branchcode => $branchcode, + status => 'R', + itemnumber => $item1->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 => $item1->itemnumber, + expirationdate => undef, + item_level_recall =>1 + })->store; + + is( $item1->recall->borrowernumber, $patron1->borrowernumber, 'Correctly returns most relevant recall' ); + + $recall2->set_cancelled; + + t::lib::Mocks::mock_preference('UseRecalls', 0); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" ); + + t::lib::Mocks::mock_preference("UseRecalls", 1); + + $item1->update({ notforloan => 1 }); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" ); + $item1->update({ notforloan => 0, itemlost => 1 }); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" ); + $item1->update({ itemlost => 0, withdrawn => 1 }); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" ); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" ); + + $item1->update({ withdrawn => 0 }); + C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode ); + + Koha::CirculationRules->set_rules({ + branchcode => $branchcode, + categorycode => $patron1->categorycode, + itemtype => $item1->effective_itemtype, + rules => { + recalls_allowed => 0, + recalls_per_record => 1, + on_shelf_recalls => 'all', + }, + }); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" ); + + Koha::CirculationRules->set_rules({ + branchcode => $branchcode, + categorycode => $patron1->categorycode, + itemtype => $item1->effective_itemtype, + rules => { + recalls_allowed => 1, + recalls_per_record => 1, + on_shelf_recalls => 'all', + }, + }); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" ); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" ); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" ); + + my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber }); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" ); + C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber }); + + $recall1->set_cancelled; + is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" ); + + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" ); + + Koha::CirculationRules->set_rules({ + branchcode => $branchcode, + categorycode => $patron1->categorycode, + itemtype => $item1->effective_itemtype, + rules => { + recalls_allowed => 1, + recalls_per_record => 1, + on_shelf_recalls => 'any', + }, + }); + C4::Circulation::AddReturn( $item1->barcode, $branchcode ); + is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" ); + + C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode ); + is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" ); + + $recall1 = Koha::Recall->new({ + borrowernumber => $patron1->borrowernumber, + recalldate => Koha::DateUtils::dt_from_string, + biblionumber => $biblio->biblionumber, + branchcode => $branchcode, + status => 'R', + itemnumber => undef, + expirationdate => undef, + item_level_recall => 0 + })->store; + + # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall. + + Koha::CirculationRules->set_rules({ + branchcode => undef, + categorycode => undef, + itemtype => $item1->effective_itemtype, + rules => { + recalls_allowed => 0, + recalls_per_record => 1, + on_shelf_recalls => 'any', + }, + }); + is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" ); + + Koha::CirculationRules->set_rules({ + branchcode => undef, + categorycode => undef, + itemtype => $item1->effective_itemtype, + rules => { + recalls_allowed => 1, + recalls_per_record => 1, + on_shelf_recalls => 'any', + }, + }); + is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" ); +}; -- 2.11.0