1 # ---------------------------------------------------------------
2 # Copyright (C) 2005 Georgia Public Library Service
3 # Bill Erickson <highfalutin@gmail.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
17 package OpenILS::Application::Circ::Holds;
18 use base qw/OpenILS::Application/;
19 use strict; use warnings;
20 use OpenILS::Application::AppUtils;
23 use OpenSRF::EX qw(:try);
27 use OpenSRF::Utils::Logger qw(:logger);
28 use OpenILS::Utils::CStoreEditor q/:funcs/;
29 use OpenILS::Utils::PermitHold;
30 use OpenSRF::Utils::SettingsClient;
31 use OpenILS::Const qw/:const/;
32 use OpenILS::Application::Circ::Transit;
33 use OpenILS::Application::Actor::Friends;
35 use DateTime::Format::ISO8601;
36 use OpenSRF::Utils qw/:datetime/;
37 use Digest::MD5 qw(md5_hex);
38 use OpenSRF::Utils::Cache;
39 my $apputils = "OpenILS::Application::AppUtils";
42 __PACKAGE__->register_method(
43 method => "test_and_create_hold_batch",
44 api_name => "open-ils.circ.holds.test_and_create.batch",
47 desc => q/This is for batch creating a set of holds where every field is identical except for the targets./,
49 { desc => 'Authentication token', type => 'string' },
50 { desc => 'Hash of named parameters. Same as for open-ils.circ.title_hold.is_possible, though the pertinent target field is automatically populated based on the hold_type and the specified list of targets.', type => 'object'},
51 { desc => 'Array of target ids', type => 'array' }
54 desc => 'Array of hold ID on success, -1 on missing arg, event (or ref to array of events) on error(s)',
59 __PACKAGE__->register_method(
60 method => "test_and_create_hold_batch",
61 api_name => "open-ils.circ.holds.test_and_create.batch.override",
64 desc => '@see open-ils.circ.holds.test_and_create.batch',
69 sub test_and_create_hold_batch {
70 my( $self, $conn, $auth, $params, $target_list, $oargs ) = @_;
73 if ($self->api_name =~ /override/) {
75 $oargs = { all => 1 } unless defined $oargs;
76 $$params{oargs} = $oargs; # for is_possible checking.
79 my $e = new_editor(authtoken=>$auth);
80 return $e->die_event unless $e->checkauth;
81 $$params{'requestor'} = $e->requestor->id;
84 if ($$params{'hold_type'} eq 'T') { $target_field = 'titleid'; }
85 elsif ($$params{'hold_type'} eq 'C') { $target_field = 'copy_id'; }
86 elsif ($$params{'hold_type'} eq 'R') { $target_field = 'copy_id'; }
87 elsif ($$params{'hold_type'} eq 'F') { $target_field = 'copy_id'; }
88 elsif ($$params{'hold_type'} eq 'I') { $target_field = 'issuanceid'; }
89 elsif ($$params{'hold_type'} eq 'V') { $target_field = 'volume_id'; }
90 elsif ($$params{'hold_type'} eq 'M') { $target_field = 'mrid'; }
91 elsif ($$params{'hold_type'} eq 'P') { $target_field = 'partid'; }
92 else { return undef; }
94 foreach (@$target_list) {
95 $$params{$target_field} = $_;
97 ($res) = $self->method_lookup(
98 'open-ils.circ.title_hold.is_possible')->run($auth, $params, $override ? $oargs : {});
99 if ($res->{'success'} == 1) {
101 $params->{'depth'} = $res->{'depth'} if $res->{'depth'};
103 # Remove oargs from params so holds can be created.
104 if ($$params{oargs}) {
105 delete $$params{oargs};
108 my $ahr = construct_hold_request_object($params);
109 my ($res2) = $self->method_lookup(
111 ? 'open-ils.circ.holds.create.override'
112 : 'open-ils.circ.holds.create'
113 )->run($auth, $ahr, $oargs);
115 'target' => $$params{$target_field},
118 $conn->respond($res2);
121 'target' => $$params{$target_field},
124 $conn->respond($res);
130 sub construct_hold_request_object {
133 my $ahr = Fieldmapper::action::hold_request->new;
136 foreach my $field (keys %{ $params }) {
137 if ($field eq 'depth') { $ahr->selection_depth($$params{$field}); }
138 elsif ($field eq 'patronid') {
139 $ahr->usr($$params{$field}); }
140 elsif ($field eq 'titleid') { $ahr->target($$params{$field}); }
141 elsif ($field eq 'copy_id') { $ahr->target($$params{$field}); }
142 elsif ($field eq 'issuanceid') { $ahr->target($$params{$field}); }
143 elsif ($field eq 'volume_id') { $ahr->target($$params{$field}); }
144 elsif ($field eq 'mrid') { $ahr->target($$params{$field}); }
145 elsif ($field eq 'partid') { $ahr->target($$params{$field}); }
147 $ahr->$field($$params{$field});
153 __PACKAGE__->register_method(
154 method => "create_hold_batch",
155 api_name => "open-ils.circ.holds.create.batch",
158 desc => q/@see open-ils.circ.holds.create.batch/,
160 { desc => 'Authentication token', type => 'string' },
161 { desc => 'Array of hold objects', type => 'array' }
164 desc => 'Array of hold ID on success, -1 on missing arg, event (or ref to array of events) on error(s)',
169 __PACKAGE__->register_method(
170 method => "create_hold_batch",
171 api_name => "open-ils.circ.holds.create.override.batch",
174 desc => '@see open-ils.circ.holds.create.batch',
179 sub create_hold_batch {
180 my( $self, $conn, $auth, $hold_list, $oargs ) = @_;
181 (my $method = $self->api_name) =~ s/\.batch//og;
182 foreach (@$hold_list) {
183 my ($res) = $self->method_lookup($method)->run($auth, $_, $oargs);
184 $conn->respond($res);
190 __PACKAGE__->register_method(
191 method => "create_hold",
192 api_name => "open-ils.circ.holds.create",
194 desc => "Create a new hold for an item. From a permissions perspective, " .
195 "the login session is used as the 'requestor' of the hold. " .
196 "The hold recipient is determined by the 'usr' setting within the hold object. " .
197 'First we verify the requestor has holds request permissions. ' .
198 'Then we verify that the recipient is allowed to make the given hold. ' .
199 'If not, we see if the requestor has "override" capabilities. If not, ' .
200 'a permission exception is returned. If permissions allow, we cycle ' .
201 'through the set of holds objects and create. ' .
202 'If the recipient does not have permission to place multiple holds ' .
203 'on a single title and said operation is attempted, a permission ' .
204 'exception is returned',
206 { desc => 'Authentication token', type => 'string' },
207 { desc => 'Hold object for hold to be created',
208 type => 'object', class => 'ahr' }
211 desc => 'New ahr ID on success, -1 on missing arg, event (or ref to array of events) on error(s)',
216 __PACKAGE__->register_method(
217 method => "create_hold",
218 api_name => "open-ils.circ.holds.create.override",
219 notes => '@see open-ils.circ.holds.create',
221 desc => "If the recipient is not allowed to receive the requested hold, " .
222 "call this method to attempt the override",
224 { desc => 'Authentication token', type => 'string' },
226 desc => 'Hold object for hold to be created',
227 type => 'object', class => 'ahr'
231 desc => 'New hold (ahr) ID on success, -1 on missing arg, event (or ref to array of events) on error(s)',
237 my( $self, $conn, $auth, $hold, $oargs ) = @_;
238 return -1 unless $hold;
239 my $e = new_editor(authtoken=>$auth, xact=>1);
240 return $e->die_event unless $e->checkauth;
243 if ($self->api_name =~ /override/) {
245 $oargs = { all => 1 } unless defined $oargs;
250 my $requestor = $e->requestor;
251 my $recipient = $requestor;
253 if( $requestor->id ne $hold->usr ) {
254 # Make sure the requestor is allowed to place holds for
255 # the recipient if they are not the same people
256 $recipient = $e->retrieve_actor_user($hold->usr) or return $e->die_event;
257 $e->allowed('REQUEST_HOLDS', $recipient->home_ou) or return $e->die_event;
260 # If the related org setting tells us to, block if patron privs have expired
261 my $expire_setting = $U->ou_ancestor_setting_value($recipient->home_ou, OILS_SETTING_BLOCK_HOLD_FOR_EXPIRED_PATRON);
262 if ($expire_setting) {
263 my $expire = DateTime::Format::ISO8601->new->parse_datetime(
264 cleanse_ISO8601($recipient->expire_date));
266 push( @events, OpenILS::Event->new(
267 'PATRON_ACCOUNT_EXPIRED',
268 "payload" => {"fail_part" => "actor.usr.privs_expired"}
269 )) if( CORE::time > $expire->epoch ) ;
272 # Now make sure the recipient is allowed to receive the specified hold
273 my $porg = $recipient->home_ou;
274 my $rid = $e->requestor->id;
275 my $t = $hold->hold_type;
277 # See if a duplicate hold already exists
279 usr => $recipient->id,
281 fulfillment_time => undef,
282 target => $hold->target,
283 cancel_time => undef,
286 $sargs->{holdable_formats} = $hold->holdable_formats if $t eq 'M';
288 my $existing = $e->search_action_hold_request($sargs);
289 push( @events, OpenILS::Event->new('HOLD_EXISTS')) if @$existing;
291 my $checked_out = hold_item_is_checked_out($e, $recipient->id, $hold->hold_type, $hold->target);
292 push( @events, OpenILS::Event->new('HOLD_ITEM_CHECKED_OUT')) if $checked_out;
294 if ( $t eq OILS_HOLD_TYPE_METARECORD ) {
295 return $e->die_event unless $e->allowed('MR_HOLDS', $porg);
296 } elsif ( $t eq OILS_HOLD_TYPE_TITLE ) {
297 return $e->die_event unless $e->allowed('TITLE_HOLDS', $porg);
298 } elsif ( $t eq OILS_HOLD_TYPE_VOLUME ) {
299 return $e->die_event unless $e->allowed('VOLUME_HOLDS', $porg);
300 } elsif ( $t eq OILS_HOLD_TYPE_MONOPART ) {
301 return $e->die_event unless $e->allowed('TITLE_HOLDS', $porg);
302 } elsif ( $t eq OILS_HOLD_TYPE_ISSUANCE ) {
303 return $e->die_event unless $e->allowed('ISSUANCE_HOLDS', $porg);
304 } elsif ( $t eq OILS_HOLD_TYPE_COPY ) {
305 return $e->die_event unless $e->allowed('COPY_HOLDS', $porg);
306 } elsif ( $t eq OILS_HOLD_TYPE_FORCE || $t eq OILS_HOLD_TYPE_RECALL ) {
307 my $copy = $e->retrieve_asset_copy($hold->target)
308 or return $e->die_event;
309 if ( $t eq OILS_HOLD_TYPE_FORCE ) {
310 return $e->die_event unless $e->allowed('COPY_HOLDS_FORCE', $copy->circ_lib);
311 } elsif ( $t eq OILS_HOLD_TYPE_RECALL ) {
312 return $e->die_event unless $e->allowed('COPY_HOLDS_RECALL', $copy->circ_lib);
321 for my $evt (@events) {
323 my $name = $evt->{textcode};
324 if($oargs->{all} || grep { $_ eq $name } @{$oargs->{events}}) {
325 return $e->die_event unless $e->allowed("$name.override", $porg);
333 # Check for hold expiration in the past, and set it to empty string.
334 $hold->expire_time(undef) if ($hold->expire_time && $U->datecmp($hold->expire_time) == -1);
336 # set the configured expire time
337 unless($hold->expire_time) {
338 $hold->expire_time(calculate_expire_time($recipient->home_ou));
341 $hold->requestor($e->requestor->id);
342 $hold->request_lib($e->requestor->ws_ou);
343 $hold->selection_ou($hold->pickup_lib) unless $hold->selection_ou;
344 $hold = $e->create_action_hold_request($hold) or return $e->die_event;
348 $conn->respond_complete($hold->id);
351 'open-ils.storage.action.hold_request.copy_targeter',
352 undef, $hold->id ) unless $U->is_true($hold->frozen);
357 # makes sure that a user has permission to place the type of requested hold
358 # returns the Perm exception if not allowed, returns undef if all is well
359 sub _check_holds_perm {
360 my($type, $user_id, $org_id) = @_;
364 $evt = $apputils->check_perms($user_id, $org_id, "MR_HOLDS" );
365 } elsif ($type eq "T") {
366 $evt = $apputils->check_perms($user_id, $org_id, "TITLE_HOLDS" );
367 } elsif($type eq "V") {
368 $evt = $apputils->check_perms($user_id, $org_id, "VOLUME_HOLDS");
369 } elsif($type eq "C") {
370 $evt = $apputils->check_perms($user_id, $org_id, "COPY_HOLDS" );
377 # tests if the given user is allowed to place holds on another's behalf
378 sub _check_request_holds_perm {
381 if (my $evt = $apputils->check_perms(
382 $user_id, $org_id, "REQUEST_HOLDS")) {
387 my $ses_is_req_note = 'The login session is the requestor. If the requestor is different from the user, ' .
388 'then the requestor must have VIEW_HOLD permissions';
390 __PACKAGE__->register_method(
391 method => "retrieve_holds_by_id",
392 api_name => "open-ils.circ.holds.retrieve_by_id",
394 desc => "Retrieve the hold, with hold transits attached, for the specified ID. $ses_is_req_note",
396 { desc => 'Authentication token', type => 'string' },
397 { desc => 'Hold ID', type => 'number' }
400 desc => 'Hold object with transits attached, event on error',
406 sub retrieve_holds_by_id {
407 my($self, $client, $auth, $hold_id) = @_;
408 my $e = new_editor(authtoken=>$auth);
409 $e->checkauth or return $e->event;
410 $e->allowed('VIEW_HOLD') or return $e->event;
412 my $holds = $e->search_action_hold_request(
414 { id => $hold_id , fulfillment_time => undef },
416 order_by => { ahr => "request_time" },
418 flesh_fields => {ahr => ['notes']}
423 flesh_hold_transits($holds);
424 flesh_hold_notices($holds, $e);
429 __PACKAGE__->register_method(
430 method => "retrieve_holds",
431 api_name => "open-ils.circ.holds.retrieve",
433 desc => "Retrieves all the holds, with hold transits attached, for the specified user. $ses_is_req_note",
435 { desc => 'Authentication token', type => 'string' },
436 { desc => 'User ID', type => 'integer' },
437 { desc => 'Available Only', type => 'boolean' }
440 desc => 'list of holds, event on error',
445 __PACKAGE__->register_method(
446 method => "retrieve_holds",
447 api_name => "open-ils.circ.holds.id_list.retrieve",
450 desc => "Retrieves all the hold IDs, for the specified user. $ses_is_req_note",
452 { desc => 'Authentication token', type => 'string' },
453 { desc => 'User ID', type => 'integer' },
454 { desc => 'Available Only', type => 'boolean' }
457 desc => 'list of holds, event on error',
462 __PACKAGE__->register_method(
463 method => "retrieve_holds",
464 api_name => "open-ils.circ.holds.canceled.retrieve",
467 desc => "Retrieves all the cancelled holds for the specified user. $ses_is_req_note",
469 { desc => 'Authentication token', type => 'string' },
470 { desc => 'User ID', type => 'integer' }
473 desc => 'list of holds, event on error',
478 __PACKAGE__->register_method(
479 method => "retrieve_holds",
480 api_name => "open-ils.circ.holds.canceled.id_list.retrieve",
483 desc => "Retrieves list of cancelled hold IDs for the specified user. $ses_is_req_note",
485 { desc => 'Authentication token', type => 'string' },
486 { desc => 'User ID', type => 'integer' }
489 desc => 'list of hold IDs, event on error',
496 my ($self, $client, $auth, $user_id, $available) = @_;
498 my $e = new_editor(authtoken=>$auth);
499 return $e->event unless $e->checkauth;
500 $user_id = $e->requestor->id unless defined $user_id;
502 my $notes_filter = {staff => 'f'};
503 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
504 unless($user_id == $e->requestor->id) {
505 if($e->allowed('VIEW_HOLD', $user->home_ou)) {
506 $notes_filter = {staff => 't'}
508 my $allowed = OpenILS::Application::Actor::Friends->friend_perm_allowed(
509 $e, $user_id, $e->requestor->id, 'hold.view');
510 return $e->event unless $allowed;
513 # staff member looking at his/her own holds can see staff and non-staff notes
514 $notes_filter = {} if $e->allowed('VIEW_HOLD', $user->home_ou);
518 select => {ahr => ['id']},
520 where => {usr => $user_id, fulfillment_time => undef}
523 if($self->api_name =~ /canceled/) {
525 # Fetch the canceled holds
526 # order cancelled holds by cancel time, most recent first
528 $holds_query->{order_by} = [{class => 'ahr', field => 'cancel_time', direction => 'desc'}];
531 my $cancel_count = $U->ou_ancestor_setting_value(
532 $e->requestor->ws_ou, 'circ.holds.canceled.display_count', $e);
534 unless($cancel_count) {
535 $cancel_age = $U->ou_ancestor_setting_value(
536 $e->requestor->ws_ou, 'circ.holds.canceled.display_age', $e);
538 # if no settings are defined, default to last 10 cancelled holds
539 $cancel_count = 10 unless $cancel_age;
542 if($cancel_count) { # limit by count
544 $holds_query->{where}->{cancel_time} = {'!=' => undef};
545 $holds_query->{limit} = $cancel_count;
547 } elsif($cancel_age) { # limit by age
549 # find all of the canceled holds that were canceled within the configured time frame
550 my $date = DateTime->now->subtract(seconds => OpenSRF::Utils::interval_to_seconds($cancel_age));
551 $date = $U->epoch2ISO8601($date->epoch);
552 $holds_query->{where}->{cancel_time} = {'>=' => $date};
557 # order non-cancelled holds by ready-for-pickup, then active, followed by suspended
558 # "compare" sorts false values to the front. testing pickup_lib != current_shelf_lib
559 # will sort by pl = csl > pl != csl > followed by csl is null;
560 $holds_query->{order_by} = [
562 field => 'pickup_lib',
563 compare => {'!=' => {'+ahr' => 'current_shelf_lib'}}},
564 {class => 'ahr', field => 'shelf_time'},
565 {class => 'ahr', field => 'frozen'},
566 {class => 'ahr', field => 'request_time'}
569 $holds_query->{where}->{cancel_time} = undef;
571 $holds_query->{where}->{shelf_time} = {'!=' => undef};
573 $holds_query->{where}->{pickup_lib} = {'=' => {'+ahr' => 'current_shelf_lib'}};
577 my $hold_ids = $e->json_query($holds_query);
578 $hold_ids = [ map { $_->{id} } @$hold_ids ];
580 return $hold_ids if $self->api_name =~ /id_list/;
583 for my $hold_id ( @$hold_ids ) {
585 my $hold = $e->retrieve_action_hold_request($hold_id);
586 $hold->notes($e->search_action_hold_request_note({hold => $hold_id, %$notes_filter}));
589 $e->search_action_hold_transit_copy([
591 {order_by => {ahtc => 'source_send_time desc'}, limit => 1}])->[0]
601 __PACKAGE__->register_method(
602 method => 'user_hold_count',
603 api_name => 'open-ils.circ.hold.user.count'
606 sub user_hold_count {
607 my ( $self, $conn, $auth, $userid ) = @_;
608 my $e = new_editor( authtoken => $auth );
609 return $e->event unless $e->checkauth;
610 my $patron = $e->retrieve_actor_user($userid)
612 return $e->event unless $e->allowed( 'VIEW_HOLD', $patron->home_ou );
613 return __user_hold_count( $self, $e, $userid );
616 sub __user_hold_count {
617 my ( $self, $e, $userid ) = @_;
618 my $holds = $e->search_action_hold_request(
621 fulfillment_time => undef,
622 cancel_time => undef,
627 return scalar(@$holds);
631 __PACKAGE__->register_method(
632 method => "retrieve_holds_by_pickup_lib",
633 api_name => "open-ils.circ.holds.retrieve_by_pickup_lib",
635 "Retrieves all the holds, with hold transits attached, for the specified pickup_ou id."
638 __PACKAGE__->register_method(
639 method => "retrieve_holds_by_pickup_lib",
640 api_name => "open-ils.circ.holds.id_list.retrieve_by_pickup_lib",
641 notes => "Retrieves all the hold ids for the specified pickup_ou id. "
644 sub retrieve_holds_by_pickup_lib {
645 my ($self, $client, $login_session, $ou_id) = @_;
647 #FIXME -- put an appropriate permission check here
648 #my( $user, $target, $evt ) = $apputils->checkses_requestor(
649 # $login_session, $user_id, 'VIEW_HOLD' );
650 #return $evt if $evt;
652 my $holds = $apputils->simplereq(
654 "open-ils.cstore.direct.action.hold_request.search.atomic",
656 pickup_lib => $ou_id ,
657 fulfillment_time => undef,
660 { order_by => { ahr => "request_time" } }
663 if ( ! $self->api_name =~ /id_list/ ) {
664 flesh_hold_transits($holds);
668 return [ map { $_->id } @$holds ];
672 __PACKAGE__->register_method(
673 method => "uncancel_hold",
674 api_name => "open-ils.circ.hold.uncancel"
678 my($self, $client, $auth, $hold_id) = @_;
679 my $e = new_editor(authtoken=>$auth, xact=>1);
680 return $e->die_event unless $e->checkauth;
682 my $hold = $e->retrieve_action_hold_request($hold_id)
683 or return $e->die_event;
684 return $e->die_event unless $e->allowed('CANCEL_HOLDS', $hold->request_lib);
686 if ($hold->fulfillment_time) {
690 unless ($hold->cancel_time) {
695 # if configured to reset the request time, also reset the expire time
696 if($U->ou_ancestor_setting_value(
697 $hold->request_lib, 'circ.holds.uncancel.reset_request_time', $e)) {
699 $hold->request_time('now');
700 $hold->expire_time(calculate_expire_time($hold->request_lib));
703 $hold->clear_cancel_time;
704 $hold->clear_cancel_cause;
705 $hold->clear_cancel_note;
706 $hold->clear_shelf_time;
707 $hold->clear_current_copy;
708 $hold->clear_capture_time;
709 $hold->clear_prev_check_time;
710 $hold->clear_shelf_expire_time;
711 $hold->clear_current_shelf_lib;
713 $e->update_action_hold_request($hold) or return $e->die_event;
716 $U->storagereq('open-ils.storage.action.hold_request.copy_targeter', undef, $hold_id);
722 __PACKAGE__->register_method(
723 method => "cancel_hold",
724 api_name => "open-ils.circ.hold.cancel",
726 desc => 'Cancels the specified hold. The login session is the requestor. If the requestor is different from the usr field ' .
727 'on the hold, the requestor must have CANCEL_HOLDS permissions. The hold may be either the hold object or the hold id',
729 {desc => 'Authentication token', type => 'string'},
730 {desc => 'Hold ID', type => 'number'},
731 {desc => 'Cause of Cancellation', type => 'string'},
732 {desc => 'Note', type => 'string'}
735 desc => '1 on success, event on error'
741 my($self, $client, $auth, $holdid, $cause, $note) = @_;
743 my $e = new_editor(authtoken=>$auth, xact=>1);
744 return $e->die_event unless $e->checkauth;
746 my $hold = $e->retrieve_action_hold_request($holdid)
747 or return $e->die_event;
749 if( $e->requestor->id ne $hold->usr ) {
750 return $e->die_event unless $e->allowed('CANCEL_HOLDS');
753 if ($hold->cancel_time) {
758 # If the hold is captured, reset the copy status
759 if( $hold->capture_time and $hold->current_copy ) {
761 my $copy = $e->retrieve_asset_copy($hold->current_copy)
762 or return $e->die_event;
764 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
765 $logger->info("canceling hold $holdid whose item is on the holds shelf");
766 # $logger->info("setting copy to status 'reshelving' on hold cancel");
767 # $copy->status(OILS_COPY_STATUS_RESHELVING);
768 # $copy->editor($e->requestor->id);
769 # $copy->edit_date('now');
770 # $e->update_asset_copy($copy) or return $e->event;
772 } elsif( $copy->status == OILS_COPY_STATUS_IN_TRANSIT ) {
775 $logger->warn("! canceling hold [$hid] that is in transit");
776 my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id},{idlist=>1})->[0];
779 my $trans = $e->retrieve_action_transit_copy($transid);
780 # Leave the transit alive, but set the copy status to
781 # reshelving so it will be properly reshelved when it gets back home
783 $trans->copy_status( OILS_COPY_STATUS_RESHELVING );
784 $e->update_action_transit_copy($trans) or return $e->die_event;
790 $hold->cancel_time('now');
791 $hold->cancel_cause($cause);
792 $hold->cancel_note($note);
793 $e->update_action_hold_request($hold)
794 or return $e->die_event;
796 delete_hold_copy_maps($self, $e, $hold->id);
800 # re-fetch the hold to pick up the real cancel_time (not "now") for A/T
802 $hold = $e->retrieve_action_hold_request($hold->id) or return $e->die_event;
805 if ($e->requestor->id == $hold->usr) {
806 $U->create_events_for_hook('hold_request.cancel.patron', $hold, $hold->pickup_lib);
808 $U->create_events_for_hook('hold_request.cancel.staff', $hold, $hold->pickup_lib);
814 sub delete_hold_copy_maps {
819 my $maps = $editor->search_action_hold_copy_map({hold=>$holdid});
821 $editor->delete_action_hold_copy_map($_)
822 or return $editor->event;
828 my $update_hold_desc = 'The login session is the requestor. ' .
829 'If the requestor is different from the usr field on the hold, ' .
830 'the requestor must have UPDATE_HOLDS permissions. ' .
831 'If supplying a hash of hold data, "id" must be included. ' .
832 'The hash is ignored if a hold object is supplied, ' .
833 'so you should supply only one kind of hold data argument.' ;
835 __PACKAGE__->register_method(
836 method => "update_hold",
837 api_name => "open-ils.circ.hold.update",
839 desc => "Updates the specified hold. $update_hold_desc",
841 {desc => 'Authentication token', type => 'string'},
842 {desc => 'Hold Object', type => 'object'},
843 {desc => 'Hash of values to be applied', type => 'object'}
846 desc => 'Hold ID on success, event on error',
852 __PACKAGE__->register_method(
853 method => "batch_update_hold",
854 api_name => "open-ils.circ.hold.update.batch",
857 desc => "Updates the specified hold(s). $update_hold_desc",
859 {desc => 'Authentication token', type => 'string'},
860 {desc => 'Array of hold obejcts', type => 'array' },
861 {desc => 'Array of hashes of values to be applied', type => 'array' }
864 desc => 'Hold ID per success, event per error',
870 my($self, $client, $auth, $hold, $values) = @_;
871 my $e = new_editor(authtoken=>$auth, xact=>1);
872 return $e->die_event unless $e->checkauth;
873 my $resp = update_hold_impl($self, $e, $hold, $values);
874 if ($U->event_code($resp)) {
878 $e->commit; # FIXME: update_hold_impl already does $e->commit ??
882 sub batch_update_hold {
883 my($self, $client, $auth, $hold_list, $values_list) = @_;
884 my $e = new_editor(authtoken=>$auth);
885 return $e->die_event unless $e->checkauth;
887 my $count = ($hold_list) ? scalar(@$hold_list) : scalar(@$values_list); # FIXME: we don't know for sure that we got $values_list. we could have neither list.
889 $values_list ||= []; # FIXME: either move this above $count declaration, or send an event if both lists undef. Probably the latter.
891 # FIXME: Failing over to [] guarantees warnings for "Use of unitialized value" in update_hold_impl call.
892 # FIXME: We should be sure we only call update_hold_impl with hold object OR hash, not both.
894 for my $idx (0..$count-1) {
896 my $resp = update_hold_impl($self, $e, $hold_list->[$idx], $values_list->[$idx]);
897 $e->xact_commit unless $U->event_code($resp);
898 $client->respond($resp);
902 return undef; # not in the register return type, assuming we should always have at least one list populated
905 sub update_hold_impl {
906 my($self, $e, $hold, $values) = @_;
908 my $need_retarget = 0;
911 $hold = $e->retrieve_action_hold_request($values->{id})
912 or return $e->die_event;
913 for my $k (keys %$values) {
914 # Outside of pickup_lib (covered by the first regex) I don't know when these would currently change.
915 # But hey, why not cover things that may happen later?
916 if ($k =~ '_(lib|ou)$' || $k eq 'target' || $k eq 'hold_type' || $k eq 'requestor' || $k eq 'selection_depth' || $k eq 'holdable_formats') {
917 if (defined $values->{$k} && defined $hold->$k() && $values->{$k} ne $hold->$k()) {
918 # Value changed? RETARGET!
920 } elsif (defined $hold->$k() != defined $values->{$k}) {
921 # Value being set or cleared? RETARGET!
925 if (defined $values->{$k}) {
926 $hold->$k($values->{$k});
928 my $f = "clear_$k"; $hold->$f();
933 my $orig_hold = $e->retrieve_action_hold_request($hold->id)
934 or return $e->die_event;
936 # don't allow the user to be changed
937 return OpenILS::Event->new('BAD_PARAMS') if $hold->usr != $orig_hold->usr;
939 if($hold->usr ne $e->requestor->id) {
940 # if the hold is for a different user, make sure the
941 # requestor has the appropriate permissions
942 my $usr = $e->retrieve_actor_user($hold->usr)
943 or return $e->die_event;
944 return $e->die_event unless $e->allowed('UPDATE_HOLD', $usr->home_ou);
948 # --------------------------------------------------------------
949 # Changing the request time is like playing God
950 # --------------------------------------------------------------
951 if($hold->request_time ne $orig_hold->request_time) {
952 return OpenILS::Event->new('BAD_PARAMS') if $hold->fulfillment_time;
953 return $e->die_event unless $e->allowed('UPDATE_HOLD_REQUEST_TIME', $hold->pickup_lib);
957 # --------------------------------------------------------------
958 # Code for making sure staff have appropriate permissons for cut_in_line
959 # This, as is, doesn't prevent a user from cutting their own holds in line
961 # --------------------------------------------------------------
962 if($U->is_true($hold->cut_in_line) ne $U->is_true($orig_hold->cut_in_line)) {
963 return $e->die_event unless $e->allowed('UPDATE_HOLD_REQUEST_TIME', $hold->pickup_lib);
967 # --------------------------------------------------------------
968 # Disallow hold suspencion if the hold is already captured.
969 # --------------------------------------------------------------
970 if ($U->is_true($hold->frozen) and not $U->is_true($orig_hold->frozen)) {
971 $hold_status = _hold_status($e, $hold);
972 if ($hold_status > 2 && $hold_status != 7) { # hold is captured
973 $logger->info("bypassing hold freeze on captured hold");
974 return OpenILS::Event->new('HOLD_SUSPEND_AFTER_CAPTURE');
979 # --------------------------------------------------------------
980 # if the hold is on the holds shelf or in transit and the pickup
981 # lib changes we need to create a new transit.
982 # --------------------------------------------------------------
983 if($orig_hold->pickup_lib ne $hold->pickup_lib) {
985 $hold_status = _hold_status($e, $hold) unless $hold_status;
987 if($hold_status == 3) { # in transit
989 return $e->die_event unless $e->allowed('UPDATE_PICKUP_LIB_FROM_TRANSIT', $orig_hold->pickup_lib);
990 return $e->die_event unless $e->allowed('UPDATE_PICKUP_LIB_FROM_TRANSIT', $hold->pickup_lib);
992 $logger->info("updating pickup lib for hold ".$hold->id." while already in transit");
994 # update the transit to reflect the new pickup location
995 my $transit = $e->search_action_hold_transit_copy(
996 {hold=>$hold->id, dest_recv_time => undef})->[0]
997 or return $e->die_event;
999 $transit->prev_dest($transit->dest); # mark the previous destination on the transit
1000 $transit->dest($hold->pickup_lib);
1001 $e->update_action_hold_transit_copy($transit) or return $e->die_event;
1003 } elsif($hold_status == 4 or $hold_status == 5 or $hold_status == 8) { # on holds shelf
1005 return $e->die_event unless $e->allowed('UPDATE_PICKUP_LIB_FROM_HOLDS_SHELF', $orig_hold->pickup_lib);
1006 return $e->die_event unless $e->allowed('UPDATE_PICKUP_LIB_FROM_HOLDS_SHELF', $hold->pickup_lib);
1008 $logger->info("updating pickup lib for hold ".$hold->id." while on holds shelf");
1010 if ($hold->pickup_lib eq $orig_hold->current_shelf_lib) {
1011 # This can happen if the pickup lib is changed while the hold is
1012 # on the shelf, then changed back to the original pickup lib.
1013 # Restore the original shelf_expire_time to prevent abuse.
1014 set_hold_shelf_expire_time(undef, $hold, $e, $hold->shelf_time);
1017 # clear to prevent premature shelf expiration
1018 $hold->clear_shelf_expire_time;
1023 if($U->is_true($hold->frozen)) {
1024 $logger->info("clearing current_copy and check_time for frozen hold ".$hold->id);
1025 $hold->clear_current_copy;
1026 $hold->clear_prev_check_time;
1027 # Clear expire_time to prevent frozen holds from expiring.
1028 $logger->info("clearing expire_time for frozen hold ".$hold->id);
1029 $hold->clear_expire_time;
1032 # If the hold_expire_time is in the past && is not equal to the
1033 # original expire_time, then reset the expire time to be in the
1035 if ($hold->expire_time && $U->datecmp($hold->expire_time) == -1 && $U->datecmp($hold->expire_time, $orig_hold->expire_time) != 0) {
1036 $hold->expire_time(calculate_expire_time($hold->request_lib));
1039 # If the hold is reactivated, reset the expire_time.
1040 if(!$U->is_true($hold->frozen) && $U->is_true($orig_hold->frozen)) {
1041 $logger->info("Reset expire_time on activated hold ".$hold->id);
1042 $hold->expire_time(calculate_expire_time($hold->request_lib));
1045 $e->update_action_hold_request($hold) or return $e->die_event;
1048 if(!$U->is_true($hold->frozen) && $U->is_true($orig_hold->frozen)) {
1049 $logger->info("Running targeter on activated hold ".$hold->id);
1050 $U->storagereq( 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
1053 # a change to mint-condition changes the set of potential copies, so retarget the hold;
1054 if($U->is_true($hold->mint_condition) and !$U->is_true($orig_hold->mint_condition)) {
1055 _reset_hold($self, $e->requestor, $hold)
1056 } elsif($need_retarget && !defined $hold->capture_time()) { # If needed, retarget the hold due to changes
1058 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
1064 # this does not update the hold in the DB. It only
1065 # sets the shelf_expire_time field on the hold object.
1066 # start_time is optional and defaults to 'now'
1067 sub set_hold_shelf_expire_time {
1068 my ($class, $hold, $editor, $start_time) = @_;
1070 my $shelf_expire = $U->ou_ancestor_setting_value(
1072 'circ.holds.default_shelf_expire_interval',
1076 return undef unless $shelf_expire;
1078 $start_time = ($start_time) ?
1079 DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($start_time)) :
1082 my $seconds = OpenSRF::Utils->interval_to_seconds($shelf_expire);
1083 my $expire_time = $start_time->add(seconds => $seconds);
1085 # if the shelf expire time overlaps with a pickup lib's
1086 # closed date, push it out to the first open date
1087 my $dateinfo = $U->storagereq(
1088 'open-ils.storage.actor.org_unit.closed_date.overlap',
1089 $hold->pickup_lib, $expire_time->strftime('%FT%T%z'));
1092 my $dt_parser = DateTime::Format::ISO8601->new;
1093 $expire_time = $dt_parser->parse_datetime(cleanse_ISO8601($dateinfo->{end}));
1095 # TODO: enable/disable time bump via setting?
1096 $expire_time->set(hour => '23', minute => '59', second => '59');
1098 $logger->info("circulator: shelf_expire_time overlaps".
1099 " with closed date, pushing expire time to $expire_time");
1102 $hold->shelf_expire_time($expire_time->strftime('%FT%T%z'));
1108 my($e, $orig_hold, $hold, $copy) = @_;
1109 my $src = $orig_hold->pickup_lib;
1110 my $dest = $hold->pickup_lib;
1112 $logger->info("putting hold into transit on pickup_lib update");
1114 my $transit = Fieldmapper::action::hold_transit_copy->new;
1115 $transit->hold($hold->id);
1116 $transit->source($src);
1117 $transit->dest($dest);
1118 $transit->target_copy($copy->id);
1119 $transit->source_send_time('now');
1120 $transit->copy_status(OILS_COPY_STATUS_ON_HOLDS_SHELF);
1122 $copy->status(OILS_COPY_STATUS_IN_TRANSIT);
1123 $copy->editor($e->requestor->id);
1124 $copy->edit_date('now');
1126 $e->create_action_hold_transit_copy($transit) or return $e->die_event;
1127 $e->update_asset_copy($copy) or return $e->die_event;
1131 # if the hold is frozen, this method ensures that the hold is not "targeted",
1132 # that is, it clears the current_copy and prev_check_time to essentiallly
1133 # reset the hold. If it is being activated, it runs the targeter in the background
1134 sub update_hold_if_frozen {
1135 my($self, $e, $hold, $orig_hold) = @_;
1136 return if $hold->capture_time;
1138 if($U->is_true($hold->frozen)) {
1139 $logger->info("clearing current_copy and check_time for frozen hold ".$hold->id);
1140 $hold->clear_current_copy;
1141 $hold->clear_prev_check_time;
1144 if($U->is_true($orig_hold->frozen)) {
1145 $logger->info("Running targeter on activated hold ".$hold->id);
1146 $U->storagereq( 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
1151 __PACKAGE__->register_method(
1152 method => "hold_note_CUD",
1153 api_name => "open-ils.circ.hold_request.note.cud",
1155 desc => 'Create, update or delete a hold request note. If the operator (from Auth. token) '
1156 . 'is not the owner of the hold, the UPDATE_HOLD permission is required',
1158 { desc => 'Authentication token', type => 'string' },
1159 { desc => 'Hold note object', type => 'object' }
1162 desc => 'Returns the note ID, event on error'
1168 my($self, $conn, $auth, $note) = @_;
1170 my $e = new_editor(authtoken => $auth, xact => 1);
1171 return $e->die_event unless $e->checkauth;
1173 my $hold = $e->retrieve_action_hold_request($note->hold)
1174 or return $e->die_event;
1176 if($hold->usr ne $e->requestor->id) {
1177 my $usr = $e->retrieve_actor_user($hold->usr);
1178 return $e->die_event unless $e->allowed('UPDATE_HOLD', $usr->home_ou);
1179 $note->staff('t') if $note->isnew;
1183 $e->create_action_hold_request_note($note) or return $e->die_event;
1184 } elsif($note->ischanged) {
1185 $e->update_action_hold_request_note($note) or return $e->die_event;
1186 } elsif($note->isdeleted) {
1187 $e->delete_action_hold_request_note($note) or return $e->die_event;
1195 __PACKAGE__->register_method(
1196 method => "retrieve_hold_status",
1197 api_name => "open-ils.circ.hold.status.retrieve",
1199 desc => 'Calculates the current status of the hold. The requestor must have ' .
1200 'VIEW_HOLD permissions if the hold is for a user other than the requestor' ,
1202 { desc => 'Hold ID', type => 'number' }
1205 # type => 'number', # event sometimes
1206 desc => <<'END_OF_DESC'
1207 Returns event on error or:
1208 -1 on error (for now),
1209 1 for 'waiting for copy to become available',
1210 2 for 'waiting for copy capture',
1213 5 for 'hold-shelf-delay'
1216 8 for 'captured, on wrong hold shelf'
1222 sub retrieve_hold_status {
1223 my($self, $client, $auth, $hold_id) = @_;
1225 my $e = new_editor(authtoken => $auth);
1226 return $e->event unless $e->checkauth;
1227 my $hold = $e->retrieve_action_hold_request($hold_id)
1228 or return $e->event;
1230 if( $e->requestor->id != $hold->usr ) {
1231 return $e->event unless $e->allowed('VIEW_HOLD');
1234 return _hold_status($e, $hold);
1240 if ($hold->cancel_time) {
1243 if ($U->is_true($hold->frozen) && !$hold->capture_time) {
1246 if ($hold->current_shelf_lib and $hold->current_shelf_lib ne $hold->pickup_lib) {
1249 return 1 unless $hold->current_copy;
1250 return 2 unless $hold->capture_time;
1252 my $copy = $hold->current_copy;
1253 unless( ref $copy ) {
1254 $copy = $e->retrieve_asset_copy($hold->current_copy)
1255 or return $e->event;
1258 return 3 if $copy->status == OILS_COPY_STATUS_IN_TRANSIT;
1260 if($copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF) {
1262 my $hs_wait_interval = $U->ou_ancestor_setting_value($hold->pickup_lib, 'circ.hold_shelf_status_delay');
1263 return 4 unless $hs_wait_interval;
1265 # if a hold_shelf_status_delay interval is defined and start_time plus
1266 # the interval is greater than now, consider the hold to be in the virtual
1267 # "on its way to the holds shelf" status. Return 5.
1269 my $transit = $e->search_action_hold_transit_copy({hold => $hold->id})->[0];
1270 my $start_time = ($transit) ? $transit->dest_recv_time : $hold->capture_time;
1271 $start_time = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($start_time));
1272 my $end_time = $start_time->add(seconds => OpenSRF::Utils::interval_to_seconds($hs_wait_interval));
1274 return 5 if $end_time > DateTime->now;
1283 __PACKAGE__->register_method(
1284 method => "retrieve_hold_queue_stats",
1285 api_name => "open-ils.circ.hold.queue_stats.retrieve",
1287 desc => 'Returns summary data about the state of a hold',
1289 { desc => 'Authentication token', type => 'string'},
1290 { desc => 'Hold ID', type => 'number'},
1293 desc => q/Summary object with keys:
1294 total_holds : total holds in queue
1295 queue_position : current queue position
1296 potential_copies : number of potential copies for this hold
1297 estimated_wait : estimated wait time in days
1298 status : hold status
1299 -1 => error or unexpected state,
1300 1 => 'waiting for copy to become available',
1301 2 => 'waiting for copy capture',
1304 5 => 'hold-shelf-delay'
1311 sub retrieve_hold_queue_stats {
1312 my($self, $conn, $auth, $hold_id) = @_;
1313 my $e = new_editor(authtoken => $auth);
1314 return $e->event unless $e->checkauth;
1315 my $hold = $e->retrieve_action_hold_request($hold_id) or return $e->event;
1316 if($e->requestor->id != $hold->usr) {
1317 return $e->event unless $e->allowed('VIEW_HOLD');
1319 return retrieve_hold_queue_status_impl($e, $hold);
1322 sub retrieve_hold_queue_status_impl {
1326 # The holds queue is defined as the distinct set of holds that share at
1327 # least one potential copy with the context hold, plus any holds that
1328 # share the same hold type and target. The latter part exists to
1329 # accomodate holds that currently have no potential copies
1330 my $q_holds = $e->json_query({
1332 # fetch cut_in_line and request_time since they're in the order_by
1333 # and we're asking for distinct values
1334 select => {ahr => ['id', 'cut_in_line', 'request_time']},
1338 select => { ahcm => ['hold'] },
1343 'field' => 'target_copy',
1344 'fkey' => 'target_copy'
1348 where => { '+ahcm2' => { hold => $hold->id } },
1355 "field" => "cut_in_line",
1356 "transform" => "coalesce",
1358 "direction" => "desc"
1360 { "class" => "ahr", "field" => "request_time" }
1365 if (!@$q_holds) { # none? maybe we don't have a map ...
1366 $q_holds = $e->json_query({
1367 select => {ahr => ['id', 'cut_in_line', 'request_time']},
1372 "field" => "cut_in_line",
1373 "transform" => "coalesce",
1375 "direction" => "desc"
1377 { "class" => "ahr", "field" => "request_time" }
1380 hold_type => $hold->hold_type,
1381 target => $hold->target,
1382 capture_time => undef,
1383 cancel_time => undef,
1385 {expire_time => undef },
1386 {expire_time => {'>' => 'now'}}
1394 for my $h (@$q_holds) {
1395 last if $h->{id} == $hold->id;
1399 my $hold_data = $e->json_query({
1401 acp => [ {column => 'id', transform => 'count', aggregate => 1, alias => 'count'} ],
1402 ccm => [ {column =>'avg_wait_time'} ]
1408 ccm => {type => 'left'}
1413 where => {'+ahcm' => {hold => $hold->id} }
1416 my $user_org = $e->json_query({select => {au => ['home_ou']}, from => 'au', where => {id => $hold->usr}})->[0]->{home_ou};
1418 my $default_wait = $U->ou_ancestor_setting_value($user_org, OILS_SETTING_HOLD_ESIMATE_WAIT_INTERVAL);
1419 my $min_wait = $U->ou_ancestor_setting_value($user_org, 'circ.holds.min_estimated_wait_interval');
1420 $min_wait = OpenSRF::Utils::interval_to_seconds($min_wait || '0 seconds');
1421 $default_wait ||= '0 seconds';
1423 # Estimated wait time is the average wait time across the set
1424 # of potential copies, divided by the number of potential copies
1425 # times the queue position.
1427 my $combined_secs = 0;
1428 my $num_potentials = 0;
1430 for my $wait_data (@$hold_data) {
1431 my $count += $wait_data->{count};
1432 $combined_secs += $count *
1433 OpenSRF::Utils::interval_to_seconds($wait_data->{avg_wait_time} || $default_wait);
1434 $num_potentials += $count;
1437 my $estimated_wait = -1;
1439 if($num_potentials) {
1440 my $avg_wait = $combined_secs / $num_potentials;
1441 $estimated_wait = $qpos * ($avg_wait / $num_potentials);
1442 $estimated_wait = $min_wait if $estimated_wait < $min_wait and $estimated_wait != -1;
1446 total_holds => scalar(@$q_holds),
1447 queue_position => $qpos,
1448 potential_copies => $num_potentials,
1449 status => _hold_status( $e, $hold ),
1450 estimated_wait => int($estimated_wait)
1455 sub fetch_open_hold_by_current_copy {
1458 my $hold = $apputils->simplereq(
1460 'open-ils.cstore.direct.action.hold_request.search.atomic',
1461 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
1462 return $hold->[0] if ref($hold);
1466 sub fetch_related_holds {
1469 return $apputils->simplereq(
1471 'open-ils.cstore.direct.action.hold_request.search.atomic',
1472 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
1476 __PACKAGE__->register_method(
1477 method => "hold_pull_list",
1478 api_name => "open-ils.circ.hold_pull_list.retrieve",
1480 desc => 'Returns (reference to) a list of holds that need to be "pulled" by a given location. ' .
1481 'The location is determined by the login session.',
1483 { desc => 'Limit (optional)', type => 'number'},
1484 { desc => 'Offset (optional)', type => 'number'},
1487 desc => 'reference to a list of holds, or event on failure',
1492 __PACKAGE__->register_method(
1493 method => "hold_pull_list",
1494 api_name => "open-ils.circ.hold_pull_list.id_list.retrieve",
1496 desc => 'Returns (reference to) a list of holds IDs that need to be "pulled" by a given location. ' .
1497 'The location is determined by the login session.',
1499 { desc => 'Limit (optional)', type => 'number'},
1500 { desc => 'Offset (optional)', type => 'number'},
1503 desc => 'reference to a list of holds, or event on failure',
1508 __PACKAGE__->register_method(
1509 method => "hold_pull_list",
1510 api_name => "open-ils.circ.hold_pull_list.retrieve.count",
1512 desc => 'Returns a count of holds that need to be "pulled" by a given location. ' .
1513 'The location is determined by the login session.',
1515 { desc => 'Limit (optional)', type => 'number'},
1516 { desc => 'Offset (optional)', type => 'number'},
1519 desc => 'Holds count (integer), or event on failure',
1526 sub hold_pull_list {
1527 my( $self, $conn, $authtoken, $limit, $offset ) = @_;
1528 my( $reqr, $evt ) = $U->checkses($authtoken);
1529 return $evt if $evt;
1531 my $org = $reqr->ws_ou || $reqr->home_ou;
1532 # the perm locaiton shouldn't really matter here since holds
1533 # will exist all over and VIEW_HOLDS should be universal
1534 $evt = $U->check_perms($reqr->id, $org, 'VIEW_HOLD');
1535 return $evt if $evt;
1537 if($self->api_name =~ /count/) {
1539 my $count = $U->storagereq(
1540 'open-ils.storage.direct.action.hold_request.pull_list.current_copy_circ_lib.status_filtered.count',
1541 $org, $limit, $offset );
1543 $logger->info("Grabbing pull list for org unit $org with $count items");
1546 } elsif( $self->api_name =~ /id_list/ ) {
1547 return $U->storagereq(
1548 'open-ils.storage.direct.action.hold_request.pull_list.id_list.current_copy_circ_lib.status_filtered.atomic',
1549 $org, $limit, $offset );
1552 return $U->storagereq(
1553 'open-ils.storage.direct.action.hold_request.pull_list.search.current_copy_circ_lib.status_filtered.atomic',
1554 $org, $limit, $offset );
1558 __PACKAGE__->register_method(
1559 method => "print_hold_pull_list",
1560 api_name => "open-ils.circ.hold_pull_list.print",
1562 desc => 'Returns an HTML-formatted holds pull list',
1564 { desc => 'Authtoken', type => 'string'},
1565 { desc => 'Org unit ID. Optional, defaults to workstation org unit', type => 'number'},
1568 desc => 'HTML string',
1574 sub print_hold_pull_list {
1575 my($self, $client, $auth, $org_id) = @_;
1577 my $e = new_editor(authtoken=>$auth);
1578 return $e->event unless $e->checkauth;
1580 $org_id = (defined $org_id) ? $org_id : $e->requestor->ws_ou;
1581 return $e->event unless $e->allowed('VIEW_HOLD', $org_id);
1583 my $hold_ids = $U->storagereq(
1584 'open-ils.storage.direct.action.hold_request.pull_list.id_list.current_copy_circ_lib.status_filtered.atomic',
1587 return undef unless @$hold_ids;
1589 $client->status(new OpenSRF::DomainObject::oilsContinueStatus);
1591 # Holds will /NOT/ be in order after this ...
1592 my $holds = $e->search_action_hold_request({id => $hold_ids}, {substream => 1});
1593 $client->status(new OpenSRF::DomainObject::oilsContinueStatus);
1595 # ... so we must resort.
1596 my $hold_map = +{map { $_->id => $_ } @$holds};
1597 my $sorted_holds = [];
1598 push @$sorted_holds, $hold_map->{$_} foreach @$hold_ids;
1600 return $U->fire_object_event(
1601 undef, "ahr.format.pull_list", $sorted_holds,
1602 $org_id, undef, undef, $client
1607 __PACKAGE__->register_method(
1608 method => "print_hold_pull_list_stream",
1610 api_name => "open-ils.circ.hold_pull_list.print.stream",
1612 desc => 'Returns a stream of fleshed holds',
1614 { desc => 'Authtoken', type => 'string'},
1615 { desc => 'Hash of optional param: Org unit ID (defaults to workstation org unit), limit, offset, sort (array of: acplo.position, prefix, call_number, suffix, request_time)',
1620 desc => 'A stream of fleshed holds',
1626 sub print_hold_pull_list_stream {
1627 my($self, $client, $auth, $params) = @_;
1629 my $e = new_editor(authtoken=>$auth);
1630 return $e->die_event unless $e->checkauth;
1632 delete($$params{org_id}) unless (int($$params{org_id}));
1633 delete($$params{limit}) unless (int($$params{limit}));
1634 delete($$params{offset}) unless (int($$params{offset}));
1635 delete($$params{chunk_size}) unless (int($$params{chunk_size}));
1636 delete($$params{chunk_size}) if ($$params{chunk_size} && $$params{chunk_size} > 50); # keep the size reasonable
1637 $$params{chunk_size} ||= 10;
1639 $$params{org_id} = (defined $$params{org_id}) ? $$params{org_id}: $e->requestor->ws_ou;
1640 return $e->die_event unless $e->allowed('VIEW_HOLD', $$params{org_id });
1643 if ($$params{sort} && @{ $$params{sort} }) {
1644 for my $s (@{ $$params{sort} }) {
1645 if ($s eq 'acplo.position') {
1647 "class" => "acplo", "field" => "position",
1648 "transform" => "coalesce", "params" => [999]
1650 } elsif ($s eq 'prefix') {
1651 push @$sort, {"class" => "acnp", "field" => "label_sortkey"};
1652 } elsif ($s eq 'call_number') {
1653 push @$sort, {"class" => "acn", "field" => "label_sortkey"};
1654 } elsif ($s eq 'suffix') {
1655 push @$sort, {"class" => "acns", "field" => "label_sortkey"};
1656 } elsif ($s eq 'request_time') {
1657 push @$sort, {"class" => "ahr", "field" => "request_time"};
1661 push @$sort, {"class" => "ahr", "field" => "request_time"};
1664 my $holds_ids = $e->json_query(
1666 "select" => {"ahr" => ["id"]},
1671 "fkey" => "current_copy",
1673 "circ_lib" => $$params{org_id}, "status" => [0,7]
1678 "fkey" => "call_number",
1692 "fkey" => "circ_lib",
1695 "location" => {"=" => {"+acp" => "location"}}
1704 "capture_time" => undef,
1705 "cancel_time" => undef,
1707 {"expire_time" => undef },
1708 {"expire_time" => {">" => "now"}}
1712 (@$sort ? (order_by => $sort) : ()),
1713 ($$params{limit} ? (limit => $$params{limit}) : ()),
1714 ($$params{offset} ? (offset => $$params{offset}) : ())
1715 }, {"substream" => 1}
1716 ) or return $e->die_event;
1718 $logger->info("about to stream back " . scalar(@$holds_ids) . " holds");
1721 for my $hid (@$holds_ids) {
1722 push @chunk, $e->retrieve_action_hold_request([
1726 "ahr" => ["usr", "current_copy"],
1728 "acp" => ["location", "call_number", "parts"],
1729 "acn" => ["record","prefix","suffix"]
1734 if (@chunk >= $$params{chunk_size}) {
1735 $client->respond( \@chunk );
1739 $client->respond_complete( \@chunk ) if (@chunk);
1746 __PACKAGE__->register_method(
1747 method => 'fetch_hold_notify',
1748 api_name => 'open-ils.circ.hold_notification.retrieve_by_hold',
1751 Returns a list of hold notification objects based on hold id.
1752 @param authtoken The loggin session key
1753 @param holdid The id of the hold whose notifications we want to retrieve
1754 @return An array of hold notification objects, event on error.
1758 sub fetch_hold_notify {
1759 my( $self, $conn, $authtoken, $holdid ) = @_;
1760 my( $requestor, $evt ) = $U->checkses($authtoken);
1761 return $evt if $evt;
1762 my ($hold, $patron);
1763 ($hold, $evt) = $U->fetch_hold($holdid);
1764 return $evt if $evt;
1765 ($patron, $evt) = $U->fetch_user($hold->usr);
1766 return $evt if $evt;
1768 $evt = $U->check_perms($requestor->id, $patron->home_ou, 'VIEW_HOLD_NOTIFICATION');
1769 return $evt if $evt;
1771 $logger->info("User ".$requestor->id." fetching hold notifications for hold $holdid");
1772 return $U->cstorereq(
1773 'open-ils.cstore.direct.action.hold_notification.search.atomic', {hold => $holdid} );
1777 __PACKAGE__->register_method(
1778 method => 'create_hold_notify',
1779 api_name => 'open-ils.circ.hold_notification.create',
1781 Creates a new hold notification object
1782 @param authtoken The login session key
1783 @param notification The hold notification object to create
1784 @return ID of the new object on success, Event on error
1788 sub create_hold_notify {
1789 my( $self, $conn, $auth, $note ) = @_;
1790 my $e = new_editor(authtoken=>$auth, xact=>1);
1791 return $e->die_event unless $e->checkauth;
1793 my $hold = $e->retrieve_action_hold_request($note->hold)
1794 or return $e->die_event;
1795 my $patron = $e->retrieve_actor_user($hold->usr)
1796 or return $e->die_event;
1798 return $e->die_event unless
1799 $e->allowed('CREATE_HOLD_NOTIFICATION', $patron->home_ou);
1801 $note->notify_staff($e->requestor->id);
1802 $e->create_action_hold_notification($note) or return $e->die_event;
1807 __PACKAGE__->register_method(
1808 method => 'create_hold_note',
1809 api_name => 'open-ils.circ.hold_note.create',
1811 Creates a new hold request note object
1812 @param authtoken The login session key
1813 @param note The hold note object to create
1814 @return ID of the new object on success, Event on error
1818 sub create_hold_note {
1819 my( $self, $conn, $auth, $note ) = @_;
1820 my $e = new_editor(authtoken=>$auth, xact=>1);
1821 return $e->die_event unless $e->checkauth;
1823 my $hold = $e->retrieve_action_hold_request($note->hold)
1824 or return $e->die_event;
1825 my $patron = $e->retrieve_actor_user($hold->usr)
1826 or return $e->die_event;
1828 return $e->die_event unless
1829 $e->allowed('UPDATE_HOLD', $patron->home_ou); # FIXME: Using permcrud perm listed in fm_IDL.xml for ahrn. Probably want something more specific
1831 $e->create_action_hold_request_note($note) or return $e->die_event;
1836 __PACKAGE__->register_method(
1837 method => 'reset_hold',
1838 api_name => 'open-ils.circ.hold.reset',
1840 Un-captures and un-targets a hold, essentially returning
1841 it to the state it was in directly after it was placed,
1842 then attempts to re-target the hold
1843 @param authtoken The login session key
1844 @param holdid The id of the hold
1850 my( $self, $conn, $auth, $holdid ) = @_;
1852 my ($hold, $evt) = $U->fetch_hold($holdid);
1853 return $evt if $evt;
1854 ($reqr, $evt) = $U->checksesperm($auth, 'UPDATE_HOLD');
1855 return $evt if $evt;
1856 $evt = _reset_hold($self, $reqr, $hold);
1857 return $evt if $evt;
1862 __PACKAGE__->register_method(
1863 method => 'reset_hold_batch',
1864 api_name => 'open-ils.circ.hold.reset.batch'
1867 sub reset_hold_batch {
1868 my($self, $conn, $auth, $hold_ids) = @_;
1870 my $e = new_editor(authtoken => $auth);
1871 return $e->event unless $e->checkauth;
1873 for my $hold_id ($hold_ids) {
1875 my $hold = $e->retrieve_action_hold_request(
1876 [$hold_id, {flesh => 1, flesh_fields => {ahr => ['usr']}}])
1877 or return $e->event;
1879 next unless $e->allowed('UPDATE_HOLD', $hold->usr->home_ou);
1880 _reset_hold($self, $e->requestor, $hold);
1888 my ($self, $reqr, $hold) = @_;
1890 my $e = new_editor(xact =>1, requestor => $reqr);
1892 $logger->info("reseting hold ".$hold->id);
1894 my $hid = $hold->id;
1896 if( $hold->capture_time and $hold->current_copy ) {
1898 my $copy = $e->retrieve_asset_copy($hold->current_copy)
1899 or return $e->die_event;
1901 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
1902 $logger->info("setting copy to status 'reshelving' on hold retarget");
1903 $copy->status(OILS_COPY_STATUS_RESHELVING);
1904 $copy->editor($e->requestor->id);
1905 $copy->edit_date('now');
1906 $e->update_asset_copy($copy) or return $e->die_event;
1908 } elsif( $copy->status == OILS_COPY_STATUS_IN_TRANSIT ) {
1910 # We don't want the copy to remain "in transit"
1911 $copy->status(OILS_COPY_STATUS_RESHELVING);
1912 $logger->warn("! reseting hold [$hid] that is in transit");
1913 my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id},{idlist=>1})->[0];
1916 my $trans = $e->retrieve_action_transit_copy($transid);
1918 $logger->info("Aborting transit [$transid] on hold [$hid] reset...");
1919 my $evt = OpenILS::Application::Circ::Transit::__abort_transit($e, $trans, $copy, 1, 1);
1920 $logger->info("Transit abort completed with result $evt");
1921 unless ("$evt" eq 1) {
1930 $hold->clear_capture_time;
1931 $hold->clear_current_copy;
1932 $hold->clear_shelf_time;
1933 $hold->clear_shelf_expire_time;
1934 $hold->clear_current_shelf_lib;
1936 $e->update_action_hold_request($hold) or return $e->die_event;
1940 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
1946 __PACKAGE__->register_method(
1947 method => 'fetch_open_title_holds',
1948 api_name => 'open-ils.circ.open_holds.retrieve',
1950 Returns a list ids of un-fulfilled holds for a given title id
1951 @param authtoken The login session key
1952 @param id the id of the item whose holds we want to retrieve
1953 @param type The hold type - M, T, I, V, C, F, R
1957 sub fetch_open_title_holds {
1958 my( $self, $conn, $auth, $id, $type, $org ) = @_;
1959 my $e = new_editor( authtoken => $auth );
1960 return $e->event unless $e->checkauth;
1963 $org ||= $e->requestor->ws_ou;
1965 # return $e->search_action_hold_request(
1966 # { target => $id, hold_type => $type, fulfillment_time => undef }, {idlist=>1});
1968 # XXX make me return IDs in the future ^--
1969 my $holds = $e->search_action_hold_request(
1972 cancel_time => undef,
1974 fulfillment_time => undef
1978 flesh_hold_transits($holds);
1983 sub flesh_hold_transits {
1985 for my $hold ( @$holds ) {
1987 $apputils->simplereq(
1989 "open-ils.cstore.direct.action.hold_transit_copy.search.atomic",
1990 { hold => $hold->id },
1991 { order_by => { ahtc => 'id desc' }, limit => 1 }
1997 sub flesh_hold_notices {
1998 my( $holds, $e ) = @_;
1999 $e ||= new_editor();
2001 for my $hold (@$holds) {
2002 my $notices = $e->search_action_hold_notification(
2004 { hold => $hold->id },
2005 { order_by => { anh => 'notify_time desc' } },
2010 $hold->notify_count(scalar(@$notices));
2012 my $n = $e->retrieve_action_hold_notification($$notices[0])
2013 or return $e->event;
2014 $hold->notify_time($n->notify_time);
2020 __PACKAGE__->register_method(
2021 method => 'fetch_captured_holds',
2022 api_name => 'open-ils.circ.captured_holds.on_shelf.retrieve',
2026 Returns a list of un-fulfilled holds (on the Holds Shelf) for a given title id
2027 @param authtoken The login session key
2028 @param org The org id of the location in question
2029 @param match_copy A specific copy to limit to
2033 __PACKAGE__->register_method(
2034 method => 'fetch_captured_holds',
2035 api_name => 'open-ils.circ.captured_holds.id_list.on_shelf.retrieve',
2039 Returns list ids of un-fulfilled holds (on the Holds Shelf) for a given title id
2040 @param authtoken The login session key
2041 @param org The org id of the location in question
2042 @param match_copy A specific copy to limit to
2046 __PACKAGE__->register_method(
2047 method => 'fetch_captured_holds',
2048 api_name => 'open-ils.circ.captured_holds.id_list.expired_on_shelf.retrieve',
2052 Returns list ids of shelf-expired un-fulfilled holds for a given title id
2053 @param authtoken The login session key
2054 @param org The org id of the location in question
2055 @param match_copy A specific copy to limit to
2060 sub fetch_captured_holds {
2061 my( $self, $conn, $auth, $org, $match_copy ) = @_;
2063 my $e = new_editor(authtoken => $auth);
2064 return $e->die_event unless $e->checkauth;
2065 return $e->die_event unless $e->allowed('VIEW_HOLD'); # XXX rely on editor perm
2067 $org ||= $e->requestor->ws_ou;
2069 my $current_copy = { '!=' => undef };
2070 $current_copy = { '=' => $match_copy } if $match_copy;
2073 select => { alhr => ['id'] },
2078 fkey => 'current_copy'
2083 '+acp' => { status => OILS_COPY_STATUS_ON_HOLDS_SHELF, deleted => 'f' },
2085 capture_time => { "!=" => undef },
2086 current_copy => $current_copy,
2087 fulfillment_time => undef,
2088 current_shelf_lib => $org
2092 if($self->api_name =~ /expired/) {
2093 $query->{'where'}->{'+alhr'}->{'-or'} = {
2094 shelf_expire_time => { '<' => 'now'},
2095 cancel_time => { '!=' => undef },
2098 my $hold_ids = $e->json_query( $query );
2100 for my $hold_id (@$hold_ids) {
2101 if($self->api_name =~ /id_list/) {
2102 $conn->respond($hold_id->{id});
2106 $e->retrieve_action_hold_request([
2110 flesh_fields => {ahr => ['notifications', 'transit', 'notes']},
2111 order_by => {anh => 'notify_time desc'}
2121 __PACKAGE__->register_method(
2122 method => "print_expired_holds_stream",
2123 api_name => "open-ils.circ.captured_holds.expired.print.stream",
2127 sub print_expired_holds_stream {
2128 my ($self, $client, $auth, $params) = @_;
2130 # No need to check specific permissions: we're going to call another method
2131 # that will do that.
2132 my $e = new_editor("authtoken" => $auth);
2133 return $e->die_event unless $e->checkauth;
2135 delete($$params{org_id}) unless (int($$params{org_id}));
2136 delete($$params{limit}) unless (int($$params{limit}));
2137 delete($$params{offset}) unless (int($$params{offset}));
2138 delete($$params{chunk_size}) unless (int($$params{chunk_size}));
2139 delete($$params{chunk_size}) if ($$params{chunk_size} && $$params{chunk_size} > 50); # keep the size reasonable
2140 $$params{chunk_size} ||= 10;
2142 $$params{org_id} = (defined $$params{org_id}) ? $$params{org_id}: $e->requestor->ws_ou;
2144 my @hold_ids = $self->method_lookup(
2145 "open-ils.circ.captured_holds.id_list.expired_on_shelf.retrieve"
2146 )->run($auth, $params->{"org_id"});
2151 } elsif (defined $U->event_code($hold_ids[0])) {
2153 return $hold_ids[0];
2156 $logger->info("about to stream back up to " . scalar(@hold_ids) . " expired holds");
2159 my @hid_chunk = splice @hold_ids, 0, $params->{"chunk_size"};
2161 my $result_chunk = $e->json_query({
2163 "acp" => ["barcode"],
2165 first_given_name second_given_name family_name alias
2174 "field" => "id", "fkey" => "current_copy",
2177 "field" => "id", "fkey" => "call_number",
2180 "field" => "id", "fkey" => "record"
2184 "acpl" => {"field" => "id", "fkey" => "location"}
2187 "au" => {"field" => "id", "fkey" => "usr"}
2190 "where" => {"+ahr" => {"id" => \@hid_chunk}}
2191 }) or return $e->die_event;
2192 $client->respond($result_chunk);
2199 __PACKAGE__->register_method(
2200 method => "check_title_hold_batch",
2201 api_name => "open-ils.circ.title_hold.is_possible.batch",
2204 desc => '@see open-ils.circ.title_hold.is_possible.batch',
2206 { desc => 'Authentication token', type => 'string'},
2207 { desc => 'Array of Hash of named parameters', type => 'array'},
2210 desc => 'Array of response objects',
2216 sub check_title_hold_batch {
2217 my($self, $client, $authtoken, $param_list, $oargs) = @_;
2218 foreach (@$param_list) {
2219 my ($res) = $self->method_lookup('open-ils.circ.title_hold.is_possible')->run($authtoken, $_, $oargs);
2220 $client->respond($res);
2226 __PACKAGE__->register_method(
2227 method => "check_title_hold",
2228 api_name => "open-ils.circ.title_hold.is_possible",
2230 desc => 'Determines if a hold were to be placed by a given user, ' .
2231 'whether or not said hold would have any potential copies to fulfill it.' .
2232 'The named paramaters of the second argument include: ' .
2233 'patronid, titleid, volume_id, copy_id, mrid, depth, pickup_lib, hold_type, selection_ou. ' .
2234 'See perldoc ' . __PACKAGE__ . ' for more info on these fields.' ,
2236 { desc => 'Authentication token', type => 'string'},
2237 { desc => 'Hash of named parameters', type => 'object'},
2240 desc => 'List of new message IDs (empty if none)',
2246 =head3 check_title_hold (token, hash)
2248 The named fields in the hash are:
2250 patronid - ID of the hold recipient (required)
2251 depth - hold range depth (default 0)
2252 pickup_lib - destination for hold, fallback value for selection_ou
2253 selection_ou - ID of org_unit establishing hard and soft hold boundary settings
2254 issuanceid - ID of the issuance to be held, required for Issuance level hold
2255 partid - ID of the monograph part to be held, required for monograph part level hold
2256 titleid - ID (BRN) of the title to be held, required for Title level hold
2257 volume_id - required for Volume level hold
2258 copy_id - required for Copy level hold
2259 mrid - required for Meta-record level hold
2260 hold_type - T, C (or R or F), I, V or M for Title, Copy, Issuance, Volume or Meta-record (default "T")
2262 All key/value pairs are passed on to do_possibility_checks.
2266 # FIXME: better params checking. what other params are required, if any?
2267 # FIXME: 3 copies of values confusing: $x, $params->{x} and $params{x}
2268 # FIXME: for example, $depth gets a default value, but then $$params{depth} is still
2269 # used in conditionals, where it may be undefined, causing a warning.
2270 # FIXME: specify proper usage/interaction of selection_ou and pickup_lib
2272 sub check_title_hold {
2273 my( $self, $client, $authtoken, $params ) = @_;
2274 my $e = new_editor(authtoken=>$authtoken);
2275 return $e->event unless $e->checkauth;
2277 my %params = %$params;
2278 my $depth = $params{depth} || 0;
2279 my $selection_ou = $params{selection_ou} || $params{pickup_lib};
2280 my $oargs = $params{oargs} || {};
2282 if($oargs->{events}) {
2283 @{$oargs->{events}} = grep { $e->allowed($_ . '.override', $e->requestor->ws_ou); } @{$oargs->{events}};
2287 my $patron = $e->retrieve_actor_user($params{patronid})
2288 or return $e->event;
2290 if( $e->requestor->id ne $patron->id ) {
2291 return $e->event unless
2292 $e->allowed('VIEW_HOLD_PERMIT', $patron->home_ou);
2295 return OpenILS::Event->new('PATRON_BARRED') if $U->is_true($patron->barred);
2297 my $request_lib = $e->retrieve_actor_org_unit($e->requestor->ws_ou)
2298 or return $e->event;
2300 my $soft_boundary = $U->ou_ancestor_setting_value($selection_ou, OILS_SETTING_HOLD_SOFT_BOUNDARY);
2301 my $hard_boundary = $U->ou_ancestor_setting_value($selection_ou, OILS_SETTING_HOLD_HARD_BOUNDARY);
2304 my $return_depth = $hard_boundary; # default depth to return on success
2305 if(defined $soft_boundary and $depth < $soft_boundary) {
2306 # work up the tree and as soon as we find a potential copy, use that depth
2307 # also, make sure we don't go past the hard boundary if it exists
2309 # our min boundary is the greater of user-specified boundary or hard boundary
2310 my $min_depth = (defined $hard_boundary and $hard_boundary > $depth) ?
2311 $hard_boundary : $depth;
2313 my $depth = $soft_boundary;
2314 while($depth >= $min_depth) {
2315 $logger->info("performing hold possibility check with soft boundary $depth");
2316 @status = do_possibility_checks($e, $patron, $request_lib, $depth, %params);
2318 $return_depth = $depth;
2323 } elsif(defined $hard_boundary and $depth < $hard_boundary) {
2324 # there is no soft boundary, enforce the hard boundary if it exists
2325 $logger->info("performing hold possibility check with hard boundary $hard_boundary");
2326 @status = do_possibility_checks($e, $patron, $request_lib, $hard_boundary, %params);
2328 # no boundaries defined, fall back to user specifed boundary or no boundary
2329 $logger->info("performing hold possibility check with no boundary");
2330 @status = do_possibility_checks($e, $patron, $request_lib, $params{depth}, %params);
2333 my $place_unfillable = 0;
2334 $place_unfillable = 1 if $e->allowed('PLACE_UNFILLABLE_HOLD', $e->requestor->ws_ou);
2339 "depth" => $return_depth,
2340 "local_avail" => $status[1]
2342 } elsif ($status[2]) {
2343 my $n = scalar @{$status[2]};
2344 return {"success" => 0, "last_event" => $status[2]->[$n - 1], "age_protected_copy" => $status[3], "place_unfillable" => $place_unfillable};
2346 return {"success" => 0, "age_protected_copy" => $status[3], "place_unfillable" => $place_unfillable};
2352 sub do_possibility_checks {
2353 my($e, $patron, $request_lib, $depth, %params) = @_;
2355 my $issuanceid = $params{issuanceid} || "";
2356 my $partid = $params{partid} || "";
2357 my $titleid = $params{titleid} || "";
2358 my $volid = $params{volume_id};
2359 my $copyid = $params{copy_id};
2360 my $mrid = $params{mrid} || "";
2361 my $pickup_lib = $params{pickup_lib};
2362 my $hold_type = $params{hold_type} || 'T';
2363 my $selection_ou = $params{selection_ou} || $pickup_lib;
2364 my $holdable_formats = $params{holdable_formats};
2365 my $oargs = $params{oargs} || {};
2372 if( $hold_type eq OILS_HOLD_TYPE_FORCE || $hold_type eq OILS_HOLD_TYPE_RECALL || $hold_type eq OILS_HOLD_TYPE_COPY ) {
2374 return $e->event unless $copy = $e->retrieve_asset_copy($copyid);
2375 return $e->event unless $volume = $e->retrieve_asset_call_number($copy->call_number);
2376 return $e->event unless $title = $e->retrieve_biblio_record_entry($volume->record);
2378 return (1, 1, []) if( $hold_type eq OILS_HOLD_TYPE_RECALL || $hold_type eq OILS_HOLD_TYPE_FORCE);
2379 return verify_copy_for_hold(
2380 $patron, $e->requestor, $title, $copy, $pickup_lib, $request_lib, $oargs
2383 } elsif( $hold_type eq OILS_HOLD_TYPE_VOLUME ) {
2385 return $e->event unless $volume = $e->retrieve_asset_call_number($volid);
2386 return $e->event unless $title = $e->retrieve_biblio_record_entry($volume->record);
2388 return _check_volume_hold_is_possible(
2389 $volume, $title, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou, $oargs
2392 } elsif( $hold_type eq OILS_HOLD_TYPE_TITLE ) {
2394 return _check_title_hold_is_possible(
2395 $titleid, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou, undef, $oargs
2398 } elsif( $hold_type eq OILS_HOLD_TYPE_ISSUANCE ) {
2400 return _check_issuance_hold_is_possible(
2401 $issuanceid, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou, $oargs
2404 } elsif( $hold_type eq OILS_HOLD_TYPE_MONOPART ) {
2406 return _check_monopart_hold_is_possible(
2407 $partid, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou, $oargs
2410 } elsif( $hold_type eq OILS_HOLD_TYPE_METARECORD ) {
2412 my $maps = $e->search_metabib_metarecord_source_map({metarecord=>$mrid});
2413 my @recs = map { $_->source } @$maps;
2415 for my $rec (@recs) {
2416 @status = _check_title_hold_is_possible(
2417 $rec, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou, $holdable_formats, $oargs
2423 # else { Unrecognized hold_type ! } # FIXME: return error? or 0?
2427 sub create_ranged_org_filter {
2428 my($e, $selection_ou, $depth) = @_;
2430 # find the orgs from which this hold may be fulfilled,
2431 # based on the selection_ou and depth
2433 my $top_org = $e->search_actor_org_unit([
2434 {parent_ou => undef},
2435 {flesh=>1, flesh_fields=>{aou=>['ou_type']}}])->[0];
2438 return () if $depth == $top_org->ou_type->depth;
2440 my $org_list = $U->storagereq('open-ils.storage.actor.org_unit.descendants.atomic', $selection_ou, $depth);
2441 %org_filter = (circ_lib => []);
2442 push(@{$org_filter{circ_lib}}, $_->id) for @$org_list;
2444 $logger->info("hold org filter at depth $depth and selection_ou ".
2445 "$selection_ou created list of @{$org_filter{circ_lib}}");
2451 sub _check_title_hold_is_possible {
2452 my( $titleid, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou, $holdable_formats, $oargs ) = @_;
2454 my ($types, $formats, $lang);
2455 if (defined($holdable_formats)) {
2456 ($types, $formats, $lang) = split '-', $holdable_formats;
2459 my $e = new_editor();
2460 my %org_filter = create_ranged_org_filter($e, $selection_ou, $depth);
2462 # this monster will grab the id and circ_lib of all of the "holdable" copies for the given record
2463 my $copies = $e->json_query(
2465 select => { acp => ['id', 'circ_lib'] },
2470 fkey => 'call_number',
2474 filter => { id => $titleid },
2482 ( $types ? (item_type => [split '', $types]) : () ),
2483 ( $formats ? (item_form => [split '', $formats]) : () ),
2484 ( $lang ? (item_lang => $lang) : () )
2489 acpl => { field => 'id', filter => { holdable => 't'}, fkey => 'location' },
2490 ccs => { field => 'id', filter => { holdable => 't'}, fkey => 'status' },
2491 acpm => { field => 'target_copy', type => 'left' } # ignore part-linked copies
2495 '+acp' => { circulate => 't', deleted => 'f', holdable => 't', %org_filter },
2496 '+acpm' => { target_copy => undef } # ignore part-linked copies
2501 $logger->info("title possible found ".scalar(@$copies)." potential copies");
2505 "HIGH_LEVEL_HOLD_HAS_NO_COPIES",
2506 "payload" => {"fail_part" => "no_ultimate_items"}
2511 # -----------------------------------------------------------------------
2512 # sort the copies into buckets based on their circ_lib proximity to
2513 # the patron's home_ou.
2514 # -----------------------------------------------------------------------
2516 my $home_org = $patron->home_ou;
2517 my $req_org = $request_lib->id;
2519 $logger->info("prox cache $home_org " . $prox_cache{$home_org});
2521 $prox_cache{$home_org} =
2522 $e->search_actor_org_unit_proximity({from_org => $home_org})
2523 unless $prox_cache{$home_org};
2524 my $home_prox = $prox_cache{$home_org};
2527 my %hash = map { ($_->to_org => $_->prox) } @$home_prox;
2528 push( @{$buckets{ $hash{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2530 my @keys = sort { $a <=> $b } keys %buckets;
2533 if( $home_org ne $req_org ) {
2534 # -----------------------------------------------------------------------
2535 # shove the copies close to the request_lib into the primary buckets
2536 # directly before the farthest away copies. That way, they are not
2537 # given priority, but they are checked before the farthest copies.
2538 # -----------------------------------------------------------------------
2539 $prox_cache{$req_org} =
2540 $e->search_actor_org_unit_proximity({from_org => $req_org})
2541 unless $prox_cache{$req_org};
2542 my $req_prox = $prox_cache{$req_org};
2545 my %hash2 = map { ($_->to_org => $_->prox) } @$req_prox;
2546 push( @{$buckets2{ $hash2{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2548 my $highest_key = $keys[@keys - 1]; # the farthest prox in the exising buckets
2549 my $new_key = $highest_key - 0.5; # right before the farthest prox
2550 my @keys2 = sort { $a <=> $b } keys %buckets2;
2551 for my $key (@keys2) {
2552 last if $key >= $highest_key;
2553 push( @{$buckets{$new_key}}, $_ ) for @{$buckets2{$key}};
2557 @keys = sort { $a <=> $b } keys %buckets;
2562 my $age_protect_only = 0;
2563 OUTER: for my $key (@keys) {
2564 my @cps = @{$buckets{$key}};
2566 $logger->info("looking at " . scalar(@{$buckets{$key}}). " copies in proximity bucket $key");
2568 for my $copyid (@cps) {
2570 next if $seen{$copyid};
2571 $seen{$copyid} = 1; # there could be dupes given the merged buckets
2572 my $copy = $e->retrieve_asset_copy($copyid);
2573 $logger->debug("looking at bucket_key=$key, copy $copyid : circ_lib = " . $copy->circ_lib);
2575 unless($title) { # grab the title if we don't already have it
2576 my $vol = $e->retrieve_asset_call_number(
2577 [ $copy->call_number, { flesh => 1, flesh_fields => { bre => ['fixed_fields'], acn => ['record'] } } ] );
2578 $title = $vol->record;
2581 @status = verify_copy_for_hold(
2582 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib, $oargs);
2584 $age_protect_only ||= $status[3];
2585 last OUTER if $status[0];
2589 $status[3] = $age_protect_only;
2593 sub _check_issuance_hold_is_possible {
2594 my( $issuanceid, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou, $oargs ) = @_;
2596 my $e = new_editor();
2597 my %org_filter = create_ranged_org_filter($e, $selection_ou, $depth);
2599 # this monster will grab the id and circ_lib of all of the "holdable" copies for the given record
2600 my $copies = $e->json_query(
2602 select => { acp => ['id', 'circ_lib'] },
2608 filter => { issuance => $issuanceid }
2610 acpl => { field => 'id', filter => { holdable => 't'}, fkey => 'location' },
2611 ccs => { field => 'id', filter => { holdable => 't'}, fkey => 'status' }
2615 '+acp' => { circulate => 't', deleted => 'f', holdable => 't', %org_filter }
2621 $logger->info("issuance possible found ".scalar(@$copies)." potential copies");
2625 $empty_ok = $e->retrieve_config_global_flag('circ.holds.empty_issuance_ok');
2626 $empty_ok = ($empty_ok and $U->is_true($empty_ok->enabled));
2631 "HIGH_LEVEL_HOLD_HAS_NO_COPIES",
2632 "payload" => {"fail_part" => "no_ultimate_items"}
2640 # -----------------------------------------------------------------------
2641 # sort the copies into buckets based on their circ_lib proximity to
2642 # the patron's home_ou.
2643 # -----------------------------------------------------------------------
2645 my $home_org = $patron->home_ou;
2646 my $req_org = $request_lib->id;
2648 $logger->info("prox cache $home_org " . $prox_cache{$home_org});
2650 $prox_cache{$home_org} =
2651 $e->search_actor_org_unit_proximity({from_org => $home_org})
2652 unless $prox_cache{$home_org};
2653 my $home_prox = $prox_cache{$home_org};
2656 my %hash = map { ($_->to_org => $_->prox) } @$home_prox;
2657 push( @{$buckets{ $hash{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2659 my @keys = sort { $a <=> $b } keys %buckets;
2662 if( $home_org ne $req_org ) {
2663 # -----------------------------------------------------------------------
2664 # shove the copies close to the request_lib into the primary buckets
2665 # directly before the farthest away copies. That way, they are not
2666 # given priority, but they are checked before the farthest copies.
2667 # -----------------------------------------------------------------------
2668 $prox_cache{$req_org} =
2669 $e->search_actor_org_unit_proximity({from_org => $req_org})
2670 unless $prox_cache{$req_org};
2671 my $req_prox = $prox_cache{$req_org};
2674 my %hash2 = map { ($_->to_org => $_->prox) } @$req_prox;
2675 push( @{$buckets2{ $hash2{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2677 my $highest_key = $keys[@keys - 1]; # the farthest prox in the exising buckets
2678 my $new_key = $highest_key - 0.5; # right before the farthest prox
2679 my @keys2 = sort { $a <=> $b } keys %buckets2;
2680 for my $key (@keys2) {
2681 last if $key >= $highest_key;
2682 push( @{$buckets{$new_key}}, $_ ) for @{$buckets2{$key}};
2686 @keys = sort { $a <=> $b } keys %buckets;
2691 my $age_protect_only = 0;
2692 OUTER: for my $key (@keys) {
2693 my @cps = @{$buckets{$key}};
2695 $logger->info("looking at " . scalar(@{$buckets{$key}}). " copies in proximity bucket $key");
2697 for my $copyid (@cps) {
2699 next if $seen{$copyid};
2700 $seen{$copyid} = 1; # there could be dupes given the merged buckets
2701 my $copy = $e->retrieve_asset_copy($copyid);
2702 $logger->debug("looking at bucket_key=$key, copy $copyid : circ_lib = " . $copy->circ_lib);
2704 unless($title) { # grab the title if we don't already have it
2705 my $vol = $e->retrieve_asset_call_number(
2706 [ $copy->call_number, { flesh => 1, flesh_fields => { bre => ['fixed_fields'], acn => ['record'] } } ] );
2707 $title = $vol->record;
2710 @status = verify_copy_for_hold(
2711 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib, $oargs);
2713 $age_protect_only ||= $status[3];
2714 last OUTER if $status[0];
2719 if (!defined($empty_ok)) {
2720 $empty_ok = $e->retrieve_config_global_flag('circ.holds.empty_issuance_ok');
2721 $empty_ok = ($empty_ok and $U->is_true($empty_ok->enabled));
2724 return (1,0) if ($empty_ok);
2726 $status[3] = $age_protect_only;
2730 sub _check_monopart_hold_is_possible {
2731 my( $partid, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou, $oargs ) = @_;
2733 my $e = new_editor();
2734 my %org_filter = create_ranged_org_filter($e, $selection_ou, $depth);
2736 # this monster will grab the id and circ_lib of all of the "holdable" copies for the given record
2737 my $copies = $e->json_query(
2739 select => { acp => ['id', 'circ_lib'] },
2743 field => 'target_copy',
2745 filter => { part => $partid }
2747 acpl => { field => 'id', filter => { holdable => 't'}, fkey => 'location' },
2748 ccs => { field => 'id', filter => { holdable => 't'}, fkey => 'status' }
2752 '+acp' => { circulate => 't', deleted => 'f', holdable => 't', %org_filter }
2758 $logger->info("monopart possible found ".scalar(@$copies)." potential copies");
2762 $empty_ok = $e->retrieve_config_global_flag('circ.holds.empty_part_ok');
2763 $empty_ok = ($empty_ok and $U->is_true($empty_ok->enabled));
2768 "HIGH_LEVEL_HOLD_HAS_NO_COPIES",
2769 "payload" => {"fail_part" => "no_ultimate_items"}
2777 # -----------------------------------------------------------------------
2778 # sort the copies into buckets based on their circ_lib proximity to
2779 # the patron's home_ou.
2780 # -----------------------------------------------------------------------
2782 my $home_org = $patron->home_ou;
2783 my $req_org = $request_lib->id;
2785 $logger->info("prox cache $home_org " . $prox_cache{$home_org});
2787 $prox_cache{$home_org} =
2788 $e->search_actor_org_unit_proximity({from_org => $home_org})
2789 unless $prox_cache{$home_org};
2790 my $home_prox = $prox_cache{$home_org};
2793 my %hash = map { ($_->to_org => $_->prox) } @$home_prox;
2794 push( @{$buckets{ $hash{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2796 my @keys = sort { $a <=> $b } keys %buckets;
2799 if( $home_org ne $req_org ) {
2800 # -----------------------------------------------------------------------
2801 # shove the copies close to the request_lib into the primary buckets
2802 # directly before the farthest away copies. That way, they are not
2803 # given priority, but they are checked before the farthest copies.
2804 # -----------------------------------------------------------------------
2805 $prox_cache{$req_org} =
2806 $e->search_actor_org_unit_proximity({from_org => $req_org})
2807 unless $prox_cache{$req_org};
2808 my $req_prox = $prox_cache{$req_org};
2811 my %hash2 = map { ($_->to_org => $_->prox) } @$req_prox;
2812 push( @{$buckets2{ $hash2{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2814 my $highest_key = $keys[@keys - 1]; # the farthest prox in the exising buckets
2815 my $new_key = $highest_key - 0.5; # right before the farthest prox
2816 my @keys2 = sort { $a <=> $b } keys %buckets2;
2817 for my $key (@keys2) {
2818 last if $key >= $highest_key;
2819 push( @{$buckets{$new_key}}, $_ ) for @{$buckets2{$key}};
2823 @keys = sort { $a <=> $b } keys %buckets;
2828 my $age_protect_only = 0;
2829 OUTER: for my $key (@keys) {
2830 my @cps = @{$buckets{$key}};
2832 $logger->info("looking at " . scalar(@{$buckets{$key}}). " copies in proximity bucket $key");
2834 for my $copyid (@cps) {
2836 next if $seen{$copyid};
2837 $seen{$copyid} = 1; # there could be dupes given the merged buckets
2838 my $copy = $e->retrieve_asset_copy($copyid);
2839 $logger->debug("looking at bucket_key=$key, copy $copyid : circ_lib = " . $copy->circ_lib);
2841 unless($title) { # grab the title if we don't already have it
2842 my $vol = $e->retrieve_asset_call_number(
2843 [ $copy->call_number, { flesh => 1, flesh_fields => { bre => ['fixed_fields'], acn => ['record'] } } ] );
2844 $title = $vol->record;
2847 @status = verify_copy_for_hold(
2848 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib, $oargs);
2850 $age_protect_only ||= $status[3];
2851 last OUTER if $status[0];
2856 if (!defined($empty_ok)) {
2857 $empty_ok = $e->retrieve_config_global_flag('circ.holds.empty_part_ok');
2858 $empty_ok = ($empty_ok and $U->is_true($empty_ok->enabled));
2861 return (1,0) if ($empty_ok);
2863 $status[3] = $age_protect_only;
2868 sub _check_volume_hold_is_possible {
2869 my( $vol, $title, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou, $oargs ) = @_;
2870 my %org_filter = create_ranged_org_filter(new_editor(), $selection_ou, $depth);
2871 my $copies = new_editor->search_asset_copy({call_number => $vol->id, %org_filter});
2872 $logger->info("checking possibility of volume hold for volume ".$vol->id);
2874 my $filter_copies = [];
2875 for my $copy (@$copies) {
2876 # ignore part-mapped copies for regular volume level holds
2877 push(@$filter_copies, $copy) unless
2878 new_editor->search_asset_copy_part_map({target_copy => $copy->id})->[0];
2880 $copies = $filter_copies;
2885 "HIGH_LEVEL_HOLD_HAS_NO_COPIES",
2886 "payload" => {"fail_part" => "no_ultimate_items"}
2892 my $age_protect_only = 0;
2893 for my $copy ( @$copies ) {
2894 @status = verify_copy_for_hold(
2895 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib, $oargs );
2896 $age_protect_only ||= $status[3];
2899 $status[3] = $age_protect_only;
2905 sub verify_copy_for_hold {
2906 my( $patron, $requestor, $title, $copy, $pickup_lib, $request_lib, $oargs ) = @_;
2907 # $oargs should be undef unless we're overriding.
2908 $logger->info("checking possibility of copy in hold request for copy ".$copy->id);
2909 my $permitted = OpenILS::Utils::PermitHold::permit_copy_hold(
2912 requestor => $requestor,
2915 title_descriptor => $title->fixed_fields,
2916 pickup_lib => $pickup_lib,
2917 request_lib => $request_lib,
2919 show_event_list => 1
2923 # Check for override permissions on events.
2924 if ($oargs && $permitted && scalar @$permitted) {
2925 # Remove the events from permitted that we can override.
2926 if ($oargs->{events}) {
2927 foreach my $evt (@{$oargs->{events}}) {
2928 $permitted = [grep {$_->{textcode} ne $evt} @{$permitted}];
2931 # Now, we handle the override all case by checking remaining
2932 # events against override permissions.
2933 if (scalar @$permitted && $oargs->{all}) {
2934 # Pre-set events and failed members of oargs to empty
2935 # arrays, if they are not set, yet.
2936 $oargs->{events} = [] unless ($oargs->{events});
2937 $oargs->{failed} = [] unless ($oargs->{failed});
2938 # When we're done with these checks, we swap permitted
2939 # with a reference to @disallowed.
2940 my @disallowed = ();
2941 foreach my $evt (@{$permitted}) {
2942 # Check if we've already seen the event in this
2943 # session and it failed.
2944 if (grep {$_ eq $evt->{textcode}} @{$oargs->{failed}}) {
2945 push(@disallowed, $evt);
2947 # We have to check if the requestor has the
2948 # override permission.
2950 # AppUtils::check_user_perms returns the perm if
2951 # the user doesn't have it, undef if they do.
2952 if ($apputils->check_user_perms($requestor->id, $requestor->ws_ou, $evt->{textcode} . '.override')) {
2953 push(@disallowed, $evt);
2954 push(@{$oargs->{failed}}, $evt->{textcode});
2956 push(@{$oargs->{events}}, $evt->{textcode});
2960 $permitted = \@disallowed;
2964 my $age_protect_only = 0;
2965 if (@$permitted == 1 && @$permitted[0]->{textcode} eq 'ITEM_AGE_PROTECTED') {
2966 $age_protect_only = 1;
2970 (not scalar @$permitted), # true if permitted is an empty arrayref
2971 ( # XXX This test is of very dubious value; someone should figure
2972 # out what if anything is checking this value
2973 ($copy->circ_lib == $pickup_lib) and
2974 ($copy->status == OILS_COPY_STATUS_AVAILABLE)
2983 sub find_nearest_permitted_hold {
2986 my $editor = shift; # CStoreEditor object
2987 my $copy = shift; # copy to target
2988 my $user = shift; # staff
2989 my $check_only = shift; # do no updates, just see if the copy could fulfill a hold
2991 my $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND');
2993 my $bc = $copy->barcode;
2995 # find any existing holds that already target this copy
2996 my $old_holds = $editor->search_action_hold_request(
2997 { current_copy => $copy->id,
2998 cancel_time => undef,
2999 capture_time => undef
3003 my $hold_stall_interval = $U->ou_ancestor_setting_value($user->ws_ou, OILS_SETTING_HOLD_SOFT_STALL);
3005 $logger->info("circulator: searching for best hold at org ".$user->ws_ou.
3006 " and copy $bc with a hold stalling interval of ". ($hold_stall_interval || "(none)"));
3008 my $fifo = $U->ou_ancestor_setting_value($user->ws_ou, 'circ.holds_fifo');
3010 # the nearest_hold API call now needs this
3011 $copy->call_number($editor->retrieve_asset_call_number($copy->call_number))
3012 unless ref $copy->call_number;
3014 # search for what should be the best holds for this copy to fulfill
3015 my $best_holds = $U->storagereq(
3016 "open-ils.storage.action.hold_request.nearest_hold.atomic",
3017 $user->ws_ou, $copy, 100, $hold_stall_interval, $fifo );
3019 # Add any pre-targeted holds to the list too? Unless they are already there, anyway.
3021 for my $holdid (@$old_holds) {
3022 next unless $holdid;
3023 push(@$best_holds, $holdid) unless ( grep { ''.$holdid eq ''.$_ } @$best_holds );
3027 unless(@$best_holds) {
3028 $logger->info("circulator: no suitable holds found for copy $bc");
3029 return (undef, $evt);
3035 # for each potential hold, we have to run the permit script
3036 # to make sure the hold is actually permitted.
3039 for my $holdid (@$best_holds) {
3040 next unless $holdid;
3041 $logger->info("circulator: checking if hold $holdid is permitted for copy $bc");
3043 my $hold = $editor->retrieve_action_hold_request($holdid) or next;
3044 my $reqr = $reqr_cache{$hold->requestor} || $editor->retrieve_actor_user($hold->requestor);
3045 my $rlib = $org_cache{$hold->request_lib} || $editor->retrieve_actor_org_unit($hold->request_lib);
3047 $reqr_cache{$hold->requestor} = $reqr;
3048 $org_cache{$hold->request_lib} = $rlib;
3050 # see if this hold is permitted
3051 my $permitted = OpenILS::Utils::PermitHold::permit_copy_hold(
3053 patron_id => $hold->usr,
3056 pickup_lib => $hold->pickup_lib,
3057 request_lib => $rlib,
3069 unless( $best_hold ) { # no "good" permitted holds were found
3071 $logger->info("circulator: no suitable holds found for copy $bc");
3072 return (undef, $evt);
3075 $logger->info("circulator: best hold ".$best_hold->id." found for copy $bc");
3077 # indicate a permitted hold was found
3078 return $best_hold if $check_only;
3080 # we've found a permitted hold. we need to "grab" the copy
3081 # to prevent re-targeted holds (next part) from re-grabbing the copy
3082 $best_hold->current_copy($copy->id);
3083 $editor->update_action_hold_request($best_hold)
3084 or return (undef, $editor->event);
3089 # re-target any other holds that already target this copy
3090 for my $old_hold (@$old_holds) {
3091 next if $old_hold->id eq $best_hold->id; # don't re-target the hold we want
3092 $logger->info("circulator: clearing current_copy and prev_check_time on hold ".
3093 $old_hold->id." after a better hold [".$best_hold->id."] was found");
3094 $old_hold->clear_current_copy;
3095 $old_hold->clear_prev_check_time;
3096 $editor->update_action_hold_request($old_hold)
3097 or return (undef, $editor->event);
3098 push(@retarget, $old_hold->id);
3101 return ($best_hold, undef, (@retarget) ? \@retarget : undef);
3109 __PACKAGE__->register_method(
3110 method => 'all_rec_holds',
3111 api_name => 'open-ils.circ.holds.retrieve_all_from_title',
3115 my( $self, $conn, $auth, $title_id, $args ) = @_;
3117 my $e = new_editor(authtoken=>$auth);
3118 $e->checkauth or return $e->event;
3119 $e->allowed('VIEW_HOLD') or return $e->event;
3122 $args->{fulfillment_time} = undef; # we don't want to see old fulfilled holds
3123 $args->{cancel_time} = undef;
3125 my $resp = { volume_holds => [], copy_holds => [], recall_holds => [], force_holds => [], metarecord_holds => [], part_holds => [], issuance_holds => [] };
3127 my $mr_map = $e->search_metabib_metarecord_source_map({source => $title_id})->[0];
3129 $resp->{metarecord_holds} = $e->search_action_hold_request(
3130 { hold_type => OILS_HOLD_TYPE_METARECORD,
3131 target => $mr_map->metarecord,
3137 $resp->{title_holds} = $e->search_action_hold_request(
3139 hold_type => OILS_HOLD_TYPE_TITLE,
3140 target => $title_id,
3144 my $parts = $e->search_biblio_monograph_part(
3150 $resp->{part_holds} = $e->search_action_hold_request(
3152 hold_type => OILS_HOLD_TYPE_MONOPART,
3158 my $subs = $e->search_serial_subscription(
3159 { record_entry => $title_id }, {idlist=>1});
3162 my $issuances = $e->search_serial_issuance(
3163 {subscription => $subs}, {idlist=>1}
3167 $resp->{issuance_holds} = $e->search_action_hold_request(
3169 hold_type => OILS_HOLD_TYPE_ISSUANCE,
3170 target => $issuances,
3177 my $vols = $e->search_asset_call_number(
3178 { record => $title_id, deleted => 'f' }, {idlist=>1});
3180 return $resp unless @$vols;
3182 $resp->{volume_holds} = $e->search_action_hold_request(
3184 hold_type => OILS_HOLD_TYPE_VOLUME,
3189 my $copies = $e->search_asset_copy(
3190 { call_number => $vols, deleted => 'f' }, {idlist=>1});
3192 return $resp unless @$copies;
3194 $resp->{copy_holds} = $e->search_action_hold_request(
3196 hold_type => OILS_HOLD_TYPE_COPY,
3201 $resp->{recall_holds} = $e->search_action_hold_request(
3203 hold_type => OILS_HOLD_TYPE_RECALL,
3208 $resp->{force_holds} = $e->search_action_hold_request(
3210 hold_type => OILS_HOLD_TYPE_FORCE,
3222 __PACKAGE__->register_method(
3223 method => 'uber_hold',
3225 api_name => 'open-ils.circ.hold.details.retrieve'
3229 my($self, $client, $auth, $hold_id, $args) = @_;
3230 my $e = new_editor(authtoken=>$auth);
3231 $e->checkauth or return $e->event;
3232 return uber_hold_impl($e, $hold_id, $args);
3235 __PACKAGE__->register_method(
3236 method => 'batch_uber_hold',
3239 api_name => 'open-ils.circ.hold.details.batch.retrieve'
3242 sub batch_uber_hold {
3243 my($self, $client, $auth, $hold_ids, $args) = @_;
3244 my $e = new_editor(authtoken=>$auth);
3245 $e->checkauth or return $e->event;
3246 $client->respond(uber_hold_impl($e, $_, $args)) for @$hold_ids;
3250 sub uber_hold_impl {
3251 my($e, $hold_id, $args) = @_;
3254 my $hold = $e->retrieve_action_hold_request(
3259 flesh_fields => { ahr => [ 'current_copy', 'usr', 'notes' ] }
3262 ) or return $e->event;
3264 if($hold->usr->id ne $e->requestor->id) {
3265 # caller is asking for someone else's hold
3266 $e->allowed('VIEW_HOLD') or return $e->event;
3267 $hold->notes( # filter out any non-staff ("private") notes (unless marked as public)
3268 [ grep { $U->is_true($_->staff) or $U->is_true($_->pub) } @{$hold->notes} ] );
3271 # caller is asking for own hold, but may not have permission to view staff notes
3272 unless($e->allowed('VIEW_HOLD')) {
3273 $hold->notes( # filter out any staff notes (unless marked as public)
3274 [ grep { !$U->is_true($_->staff) or $U->is_true($_->pub) } @{$hold->notes} ] );
3278 my $user = $hold->usr;
3279 $hold->usr($user->id);
3282 my( $mvr, $volume, $copy, $issuance, $part, $bre ) = find_hold_mvr($e, $hold, $args->{suppress_mvr});
3284 flesh_hold_notices([$hold], $e) unless $args->{suppress_notices};
3285 flesh_hold_transits([$hold]) unless $args->{suppress_transits};
3287 my $details = retrieve_hold_queue_status_impl($e, $hold);
3292 ($copy ? (copy => $copy) : ()),
3293 ($volume ? (volume => $volume) : ()),
3294 ($issuance ? (issuance => $issuance) : ()),
3295 ($part ? (part => $part) : ()),
3296 ($args->{include_bre} ? (bre => $bre) : ()),
3297 ($args->{suppress_mvr} ? () : (mvr => $mvr)),
3301 unless($args->{suppress_patron_details}) {
3302 my $card = $e->retrieve_actor_card($user->card) or return $e->event;
3303 $resp->{patron_first} = $user->first_given_name,
3304 $resp->{patron_last} = $user->family_name,
3305 $resp->{patron_barcode} = $card->barcode,
3306 $resp->{patron_alias} = $user->alias,
3314 # -----------------------------------------------------
3315 # Returns the MVR object that represents what the
3317 # -----------------------------------------------------
3319 my( $e, $hold, $no_mvr ) = @_;
3327 if( $hold->hold_type eq OILS_HOLD_TYPE_METARECORD ) {
3328 my $mr = $e->retrieve_metabib_metarecord($hold->target)
3329 or return $e->event;
3330 $tid = $mr->master_record;
3332 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_TITLE ) {
3333 $tid = $hold->target;
3335 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_VOLUME ) {
3336 $volume = $e->retrieve_asset_call_number($hold->target)
3337 or return $e->event;
3338 $tid = $volume->record;
3340 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_ISSUANCE ) {
3341 $issuance = $e->retrieve_serial_issuance([
3343 {flesh => 1, flesh_fields => {siss => [ qw/subscription/ ]}}
3344 ]) or return $e->event;
3346 $tid = $issuance->subscription->record_entry;
3348 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_MONOPART ) {
3349 $part = $e->retrieve_biblio_monograph_part([
3351 ]) or return $e->event;
3353 $tid = $part->record;
3355 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_COPY || $hold->hold_type eq OILS_HOLD_TYPE_RECALL || $hold->hold_type eq OILS_HOLD_TYPE_FORCE ) {
3356 $copy = $e->retrieve_asset_copy([
3358 {flesh => 1, flesh_fields => {acp => ['call_number']}}
3359 ]) or return $e->event;
3361 $volume = $copy->call_number;
3362 $tid = $volume->record;
3365 if(!$copy and ref $hold->current_copy ) {
3366 $copy = $hold->current_copy;
3367 $hold->current_copy($copy->id);
3370 if(!$volume and $copy) {
3371 $volume = $e->retrieve_asset_call_number($copy->call_number);
3374 # TODO return metarcord mvr for M holds
3375 my $title = $e->retrieve_biblio_record_entry($tid);
3376 return ( ($no_mvr) ? undef : $U->record_to_mvr($title), $volume, $copy, $issuance, $part, $title );
3379 __PACKAGE__->register_method(
3380 method => 'clear_shelf_cache',
3381 api_name => 'open-ils.circ.hold.clear_shelf.get_cache',
3385 Returns the holds processed with the given cache key
3390 sub clear_shelf_cache {
3391 my($self, $client, $auth, $cache_key, $chunk_size) = @_;
3392 my $e = new_editor(authtoken => $auth, xact => 1);
3393 return $e->die_event unless $e->checkauth and $e->allowed('VIEW_HOLD');
3396 my $hold_data = OpenSRF::Utils::Cache->new('global')->get_cache($cache_key);
3399 $logger->info("no hold data found in cache"); # XXX TODO return event
3405 foreach (keys %$hold_data) {
3406 $maximum += scalar(@{ $hold_data->{$_} });
3408 $client->respond({"maximum" => $maximum, "progress" => 0});
3410 for my $action (sort keys %$hold_data) {
3411 while (@{$hold_data->{$action}}) {
3412 my @hid_chunk = splice @{$hold_data->{$action}}, 0, $chunk_size;
3414 my $result_chunk = $e->json_query({
3416 "acp" => ["barcode"],
3418 first_given_name second_given_name family_name alias
3421 "acnp" => [{column => "label", alias => "prefix"}],
3422 "acns" => [{column => "label", alias => "suffix"}],
3430 "field" => "id", "fkey" => "current_copy",
3433 "field" => "id", "fkey" => "call_number",
3436 "field" => "id", "fkey" => "record"
3439 "field" => "id", "fkey" => "prefix"
3442 "field" => "id", "fkey" => "suffix"
3446 "acpl" => {"field" => "id", "fkey" => "location"}
3449 "au" => {"field" => "id", "fkey" => "usr"}
3452 "where" => {"+ahr" => {"id" => \@hid_chunk}}
3453 }, {"substream" => 1}) or return $e->die_event;
3457 +{"action" => $action, "hold_details" => $_}
3468 __PACKAGE__->register_method(
3469 method => 'clear_shelf_process',
3471 api_name => 'open-ils.circ.hold.clear_shelf.process',
3474 1. Find all holds that have expired on the holds shelf
3476 3. If a clear-shelf status is configured, put targeted copies into this status
3477 4. Divide copies into 3 groups: items to transit, items to reshelve, and items
3478 that are needed for holds. No subsequent action is taken on the holds
3479 or items after grouping.
3484 sub clear_shelf_process {
3485 my($self, $client, $auth, $org_id, $match_copy) = @_;
3487 my $e = new_editor(authtoken=>$auth);
3488 $e->checkauth or return $e->die_event;
3489 my $cache = OpenSRF::Utils::Cache->new('global');
3491 $org_id ||= $e->requestor->ws_ou;
3492 $e->allowed('UPDATE_HOLD', $org_id) or return $e->die_event;
3494 my $copy_status = $U->ou_ancestor_setting_value($org_id, 'circ.holds.clear_shelf.copy_status');
3496 my @hold_ids = $self->method_lookup(
3497 "open-ils.circ.captured_holds.id_list.expired_on_shelf.retrieve"
3498 )->run($auth, $org_id, $match_copy);
3503 my @canceled_holds; # newly canceled holds
3504 my $chunk_size = 25; # chunked status updates
3506 for my $hold_id (@hold_ids) {
3508 $logger->info("Clear shelf processing hold $hold_id");
3510 my $hold = $e->retrieve_action_hold_request([
3513 flesh_fields => {ahr => ['current_copy']}
3517 if (!$hold->cancel_time) { # may be canceled but still on the holds shelf
3518 $hold->cancel_time('now');
3519 $hold->cancel_cause(2); # Hold Shelf expiration
3520 $e->update_action_hold_request($hold) or return $e->die_event;
3521 delete_hold_copy_maps($self, $e, $hold->id) and return $e->die_event;
3522 push(@canceled_holds, $hold_id);
3525 my $copy = $hold->current_copy;
3527 if($copy_status or $copy_status == 0) {
3528 # if a clear-shelf copy status is defined, update the copy
3529 $copy->status($copy_status);
3530 $copy->edit_date('now');
3531 $copy->editor($e->requestor->id);
3532 $e->update_asset_copy($copy) or return $e->die_event;
3535 push(@holds, $hold);
3536 $client->respond({maximum => scalar(@holds), progress => $counter}) if ( (++$counter % $chunk_size) == 0);
3545 pl_changed => pickup_lib_changed_on_shelf_holds($e, $org_id, \@hold_ids)
3548 for my $hold (@holds) {
3550 my $copy = $hold->current_copy;
3551 my ($alt_hold) = __PACKAGE__->find_nearest_permitted_hold($e, $copy, $e->requestor, 1);
3553 if($alt_hold and !$match_copy) {
3555 push(@{$cache_data{hold}}, $hold->id); # copy is needed for a hold
3557 } elsif($copy->circ_lib != $e->requestor->ws_ou) {
3559 push(@{$cache_data{transit}}, $hold->id); # copy needs to transit
3563 push(@{$cache_data{shelf}}, $hold->id); # copy needs to go back to the shelf
3567 my $cache_key = md5_hex(time . $$ . rand());
3568 $logger->info("clear_shelf_cache: storing under $cache_key");
3569 $cache->put_cache($cache_key, \%cache_data, 7200); # TODO: 2 hours. configurable?
3571 # tell the client we're done
3572 $client->respond_complete({cache_key => $cache_key});
3575 # fire off the hold cancelation trigger and wait for response so don't flood the service
3577 # refetch the holds to pick up the caclulated cancel_time,
3578 # which may be needed by Action/Trigger
3580 my $updated_holds = [];
3581 $updated_holds = $e->search_action_hold_request({id => \@canceled_holds}, {substream => 1}) if (@canceled_holds > 0);
3584 $U->create_events_for_hook(
3585 'hold_request.cancel.expire_holds_shelf',
3586 $_, $org_id, undef, undef, 1) for @$updated_holds;
3589 # tell the client we're done
3590 $client->respond_complete;
3594 # returns IDs for holds that are on the holds shelf but
3595 # have had their pickup_libs change while on the shelf.
3596 sub pickup_lib_changed_on_shelf_holds {
3599 my $ignore_holds = shift;
3600 $ignore_holds = [$ignore_holds] if !ref($ignore_holds);
3603 select => { alhr => ['id'] },
3608 fkey => 'current_copy'
3613 '+acp' => { status => OILS_COPY_STATUS_ON_HOLDS_SHELF },
3615 capture_time => { "!=" => undef },
3616 fulfillment_time => undef,
3617 current_shelf_lib => $org_id,
3618 pickup_lib => {'!=' => {'+alhr' => 'current_shelf_lib'}}
3623 $query->{where}->{'+alhr'}->{id} =
3624 {'not in' => $ignore_holds} if @$ignore_holds;
3626 my $hold_ids = $e->json_query($query);
3627 return [ map { $_->{id} } @$hold_ids ];
3630 __PACKAGE__->register_method(
3631 method => 'usr_hold_summary',
3632 api_name => 'open-ils.circ.holds.user_summary',
3634 Returns a summary of holds statuses for a given user
3638 sub usr_hold_summary {
3639 my($self, $conn, $auth, $user_id) = @_;
3641 my $e = new_editor(authtoken=>$auth);
3642 $e->checkauth or return $e->event;
3643 $e->allowed('VIEW_HOLD') or return $e->event;
3645 my $holds = $e->search_action_hold_request(
3648 fulfillment_time => undef,
3649 cancel_time => undef,
3653 my %summary = (1 => 0, 2 => 0, 3 => 0, 4 => 0);
3654 $summary{_hold_status($e, $_)} += 1 for @$holds;
3660 __PACKAGE__->register_method(
3661 method => 'hold_has_copy_at',
3662 api_name => 'open-ils.circ.hold.has_copy_at',
3665 'Returns the ID of the found copy and name of the shelving location if there is ' .
3666 'an available copy at the specified org unit. Returns empty hash otherwise. ' .
3667 'The anticipated use for this method is to determine whether an item is ' .
3668 'available at the library where the user is placing the hold (or, alternatively, '.
3669 'at the pickup library) to encourage bypassing the hold placement and just ' .
3670 'checking out the item.' ,
3672 { desc => 'Authentication Token', type => 'string' },
3673 { desc => 'Method Arguments. Options include: hold_type, hold_target, org_unit. '
3674 . 'hold_type is the hold type code (T, V, C, M, ...). '
3675 . 'hold_target is the identifier of the hold target object. '
3676 . 'org_unit is org unit ID.',
3681 desc => q/Result hash like { "copy" : copy_id, "location" : location_name }, empty hash on misses, event on error./,
3687 sub hold_has_copy_at {
3688 my($self, $conn, $auth, $args) = @_;
3690 my $e = new_editor(authtoken=>$auth);
3691 $e->checkauth or return $e->event;
3693 my $hold_type = $$args{hold_type};
3694 my $hold_target = $$args{hold_target};
3695 my $org_unit = $$args{org_unit};
3698 select => {acp => ['id'], acpl => ['name']},
3701 acpl => {field => 'id', filter => { holdable => 't'}, fkey => 'location'},
3702 ccs => {field => 'id', filter => { holdable => 't'}, fkey => 'status' }
3705 where => {'+acp' => { circulate => 't', deleted => 'f', holdable => 't', circ_lib => $org_unit, status => [0,7]}},
3709 if($hold_type eq 'C' or $hold_type eq 'F' or $hold_type eq 'R') {
3711 $query->{where}->{'+acp'}->{id} = $hold_target;
3713 } elsif($hold_type eq 'V') {
3715 $query->{where}->{'+acp'}->{call_number} = $hold_target;
3717 } elsif($hold_type eq 'P') {
3719 $query->{from}->{acp}->{acpm} = {
3720 field => 'target_copy',
3722 filter => {part => $hold_target},
3725 } elsif($hold_type eq 'I') {
3727 $query->{from}->{acp}->{sitem} = {
3730 filter => {issuance => $hold_target},
3733 } elsif($hold_type eq 'T') {
3735 $query->{from}->{acp}->{acn} = {
3737 fkey => 'call_number',
3741 filter => {id => $hold_target},
3749 $query->{from}->{acp}->{acn} = {
3751 fkey => 'call_number',
3760 filter => {metarecord => $hold_target},
3768 my $res = $e->json_query($query)->[0] or return {};
3769 return {copy => $res->{id}, location => $res->{name}} if $res;
3773 # returns true if the user already has an item checked out
3774 # that could be used to fulfill the requested hold.
3775 sub hold_item_is_checked_out {
3776 my($e, $user_id, $hold_type, $hold_target) = @_;
3779 select => {acp => ['id']},
3780 from => {acp => {}},
3784 in => { # copies for circs the user has checked out
3785 select => {circ => ['target_copy']},
3789 checkin_time => undef,
3791 {stop_fines => ["MAXFINES","LONGOVERDUE"]},
3792 {stop_fines => undef}
3802 if($hold_type eq 'C' || $hold_type eq 'R' || $hold_type eq 'F') {
3804 $query->{where}->{'+acp'}->{id}->{in}->{where}->{'target_copy'} = $hold_target;
3806 } elsif($hold_type eq 'V') {
3808 $query->{where}->{'+acp'}->{call_number} = $hold_target;
3810 } elsif($hold_type eq 'P') {
3812 $query->{from}->{acp}->{acpm} = {
3813 field => 'target_copy',
3815 filter => {part => $hold_target},
3818 } elsif($hold_type eq 'I') {
3820 $query->{from}->{acp}->{sitem} = {
3823 filter => {issuance => $hold_target},
3826 } elsif($hold_type eq 'T') {
3828 $query->{from}->{acp}->{acn} = {
3830 fkey => 'call_number',
3834 filter => {id => $hold_target},
3842 $query->{from}->{acp}->{acn} = {
3844 fkey => 'call_number',
3853 filter => {metarecord => $hold_target},
3861 return $e->json_query($query)->[0];
3864 __PACKAGE__->register_method(
3865 method => 'change_hold_title',
3866 api_name => 'open-ils.circ.hold.change_title',
3869 Updates all title level holds targeting the specified bibs to point a new bib./,
3871 { desc => 'Authentication Token', type => 'string' },
3872 { desc => 'New Target Bib Id', type => 'number' },
3873 { desc => 'Old Target Bib Ids', type => 'array' },
3875 return => { desc => '1 on success' }
3879 __PACKAGE__->register_method(
3880 method => 'change_hold_title_for_specific_holds',
3881 api_name => 'open-ils.circ.hold.change_title.specific_holds',
3884 Updates specified holds to target new bib./,
3886 { desc => 'Authentication Token', type => 'string' },
3887 { desc => 'New Target Bib Id', type => 'number' },
3888 { desc => 'Holds Ids for holds to update', type => 'array' },
3890 return => { desc => '1 on success' }
3895 sub change_hold_title {
3896 my( $self, $client, $auth, $new_bib_id, $bib_ids ) = @_;
3898 my $e = new_editor(authtoken=>$auth, xact=>1);
3899 return $e->die_event unless $e->checkauth;
3901 my $holds = $e->search_action_hold_request(
3904 cancel_time => undef,
3905 fulfillment_time => undef,
3911 flesh_fields => { ahr => ['usr'] }
3917 for my $hold (@$holds) {
3918 $e->allowed('UPDATE_HOLD', $hold->usr->home_ou) or return $e->die_event;
3919 $logger->info("Changing hold " . $hold->id . " target from " . $hold->target . " to $new_bib_id in title hold target change");
3920 $hold->target( $new_bib_id );
3921 $e->update_action_hold_request($hold) or return $e->die_event;
3926 _reset_hold($self, $e->requestor, $_) for @$holds;
3931 sub change_hold_title_for_specific_holds {
3932 my( $self, $client, $auth, $new_bib_id, $hold_ids ) = @_;
3934 my $e = new_editor(authtoken=>$auth, xact=>1);
3935 return $e->die_event unless $e->checkauth;
3937 my $holds = $e->search_action_hold_request(
3940 cancel_time => undef,
3941 fulfillment_time => undef,
3947 flesh_fields => { ahr => ['usr'] }
3953 for my $hold (@$holds) {
3954 $e->allowed('UPDATE_HOLD', $hold->usr->home_ou) or return $e->die_event;
3955 $logger->info("Changing hold " . $hold->id . " target from " . $hold->target . " to $new_bib_id in title hold target change");
3956 $hold->target( $new_bib_id );
3957 $e->update_action_hold_request($hold) or return $e->die_event;
3962 _reset_hold($self, $e->requestor, $_) for @$holds;
3967 __PACKAGE__->register_method(
3968 method => 'rec_hold_count',
3969 api_name => 'open-ils.circ.bre.holds.count',
3971 desc => q/Returns the total number of holds that target the
3972 selected bib record or its associated copies and call_numbers/,
3974 { desc => 'Bib ID', type => 'number' },
3975 { desc => q/Optional arguments. Supported arguments include:
3976 "pickup_lib_descendant" -> limit holds to those whose pickup
3977 library is equal to or is a child of the provided org unit/,
3981 return => {desc => 'Hold count', type => 'number'}
3985 __PACKAGE__->register_method(
3986 method => 'rec_hold_count',
3987 api_name => 'open-ils.circ.mmr.holds.count',
3989 desc => q/Returns the total number of holds that target the
3990 selected metarecord or its associated copies, call_numbers, and bib records/,
3992 { desc => 'Metarecord ID', type => 'number' },
3994 return => {desc => 'Hold count', type => 'number'}
3998 # XXX Need to add type I (and, soon, type P) holds to these counts
3999 sub rec_hold_count {
4000 my($self, $conn, $target_id, $args) = @_;
4007 filter => {metarecord => $target_id}
4014 filter => { id => $target_id },
4019 if($self->api_name =~ /mmr/) {
4020 delete $bre_join->{bre}->{filter};
4021 $bre_join->{bre}->{join} = $mmr_join;
4027 fkey => 'call_number',
4033 select => {ahr => [{column => 'id', transform => 'count', alias => 'count'}]},
4037 cancel_time => undef,
4038 fulfillment_time => undef,
4042 hold_type => [qw/C F R/],
4045 select => {acp => ['id']},
4046 from => { acp => $cn_join }
4056 select => {acn => ['id']},
4057 from => {acn => $bre_join}
4065 target => $target_id
4073 if($self->api_name =~ /mmr/) {
4074 $query->{where}->{'+ahr'}->{'-or'}->[2] = {
4079 select => {bre => ['id']},
4080 from => {bre => $mmr_join}
4086 $query->{where}->{'+ahr'}->{'-or'}->[3] = {
4089 target => $target_id
4095 if (my $pld = $args->{pickup_lib_descendant}) {
4096 $query->{where}->{'+ahr'}->{pickup_lib} = {
4098 select => {aou => [{
4100 transform => 'actor.org_unit_descendants',
4101 result_field => 'id'
4104 where => {id => $pld}
4110 return new_editor()->json_query($query)->[0]->{count};
4113 # A helper function to calculate a hold's expiration time at a given
4114 # org_unit. Takes the org_unit as an argument and returns either the
4115 # hold expire time as an ISO8601 string or undef if there is no hold
4116 # expiration interval set for the subject ou.
4117 sub calculate_expire_time
4120 my $interval = $U->ou_ancestor_setting_value($ou, OILS_SETTING_HOLD_EXPIRE);
4122 my $date = DateTime->now->add(seconds => OpenSRF::Utils::interval_to_seconds($interval));
4123 return $U->epoch2ISO8601($date->epoch);