Skip to content

Commit 8444e2a

Browse files
committed
Add support for OPACHoldsIfAvailableAtPickup and OPACHoldsIfAvailableAtPickupExceptions
1 parent df3625c commit 8444e2a

4 files changed

Lines changed: 89 additions & 17 deletions

File tree

Koha/Plugin/Fi/KohaSuomi/DI/Koha/Availability/Checks/Item.pm

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,16 +364,20 @@ sub onloan {
364364
365365
Gets list of available pickup locations for item hold.
366366
367+
$context_cache is a hash ref used to store data between calls to avoid repeated checks.
368+
367369
Returns Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item::PickupLocations.
368370
369371
=cut
370372

371373
sub pickup_locations {
372-
my ($self) = @_;
374+
my ($self, $patron, $context_cache) = @_;
373375

374376
my $pickup_libraries = Koha::Libraries->search({
375377
pickup_location => 1 })->unblessed;
376378
my $pickup_locations = [];
379+
my $filtered = Mojo::JSON->false;
380+
377381
if (C4::Context->preference('UseBranchTransferLimits')) {
378382
my $limit_type = C4::Context->preference('BranchTransferLimitsType');
379383
my $limits = Koha::Item::Transfer::Limits->search({
@@ -382,20 +386,29 @@ sub pickup_locations {
382386
})->unblessed;
383387

384388
foreach my $library (@$pickup_libraries) {
385-
if (!grep { $library->{branchcode} eq $_->{toBranch} } @$limits) {
389+
if (!grep { $library->{branchcode} eq $_->{toBranch} } @$limits
390+
&& $self->_pickup_location_allowed($library->{branchcode}, $patron, $context_cache)
391+
) {
386392
push @{$pickup_locations}, $library->{branchcode};
393+
} else {
394+
$filtered = Mojo::JSON->true;
387395
}
388396
}
389397
} else {
390398
foreach my $library (@$pickup_libraries) {
391-
push @{$pickup_locations}, $library->{branchcode};
399+
if ($self->_pickup_location_allowed($library->{branchcode}, $patron, $context_cache)) {
400+
push @{$pickup_locations}, $library->{branchcode};
401+
} else {
402+
$filtered = Mojo::JSON->true;
403+
}
392404
}
393405
}
394406

395407
@$pickup_locations = sort { $a cmp $b } @$pickup_locations;
396408
return Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item::PickupLocations->new(
397409
from_library => $self->item->holdingbranch,
398410
to_libraries => $pickup_locations,
411+
filtered => $filtered
399412
);
400413
}
401414

@@ -519,4 +532,45 @@ sub withdrawn {
519532
return;
520533
}
521534

535+
=head3 _pickup_library_allowed
536+
537+
if ($self->_pickup_location_allowed($location, $patron, $context_cache))...
538+
539+
Checks if pickup location is allowed. $context_cache is a hash ref used to store data between calls to avoid repeated checks.
540+
541+
=cut
542+
543+
sub _pickup_location_allowed {
544+
my ($self, $location, $patron, $context_cache) = @_;
545+
546+
if (!defined $context_cache->{can_place_hold_if_available_at_pickup}) {
547+
my $can_place = C4::Context->preference('OPACHoldsIfAvailableAtPickup');
548+
unless ($can_place || !$patron) {
549+
my @patron_categories = split ',', C4::Context->preference('OPACHoldsIfAvailableAtPickupExceptions');
550+
if (@patron_categories) {
551+
my $categorycode = $patron->categorycode;
552+
$can_place = grep { $_ eq $categorycode } @patron_categories;
553+
}
554+
}
555+
$context_cache->{can_place_hold_if_available_at_pickup} = $can_place;
556+
}
557+
return 1 if $context_cache->{can_place_hold_if_available_at_pickup};
558+
559+
# Filter out pickup locations with available items
560+
if (!defined $context_cache->{items_available_by_location}->{$location}) {
561+
$context_cache->{items_available_by_location}->{$location} = Koha::Items->search(
562+
{
563+
'me.biblionumber' => $self->item->biblionumber,
564+
'me.holdingbranch' => $location,
565+
'me.itemlost' => 0,
566+
'me.damaged' => 0,
567+
'issue.itemnumber' => undef
568+
},
569+
{ join => 'issue' }
570+
)->count;
571+
}
572+
return $context_cache->{items_available_by_location}->{$location} ? 0 : 1;
573+
}
574+
575+
522576
1;

Koha/Plugin/Fi/KohaSuomi/DI/Koha/Biblio/Availability/Hold.pm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ sub new {
8787
# in parameter 'limit'.
8888
$self->{'limit'} = $params->{'limit'};
8989

90+
$self->{'context_cache'} = {};
91+
9092
return $self;
9193
}
9294

@@ -211,7 +213,7 @@ sub _item_looper {
211213
}
212214

213215
sub _item_check {
214-
my ($self, $item, $patron, $holds, $nonfound_holds) = @_;
216+
my ($self, $item, $patron, $holds, $nonfound_holds, $context_cache) = @_;
215217

216218
my $item_availability = Koha::Plugin::Fi::KohaSuomi::DI::Koha::Item::Availability::Hold->new({
217219
item => $item,
@@ -226,7 +228,7 @@ sub _item_check {
226228
use_cache => 1,
227229
nonfound_holds => $nonfound_holds
228230
});
229-
$item_availability->common_item_checks({ holds => $holds });
231+
$item_availability->common_item_checks({ holds => $holds, context_cache => $self->{'context_cache'} });
230232
$item_availability->common_library_item_rule_checks;
231233
$item_availability->opac_specific_issuing_rule_checks;
232234
return $item_availability;

Koha/Plugin/Fi/KohaSuomi/DI/Koha/Exceptions/Item.pm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ use Exception::Class (
7373
'Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item::PickupLocations' => {
7474
isa => 'Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item',
7575
description => "Item can only be transferred to following libraries",
76-
fields => ["from_library", "to_libraries"],
76+
fields => ["from_library", "to_libraries", "filtered"],
77+
},
78+
'Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item::NoPickUpLocations' => {
79+
isa => 'Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item',
80+
description => "Item does not have valid pick up locations."
7781
},
7882
'Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item::UnknownBarcode' => {
7983
isa => 'Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item',

Koha/Plugin/Fi/KohaSuomi/DI/Koha/Item/Availability/Hold.pm

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ sub in_opac {
111111
$self->common_biblio_checks;
112112
$self->common_biblioitem_checks;
113113
$self->common_issuing_rule_checks;
114-
$self->common_item_checks;
114+
my $cache = {};
115+
$self->common_item_checks({'context_cache' => $cache});
115116
$self->common_library_item_rule_checks;
116117
$self->common_patron_checks;
117118
$self->opac_specific_issuing_rule_checks;
@@ -237,20 +238,31 @@ sub common_item_checks {
237238
}
238239

239240
$self->unavailable($reason) if $reason = $itemcalc->from_another_library;
240-
if ($self->{'query_pickup_locations'} && ($reason = $itemcalc->pickup_locations)) {
241-
if (@{$reason->to_libraries} == 0) {
242-
$self->unavailable(Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item::CannotBeTransferred->new(
243-
from_library => $item->holdingbranch
244-
));
245-
}
246-
$self->note($reason);
247-
}
248241
if ($self->to_branch && ($reason = $itemcalc->transfer_limit($self->to_branch))) {
249242
$self->unavailable($reason);
250243
}
244+
if ($self->patron) {
245+
$self->unavailable($reason) if $reason = $itemcalc->held_by_patron($patron, $params);
246+
}
251247

252-
return $self unless $self->patron;
253-
$self->unavailable($reason) if $reason = $itemcalc->held_by_patron($patron, $params);
248+
if ($self->{'query_pickup_locations'}) {
249+
die('Context cache not provided') unless defined $params->{context_cache};
250+
if ($reason = $itemcalc->pickup_locations($patron, $params->{context_cache})) {
251+
if (@{$reason->to_libraries} == 0) {
252+
if (@{$reason->filtered}) {
253+
$self->unavailable(Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item::NoPickUpLocations->new(
254+
from_library => $item->holdingbranch
255+
));
256+
} else {
257+
$self->unavailable(Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Item::CannotBeTransferred->new(
258+
from_library => $item->holdingbranch
259+
));
260+
}
261+
}
262+
263+
$self->note($reason);
264+
}
265+
}
254266

255267
return $self;
256268
}

0 commit comments

Comments
 (0)