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} = {'=' => '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']},
1341 'field' => 'target_copy',
1342 'fkey' => 'target_copy'
1351 "field" => "cut_in_line",
1352 "transform" => "coalesce",
1354 "direction" => "desc"
1356 { "class" => "ahr", "field" => "request_time" }
1360 '+ahcm2' => { hold => $hold->id }
1364 if (!@$q_holds) { # none? maybe we don't have a map ...
1365 $q_holds = $e->json_query({
1366 select => {ahr => ['id', 'cut_in_line', 'request_time']},
1371 "field" => "cut_in_line",
1372 "transform" => "coalesce",
1374 "direction" => "desc"
1376 { "class" => "ahr", "field" => "request_time" }
1379 hold_type => $hold->hold_type,
1380 target => $hold->target
1387 for my $h (@$q_holds) {
1388 last if $h->{id} == $hold->id;
1392 my $hold_data = $e->json_query({
1394 acp => [ {column => 'id', transform => 'count', aggregate => 1, alias => 'count'} ],
1395 ccm => [ {column =>'avg_wait_time'} ]
1401 ccm => {type => 'left'}
1406 where => {'+ahcm' => {hold => $hold->id} }
1409 my $user_org = $e->json_query({select => {au => ['home_ou']}, from => 'au', where => {id => $hold->usr}})->[0]->{home_ou};
1411 my $default_wait = $U->ou_ancestor_setting_value($user_org, OILS_SETTING_HOLD_ESIMATE_WAIT_INTERVAL);
1412 my $min_wait = $U->ou_ancestor_setting_value($user_org, 'circ.holds.min_estimated_wait_interval');
1413 $min_wait = OpenSRF::Utils::interval_to_seconds($min_wait || '0 seconds');
1414 $default_wait ||= '0 seconds';
1416 # Estimated wait time is the average wait time across the set
1417 # of potential copies, divided by the number of potential copies
1418 # times the queue position.
1420 my $combined_secs = 0;
1421 my $num_potentials = 0;
1423 for my $wait_data (@$hold_data) {
1424 my $count += $wait_data->{count};
1425 $combined_secs += $count *
1426 OpenSRF::Utils::interval_to_seconds($wait_data->{avg_wait_time} || $default_wait);
1427 $num_potentials += $count;
1430 my $estimated_wait = -1;
1432 if($num_potentials) {
1433 my $avg_wait = $combined_secs / $num_potentials;
1434 $estimated_wait = $qpos * ($avg_wait / $num_potentials);
1435 $estimated_wait = $min_wait if $estimated_wait < $min_wait and $estimated_wait != -1;
1439 total_holds => scalar(@$q_holds),
1440 queue_position => $qpos,
1441 potential_copies => $num_potentials,
1442 status => _hold_status( $e, $hold ),
1443 estimated_wait => int($estimated_wait)
1448 sub fetch_open_hold_by_current_copy {
1451 my $hold = $apputils->simplereq(
1453 'open-ils.cstore.direct.action.hold_request.search.atomic',
1454 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
1455 return $hold->[0] if ref($hold);
1459 sub fetch_related_holds {
1462 return $apputils->simplereq(
1464 'open-ils.cstore.direct.action.hold_request.search.atomic',
1465 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
1469 __PACKAGE__->register_method(
1470 method => "hold_pull_list",
1471 api_name => "open-ils.circ.hold_pull_list.retrieve",
1473 desc => 'Returns (reference to) a list of holds that need to be "pulled" by a given location. ' .
1474 'The location is determined by the login session.',
1476 { desc => 'Limit (optional)', type => 'number'},
1477 { desc => 'Offset (optional)', type => 'number'},
1480 desc => 'reference to a list of holds, or event on failure',
1485 __PACKAGE__->register_method(
1486 method => "hold_pull_list",
1487 api_name => "open-ils.circ.hold_pull_list.id_list.retrieve",
1489 desc => 'Returns (reference to) a list of holds IDs that need to be "pulled" by a given location. ' .
1490 'The location is determined by the login session.',
1492 { desc => 'Limit (optional)', type => 'number'},
1493 { desc => 'Offset (optional)', type => 'number'},
1496 desc => 'reference to a list of holds, or event on failure',
1501 __PACKAGE__->register_method(
1502 method => "hold_pull_list",
1503 api_name => "open-ils.circ.hold_pull_list.retrieve.count",
1505 desc => 'Returns a count of holds that need to be "pulled" by a given location. ' .
1506 'The location is determined by the login session.',
1508 { desc => 'Limit (optional)', type => 'number'},
1509 { desc => 'Offset (optional)', type => 'number'},
1512 desc => 'Holds count (integer), or event on failure',
1519 sub hold_pull_list {
1520 my( $self, $conn, $authtoken, $limit, $offset ) = @_;
1521 my( $reqr, $evt ) = $U->checkses($authtoken);
1522 return $evt if $evt;
1524 my $org = $reqr->ws_ou || $reqr->home_ou;
1525 # the perm locaiton shouldn't really matter here since holds
1526 # will exist all over and VIEW_HOLDS should be universal
1527 $evt = $U->check_perms($reqr->id, $org, 'VIEW_HOLD');
1528 return $evt if $evt;
1530 if($self->api_name =~ /count/) {
1532 my $count = $U->storagereq(
1533 'open-ils.storage.direct.action.hold_request.pull_list.current_copy_circ_lib.status_filtered.count',
1534 $org, $limit, $offset );
1536 $logger->info("Grabbing pull list for org unit $org with $count items");
1539 } elsif( $self->api_name =~ /id_list/ ) {
1540 return $U->storagereq(
1541 'open-ils.storage.direct.action.hold_request.pull_list.id_list.current_copy_circ_lib.status_filtered.atomic',
1542 $org, $limit, $offset );
1545 return $U->storagereq(
1546 'open-ils.storage.direct.action.hold_request.pull_list.search.current_copy_circ_lib.status_filtered.atomic',
1547 $org, $limit, $offset );
1551 __PACKAGE__->register_method(
1552 method => "print_hold_pull_list",
1553 api_name => "open-ils.circ.hold_pull_list.print",
1555 desc => 'Returns an HTML-formatted holds pull list',
1557 { desc => 'Authtoken', type => 'string'},
1558 { desc => 'Org unit ID. Optional, defaults to workstation org unit', type => 'number'},
1561 desc => 'HTML string',
1567 sub print_hold_pull_list {
1568 my($self, $client, $auth, $org_id) = @_;
1570 my $e = new_editor(authtoken=>$auth);
1571 return $e->event unless $e->checkauth;
1573 $org_id = (defined $org_id) ? $org_id : $e->requestor->ws_ou;
1574 return $e->event unless $e->allowed('VIEW_HOLD', $org_id);
1576 my $hold_ids = $U->storagereq(
1577 'open-ils.storage.direct.action.hold_request.pull_list.id_list.current_copy_circ_lib.status_filtered.atomic',
1580 return undef unless @$hold_ids;
1582 $client->status(new OpenSRF::DomainObject::oilsContinueStatus);
1584 # Holds will /NOT/ be in order after this ...
1585 my $holds = $e->search_action_hold_request({id => $hold_ids}, {substream => 1});
1586 $client->status(new OpenSRF::DomainObject::oilsContinueStatus);
1588 # ... so we must resort.
1589 my $hold_map = +{map { $_->id => $_ } @$holds};
1590 my $sorted_holds = [];
1591 push @$sorted_holds, $hold_map->{$_} foreach @$hold_ids;
1593 return $U->fire_object_event(
1594 undef, "ahr.format.pull_list", $sorted_holds,
1595 $org_id, undef, undef, $client
1600 __PACKAGE__->register_method(
1601 method => "print_hold_pull_list_stream",
1603 api_name => "open-ils.circ.hold_pull_list.print.stream",
1605 desc => 'Returns a stream of fleshed holds',
1607 { desc => 'Authtoken', type => 'string'},
1608 { 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)',
1613 desc => 'A stream of fleshed holds',
1619 sub print_hold_pull_list_stream {
1620 my($self, $client, $auth, $params) = @_;
1622 my $e = new_editor(authtoken=>$auth);
1623 return $e->die_event unless $e->checkauth;
1625 delete($$params{org_id}) unless (int($$params{org_id}));
1626 delete($$params{limit}) unless (int($$params{limit}));
1627 delete($$params{offset}) unless (int($$params{offset}));
1628 delete($$params{chunk_size}) unless (int($$params{chunk_size}));
1629 delete($$params{chunk_size}) if ($$params{chunk_size} && $$params{chunk_size} > 50); # keep the size reasonable
1630 $$params{chunk_size} ||= 10;
1632 $$params{org_id} = (defined $$params{org_id}) ? $$params{org_id}: $e->requestor->ws_ou;
1633 return $e->die_event unless $e->allowed('VIEW_HOLD', $$params{org_id });
1636 if ($$params{sort} && @{ $$params{sort} }) {
1637 for my $s (@{ $$params{sort} }) {
1638 if ($s eq 'acplo.position') {
1640 "class" => "acplo", "field" => "position",
1641 "transform" => "coalesce", "params" => [999]
1643 } elsif ($s eq 'prefix') {
1644 push @$sort, {"class" => "acnp", "field" => "label_sortkey"};
1645 } elsif ($s eq 'call_number') {
1646 push @$sort, {"class" => "acn", "field" => "label_sortkey"};
1647 } elsif ($s eq 'suffix') {
1648 push @$sort, {"class" => "acns", "field" => "label_sortkey"};
1649 } elsif ($s eq 'request_time') {
1650 push @$sort, {"class" => "ahr", "field" => "request_time"};
1654 push @$sort, {"class" => "ahr", "field" => "request_time"};
1657 my $holds_ids = $e->json_query(
1659 "select" => {"ahr" => ["id"]},
1664 "fkey" => "current_copy",
1666 "circ_lib" => $$params{org_id}, "status" => [0,7]
1671 "fkey" => "call_number",
1685 "fkey" => "circ_lib",
1688 "location" => {"=" => {"+acp" => "location"}}
1697 "capture_time" => undef,
1698 "cancel_time" => undef,
1700 {"expire_time" => undef },
1701 {"expire_time" => {">" => "now"}}
1705 (@$sort ? (order_by => $sort) : ()),
1706 ($$params{limit} ? (limit => $$params{limit}) : ()),
1707 ($$params{offset} ? (offset => $$params{offset}) : ())
1708 }, {"substream" => 1}
1709 ) or return $e->die_event;
1711 $logger->info("about to stream back " . scalar(@$holds_ids) . " holds");
1714 for my $hid (@$holds_ids) {
1715 push @chunk, $e->retrieve_action_hold_request([
1719 "ahr" => ["usr", "current_copy"],
1721 "acp" => ["location", "call_number", "parts"],
1722 "acn" => ["record","prefix","suffix"]
1727 if (@chunk >= $$params{chunk_size}) {
1728 $client->respond( \@chunk );
1732 $client->respond_complete( \@chunk ) if (@chunk);
1739 __PACKAGE__->register_method(
1740 method => 'fetch_hold_notify',
1741 api_name => 'open-ils.circ.hold_notification.retrieve_by_hold',
1744 Returns a list of hold notification objects based on hold id.
1745 @param authtoken The loggin session key
1746 @param holdid The id of the hold whose notifications we want to retrieve
1747 @return An array of hold notification objects, event on error.
1751 sub fetch_hold_notify {
1752 my( $self, $conn, $authtoken, $holdid ) = @_;
1753 my( $requestor, $evt ) = $U->checkses($authtoken);
1754 return $evt if $evt;
1755 my ($hold, $patron);
1756 ($hold, $evt) = $U->fetch_hold($holdid);
1757 return $evt if $evt;
1758 ($patron, $evt) = $U->fetch_user($hold->usr);
1759 return $evt if $evt;
1761 $evt = $U->check_perms($requestor->id, $patron->home_ou, 'VIEW_HOLD_NOTIFICATION');
1762 return $evt if $evt;
1764 $logger->info("User ".$requestor->id." fetching hold notifications for hold $holdid");
1765 return $U->cstorereq(
1766 'open-ils.cstore.direct.action.hold_notification.search.atomic', {hold => $holdid} );
1770 __PACKAGE__->register_method(
1771 method => 'create_hold_notify',
1772 api_name => 'open-ils.circ.hold_notification.create',
1774 Creates a new hold notification object
1775 @param authtoken The login session key
1776 @param notification The hold notification object to create
1777 @return ID of the new object on success, Event on error
1781 sub create_hold_notify {
1782 my( $self, $conn, $auth, $note ) = @_;
1783 my $e = new_editor(authtoken=>$auth, xact=>1);
1784 return $e->die_event unless $e->checkauth;
1786 my $hold = $e->retrieve_action_hold_request($note->hold)
1787 or return $e->die_event;
1788 my $patron = $e->retrieve_actor_user($hold->usr)
1789 or return $e->die_event;
1791 return $e->die_event unless
1792 $e->allowed('CREATE_HOLD_NOTIFICATION', $patron->home_ou);
1794 $note->notify_staff($e->requestor->id);
1795 $e->create_action_hold_notification($note) or return $e->die_event;
1800 __PACKAGE__->register_method(
1801 method => 'create_hold_note',
1802 api_name => 'open-ils.circ.hold_note.create',
1804 Creates a new hold request note object
1805 @param authtoken The login session key
1806 @param note The hold note object to create
1807 @return ID of the new object on success, Event on error
1811 sub create_hold_note {
1812 my( $self, $conn, $auth, $note ) = @_;
1813 my $e = new_editor(authtoken=>$auth, xact=>1);
1814 return $e->die_event unless $e->checkauth;
1816 my $hold = $e->retrieve_action_hold_request($note->hold)
1817 or return $e->die_event;
1818 my $patron = $e->retrieve_actor_user($hold->usr)
1819 or return $e->die_event;
1821 return $e->die_event unless
1822 $e->allowed('UPDATE_HOLD', $patron->home_ou); # FIXME: Using permcrud perm listed in fm_IDL.xml for ahrn. Probably want something more specific
1824 $e->create_action_hold_request_note($note) or return $e->die_event;
1829 __PACKAGE__->register_method(
1830 method => 'reset_hold',
1831 api_name => 'open-ils.circ.hold.reset',
1833 Un-captures and un-targets a hold, essentially returning
1834 it to the state it was in directly after it was placed,
1835 then attempts to re-target the hold
1836 @param authtoken The login session key
1837 @param holdid The id of the hold
1843 my( $self, $conn, $auth, $holdid ) = @_;
1845 my ($hold, $evt) = $U->fetch_hold($holdid);
1846 return $evt if $evt;
1847 ($reqr, $evt) = $U->checksesperm($auth, 'UPDATE_HOLD');
1848 return $evt if $evt;
1849 $evt = _reset_hold($self, $reqr, $hold);
1850 return $evt if $evt;
1855 __PACKAGE__->register_method(
1856 method => 'reset_hold_batch',
1857 api_name => 'open-ils.circ.hold.reset.batch'
1860 sub reset_hold_batch {
1861 my($self, $conn, $auth, $hold_ids) = @_;
1863 my $e = new_editor(authtoken => $auth);
1864 return $e->event unless $e->checkauth;
1866 for my $hold_id ($hold_ids) {
1868 my $hold = $e->retrieve_action_hold_request(
1869 [$hold_id, {flesh => 1, flesh_fields => {ahr => ['usr']}}])
1870 or return $e->event;
1872 next unless $e->allowed('UPDATE_HOLD', $hold->usr->home_ou);
1873 _reset_hold($self, $e->requestor, $hold);
1881 my ($self, $reqr, $hold) = @_;
1883 my $e = new_editor(xact =>1, requestor => $reqr);
1885 $logger->info("reseting hold ".$hold->id);
1887 my $hid = $hold->id;
1889 if( $hold->capture_time and $hold->current_copy ) {
1891 my $copy = $e->retrieve_asset_copy($hold->current_copy)
1892 or return $e->die_event;
1894 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
1895 $logger->info("setting copy to status 'reshelving' on hold retarget");
1896 $copy->status(OILS_COPY_STATUS_RESHELVING);
1897 $copy->editor($e->requestor->id);
1898 $copy->edit_date('now');
1899 $e->update_asset_copy($copy) or return $e->die_event;
1901 } elsif( $copy->status == OILS_COPY_STATUS_IN_TRANSIT ) {
1903 # We don't want the copy to remain "in transit"
1904 $copy->status(OILS_COPY_STATUS_RESHELVING);
1905 $logger->warn("! reseting hold [$hid] that is in transit");
1906 my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id},{idlist=>1})->[0];
1909 my $trans = $e->retrieve_action_transit_copy($transid);
1911 $logger->info("Aborting transit [$transid] on hold [$hid] reset...");
1912 my $evt = OpenILS::Application::Circ::Transit::__abort_transit($e, $trans, $copy, 1, 1);
1913 $logger->info("Transit abort completed with result $evt");
1914 unless ("$evt" eq 1) {
1923 $hold->clear_capture_time;
1924 $hold->clear_current_copy;
1925 $hold->clear_shelf_time;
1926 $hold->clear_shelf_expire_time;
1927 $hold->clear_current_shelf_lib;
1929 $e->update_action_hold_request($hold) or return $e->die_event;
1933 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
1939 __PACKAGE__->register_method(
1940 method => 'fetch_open_title_holds',
1941 api_name => 'open-ils.circ.open_holds.retrieve',
1943 Returns a list ids of un-fulfilled holds for a given title id
1944 @param authtoken The login session key
1945 @param id the id of the item whose holds we want to retrieve
1946 @param type The hold type - M, T, I, V, C, F, R
1950 sub fetch_open_title_holds {
1951 my( $self, $conn, $auth, $id, $type, $org ) = @_;
1952 my $e = new_editor( authtoken => $auth );
1953 return $e->event unless $e->checkauth;
1956 $org ||= $e->requestor->ws_ou;
1958 # return $e->search_action_hold_request(
1959 # { target => $id, hold_type => $type, fulfillment_time => undef }, {idlist=>1});
1961 # XXX make me return IDs in the future ^--
1962 my $holds = $e->search_action_hold_request(
1965 cancel_time => undef,
1967 fulfillment_time => undef
1971 flesh_hold_transits($holds);
1976 sub flesh_hold_transits {
1978 for my $hold ( @$holds ) {
1980 $apputils->simplereq(
1982 "open-ils.cstore.direct.action.hold_transit_copy.search.atomic",
1983 { hold => $hold->id },
1984 { order_by => { ahtc => 'id desc' }, limit => 1 }
1990 sub flesh_hold_notices {
1991 my( $holds, $e ) = @_;
1992 $e ||= new_editor();
1994 for my $hold (@$holds) {
1995 my $notices = $e->search_action_hold_notification(
1997 { hold => $hold->id },
1998 { order_by => { anh => 'notify_time desc' } },
2003 $hold->notify_count(scalar(@$notices));
2005 my $n = $e->retrieve_action_hold_notification($$notices[0])
2006 or return $e->event;
2007 $hold->notify_time($n->notify_time);
2013 __PACKAGE__->register_method(
2014 method => 'fetch_captured_holds',
2015 api_name => 'open-ils.circ.captured_holds.on_shelf.retrieve',
2019 Returns a list of un-fulfilled holds (on the Holds Shelf) for a given title id
2020 @param authtoken The login session key
2021 @param org The org id of the location in question
2022 @param match_copy A specific copy to limit to
2026 __PACKAGE__->register_method(
2027 method => 'fetch_captured_holds',
2028 api_name => 'open-ils.circ.captured_holds.id_list.on_shelf.retrieve',
2032 Returns list ids of un-fulfilled holds (on the Holds Shelf) for a given title id
2033 @param authtoken The login session key
2034 @param org The org id of the location in question
2035 @param match_copy A specific copy to limit to
2039 __PACKAGE__->register_method(
2040 method => 'fetch_captured_holds',
2041 api_name => 'open-ils.circ.captured_holds.id_list.expired_on_shelf.retrieve',
2045 Returns list ids of shelf-expired un-fulfilled holds for a given title id
2046 @param authtoken The login session key
2047 @param org The org id of the location in question
2048 @param match_copy A specific copy to limit to
2053 sub fetch_captured_holds {
2054 my( $self, $conn, $auth, $org, $match_copy ) = @_;
2056 my $e = new_editor(authtoken => $auth);
2057 return $e->die_event unless $e->checkauth;
2058 return $e->die_event unless $e->allowed('VIEW_HOLD'); # XXX rely on editor perm
2060 $org ||= $e->requestor->ws_ou;
2062 my $current_copy = { '!=' => undef };
2063 $current_copy = { '=' => $match_copy } if $match_copy;
2066 select => { alhr => ['id'] },
2071 fkey => 'current_copy'
2076 '+acp' => { status => OILS_COPY_STATUS_ON_HOLDS_SHELF },
2078 capture_time => { "!=" => undef },
2079 current_copy => $current_copy,
2080 fulfillment_time => undef,
2081 current_shelf_lib => $org
2085 if($self->api_name =~ /expired/) {
2086 $query->{'where'}->{'+alhr'}->{'-or'} = {
2087 shelf_expire_time => { '<' => 'now'},
2088 cancel_time => { '!=' => undef },
2091 my $hold_ids = $e->json_query( $query );
2093 for my $hold_id (@$hold_ids) {
2094 if($self->api_name =~ /id_list/) {
2095 $conn->respond($hold_id->{id});
2099 $e->retrieve_action_hold_request([
2103 flesh_fields => {ahr => ['notifications', 'transit', 'notes']},
2104 order_by => {anh => 'notify_time desc'}
2114 __PACKAGE__->register_method(
2115 method => "print_expired_holds_stream",
2116 api_name => "open-ils.circ.captured_holds.expired.print.stream",
2120 sub print_expired_holds_stream {
2121 my ($self, $client, $auth, $params) = @_;
2123 # No need to check specific permissions: we're going to call another method
2124 # that will do that.
2125 my $e = new_editor("authtoken" => $auth);
2126 return $e->die_event unless $e->checkauth;
2128 delete($$params{org_id}) unless (int($$params{org_id}));
2129 delete($$params{limit}) unless (int($$params{limit}));
2130 delete($$params{offset}) unless (int($$params{offset}));
2131 delete($$params{chunk_size}) unless (int($$params{chunk_size}));
2132 delete($$params{chunk_size}) if ($$params{chunk_size} && $$params{chunk_size} > 50); # keep the size reasonable
2133 $$params{chunk_size} ||= 10;
2135 $$params{org_id} = (defined $$params{org_id}) ? $$params{org_id}: $e->requestor->ws_ou;
2137 my @hold_ids = $self->method_lookup(
2138 "open-ils.circ.captured_holds.id_list.expired_on_shelf.retrieve"
2139 )->run($auth, $params->{"org_id"});
2144 } elsif (defined $U->event_code($hold_ids[0])) {
2146 return $hold_ids[0];
2149 $logger->info("about to stream back up to " . scalar(@hold_ids) . " expired holds");
2152 my @hid_chunk = splice @hold_ids, 0, $params->{"chunk_size"};
2154 my $result_chunk = $e->json_query({
2156 "acp" => ["barcode"],
2158 first_given_name second_given_name family_name alias
2167 "field" => "id", "fkey" => "current_copy",
2170 "field" => "id", "fkey" => "call_number",
2173 "field" => "id", "fkey" => "record"
2177 "acpl" => {"field" => "id", "fkey" => "location"}
2180 "au" => {"field" => "id", "fkey" => "usr"}
2183 "where" => {"+ahr" => {"id" => \@hid_chunk}}
2184 }) or return $e->die_event;
2185 $client->respond($result_chunk);
2192 __PACKAGE__->register_method(
2193 method => "check_title_hold_batch",
2194 api_name => "open-ils.circ.title_hold.is_possible.batch",
2197 desc => '@see open-ils.circ.title_hold.is_possible.batch',
2199 { desc => 'Authentication token', type => 'string'},
2200 { desc => 'Array of Hash of named parameters', type => 'array'},
2203 desc => 'Array of response objects',
2209 sub check_title_hold_batch {
2210 my($self, $client, $authtoken, $param_list, $oargs) = @_;
2211 foreach (@$param_list) {
2212 my ($res) = $self->method_lookup('open-ils.circ.title_hold.is_possible')->run($authtoken, $_, $oargs);
2213 $client->respond($res);
2219 __PACKAGE__->register_method(
2220 method => "check_title_hold",
2221 api_name => "open-ils.circ.title_hold.is_possible",
2223 desc => 'Determines if a hold were to be placed by a given user, ' .
2224 'whether or not said hold would have any potential copies to fulfill it.' .
2225 'The named paramaters of the second argument include: ' .
2226 'patronid, titleid, volume_id, copy_id, mrid, depth, pickup_lib, hold_type, selection_ou. ' .
2227 'See perldoc ' . __PACKAGE__ . ' for more info on these fields.' ,
2229 { desc => 'Authentication token', type => 'string'},
2230 { desc => 'Hash of named parameters', type => 'object'},
2233 desc => 'List of new message IDs (empty if none)',
2239 =head3 check_title_hold (token, hash)
2241 The named fields in the hash are:
2243 patronid - ID of the hold recipient (required)
2244 depth - hold range depth (default 0)
2245 pickup_lib - destination for hold, fallback value for selection_ou
2246 selection_ou - ID of org_unit establishing hard and soft hold boundary settings
2247 issuanceid - ID of the issuance to be held, required for Issuance level hold
2248 partid - ID of the monograph part to be held, required for monograph part level hold
2249 titleid - ID (BRN) of the title to be held, required for Title level hold
2250 volume_id - required for Volume level hold
2251 copy_id - required for Copy level hold
2252 mrid - required for Meta-record level hold
2253 hold_type - T, C (or R or F), I, V or M for Title, Copy, Issuance, Volume or Meta-record (default "T")
2255 All key/value pairs are passed on to do_possibility_checks.
2259 # FIXME: better params checking. what other params are required, if any?
2260 # FIXME: 3 copies of values confusing: $x, $params->{x} and $params{x}
2261 # FIXME: for example, $depth gets a default value, but then $$params{depth} is still
2262 # used in conditionals, where it may be undefined, causing a warning.
2263 # FIXME: specify proper usage/interaction of selection_ou and pickup_lib
2265 sub check_title_hold {
2266 my( $self, $client, $authtoken, $params ) = @_;
2267 my $e = new_editor(authtoken=>$authtoken);
2268 return $e->event unless $e->checkauth;
2270 my %params = %$params;
2271 my $depth = $params{depth} || 0;
2272 my $selection_ou = $params{selection_ou} || $params{pickup_lib};
2273 my $oargs = $params{oargs} || {};
2275 if($oargs->{events}) {
2276 @{$oargs->{events}} = grep { $e->allowed($_ . '.override', $e->requestor->ws_ou); } @{$oargs->{events}};
2280 my $patron = $e->retrieve_actor_user($params{patronid})
2281 or return $e->event;
2283 if( $e->requestor->id ne $patron->id ) {
2284 return $e->event unless
2285 $e->allowed('VIEW_HOLD_PERMIT', $patron->home_ou);
2288 return OpenILS::Event->new('PATRON_BARRED') if $U->is_true($patron->barred);
2290 my $request_lib = $e->retrieve_actor_org_unit($e->requestor->ws_ou)
2291 or return $e->event;
2293 my $soft_boundary = $U->ou_ancestor_setting_value($selection_ou, OILS_SETTING_HOLD_SOFT_BOUNDARY);
2294 my $hard_boundary = $U->ou_ancestor_setting_value($selection_ou, OILS_SETTING_HOLD_HARD_BOUNDARY);
2297 my $return_depth = $hard_boundary; # default depth to return on success
2298 if(defined $soft_boundary and $depth < $soft_boundary) {
2299 # work up the tree and as soon as we find a potential copy, use that depth
2300 # also, make sure we don't go past the hard boundary if it exists
2302 # our min boundary is the greater of user-specified boundary or hard boundary
2303 my $min_depth = (defined $hard_boundary and $hard_boundary > $depth) ?
2304 $hard_boundary : $depth;
2306 my $depth = $soft_boundary;
2307 while($depth >= $min_depth) {
2308 $logger->info("performing hold possibility check with soft boundary $depth");
2309 @status = do_possibility_checks($e, $patron, $request_lib, $depth, %params);
2311 $return_depth = $depth;
2316 } elsif(defined $hard_boundary and $depth < $hard_boundary) {
2317 # there is no soft boundary, enforce the hard boundary if it exists
2318 $logger->info("performing hold possibility check with hard boundary $hard_boundary");
2319 @status = do_possibility_checks($e, $patron, $request_lib, $hard_boundary, %params);
2321 # no boundaries defined, fall back to user specifed boundary or no boundary
2322 $logger->info("performing hold possibility check with no boundary");
2323 @status = do_possibility_checks($e, $patron, $request_lib, $params{depth}, %params);
2326 my $place_unfillable = 0;
2327 $place_unfillable = 1 if $e->allowed('PLACE_UNFILLABLE_HOLD', $e->requestor->ws_ou);
2332 "depth" => $return_depth,
2333 "local_avail" => $status[1]
2335 } elsif ($status[2]) {
2336 my $n = scalar @{$status[2]};
2337 return {"success" => 0, "last_event" => $status[2]->[$n - 1], "age_protected_copy" => $status[3], "place_unfillable" => $place_unfillable};
2339 return {"success" => 0, "age_protected_copy" => $status[3], "place_unfillable" => $place_unfillable};
2345 sub do_possibility_checks {
2346 my($e, $patron, $request_lib, $depth, %params) = @_;
2348 my $issuanceid = $params{issuanceid} || "";
2349 my $partid = $params{partid} || "";
2350 my $titleid = $params{titleid} || "";
2351 my $volid = $params{volume_id};
2352 my $copyid = $params{copy_id};
2353 my $mrid = $params{mrid} || "";
2354 my $pickup_lib = $params{pickup_lib};
2355 my $hold_type = $params{hold_type} || 'T';
2356 my $selection_ou = $params{selection_ou} || $pickup_lib;
2357 my $holdable_formats = $params{holdable_formats};
2358 my $oargs = $params{oargs} || {};
2365 if( $hold_type eq OILS_HOLD_TYPE_FORCE || $hold_type eq OILS_HOLD_TYPE_RECALL || $hold_type eq OILS_HOLD_TYPE_COPY ) {
2367 return $e->event unless $copy = $e->retrieve_asset_copy($copyid);
2368 return $e->event unless $volume = $e->retrieve_asset_call_number($copy->call_number);
2369 return $e->event unless $title = $e->retrieve_biblio_record_entry($volume->record);
2371 return (1, 1, []) if( $hold_type eq OILS_HOLD_TYPE_RECALL || $hold_type eq OILS_HOLD_TYPE_FORCE);
2372 return verify_copy_for_hold(
2373 $patron, $e->requestor, $title, $copy, $pickup_lib, $request_lib, $oargs
2376 } elsif( $hold_type eq OILS_HOLD_TYPE_VOLUME ) {
2378 return $e->event unless $volume = $e->retrieve_asset_call_number($volid);
2379 return $e->event unless $title = $e->retrieve_biblio_record_entry($volume->record);
2381 return _check_volume_hold_is_possible(
2382 $volume, $title, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou, $oargs
2385 } elsif( $hold_type eq OILS_HOLD_TYPE_TITLE ) {
2387 return _check_title_hold_is_possible(
2388 $titleid, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou, undef, $oargs
2391 } elsif( $hold_type eq OILS_HOLD_TYPE_ISSUANCE ) {
2393 return _check_issuance_hold_is_possible(
2394 $issuanceid, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou, $oargs
2397 } elsif( $hold_type eq OILS_HOLD_TYPE_MONOPART ) {
2399 return _check_monopart_hold_is_possible(
2400 $partid, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou, $oargs
2403 } elsif( $hold_type eq OILS_HOLD_TYPE_METARECORD ) {
2405 my $maps = $e->search_metabib_metarecord_source_map({metarecord=>$mrid});
2406 my @recs = map { $_->source } @$maps;
2408 for my $rec (@recs) {
2409 @status = _check_title_hold_is_possible(
2410 $rec, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou, $holdable_formats, $oargs
2416 # else { Unrecognized hold_type ! } # FIXME: return error? or 0?
2420 sub create_ranged_org_filter {
2421 my($e, $selection_ou, $depth) = @_;
2423 # find the orgs from which this hold may be fulfilled,
2424 # based on the selection_ou and depth
2426 my $top_org = $e->search_actor_org_unit([
2427 {parent_ou => undef},
2428 {flesh=>1, flesh_fields=>{aou=>['ou_type']}}])->[0];
2431 return () if $depth == $top_org->ou_type->depth;
2433 my $org_list = $U->storagereq('open-ils.storage.actor.org_unit.descendants.atomic', $selection_ou, $depth);
2434 %org_filter = (circ_lib => []);
2435 push(@{$org_filter{circ_lib}}, $_->id) for @$org_list;
2437 $logger->info("hold org filter at depth $depth and selection_ou ".
2438 "$selection_ou created list of @{$org_filter{circ_lib}}");
2444 sub _check_title_hold_is_possible {
2445 my( $titleid, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou, $holdable_formats, $oargs ) = @_;
2447 my ($types, $formats, $lang);
2448 if (defined($holdable_formats)) {
2449 ($types, $formats, $lang) = split '-', $holdable_formats;
2452 my $e = new_editor();
2453 my %org_filter = create_ranged_org_filter($e, $selection_ou, $depth);
2455 # this monster will grab the id and circ_lib of all of the "holdable" copies for the given record
2456 my $copies = $e->json_query(
2458 select => { acp => ['id', 'circ_lib'] },
2463 fkey => 'call_number',
2467 filter => { id => $titleid },
2475 ( $types ? (item_type => [split '', $types]) : () ),
2476 ( $formats ? (item_form => [split '', $formats]) : () ),
2477 ( $lang ? (item_lang => $lang) : () )
2482 acpl => { field => 'id', filter => { holdable => 't'}, fkey => 'location' },
2483 ccs => { field => 'id', filter => { holdable => 't'}, fkey => 'status' },
2484 acpm => { field => 'target_copy', type => 'left' } # ignore part-linked copies
2488 '+acp' => { circulate => 't', deleted => 'f', holdable => 't', %org_filter },
2489 '+acpm' => { target_copy => undef } # ignore part-linked copies
2494 $logger->info("title possible found ".scalar(@$copies)." potential copies");
2498 "HIGH_LEVEL_HOLD_HAS_NO_COPIES",
2499 "payload" => {"fail_part" => "no_ultimate_items"}
2504 # -----------------------------------------------------------------------
2505 # sort the copies into buckets based on their circ_lib proximity to
2506 # the patron's home_ou.
2507 # -----------------------------------------------------------------------
2509 my $home_org = $patron->home_ou;
2510 my $req_org = $request_lib->id;
2512 $logger->info("prox cache $home_org " . $prox_cache{$home_org});
2514 $prox_cache{$home_org} =
2515 $e->search_actor_org_unit_proximity({from_org => $home_org})
2516 unless $prox_cache{$home_org};
2517 my $home_prox = $prox_cache{$home_org};
2520 my %hash = map { ($_->to_org => $_->prox) } @$home_prox;
2521 push( @{$buckets{ $hash{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2523 my @keys = sort { $a <=> $b } keys %buckets;
2526 if( $home_org ne $req_org ) {
2527 # -----------------------------------------------------------------------
2528 # shove the copies close to the request_lib into the primary buckets
2529 # directly before the farthest away copies. That way, they are not
2530 # given priority, but they are checked before the farthest copies.
2531 # -----------------------------------------------------------------------
2532 $prox_cache{$req_org} =
2533 $e->search_actor_org_unit_proximity({from_org => $req_org})
2534 unless $prox_cache{$req_org};
2535 my $req_prox = $prox_cache{$req_org};
2538 my %hash2 = map { ($_->to_org => $_->prox) } @$req_prox;
2539 push( @{$buckets2{ $hash2{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2541 my $highest_key = $keys[@keys - 1]; # the farthest prox in the exising buckets
2542 my $new_key = $highest_key - 0.5; # right before the farthest prox
2543 my @keys2 = sort { $a <=> $b } keys %buckets2;
2544 for my $key (@keys2) {
2545 last if $key >= $highest_key;
2546 push( @{$buckets{$new_key}}, $_ ) for @{$buckets2{$key}};
2550 @keys = sort { $a <=> $b } keys %buckets;
2555 my $age_protect_only = 0;
2556 OUTER: for my $key (@keys) {
2557 my @cps = @{$buckets{$key}};
2559 $logger->info("looking at " . scalar(@{$buckets{$key}}). " copies in proximity bucket $key");
2561 for my $copyid (@cps) {
2563 next if $seen{$copyid};
2564 $seen{$copyid} = 1; # there could be dupes given the merged buckets
2565 my $copy = $e->retrieve_asset_copy($copyid);
2566 $logger->debug("looking at bucket_key=$key, copy $copyid : circ_lib = " . $copy->circ_lib);
2568 unless($title) { # grab the title if we don't already have it
2569 my $vol = $e->retrieve_asset_call_number(
2570 [ $copy->call_number, { flesh => 1, flesh_fields => { bre => ['fixed_fields'], acn => ['record'] } } ] );
2571 $title = $vol->record;
2574 @status = verify_copy_for_hold(
2575 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib, $oargs);
2577 $age_protect_only ||= $status[3];
2578 last OUTER if $status[0];
2582 $status[3] = $age_protect_only;
2586 sub _check_issuance_hold_is_possible {
2587 my( $issuanceid, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou, $oargs ) = @_;
2589 my $e = new_editor();
2590 my %org_filter = create_ranged_org_filter($e, $selection_ou, $depth);
2592 # this monster will grab the id and circ_lib of all of the "holdable" copies for the given record
2593 my $copies = $e->json_query(
2595 select => { acp => ['id', 'circ_lib'] },
2601 filter => { issuance => $issuanceid }
2603 acpl => { field => 'id', filter => { holdable => 't'}, fkey => 'location' },
2604 ccs => { field => 'id', filter => { holdable => 't'}, fkey => 'status' }
2608 '+acp' => { circulate => 't', deleted => 'f', holdable => 't', %org_filter }
2614 $logger->info("issuance possible found ".scalar(@$copies)." potential copies");
2618 $empty_ok = $e->retrieve_config_global_flag('circ.holds.empty_issuance_ok');
2619 $empty_ok = ($empty_ok and $U->is_true($empty_ok->enabled));
2624 "HIGH_LEVEL_HOLD_HAS_NO_COPIES",
2625 "payload" => {"fail_part" => "no_ultimate_items"}
2633 # -----------------------------------------------------------------------
2634 # sort the copies into buckets based on their circ_lib proximity to
2635 # the patron's home_ou.
2636 # -----------------------------------------------------------------------
2638 my $home_org = $patron->home_ou;
2639 my $req_org = $request_lib->id;
2641 $logger->info("prox cache $home_org " . $prox_cache{$home_org});
2643 $prox_cache{$home_org} =
2644 $e->search_actor_org_unit_proximity({from_org => $home_org})
2645 unless $prox_cache{$home_org};
2646 my $home_prox = $prox_cache{$home_org};
2649 my %hash = map { ($_->to_org => $_->prox) } @$home_prox;
2650 push( @{$buckets{ $hash{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2652 my @keys = sort { $a <=> $b } keys %buckets;
2655 if( $home_org ne $req_org ) {
2656 # -----------------------------------------------------------------------
2657 # shove the copies close to the request_lib into the primary buckets
2658 # directly before the farthest away copies. That way, they are not
2659 # given priority, but they are checked before the farthest copies.
2660 # -----------------------------------------------------------------------
2661 $prox_cache{$req_org} =
2662 $e->search_actor_org_unit_proximity({from_org => $req_org})
2663 unless $prox_cache{$req_org};
2664 my $req_prox = $prox_cache{$req_org};
2667 my %hash2 = map { ($_->to_org => $_->prox) } @$req_prox;
2668 push( @{$buckets2{ $hash2{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2670 my $highest_key = $keys[@keys - 1]; # the farthest prox in the exising buckets
2671 my $new_key = $highest_key - 0.5; # right before the farthest prox
2672 my @keys2 = sort { $a <=> $b } keys %buckets2;
2673 for my $key (@keys2) {
2674 last if $key >= $highest_key;
2675 push( @{$buckets{$new_key}}, $_ ) for @{$buckets2{$key}};
2679 @keys = sort { $a <=> $b } keys %buckets;
2684 my $age_protect_only = 0;
2685 OUTER: for my $key (@keys) {
2686 my @cps = @{$buckets{$key}};
2688 $logger->info("looking at " . scalar(@{$buckets{$key}}). " copies in proximity bucket $key");
2690 for my $copyid (@cps) {
2692 next if $seen{$copyid};
2693 $seen{$copyid} = 1; # there could be dupes given the merged buckets
2694 my $copy = $e->retrieve_asset_copy($copyid);
2695 $logger->debug("looking at bucket_key=$key, copy $copyid : circ_lib = " . $copy->circ_lib);
2697 unless($title) { # grab the title if we don't already have it
2698 my $vol = $e->retrieve_asset_call_number(
2699 [ $copy->call_number, { flesh => 1, flesh_fields => { bre => ['fixed_fields'], acn => ['record'] } } ] );
2700 $title = $vol->record;
2703 @status = verify_copy_for_hold(
2704 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib, $oargs);
2706 $age_protect_only ||= $status[3];
2707 last OUTER if $status[0];
2712 if (!defined($empty_ok)) {
2713 $empty_ok = $e->retrieve_config_global_flag('circ.holds.empty_issuance_ok');
2714 $empty_ok = ($empty_ok and $U->is_true($empty_ok->enabled));
2717 return (1,0) if ($empty_ok);
2719 $status[3] = $age_protect_only;
2723 sub _check_monopart_hold_is_possible {
2724 my( $partid, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou, $oargs ) = @_;
2726 my $e = new_editor();
2727 my %org_filter = create_ranged_org_filter($e, $selection_ou, $depth);
2729 # this monster will grab the id and circ_lib of all of the "holdable" copies for the given record
2730 my $copies = $e->json_query(
2732 select => { acp => ['id', 'circ_lib'] },
2736 field => 'target_copy',
2738 filter => { part => $partid }
2740 acpl => { field => 'id', filter => { holdable => 't'}, fkey => 'location' },
2741 ccs => { field => 'id', filter => { holdable => 't'}, fkey => 'status' }
2745 '+acp' => { circulate => 't', deleted => 'f', holdable => 't', %org_filter }
2751 $logger->info("monopart possible found ".scalar(@$copies)." potential copies");
2755 $empty_ok = $e->retrieve_config_global_flag('circ.holds.empty_part_ok');
2756 $empty_ok = ($empty_ok and $U->is_true($empty_ok->enabled));
2761 "HIGH_LEVEL_HOLD_HAS_NO_COPIES",
2762 "payload" => {"fail_part" => "no_ultimate_items"}
2770 # -----------------------------------------------------------------------
2771 # sort the copies into buckets based on their circ_lib proximity to
2772 # the patron's home_ou.
2773 # -----------------------------------------------------------------------
2775 my $home_org = $patron->home_ou;
2776 my $req_org = $request_lib->id;
2778 $logger->info("prox cache $home_org " . $prox_cache{$home_org});
2780 $prox_cache{$home_org} =
2781 $e->search_actor_org_unit_proximity({from_org => $home_org})
2782 unless $prox_cache{$home_org};
2783 my $home_prox = $prox_cache{$home_org};
2786 my %hash = map { ($_->to_org => $_->prox) } @$home_prox;
2787 push( @{$buckets{ $hash{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2789 my @keys = sort { $a <=> $b } keys %buckets;
2792 if( $home_org ne $req_org ) {
2793 # -----------------------------------------------------------------------
2794 # shove the copies close to the request_lib into the primary buckets
2795 # directly before the farthest away copies. That way, they are not
2796 # given priority, but they are checked before the farthest copies.
2797 # -----------------------------------------------------------------------
2798 $prox_cache{$req_org} =
2799 $e->search_actor_org_unit_proximity({from_org => $req_org})
2800 unless $prox_cache{$req_org};
2801 my $req_prox = $prox_cache{$req_org};
2804 my %hash2 = map { ($_->to_org => $_->prox) } @$req_prox;
2805 push( @{$buckets2{ $hash2{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
2807 my $highest_key = $keys[@keys - 1]; # the farthest prox in the exising buckets
2808 my $new_key = $highest_key - 0.5; # right before the farthest prox
2809 my @keys2 = sort { $a <=> $b } keys %buckets2;
2810 for my $key (@keys2) {
2811 last if $key >= $highest_key;
2812 push( @{$buckets{$new_key}}, $_ ) for @{$buckets2{$key}};
2816 @keys = sort { $a <=> $b } keys %buckets;
2821 my $age_protect_only = 0;
2822 OUTER: for my $key (@keys) {
2823 my @cps = @{$buckets{$key}};
2825 $logger->info("looking at " . scalar(@{$buckets{$key}}). " copies in proximity bucket $key");
2827 for my $copyid (@cps) {
2829 next if $seen{$copyid};
2830 $seen{$copyid} = 1; # there could be dupes given the merged buckets
2831 my $copy = $e->retrieve_asset_copy($copyid);
2832 $logger->debug("looking at bucket_key=$key, copy $copyid : circ_lib = " . $copy->circ_lib);
2834 unless($title) { # grab the title if we don't already have it
2835 my $vol = $e->retrieve_asset_call_number(
2836 [ $copy->call_number, { flesh => 1, flesh_fields => { bre => ['fixed_fields'], acn => ['record'] } } ] );
2837 $title = $vol->record;
2840 @status = verify_copy_for_hold(
2841 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib, $oargs);
2843 $age_protect_only ||= $status[3];
2844 last OUTER if $status[0];
2849 if (!defined($empty_ok)) {
2850 $empty_ok = $e->retrieve_config_global_flag('circ.holds.empty_part_ok');
2851 $empty_ok = ($empty_ok and $U->is_true($empty_ok->enabled));
2854 return (1,0) if ($empty_ok);
2856 $status[3] = $age_protect_only;
2861 sub _check_volume_hold_is_possible {
2862 my( $vol, $title, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou, $oargs ) = @_;
2863 my %org_filter = create_ranged_org_filter(new_editor(), $selection_ou, $depth);
2864 my $copies = new_editor->search_asset_copy({call_number => $vol->id, %org_filter});
2865 $logger->info("checking possibility of volume hold for volume ".$vol->id);
2867 my $filter_copies = [];
2868 for my $copy (@$copies) {
2869 # ignore part-mapped copies for regular volume level holds
2870 push(@$filter_copies, $copy) unless
2871 new_editor->search_asset_copy_part_map({target_copy => $copy->id})->[0];
2873 $copies = $filter_copies;
2878 "HIGH_LEVEL_HOLD_HAS_NO_COPIES",
2879 "payload" => {"fail_part" => "no_ultimate_items"}
2885 my $age_protect_only = 0;
2886 for my $copy ( @$copies ) {
2887 @status = verify_copy_for_hold(
2888 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib, $oargs );
2889 $age_protect_only ||= $status[3];
2892 $status[3] = $age_protect_only;
2898 sub verify_copy_for_hold {
2899 my( $patron, $requestor, $title, $copy, $pickup_lib, $request_lib, $oargs ) = @_;
2900 # $oargs should be undef unless we're overriding.
2901 $logger->info("checking possibility of copy in hold request for copy ".$copy->id);
2902 my $permitted = OpenILS::Utils::PermitHold::permit_copy_hold(
2905 requestor => $requestor,
2908 title_descriptor => $title->fixed_fields,
2909 pickup_lib => $pickup_lib,
2910 request_lib => $request_lib,
2912 show_event_list => 1
2916 # Check for override permissions on events.
2917 if ($oargs && $permitted && scalar @$permitted) {
2918 # Remove the events from permitted that we can override.
2919 if ($oargs->{events}) {
2920 foreach my $evt (@{$oargs->{events}}) {
2921 $permitted = [grep {$_->{textcode} ne $evt} @{$permitted}];
2924 # Now, we handle the override all case by checking remaining
2925 # events against override permissions.
2926 if (scalar @$permitted && $oargs->{all}) {
2927 # Pre-set events and failed members of oargs to empty
2928 # arrays, if they are not set, yet.
2929 $oargs->{events} = [] unless ($oargs->{events});
2930 $oargs->{failed} = [] unless ($oargs->{failed});
2931 # When we're done with these checks, we swap permitted
2932 # with a reference to @disallowed.
2933 my @disallowed = ();
2934 foreach my $evt (@{$permitted}) {
2935 # Check if we've already seen the event in this
2936 # session and it failed.
2937 if (grep {$_ eq $evt->{textcode}} @{$oargs->{failed}}) {
2938 push(@disallowed, $evt);
2940 # We have to check if the requestor has the
2941 # override permission.
2943 # AppUtils::check_user_perms returns the perm if
2944 # the user doesn't have it, undef if they do.
2945 if ($apputils->check_user_perms($requestor->id, $requestor->ws_ou, $evt->{textcode} . '.override')) {
2946 push(@disallowed, $evt);
2947 push(@{$oargs->{failed}}, $evt->{textcode});
2949 push(@{$oargs->{events}}, $evt->{textcode});
2953 $permitted = \@disallowed;
2957 my $age_protect_only = 0;
2958 if (@$permitted == 1 && @$permitted[0]->{textcode} eq 'ITEM_AGE_PROTECTED') {
2959 $age_protect_only = 1;
2963 (not scalar @$permitted), # true if permitted is an empty arrayref
2964 ( # XXX This test is of very dubious value; someone should figure
2965 # out what if anything is checking this value
2966 ($copy->circ_lib == $pickup_lib) and
2967 ($copy->status == OILS_COPY_STATUS_AVAILABLE)
2976 sub find_nearest_permitted_hold {
2979 my $editor = shift; # CStoreEditor object
2980 my $copy = shift; # copy to target
2981 my $user = shift; # staff
2982 my $check_only = shift; # do no updates, just see if the copy could fulfill a hold
2984 my $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND');
2986 my $bc = $copy->barcode;
2988 # find any existing holds that already target this copy
2989 my $old_holds = $editor->search_action_hold_request(
2990 { current_copy => $copy->id,
2991 cancel_time => undef,
2992 capture_time => undef
2996 my $hold_stall_interval = $U->ou_ancestor_setting_value($user->ws_ou, OILS_SETTING_HOLD_SOFT_STALL);
2998 $logger->info("circulator: searching for best hold at org ".$user->ws_ou.
2999 " and copy $bc with a hold stalling interval of ". ($hold_stall_interval || "(none)"));
3001 my $fifo = $U->ou_ancestor_setting_value($user->ws_ou, 'circ.holds_fifo');
3003 # search for what should be the best holds for this copy to fulfill
3004 my $best_holds = $U->storagereq(
3005 "open-ils.storage.action.hold_request.nearest_hold.atomic",
3006 $user->ws_ou, $copy->id, 100, $hold_stall_interval, $fifo );
3008 # Add any pre-targeted holds to the list too? Unless they are already there, anyway.
3010 for my $holdid (@$old_holds) {
3011 next unless $holdid;
3012 push(@$best_holds, $holdid) unless ( grep { ''.$holdid eq ''.$_ } @$best_holds );
3016 unless(@$best_holds) {
3017 $logger->info("circulator: no suitable holds found for copy $bc");
3018 return (undef, $evt);
3024 # for each potential hold, we have to run the permit script
3025 # to make sure the hold is actually permitted.
3028 for my $holdid (@$best_holds) {
3029 next unless $holdid;
3030 $logger->info("circulator: checking if hold $holdid is permitted for copy $bc");
3032 my $hold = $editor->retrieve_action_hold_request($holdid) or next;
3033 my $reqr = $reqr_cache{$hold->requestor} || $editor->retrieve_actor_user($hold->requestor);
3034 my $rlib = $org_cache{$hold->request_lib} || $editor->retrieve_actor_org_unit($hold->request_lib);
3036 $reqr_cache{$hold->requestor} = $reqr;
3037 $org_cache{$hold->request_lib} = $rlib;
3039 # see if this hold is permitted
3040 my $permitted = OpenILS::Utils::PermitHold::permit_copy_hold(
3042 patron_id => $hold->usr,
3045 pickup_lib => $hold->pickup_lib,
3046 request_lib => $rlib,
3058 unless( $best_hold ) { # no "good" permitted holds were found
3060 $logger->info("circulator: no suitable holds found for copy $bc");
3061 return (undef, $evt);
3064 $logger->info("circulator: best hold ".$best_hold->id." found for copy $bc");
3066 # indicate a permitted hold was found
3067 return $best_hold if $check_only;
3069 # we've found a permitted hold. we need to "grab" the copy
3070 # to prevent re-targeted holds (next part) from re-grabbing the copy
3071 $best_hold->current_copy($copy->id);
3072 $editor->update_action_hold_request($best_hold)
3073 or return (undef, $editor->event);
3078 # re-target any other holds that already target this copy
3079 for my $old_hold (@$old_holds) {
3080 next if $old_hold->id eq $best_hold->id; # don't re-target the hold we want
3081 $logger->info("circulator: clearing current_copy and prev_check_time on hold ".
3082 $old_hold->id." after a better hold [".$best_hold->id."] was found");
3083 $old_hold->clear_current_copy;
3084 $old_hold->clear_prev_check_time;
3085 $editor->update_action_hold_request($old_hold)
3086 or return (undef, $editor->event);
3087 push(@retarget, $old_hold->id);
3090 return ($best_hold, undef, (@retarget) ? \@retarget : undef);
3098 __PACKAGE__->register_method(
3099 method => 'all_rec_holds',
3100 api_name => 'open-ils.circ.holds.retrieve_all_from_title',
3104 my( $self, $conn, $auth, $title_id, $args ) = @_;
3106 my $e = new_editor(authtoken=>$auth);
3107 $e->checkauth or return $e->event;
3108 $e->allowed('VIEW_HOLD') or return $e->event;
3111 $args->{fulfillment_time} = undef; # we don't want to see old fulfilled holds
3112 $args->{cancel_time} = undef;
3114 my $resp = { volume_holds => [], copy_holds => [], recall_holds => [], force_holds => [], metarecord_holds => [], part_holds => [], issuance_holds => [] };
3116 my $mr_map = $e->search_metabib_metarecord_source_map({source => $title_id})->[0];
3118 $resp->{metarecord_holds} = $e->search_action_hold_request(
3119 { hold_type => OILS_HOLD_TYPE_METARECORD,
3120 target => $mr_map->metarecord,
3126 $resp->{title_holds} = $e->search_action_hold_request(
3128 hold_type => OILS_HOLD_TYPE_TITLE,
3129 target => $title_id,
3133 my $parts = $e->search_biblio_monograph_part(
3139 $resp->{part_holds} = $e->search_action_hold_request(
3141 hold_type => OILS_HOLD_TYPE_MONOPART,
3147 my $subs = $e->search_serial_subscription(
3148 { record_entry => $title_id }, {idlist=>1});
3151 my $issuances = $e->search_serial_issuance(
3152 {subscription => $subs}, {idlist=>1}
3156 $resp->{issuance_holds} = $e->search_action_hold_request(
3158 hold_type => OILS_HOLD_TYPE_ISSUANCE,
3159 target => $issuances,
3166 my $vols = $e->search_asset_call_number(
3167 { record => $title_id, deleted => 'f' }, {idlist=>1});
3169 return $resp unless @$vols;
3171 $resp->{volume_holds} = $e->search_action_hold_request(
3173 hold_type => OILS_HOLD_TYPE_VOLUME,
3178 my $copies = $e->search_asset_copy(
3179 { call_number => $vols, deleted => 'f' }, {idlist=>1});
3181 return $resp unless @$copies;
3183 $resp->{copy_holds} = $e->search_action_hold_request(
3185 hold_type => OILS_HOLD_TYPE_COPY,
3190 $resp->{recall_holds} = $e->search_action_hold_request(
3192 hold_type => OILS_HOLD_TYPE_RECALL,
3197 $resp->{force_holds} = $e->search_action_hold_request(
3199 hold_type => OILS_HOLD_TYPE_FORCE,
3211 __PACKAGE__->register_method(
3212 method => 'uber_hold',
3214 api_name => 'open-ils.circ.hold.details.retrieve'
3218 my($self, $client, $auth, $hold_id, $args) = @_;
3219 my $e = new_editor(authtoken=>$auth);
3220 $e->checkauth or return $e->event;
3221 return uber_hold_impl($e, $hold_id, $args);
3224 __PACKAGE__->register_method(
3225 method => 'batch_uber_hold',
3228 api_name => 'open-ils.circ.hold.details.batch.retrieve'
3231 sub batch_uber_hold {
3232 my($self, $client, $auth, $hold_ids, $args) = @_;
3233 my $e = new_editor(authtoken=>$auth);
3234 $e->checkauth or return $e->event;
3235 $client->respond(uber_hold_impl($e, $_, $args)) for @$hold_ids;
3239 sub uber_hold_impl {
3240 my($e, $hold_id, $args) = @_;
3243 my $hold = $e->retrieve_action_hold_request(
3248 flesh_fields => { ahr => [ 'current_copy', 'usr', 'notes' ] }
3251 ) or return $e->event;
3253 if($hold->usr->id ne $e->requestor->id) {
3254 # A user is allowed to see his/her own holds
3255 $e->allowed('VIEW_HOLD') or return $e->event;
3256 $hold->notes( # filter out any non-staff ("private") notes
3257 [ grep { !$U->is_true($_->staff) } @{$hold->notes} ] );
3260 # caller is asking for own hold, but may not have permission to view staff notes
3261 unless($e->allowed('VIEW_HOLD')) {
3262 $hold->notes( # filter out any staff notes
3263 [ grep { $U->is_true($_->staff) } @{$hold->notes} ] );
3267 my $user = $hold->usr;
3268 $hold->usr($user->id);
3271 my( $mvr, $volume, $copy, $issuance, $part, $bre ) = find_hold_mvr($e, $hold, $args->{suppress_mvr});
3273 flesh_hold_notices([$hold], $e) unless $args->{suppress_notices};
3274 flesh_hold_transits([$hold]) unless $args->{suppress_transits};
3276 my $details = retrieve_hold_queue_status_impl($e, $hold);
3281 ($copy ? (copy => $copy) : ()),
3282 ($volume ? (volume => $volume) : ()),
3283 ($issuance ? (issuance => $issuance) : ()),
3284 ($part ? (part => $part) : ()),
3285 ($args->{include_bre} ? (bre => $bre) : ()),
3286 ($args->{suppress_mvr} ? () : (mvr => $mvr)),
3290 unless($args->{suppress_patron_details}) {
3291 my $card = $e->retrieve_actor_card($user->card) or return $e->event;
3292 $resp->{patron_first} = $user->first_given_name,
3293 $resp->{patron_last} = $user->family_name,
3294 $resp->{patron_barcode} = $card->barcode,
3295 $resp->{patron_alias} = $user->alias,
3303 # -----------------------------------------------------
3304 # Returns the MVR object that represents what the
3306 # -----------------------------------------------------
3308 my( $e, $hold, $no_mvr ) = @_;
3316 if( $hold->hold_type eq OILS_HOLD_TYPE_METARECORD ) {
3317 my $mr = $e->retrieve_metabib_metarecord($hold->target)
3318 or return $e->event;
3319 $tid = $mr->master_record;
3321 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_TITLE ) {
3322 $tid = $hold->target;
3324 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_VOLUME ) {
3325 $volume = $e->retrieve_asset_call_number($hold->target)
3326 or return $e->event;
3327 $tid = $volume->record;
3329 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_ISSUANCE ) {
3330 $issuance = $e->retrieve_serial_issuance([
3332 {flesh => 1, flesh_fields => {siss => [ qw/subscription/ ]}}
3333 ]) or return $e->event;
3335 $tid = $issuance->subscription->record_entry;
3337 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_MONOPART ) {
3338 $part = $e->retrieve_biblio_monograph_part([
3340 ]) or return $e->event;
3342 $tid = $part->record;
3344 } 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 ) {
3345 $copy = $e->retrieve_asset_copy([
3347 {flesh => 1, flesh_fields => {acp => ['call_number']}}
3348 ]) or return $e->event;
3350 $volume = $copy->call_number;
3351 $tid = $volume->record;
3354 if(!$copy and ref $hold->current_copy ) {
3355 $copy = $hold->current_copy;
3356 $hold->current_copy($copy->id);
3359 if(!$volume and $copy) {
3360 $volume = $e->retrieve_asset_call_number($copy->call_number);
3363 # TODO return metarcord mvr for M holds
3364 my $title = $e->retrieve_biblio_record_entry($tid);
3365 return ( ($no_mvr) ? undef : $U->record_to_mvr($title), $volume, $copy, $issuance, $part, $title );
3368 __PACKAGE__->register_method(
3369 method => 'clear_shelf_cache',
3370 api_name => 'open-ils.circ.hold.clear_shelf.get_cache',
3374 Returns the holds processed with the given cache key
3379 sub clear_shelf_cache {
3380 my($self, $client, $auth, $cache_key, $chunk_size) = @_;
3381 my $e = new_editor(authtoken => $auth, xact => 1);
3382 return $e->die_event unless $e->checkauth and $e->allowed('VIEW_HOLD');
3385 my $hold_data = OpenSRF::Utils::Cache->new('global')->get_cache($cache_key);
3388 $logger->info("no hold data found in cache"); # XXX TODO return event
3394 foreach (keys %$hold_data) {
3395 $maximum += scalar(@{ $hold_data->{$_} });
3397 $client->respond({"maximum" => $maximum, "progress" => 0});
3399 for my $action (sort keys %$hold_data) {
3400 while (@{$hold_data->{$action}}) {
3401 my @hid_chunk = splice @{$hold_data->{$action}}, 0, $chunk_size;
3403 my $result_chunk = $e->json_query({
3405 "acp" => ["barcode"],
3407 first_given_name second_given_name family_name alias
3410 "acnp" => [{column => "label", alias => "prefix"}],
3411 "acns" => [{column => "label", alias => "suffix"}],
3419 "field" => "id", "fkey" => "current_copy",
3422 "field" => "id", "fkey" => "call_number",
3425 "field" => "id", "fkey" => "record"
3428 "field" => "id", "fkey" => "prefix"
3431 "field" => "id", "fkey" => "suffix"
3435 "acpl" => {"field" => "id", "fkey" => "location"}
3438 "au" => {"field" => "id", "fkey" => "usr"}
3441 "where" => {"+ahr" => {"id" => \@hid_chunk}}
3442 }, {"substream" => 1}) or return $e->die_event;
3446 +{"action" => $action, "hold_details" => $_}
3457 __PACKAGE__->register_method(
3458 method => 'clear_shelf_process',
3460 api_name => 'open-ils.circ.hold.clear_shelf.process',
3463 1. Find all holds that have expired on the holds shelf
3465 3. If a clear-shelf status is configured, put targeted copies into this status
3466 4. Divide copies into 3 groups: items to transit, items to reshelve, and items
3467 that are needed for holds. No subsequent action is taken on the holds
3468 or items after grouping.
3473 sub clear_shelf_process {
3474 my($self, $client, $auth, $org_id, $match_copy) = @_;
3476 my $e = new_editor(authtoken=>$auth, xact => 1);
3477 $e->checkauth or return $e->die_event;
3478 my $cache = OpenSRF::Utils::Cache->new('global');
3480 $org_id ||= $e->requestor->ws_ou;
3481 $e->allowed('UPDATE_HOLD', $org_id) or return $e->die_event;
3483 my $copy_status = $U->ou_ancestor_setting_value($org_id, 'circ.holds.clear_shelf.copy_status');
3485 my @hold_ids = $self->method_lookup(
3486 "open-ils.circ.captured_holds.id_list.expired_on_shelf.retrieve"
3487 )->run($auth, $org_id, $match_copy);
3490 my @canceled_holds; # newly canceled holds
3491 my $chunk_size = 25; # chunked status updates
3493 for my $hold_id (@hold_ids) {
3495 $logger->info("Clear shelf processing hold $hold_id");
3497 my $hold = $e->retrieve_action_hold_request([
3500 flesh_fields => {ahr => ['current_copy']}
3504 if (!$hold->cancel_time) { # may be canceled but still on the holds shelf
3505 $hold->cancel_time('now');
3506 $hold->cancel_cause(2); # Hold Shelf expiration
3507 $e->update_action_hold_request($hold) or return $e->die_event;
3508 delete_hold_copy_maps($self, $e, $hold->id) and return $e->die_event;
3509 push(@canceled_holds, $hold_id);
3512 my $copy = $hold->current_copy;
3514 if($copy_status or $copy_status == 0) {
3515 # if a clear-shelf copy status is defined, update the copy
3516 $copy->status($copy_status);
3517 $copy->edit_date('now');
3518 $copy->editor($e->requestor->id);
3519 $e->update_asset_copy($copy) or return $e->die_event;
3522 push(@holds, $hold);
3523 $client->respond({maximum => scalar(@holds), progress => $counter}) if ( (++$counter % $chunk_size) == 0);
3534 for my $hold (@holds) {
3536 my $copy = $hold->current_copy;
3537 my ($alt_hold) = __PACKAGE__->find_nearest_permitted_hold($e, $copy, $e->requestor, 1);
3539 if($alt_hold and !$match_copy) {
3541 push(@{$cache_data{hold}}, $hold->id); # copy is needed for a hold
3543 } elsif($copy->circ_lib != $e->requestor->ws_ou) {
3545 push(@{$cache_data{transit}}, $hold->id); # copy needs to transit
3549 push(@{$cache_data{shelf}}, $hold->id); # copy needs to go back to the shelf
3553 my $cache_key = md5_hex(time . $$ . rand());
3554 $logger->info("clear_shelf_cache: storing under $cache_key");
3555 $cache->put_cache($cache_key, \%cache_data, 7200); # TODO: 2 hours. configurable?
3557 # tell the client we're done
3558 $client->respond_complete({cache_key => $cache_key});
3561 # fire off the hold cancelation trigger and wait for response so don't flood the service
3563 # refetch the holds to pick up the caclulated cancel_time,
3564 # which may be needed by Action/Trigger
3566 my $updated_holds = [];
3567 $updated_holds = $e->search_action_hold_request({id => \@canceled_holds}, {substream => 1}) if (@canceled_holds > 0);
3570 $U->create_events_for_hook(
3571 'hold_request.cancel.expire_holds_shelf',
3572 $_, $org_id, undef, undef, 1) for @$updated_holds;
3575 # tell the client we're done
3576 $client->respond_complete;
3580 __PACKAGE__->register_method(
3581 method => 'usr_hold_summary',
3582 api_name => 'open-ils.circ.holds.user_summary',
3584 Returns a summary of holds statuses for a given user
3588 sub usr_hold_summary {
3589 my($self, $conn, $auth, $user_id) = @_;
3591 my $e = new_editor(authtoken=>$auth);
3592 $e->checkauth or return $e->event;
3593 $e->allowed('VIEW_HOLD') or return $e->event;
3595 my $holds = $e->search_action_hold_request(
3598 fulfillment_time => undef,
3599 cancel_time => undef,
3603 my %summary = (1 => 0, 2 => 0, 3 => 0, 4 => 0);
3604 $summary{_hold_status($e, $_)} += 1 for @$holds;
3610 __PACKAGE__->register_method(
3611 method => 'hold_has_copy_at',
3612 api_name => 'open-ils.circ.hold.has_copy_at',
3615 'Returns the ID of the found copy and name of the shelving location if there is ' .
3616 'an available copy at the specified org unit. Returns empty hash otherwise. ' .
3617 'The anticipated use for this method is to determine whether an item is ' .
3618 'available at the library where the user is placing the hold (or, alternatively, '.
3619 'at the pickup library) to encourage bypassing the hold placement and just ' .
3620 'checking out the item.' ,
3622 { desc => 'Authentication Token', type => 'string' },
3623 { desc => 'Method Arguments. Options include: hold_type, hold_target, org_unit. '
3624 . 'hold_type is the hold type code (T, V, C, M, ...). '
3625 . 'hold_target is the identifier of the hold target object. '
3626 . 'org_unit is org unit ID.',
3631 desc => q/Result hash like { "copy" : copy_id, "location" : location_name }, empty hash on misses, event on error./,
3637 sub hold_has_copy_at {
3638 my($self, $conn, $auth, $args) = @_;
3640 my $e = new_editor(authtoken=>$auth);
3641 $e->checkauth or return $e->event;
3643 my $hold_type = $$args{hold_type};
3644 my $hold_target = $$args{hold_target};
3645 my $org_unit = $$args{org_unit};
3648 select => {acp => ['id'], acpl => ['name']},
3651 acpl => {field => 'id', filter => { holdable => 't'}, fkey => 'location'},
3652 ccs => {field => 'id', filter => { holdable => 't'}, fkey => 'status' }
3655 where => {'+acp' => { circulate => 't', deleted => 'f', holdable => 't', circ_lib => $org_unit, status => [0,7]}},
3659 if($hold_type eq 'C' or $hold_type eq 'F' or $hold_type eq 'R') {
3661 $query->{where}->{'+acp'}->{id} = $hold_target;
3663 } elsif($hold_type eq 'V') {
3665 $query->{where}->{'+acp'}->{call_number} = $hold_target;
3667 } elsif($hold_type eq 'P') {
3669 $query->{from}->{acp}->{acpm} = {
3670 field => 'target_copy',
3672 filter => {part => $hold_target},
3675 } elsif($hold_type eq 'I') {
3677 $query->{from}->{acp}->{sitem} = {
3680 filter => {issuance => $hold_target},
3683 } elsif($hold_type eq 'T') {
3685 $query->{from}->{acp}->{acn} = {
3687 fkey => 'call_number',
3691 filter => {id => $hold_target},
3699 $query->{from}->{acp}->{acn} = {
3701 fkey => 'call_number',
3710 filter => {metarecord => $hold_target},
3718 my $res = $e->json_query($query)->[0] or return {};
3719 return {copy => $res->{id}, location => $res->{name}} if $res;
3723 # returns true if the user already has an item checked out
3724 # that could be used to fulfill the requested hold.
3725 sub hold_item_is_checked_out {
3726 my($e, $user_id, $hold_type, $hold_target) = @_;
3729 select => {acp => ['id']},
3730 from => {acp => {}},
3734 in => { # copies for circs the user has checked out
3735 select => {circ => ['target_copy']},
3739 checkin_time => undef,
3741 {stop_fines => ["MAXFINES","LONGOVERDUE"]},
3742 {stop_fines => undef}
3752 if($hold_type eq 'C' || $hold_type eq 'R' || $hold_type eq 'F') {
3754 $query->{where}->{'+acp'}->{id}->{in}->{where}->{'target_copy'} = $hold_target;
3756 } elsif($hold_type eq 'V') {
3758 $query->{where}->{'+acp'}->{call_number} = $hold_target;
3760 } elsif($hold_type eq 'P') {
3762 $query->{from}->{acp}->{acpm} = {
3763 field => 'target_copy',
3765 filter => {part => $hold_target},
3768 } elsif($hold_type eq 'I') {
3770 $query->{from}->{acp}->{sitem} = {
3773 filter => {issuance => $hold_target},
3776 } elsif($hold_type eq 'T') {
3778 $query->{from}->{acp}->{acn} = {
3780 fkey => 'call_number',
3784 filter => {id => $hold_target},
3792 $query->{from}->{acp}->{acn} = {
3794 fkey => 'call_number',
3803 filter => {metarecord => $hold_target},
3811 return $e->json_query($query)->[0];
3814 __PACKAGE__->register_method(
3815 method => 'change_hold_title',
3816 api_name => 'open-ils.circ.hold.change_title',
3819 Updates all title level holds targeting the specified bibs to point a new bib./,
3821 { desc => 'Authentication Token', type => 'string' },
3822 { desc => 'New Target Bib Id', type => 'number' },
3823 { desc => 'Old Target Bib Ids', type => 'array' },
3825 return => { desc => '1 on success' }
3829 __PACKAGE__->register_method(
3830 method => 'change_hold_title_for_specific_holds',
3831 api_name => 'open-ils.circ.hold.change_title.specific_holds',
3834 Updates specified holds to target new bib./,
3836 { desc => 'Authentication Token', type => 'string' },
3837 { desc => 'New Target Bib Id', type => 'number' },
3838 { desc => 'Holds Ids for holds to update', type => 'array' },
3840 return => { desc => '1 on success' }
3845 sub change_hold_title {
3846 my( $self, $client, $auth, $new_bib_id, $bib_ids ) = @_;
3848 my $e = new_editor(authtoken=>$auth, xact=>1);
3849 return $e->die_event unless $e->checkauth;
3851 my $holds = $e->search_action_hold_request(
3854 cancel_time => undef,
3855 fulfillment_time => undef,
3861 flesh_fields => { ahr => ['usr'] }
3867 for my $hold (@$holds) {
3868 $e->allowed('UPDATE_HOLD', $hold->usr->home_ou) or return $e->die_event;
3869 $logger->info("Changing hold " . $hold->id . " target from " . $hold->target . " to $new_bib_id in title hold target change");
3870 $hold->target( $new_bib_id );
3871 $e->update_action_hold_request($hold) or return $e->die_event;
3876 _reset_hold($self, $e->requestor, $_) for @$holds;
3881 sub change_hold_title_for_specific_holds {
3882 my( $self, $client, $auth, $new_bib_id, $hold_ids ) = @_;
3884 my $e = new_editor(authtoken=>$auth, xact=>1);
3885 return $e->die_event unless $e->checkauth;
3887 my $holds = $e->search_action_hold_request(
3890 cancel_time => undef,
3891 fulfillment_time => undef,
3897 flesh_fields => { ahr => ['usr'] }
3903 for my $hold (@$holds) {
3904 $e->allowed('UPDATE_HOLD', $hold->usr->home_ou) or return $e->die_event;
3905 $logger->info("Changing hold " . $hold->id . " target from " . $hold->target . " to $new_bib_id in title hold target change");
3906 $hold->target( $new_bib_id );
3907 $e->update_action_hold_request($hold) or return $e->die_event;
3912 _reset_hold($self, $e->requestor, $_) for @$holds;
3917 __PACKAGE__->register_method(
3918 method => 'rec_hold_count',
3919 api_name => 'open-ils.circ.bre.holds.count',
3921 desc => q/Returns the total number of holds that target the
3922 selected bib record or its associated copies and call_numbers/,
3924 { desc => 'Bib ID', type => 'number' },
3925 { desc => q/Optional arguments. Supported arguments include:
3926 "pickup_lib_descendant" -> limit holds to those whose pickup
3927 library is equal to or is a child of the provided org unit/,
3931 return => {desc => 'Hold count', type => 'number'}
3935 __PACKAGE__->register_method(
3936 method => 'rec_hold_count',
3937 api_name => 'open-ils.circ.mmr.holds.count',
3939 desc => q/Returns the total number of holds that target the
3940 selected metarecord or its associated copies, call_numbers, and bib records/,
3942 { desc => 'Metarecord ID', type => 'number' },
3944 return => {desc => 'Hold count', type => 'number'}
3948 # XXX Need to add type I (and, soon, type P) holds to these counts
3949 sub rec_hold_count {
3950 my($self, $conn, $target_id, $args) = @_;
3957 filter => {metarecord => $target_id}
3964 filter => { id => $target_id },
3969 if($self->api_name =~ /mmr/) {
3970 delete $bre_join->{bre}->{filter};
3971 $bre_join->{bre}->{join} = $mmr_join;
3977 fkey => 'call_number',
3983 select => {ahr => [{column => 'id', transform => 'count', alias => 'count'}]},
3987 cancel_time => undef,
3988 fulfillment_time => undef,
3992 hold_type => [qw/C F R/],
3995 select => {acp => ['id']},
3996 from => { acp => $cn_join }
4006 select => {acn => ['id']},
4007 from => {acn => $bre_join}
4015 target => $target_id
4023 if($self->api_name =~ /mmr/) {
4024 $query->{where}->{'+ahr'}->{'-or'}->[2] = {
4029 select => {bre => ['id']},
4030 from => {bre => $mmr_join}
4036 $query->{where}->{'+ahr'}->{'-or'}->[3] = {
4039 target => $target_id
4045 if (my $pld = $args->{pickup_lib_descendant}) {
4046 $query->{where}->{'+ahr'}->{pickup_lib} = {
4048 select => {aou => [{
4050 transform => 'actor.org_unit_descendants',
4051 result_field => 'id'
4054 where => {id => $pld}
4060 return new_editor()->json_query($query)->[0]->{count};
4063 # A helper function to calculate a hold's expiration time at a given
4064 # org_unit. Takes the org_unit as an argument and returns either the
4065 # hold expire time as an ISO8601 string or undef if there is no hold
4066 # expiration interval set for the subject ou.
4067 sub calculate_expire_time
4070 my $interval = $U->ou_ancestor_setting_value($ou, OILS_SETTING_HOLD_EXPIRE);
4072 my $date = DateTime->now->add(seconds => OpenSRF::Utils::interval_to_seconds($interval));
4073 return $U->epoch2ISO8601($date->epoch);