From f668fecec3771b633068f89dcead2312471f9425 Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Fri, 12 Apr 2019 10:32:48 +0000 Subject: [PATCH] Bug 22284: Opac pickup_locations This patch modifies Koha::Libraries->pickup_location and moves most of the logic to Koha::Item and Koha::Biblio in preparation for api endpoints in the future. There where 2 methods added 1) Koha::Item->pickup_locations that given a patron, returns all pickup locations of this item, considering hold fulfillment rules, and hold allowed rules. 2) Koha::Biblio->pickup_locations that given a patron, returns a distinct list of libraries returned by each of this biblio items pickup location. Koha::Libraries->pickup_location analyzes input param and calls Koha::Item->pickup_locations or Koha::Biblio->pickup_locations as needed. Also in opac-reserve.tt the way options where obtained to fill the pickup location select was modified to pass the patron as a parameter. To test: 1) opac: try to place hold on a item and check that all libraries are shown in the pickup location select. 2) intranet: in Library groups, add 2 root groups marked as local hold group and add different libraries to each. 3) opac: login as a user of a library belonging to one hold group, and search try to place a hold on an item belongin to the other hold group. 4) intranet: in Circulation and fines rules, play with 'Hold policy' and 'Hold pickup library match' rules. 5) opac: On each modification of the rules reload the page. SUCCESS => Every time you reload the page, the number of pickup locations showed in select varies. 6) prove t/db_dependent/Koha/Biblios.t t/db_dependent/Koha/Items.t SUCCESS => Result: PASS 7) Sign off Sponsored-by: VOKAL Signed-off-by: Josef Moravec Signed-off-by: Liz Rea Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize --- Koha/Biblio.pm | 27 ++ Koha/Item.pm | 55 ++- Koha/Libraries.pm | 24 +- .../bootstrap/en/modules/opac-reserve.tt | 4 +- t/db_dependent/Koha/Biblios.t | 342 ++++++++++++++++++ t/db_dependent/Koha/Items.t | 313 ++++++++++++++++ 6 files changed, 746 insertions(+), 19 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index b5ffe81642..8fdc2531be 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -169,6 +169,33 @@ sub can_be_transferred { return 0; } + +=head3 pickup_locations + +@pickup_locations = $biblio->pickup_locations( {patron => $patron } ) + +Returns possible pickup locations for this biblio items, according to patron's home library (if patron is defined and holds are allowed only from hold groups) +and if item can be transfered to each pickup location. + +=cut + +sub pickup_locations { + my ($self, $params) = @_; + + my $patron = $params->{patron}; + + my @pickup_locations; + foreach my $item_of_bib ($self->items) { + push @pickup_locations, $item_of_bib->pickup_locations( {patron => $patron} ); + } + + my %seen; + @pickup_locations = + grep { !$seen{ $_->{branchcode} }++ } @pickup_locations; + + return wantarray ? @pickup_locations : \@pickup_locations; +} + =head3 hidden_in_opac my $bool = $biblio->hidden_in_opac({ [ rules => $rules ] }) diff --git a/Koha/Item.pm b/Koha/Item.pm index b557c95abb..090826de31 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -26,7 +26,7 @@ use Koha::Database; use Koha::DateUtils qw( dt_from_string ); use C4::Context; - +use C4::Circulation; use Koha::Checkouts; use Koha::IssuingRules; use Koha::Item::Transfer::Limits; @@ -294,6 +294,59 @@ sub can_be_transferred { })->count ? 0 : 1; } +=head3 pickup_locations + +@pickup_locations = $item->pickup_locations( {patron => $patron } ) + +Returns possible pickup locations for this item, according to patron's home library (if patron is defined and holds are allowed only from hold groups) +and if item can be transfered to each pickup location. + +=cut + +sub pickup_locations { + my ($self, $params) = @_; + + my $patron = $params->{patron}; + + my $circ_control_branch = + C4::Circulation::_GetCircControlBranch( $self->unblessed(), $patron ); + my $branchitemrule = + C4::Circulation::GetBranchItemRule( $circ_control_branch, $self->itype ); + + my $branch_control = C4::Context->preference('HomeOrHoldingBranch'); + my $library = $branch_control eq 'holdingbranch' ? $self->holding_branch : $self->home_branch; + + #warn $branch_control.' '.$branchitemrule->{holdallowed}.' '.$library->branchcode.' '.$patron->branchcode; + + my @libs; + if(defined $patron) { + return @libs if $branchitemrule->{holdallowed} == 3 && !$library->validate_hold_sibling( {branchcode => $patron->branchcode} ); + return @libs if $branchitemrule->{holdallowed} == 1 && $library->branchcode ne $patron->branchcode; + } + + if ($branchitemrule->{hold_fulfillment_policy} eq 'holdgroup') { + @libs = $library->get_hold_libraries; + } elsif ($branchitemrule->{hold_fulfillment_policy} eq 'homebranch') { + push @libs, $self->home_branch; + } elsif ($branchitemrule->{hold_fulfillment_policy} eq 'holdingbranch') { + push @libs, $self->holding_branch; + } else { + @libs = Koha::Libraries->search({ + pickup_location => 1 + }, { + order_by => ['branchname'] + })->as_list; + } + + my @pickup_locations; + foreach my $library (@libs) { + if ($library->pickup_location && $self->can_be_transferred({ to => $library })) { + push @pickup_locations, $library->unblessed; + } + } + return wantarray ? @pickup_locations : \@pickup_locations; +} + =head3 article_request_type my $type = $item->article_request_type( $borrower ) diff --git a/Koha/Libraries.pm b/Koha/Libraries.pm index 56eaf4feab..bc92f0bb10 100644 --- a/Koha/Libraries.pm +++ b/Koha/Libraries.pm @@ -66,12 +66,17 @@ sub pickup_locations { my $item = $params->{'item'}; my $biblio = $params->{'biblio'}; + my $patron = $params->{'patron'}; + if ($biblio && $item) { Koha::Exceptions::BadParameter->throw( error => "Koha::Libraries->pickup_locations takes either 'biblio' or " ." 'item' as parameter, but not both." ); } + unless (! defined $patron || ref($patron) eq 'Koha::Patron') { + $patron = Koha::Items->find($patron); + } # Select libraries that are configured as pickup locations my $libraries = $self->search({ @@ -81,32 +86,19 @@ sub pickup_locations { }); return $libraries->unblessed unless $item or $biblio; - return $libraries->unblessed - unless C4::Context->preference('UseBranchTransferLimits'); - my $limittype = C4::Context->preference('BranchTransferLimitsType'); - - if ($item) { + if($item) { unless (ref($item) eq 'Koha::Item') { $item = Koha::Items->find($item); return $libraries->unblessed unless $item; } + return $item->pickup_locations( {patron => $patron} ); } else { unless (ref($biblio) eq 'Koha::Biblio') { $biblio = Koha::Biblios->find($biblio); return $libraries->unblessed unless $biblio; } + return $biblio->pickup_locations( {patron => $patron} ); } - - my @pickup_locations; - foreach my $library ($libraries->as_list) { - if ($item && $item->can_be_transferred({ to => $library })) { - push @pickup_locations, $library->unblessed; - } elsif ($biblio && $biblio->can_be_transferred({ to => $library })) { - push @pickup_locations, $library->unblessed; - } - } - - return wantarray ? @pickup_locations : \@pickup_locations; } =head3 search_filtered diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-reserve.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-reserve.tt index 17fca3c1e2..6932d49f90 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-reserve.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-reserve.tt @@ -228,12 +228,12 @@ [% UNLESS ( bibitemloo.holdable ) %] [% ELSE %] [% SET at_least_one_library_not_available_for_pickup = 0 %]