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;
34 my $apputils = "OpenILS::Application::AppUtils";
40 __PACKAGE__->register_method(
41 method => "create_hold",
42 api_name => "open-ils.circ.holds.create",
44 Create a new hold for an item. From a permissions perspective,
45 the login session is used as the 'requestor' of the hold.
46 The hold recipient is determined by the 'usr' setting within
49 First we verify the requestion has holds request permissions.
50 Then we verify that the recipient is allowed to make the given hold.
51 If not, we see if the requestor has "override" capabilities. If not,
52 a permission exception is returned. If permissions allow, we cycle
53 through the set of holds objects and create.
55 If the recipient does not have permission to place multiple holds
56 on a single title and said operation is attempted, a permission
61 __PACKAGE__->register_method(
62 method => "create_hold",
63 api_name => "open-ils.circ.holds.create.override",
65 If the recipient is not allowed to receive the requested hold,
66 call this method to attempt the override
67 @see open-ils.circ.holds.create
72 my( $self, $conn, $auth, @holds ) = @_;
73 my $e = new_editor(authtoken=>$auth, xact=>1);
74 return $e->event unless $e->checkauth;
76 my $override = 1 if $self->api_name =~ /override/;
78 my $holds = (ref($holds[0] eq 'ARRAY')) ? $holds[0] : [@holds];
82 for my $hold (@$holds) {
87 my $requestor = $e->requestor;
88 my $recipient = $requestor;
91 if( $requestor->id ne $hold->usr ) {
92 # Make sure the requestor is allowed to place holds for
93 # the recipient if they are not the same people
94 $recipient = $e->retrieve_actor_user($hold->usr) or return $e->event;
95 $e->allowed('REQUEST_HOLDS', $recipient->home_ou) or return $e->event;
98 # Now make sure the recipient is allowed to receive the specified hold
100 my $porg = $recipient->home_ou;
101 my $rid = $e->requestor->id;
102 my $t = $hold->hold_type;
104 # See if a duplicate hold already exists
106 usr => $recipient->id,
108 fulfillment_time => undef,
109 target => $hold->target,
110 cancel_time => undef,
113 $sargs->{holdable_formats} = $hold->holdable_formats if $t eq 'M';
115 my $existing = $e->search_action_hold_request($sargs);
116 push( @events, OpenILS::Event->new('HOLD_EXISTS')) if @$existing;
118 if( $t eq OILS_HOLD_TYPE_METARECORD )
119 { $pevt = $e->event unless $e->allowed('MR_HOLDS', $porg); }
121 if( $t eq OILS_HOLD_TYPE_TITLE )
122 { $pevt = $e->event unless $e->allowed('TITLE_HOLDS', $porg); }
124 if( $t eq OILS_HOLD_TYPE_VOLUME )
125 { $pevt = $e->event unless $e->allowed('VOLUME_HOLDS', $porg); }
127 if( $t eq OILS_HOLD_TYPE_COPY )
128 { $pevt = $e->event unless $e->allowed('COPY_HOLDS', $porg); }
130 return $pevt if $pevt;
134 for my $evt (@events) {
136 my $name = $evt->{textcode};
137 return $e->event unless $e->allowed("$name.override", $porg);
144 # set the configured expire time
145 unless($hold->expire_time) {
146 my $interval = $U->ou_ancestor_setting_value($recipient->home_ou, OILS_SETTING_HOLD_EXPIRE);
148 my $date = DateTime->now->add(seconds => OpenSRF::Utils::interval_to_seconds($interval));
149 $hold->expire_time($U->epoch2ISO8601($date->epoch));
153 $hold->requestor($e->requestor->id);
154 $hold->request_lib($e->requestor->ws_ou);
155 $hold->selection_ou($hold->pickup_lib) unless $hold->selection_ou;
156 $hold = $e->create_action_hold_request($hold) or return $e->event;
161 $conn->respond_complete(1);
164 next if $U->is_true($_->frozen);
166 'open-ils.storage.action.hold_request.copy_targeter',
174 my( $self, $client, $login_session, @holds) = @_;
176 if(!@holds){return 0;}
177 my( $user, $evt ) = $apputils->checkses($login_session);
181 if(ref($holds[0]) eq 'ARRAY') {
183 } else { $holds = [ @holds ]; }
185 $logger->debug("Iterating over holds requests...");
187 for my $hold (@$holds) {
190 my $type = $hold->hold_type;
192 $logger->activity("User " . $user->id .
193 " creating new hold of type $type for user " . $hold->usr);
196 if($user->id ne $hold->usr) {
197 ( $recipient, $evt ) = $apputils->fetch_user($hold->usr);
207 # am I allowed to place holds for this user?
208 if($hold->requestor ne $hold->usr) {
209 $perm = _check_request_holds_perm($user->id, $user->home_ou);
210 if($perm) { return $perm; }
213 # is this user allowed to have holds of this type?
214 $perm = _check_holds_perm($type, $hold->requestor, $recipient->home_ou);
215 return $perm if $perm;
217 #enforce the fact that the login is the one requesting the hold
218 $hold->requestor($user->id);
219 $hold->selection_ou($recipient->home_ou) unless $hold->selection_ou;
221 my $resp = $apputils->simplereq(
223 'open-ils.storage.direct.action.hold_request.create', $hold );
226 return OpenSRF::EX::ERROR ("Error creating hold");
233 # makes sure that a user has permission to place the type of requested hold
234 # returns the Perm exception if not allowed, returns undef if all is well
235 sub _check_holds_perm {
236 my($type, $user_id, $org_id) = @_;
240 if($evt = $apputils->check_perms(
241 $user_id, $org_id, "MR_HOLDS")) {
245 } elsif ($type eq "T") {
246 if($evt = $apputils->check_perms(
247 $user_id, $org_id, "TITLE_HOLDS")) {
251 } elsif($type eq "V") {
252 if($evt = $apputils->check_perms(
253 $user_id, $org_id, "VOLUME_HOLDS")) {
257 } elsif($type eq "C") {
258 if($evt = $apputils->check_perms(
259 $user_id, $org_id, "COPY_HOLDS")) {
267 # tests if the given user is allowed to place holds on another's behalf
268 sub _check_request_holds_perm {
271 if(my $evt = $apputils->check_perms(
272 $user_id, $org_id, "REQUEST_HOLDS")) {
277 __PACKAGE__->register_method(
278 method => "retrieve_holds_by_id",
279 api_name => "open-ils.circ.holds.retrieve_by_id",
281 Retrieve the hold, with hold transits attached, for the specified id The login session is the requestor and if the requestor is
282 different from the user, then the requestor must have VIEW_HOLD permissions.
286 sub retrieve_holds_by_id {
287 my($self, $client, $auth, $hold_id) = @_;
288 my $e = new_editor(authtoken=>$auth);
289 $e->checkauth or return $e->event;
290 $e->allowed('VIEW_HOLD') or return $e->event;
292 my $holds = $e->search_action_hold_request(
294 { id => $hold_id , fulfillment_time => undef },
295 { order_by => { ahr => "request_time" } }
299 flesh_hold_transits($holds);
300 flesh_hold_notices($holds, $e);
305 __PACKAGE__->register_method(
306 method => "retrieve_holds",
307 api_name => "open-ils.circ.holds.retrieve",
309 Retrieves all the holds, with hold transits attached, for the specified
310 user id. The login session is the requestor and if the requestor is
311 different from the user, then the requestor must have VIEW_HOLD permissions.
314 __PACKAGE__->register_method(
315 method => "retrieve_holds",
317 api_name => "open-ils.circ.holds.id_list.retrieve",
319 Retrieves all the hold ids for the specified
320 user id. The login session is the requestor and if the requestor is
321 different from the user, then the requestor must have VIEW_HOLD permissions.
325 my($self, $client, $login_session, $user_id) = @_;
327 my( $user, $target, $evt ) = $apputils->checkses_requestor(
328 $login_session, $user_id, 'VIEW_HOLD' );
331 my $holds = $apputils->simplereq(
333 "open-ils.cstore.direct.action.hold_request.search.atomic",
336 fulfillment_time => undef,
337 cancel_time => undef,
339 { order_by => { ahr => "request_time" } }
342 if( ! $self->api_name =~ /id_list/ ) {
343 for my $hold ( @$holds ) {
345 $apputils->simplereq(
347 "open-ils.cstore.direct.action.hold_transit_copy.search.atomic",
348 { hold => $hold->id },
349 { order_by => { ahtc => 'id desc' }, limit => 1 }
355 if( $self->api_name =~ /id_list/ ) {
356 return [ map { $_->id } @$holds ];
363 __PACKAGE__->register_method(
364 method => 'user_hold_count',
365 api_name => 'open-ils.circ.hold.user.count');
367 sub user_hold_count {
368 my( $self, $conn, $auth, $userid ) = @_;
369 my $e = new_editor(authtoken=>$auth);
370 return $e->event unless $e->checkauth;
371 my $patron = $e->retrieve_actor_user($userid)
373 return $e->event unless $e->allowed('VIEW_HOLD', $patron->home_ou);
374 return __user_hold_count($self, $e, $userid);
377 sub __user_hold_count {
378 my( $self, $e, $userid ) = @_;
379 my $holds = $e->search_action_hold_request(
381 fulfillment_time => undef,
382 cancel_time => undef,
387 return scalar(@$holds);
391 __PACKAGE__->register_method(
392 method => "retrieve_holds_by_pickup_lib",
393 api_name => "open-ils.circ.holds.retrieve_by_pickup_lib",
395 Retrieves all the holds, with hold transits attached, for the specified
399 __PACKAGE__->register_method(
400 method => "retrieve_holds_by_pickup_lib",
401 api_name => "open-ils.circ.holds.id_list.retrieve_by_pickup_lib",
403 Retrieves all the hold ids for the specified
407 sub retrieve_holds_by_pickup_lib {
408 my($self, $client, $login_session, $ou_id) = @_;
410 #FIXME -- put an appropriate permission check here
411 #my( $user, $target, $evt ) = $apputils->checkses_requestor(
412 # $login_session, $user_id, 'VIEW_HOLD' );
413 #return $evt if $evt;
415 my $holds = $apputils->simplereq(
417 "open-ils.cstore.direct.action.hold_request.search.atomic",
419 pickup_lib => $ou_id ,
420 fulfillment_time => undef,
423 { order_by => { ahr => "request_time" } });
426 if( ! $self->api_name =~ /id_list/ ) {
427 flesh_hold_transits($holds);
430 if( $self->api_name =~ /id_list/ ) {
431 return [ map { $_->id } @$holds ];
437 __PACKAGE__->register_method(
438 method => "cancel_hold",
439 api_name => "open-ils.circ.hold.cancel",
441 Cancels the specified hold. The login session
442 is the requestor and if the requestor is different from the usr field
443 on the hold, the requestor must have CANCEL_HOLDS permissions.
444 the hold may be either the hold object or the hold id
448 my($self, $client, $auth, $holdid) = @_;
450 my $e = new_editor(authtoken=>$auth, xact=>1);
451 return $e->event unless $e->checkauth;
453 my $hold = $e->retrieve_action_hold_request($holdid)
456 if( $e->requestor->id ne $hold->usr ) {
457 return $e->event unless $e->allowed('CANCEL_HOLDS');
460 return 1 if $hold->cancel_time;
462 # If the hold is captured, reset the copy status
463 if( $hold->capture_time and $hold->current_copy ) {
465 my $copy = $e->retrieve_asset_copy($hold->current_copy)
468 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
469 $logger->info("canceling hold $holdid whose item is on the holds shelf");
470 # $logger->info("setting copy to status 'reshelving' on hold cancel");
471 # $copy->status(OILS_COPY_STATUS_RESHELVING);
472 # $copy->editor($e->requestor->id);
473 # $copy->edit_date('now');
474 # $e->update_asset_copy($copy) or return $e->event;
476 } elsif( $copy->status == OILS_COPY_STATUS_IN_TRANSIT ) {
479 $logger->warn("! canceling hold [$hid] that is in transit");
480 my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id},{idlist=>1})->[0];
483 my $trans = $e->retrieve_action_transit_copy($transid);
484 # Leave the transit alive, but set the copy status to
485 # reshelving so it will be properly reshelved when it gets back home
487 $trans->copy_status( OILS_COPY_STATUS_RESHELVING );
488 $e->update_action_transit_copy($trans) or return $e->die_event;
494 $hold->cancel_time('now');
495 $e->update_action_hold_request($hold)
498 delete_hold_copy_maps($self, $e, $hold->id);
504 sub delete_hold_copy_maps {
509 my $maps = $editor->search_action_hold_copy_map({hold=>$holdid});
511 $editor->delete_action_hold_copy_map($_)
512 or return $editor->event;
518 __PACKAGE__->register_method(
519 method => "update_hold",
520 api_name => "open-ils.circ.hold.update",
522 Updates the specified hold. The login session
523 is the requestor and if the requestor is different from the usr field
524 on the hold, the requestor must have UPDATE_HOLDS permissions.
528 my($self, $client, $auth, $hold) = @_;
530 my $e = new_editor(authtoken=>$auth, xact=>1);
531 return $e->die_event unless $e->checkauth;
533 my $orig_hold = $e->retrieve_action_hold_request($hold->id)
534 or return $e->die_event;
536 # don't allow the user to be changed
537 return OpenILS::Event->new('BAD_PARAMS') if $hold->usr != $orig_hold->usr;
539 if($hold->usr ne $e->requestor->id) {
540 # if the hold is for a different user, make sure the
541 # requestor has the appropriate permissions
542 my $usr = $e->retrieve_actor_user($hold->usr)
543 or return $e->die_event;
544 return $e->die_event unless $e->allowed('UPDATE_HOLD', $usr->home_ou);
547 # --------------------------------------------------------------
548 # if the hold is on the holds shelf and the pickup lib changes,
549 # we need to create a new transit
550 # --------------------------------------------------------------
551 if( ($orig_hold->pickup_lib ne $hold->pickup_lib) and (_hold_status($e, $hold) == 4)) {
552 return $e->die_event unless $e->allowed('UPDATE_PICKUP_LIB_FROM_HOLDS_SHELF', $orig_hold->pickup_lib);
553 return $e->die_event unless $e->allowed('UPDATE_PICKUP_LIB_FROM_HOLDS_SHELF', $hold->pickup_lib);
554 my $evt = transit_hold($e, $orig_hold, $hold,
555 $e->retrieve_asset_copy($hold->current_copy));
559 update_hold_if_frozen($self, $e, $hold, $orig_hold);
560 $e->update_action_hold_request($hold) or return $e->die_event;
566 my($e, $orig_hold, $hold, $copy) = @_;
567 my $src = $orig_hold->pickup_lib;
568 my $dest = $hold->pickup_lib;
570 $logger->info("putting hold into transit on pickup_lib update");
572 my $transit = Fieldmapper::action::transit_copy->new;
573 $transit->source($src);
574 $transit->dest($dest);
575 $transit->target_copy($copy->id);
576 $transit->source_send_time('now');
577 $transit->copy_status(OILS_COPY_STATUS_ON_HOLDS_SHELF);
579 $copy->status(OILS_COPY_STATUS_IN_TRANSIT);
580 $copy->editor($e->requestor->id);
581 $copy->edit_date('now');
583 $e->create_action_transit_copy($transit) or return $e->die_event;
584 $e->update_asset_copy($copy) or return $e->die_event;
588 # if the hold is frozen, this method ensures that the hold is not "targeted",
589 # that is, it clears the current_copy and prev_check_time to essentiallly
590 # reset the hold. If it is being activated, it runs the targeter in the background
591 sub update_hold_if_frozen {
592 my($self, $e, $hold, $orig_hold) = @_;
593 return if $hold->capture_time;
595 if($U->is_true($hold->frozen)) {
596 $logger->info("clearing current_copy and check_time for frozen hold ".$hold->id);
597 $hold->clear_current_copy;
598 $hold->clear_prev_check_time;
601 if($U->is_true($orig_hold->frozen)) {
602 $logger->info("Running targeter on activated hold ".$hold->id);
603 $U->storagereq( 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
609 __PACKAGE__->register_method(
610 method => "retrieve_hold_status",
611 api_name => "open-ils.circ.hold.status.retrieve",
613 Calculates the current status of the hold.
614 the requestor must have VIEW_HOLD permissions if the hold is for a user
615 other than the requestor.
616 Returns -1 on error (for now)
617 Returns 1 for 'waiting for copy to become available'
618 Returns 2 for 'waiting for copy capture'
619 Returns 3 for 'in transit'
620 Returns 4 for 'arrived'
623 sub retrieve_hold_status {
624 my($self, $client, $auth, $hold_id) = @_;
626 my $e = new_editor(authtoken => $auth);
627 return $e->event unless $e->checkauth;
628 my $hold = $e->retrieve_action_hold_request($hold_id)
631 if( $e->requestor->id != $hold->usr ) {
632 return $e->event unless $e->allowed('VIEW_HOLD');
635 return _hold_status($e, $hold);
641 return 1 unless $hold->current_copy;
642 return 2 unless $hold->capture_time;
644 my $copy = $hold->current_copy;
645 unless( ref $copy ) {
646 $copy = $e->retrieve_asset_copy($hold->current_copy)
650 return 3 if $copy->status == OILS_COPY_STATUS_IN_TRANSIT;
651 return 4 if $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF;
658 __PACKAGE__->register_method(
659 method => "retrieve_hold_queue_stats",
660 api_name => "open-ils.circ.hold.queue_stats.retrieve",
663 Returns object with total_holds count, queue_position, potential_copies count, and status code
668 sub retrieve_hold_queue_stats {
669 my($self, $conn, $auth, $hold_id) = @_;
670 my $e = new_editor(authtoken => $auth);
671 return $e->event unless $e->checkauth;
672 my $hold = $e->retrieve_action_hold_request($hold_id) or return $e->event;
673 if($e->requestor->id != $hold->usr) {
674 return $e->event unless $e->allowed('VIEW_HOLD');
676 return retrieve_hold_queue_status_impl($e, $hold);
679 sub retrieve_hold_queue_status_impl {
683 my $hold_ids = $e->search_action_hold_request(
685 { target => $hold->target,
686 hold_type => $hold->hold_type,
687 cancel_time => undef,
688 capture_time => undef
690 {order_by => {ahr => 'request_time asc'}}
696 for my $hid (@$hold_ids) {
698 last if $hid == $hold->id;
701 my $potentials = $e->search_action_hold_copy_map({hold => $hold->id}, {idlist => 1});
702 my $num_potentials = scalar(@$potentials);
704 my $user_org = $e->json_query({select => {au => 'home_ou'}, from => 'au', where => {id => $hold->usr}})->[0]->{home_ou};
705 my $default_hold_interval = $U->ou_ancestor_setting_value($user_org, OILS_SETTING_HOLD_ESIMATE_WAIT_INTERVAL);
706 my $estimated_wait = $qpos * ($default_hold_interval / $num_potentials) if $default_hold_interval;
709 total_holds => scalar(@$hold_ids),
710 queue_position => $qpos,
711 potential_copies => $num_potentials,
712 status => _hold_status($e, $hold),
713 estimated_wait => int($estimated_wait)
718 sub fetch_open_hold_by_current_copy {
721 my $hold = $apputils->simplereq(
723 'open-ils.cstore.direct.action.hold_request.search.atomic',
724 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
725 return $hold->[0] if ref($hold);
729 sub fetch_related_holds {
732 return $apputils->simplereq(
734 'open-ils.cstore.direct.action.hold_request.search.atomic',
735 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
739 __PACKAGE__->register_method (
740 method => "hold_pull_list",
741 api_name => "open-ils.circ.hold_pull_list.retrieve",
743 Returns a list of holds that need to be "pulled"
748 __PACKAGE__->register_method (
749 method => "hold_pull_list",
750 api_name => "open-ils.circ.hold_pull_list.id_list.retrieve",
752 Returns a list of hold ID's that need to be "pulled"
759 my( $self, $conn, $authtoken, $limit, $offset ) = @_;
760 my( $reqr, $evt ) = $U->checkses($authtoken);
763 my $org = $reqr->ws_ou || $reqr->home_ou;
764 # the perm locaiton shouldn't really matter here since holds
765 # will exist all over and VIEW_HOLDS should be universal
766 $evt = $U->check_perms($reqr->id, $org, 'VIEW_HOLD');
769 if( $self->api_name =~ /id_list/ ) {
770 return $U->storagereq(
771 'open-ils.storage.direct.action.hold_request.pull_list.id_list.current_copy_circ_lib.status_filtered.atomic',
772 $org, $limit, $offset );
774 return $U->storagereq(
775 'open-ils.storage.direct.action.hold_request.pull_list.search.current_copy_circ_lib.status_filtered.atomic',
776 $org, $limit, $offset );
780 __PACKAGE__->register_method (
781 method => 'fetch_hold_notify',
782 api_name => 'open-ils.circ.hold_notification.retrieve_by_hold',
784 Returns a list of hold notification objects based on hold id.
785 @param authtoken The loggin session key
786 @param holdid The id of the hold whose notifications we want to retrieve
787 @return An array of hold notification objects, event on error.
791 sub fetch_hold_notify {
792 my( $self, $conn, $authtoken, $holdid ) = @_;
793 my( $requestor, $evt ) = $U->checkses($authtoken);
796 ($hold, $evt) = $U->fetch_hold($holdid);
798 ($patron, $evt) = $U->fetch_user($hold->usr);
801 $evt = $U->check_perms($requestor->id, $patron->home_ou, 'VIEW_HOLD_NOTIFICATION');
804 $logger->info("User ".$requestor->id." fetching hold notifications for hold $holdid");
805 return $U->cstorereq(
806 'open-ils.cstore.direct.action.hold_notification.search.atomic', {hold => $holdid} );
810 __PACKAGE__->register_method (
811 method => 'create_hold_notify',
812 api_name => 'open-ils.circ.hold_notification.create',
814 Creates a new hold notification object
815 @param authtoken The login session key
816 @param notification The hold notification object to create
817 @return ID of the new object on success, Event on error
821 sub create_hold_notify {
822 my( $self, $conn, $auth, $note ) = @_;
823 my $e = new_editor(authtoken=>$auth, xact=>1);
824 return $e->die_event unless $e->checkauth;
826 my $hold = $e->retrieve_action_hold_request($note->hold)
827 or return $e->die_event;
828 my $patron = $e->retrieve_actor_user($hold->usr)
829 or return $e->die_event;
831 return $e->die_event unless
832 $e->allowed('CREATE_HOLD_NOTIFICATION', $patron->home_ou);
834 $note->notify_staff($e->requestor->id);
835 $e->create_action_hold_notification($note) or return $e->die_event;
841 __PACKAGE__->register_method(
842 method => 'reset_hold',
843 api_name => 'open-ils.circ.hold.reset',
845 Un-captures and un-targets a hold, essentially returning
846 it to the state it was in directly after it was placed,
847 then attempts to re-target the hold
848 @param authtoken The login session key
849 @param holdid The id of the hold
855 my( $self, $conn, $auth, $holdid ) = @_;
857 my ($hold, $evt) = $U->fetch_hold($holdid);
859 ($reqr, $evt) = $U->checksesperm($auth, 'UPDATE_HOLD'); # XXX stronger permission
861 $evt = _reset_hold($self, $reqr, $hold);
867 my ($self, $reqr, $hold) = @_;
869 my $e = new_editor(xact =>1, requestor => $reqr);
871 $logger->info("reseting hold ".$hold->id);
875 if( $hold->capture_time and $hold->current_copy ) {
877 my $copy = $e->retrieve_asset_copy($hold->current_copy)
880 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
881 $logger->info("setting copy to status 'reshelving' on hold retarget");
882 $copy->status(OILS_COPY_STATUS_RESHELVING);
883 $copy->editor($e->requestor->id);
884 $copy->edit_date('now');
885 $e->update_asset_copy($copy) or return $e->event;
887 } elsif( $copy->status == OILS_COPY_STATUS_IN_TRANSIT ) {
889 # We don't want the copy to remain "in transit"
890 $copy->status(OILS_COPY_STATUS_RESHELVING);
891 $logger->warn("! reseting hold [$hid] that is in transit");
892 my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id},{idlist=>1})->[0];
895 my $trans = $e->retrieve_action_transit_copy($transid);
897 $logger->info("Aborting transit [$transid] on hold [$hid] reset...");
898 my $evt = OpenILS::Application::Circ::Transit::__abort_transit($e, $trans, $copy, 1);
899 $logger->info("Transit abort completed with result $evt");
900 return $evt unless "$evt" eq 1;
906 $hold->clear_capture_time;
907 $hold->clear_current_copy;
909 $e->update_action_hold_request($hold) or return $e->event;
913 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
919 __PACKAGE__->register_method(
920 method => 'fetch_open_title_holds',
921 api_name => 'open-ils.circ.open_holds.retrieve',
923 Returns a list ids of un-fulfilled holds for a given title id
924 @param authtoken The login session key
925 @param id the id of the item whose holds we want to retrieve
926 @param type The hold type - M, T, V, C
930 sub fetch_open_title_holds {
931 my( $self, $conn, $auth, $id, $type, $org ) = @_;
932 my $e = new_editor( authtoken => $auth );
933 return $e->event unless $e->checkauth;
936 $org ||= $e->requestor->ws_ou;
938 # return $e->search_action_hold_request(
939 # { target => $id, hold_type => $type, fulfillment_time => undef }, {idlist=>1});
941 # XXX make me return IDs in the future ^--
942 my $holds = $e->search_action_hold_request(
945 cancel_time => undef,
947 fulfillment_time => undef
951 flesh_hold_transits($holds);
956 sub flesh_hold_transits {
958 for my $hold ( @$holds ) {
960 $apputils->simplereq(
962 "open-ils.cstore.direct.action.hold_transit_copy.search.atomic",
963 { hold => $hold->id },
964 { order_by => { ahtc => 'id desc' }, limit => 1 }
970 sub flesh_hold_notices {
971 my( $holds, $e ) = @_;
974 for my $hold (@$holds) {
975 my $notices = $e->search_action_hold_notification(
977 { hold => $hold->id },
978 { order_by => { anh => 'notify_time desc' } },
983 $hold->notify_count(scalar(@$notices));
985 my $n = $e->retrieve_action_hold_notification($$notices[0])
987 $hold->notify_time($n->notify_time);
995 __PACKAGE__->register_method(
996 method => 'fetch_captured_holds',
997 api_name => 'open-ils.circ.captured_holds.on_shelf.retrieve',
999 Returns a list of un-fulfilled holds for a given title id
1000 @param authtoken The login session key
1001 @param org The org id of the location in question
1005 __PACKAGE__->register_method(
1006 method => 'fetch_captured_holds',
1007 api_name => 'open-ils.circ.captured_holds.id_list.on_shelf.retrieve',
1009 Returns a list ids of un-fulfilled holds for a given title id
1010 @param authtoken The login session key
1011 @param org The org id of the location in question
1015 sub fetch_captured_holds {
1016 my( $self, $conn, $auth, $org ) = @_;
1018 my $e = new_editor(authtoken => $auth);
1019 return $e->event unless $e->checkauth;
1020 return $e->event unless $e->allowed('VIEW_HOLD'); # XXX rely on editor perm
1022 $org ||= $e->requestor->ws_ou;
1024 my $holds = $e->search_action_hold_request(
1026 capture_time => { "!=" => undef },
1027 current_copy => { "!=" => undef },
1028 fulfillment_time => undef,
1030 cancel_time => undef,
1035 for my $h (@$holds) {
1036 my $copy = $e->retrieve_asset_copy($h->current_copy)
1037 or return $e->event;
1039 $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF;
1042 if( ! $self->api_name =~ /id_list/ ) {
1043 flesh_hold_transits(\@res);
1044 flesh_hold_notices(\@res, $e);
1047 if( $self->api_name =~ /id_list/ ) {
1048 return [ map { $_->id } @res ];
1054 __PACKAGE__->register_method(
1055 method => "check_title_hold",
1056 api_name => "open-ils.circ.title_hold.is_possible",
1058 Determines if a hold were to be placed by a given user,
1059 whether or not said hold would have any potential copies
1061 @param authtoken The login session key
1062 @param params A hash of named params including:
1063 patronid - the id of the hold recipient
1064 titleid (brn) - the id of the title to be held
1065 depth - the hold range depth (defaults to 0)
1068 sub check_title_hold {
1069 my( $self, $client, $authtoken, $params ) = @_;
1071 my %params = %$params;
1072 my $titleid = $params{titleid} ||"";
1073 my $volid = $params{volume_id};
1074 my $copyid = $params{copy_id};
1075 my $mrid = $params{mrid} ||"";
1076 my $depth = $params{depth} || 0;
1077 my $pickup_lib = $params{pickup_lib};
1078 my $hold_type = $params{hold_type} || 'T';
1079 my $selection_ou = $params{selection_ou} || $pickup_lib;
1081 my $e = new_editor(authtoken=>$authtoken);
1082 return $e->event unless $e->checkauth;
1083 my $patron = $e->retrieve_actor_user($params{patronid})
1084 or return $e->event;
1086 if( $e->requestor->id ne $patron->id ) {
1087 return $e->event unless
1088 $e->allowed('VIEW_HOLD_PERMIT', $patron->home_ou);
1091 return OpenILS::Event->new('PATRON_BARRED') if $U->is_true($patron->barred);
1093 my $request_lib = $e->retrieve_actor_org_unit($e->requestor->ws_ou)
1094 or return $e->event;
1096 my $soft_boundary = $U->ou_ancestor_setting_value($selection_ou, OILS_SETTING_HOLD_SOFT_BOUNDARY);
1097 my $hard_boundary = $U->ou_ancestor_setting_value($selection_ou, OILS_SETTING_HOLD_HARD_BOUNDARY);
1099 if(defined $soft_boundary and $$params{depth} < $soft_boundary) {
1100 # work up the tree and as soon as we find a potential copy, use that depth
1101 # also, make sure we don't go past the hard boundary if it exists
1103 # our min boundary is the greater of user-specified boundary or hard boundary
1104 my $min_depth = (defined $hard_boundary and $hard_boundary > $$params{depth}) ?
1105 $hard_boundary : $$params{depth};
1107 my $depth = $soft_boundary;
1108 while($depth >= $min_depth) {
1109 $logger->info("performing hold possibility check with soft boundary $depth");
1110 return {success => 1, depth => $depth}
1111 if do_possibility_checks($e, $patron, $request_lib, $depth, %params);
1114 return {success => 0};
1116 } elsif(defined $hard_boundary and $$params{depth} < $hard_boundary) {
1117 # there is no soft boundary, enforce the hard boundary if it exists
1118 $logger->info("performing hold possibility check with hard boundary $hard_boundary");
1119 if(do_possibility_checks($e, $patron, $request_lib, $hard_boundary, %params)) {
1120 return {success => 1, depth => $hard_boundary}
1122 return {success => 0};
1126 # no boundaries defined, fall back to user specifed boundary or no boundary
1127 $logger->info("performing hold possibility check with no boundary");
1128 if(do_possibility_checks($e, $patron, $request_lib, $params{depth}, %params)) {
1129 return {success => 1, depth => $hard_boundary};
1131 return {success => 0};
1136 sub do_possibility_checks {
1137 my($e, $patron, $request_lib, $depth, %params) = @_;
1139 my $titleid = $params{titleid} ||"";
1140 my $volid = $params{volume_id};
1141 my $copyid = $params{copy_id};
1142 my $mrid = $params{mrid} ||"";
1143 my $pickup_lib = $params{pickup_lib};
1144 my $hold_type = $params{hold_type} || 'T';
1145 my $selection_ou = $params{selection_ou} || $pickup_lib;
1152 if( $hold_type eq OILS_HOLD_TYPE_COPY ) {
1154 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
1155 $volume = $e->retrieve_asset_call_number($copy->call_number)
1156 or return $e->event;
1157 $title = $e->retrieve_biblio_record_entry($volume->record)
1158 or return $e->event;
1159 return verify_copy_for_hold(
1160 $patron, $e->requestor, $title, $copy, $pickup_lib, $request_lib );
1162 } elsif( $hold_type eq OILS_HOLD_TYPE_VOLUME ) {
1164 $volume = $e->retrieve_asset_call_number($volid)
1165 or return $e->event;
1166 $title = $e->retrieve_biblio_record_entry($volume->record)
1167 or return $e->event;
1169 return _check_volume_hold_is_possible(
1170 $volume, $title, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou);
1172 } elsif( $hold_type eq OILS_HOLD_TYPE_TITLE ) {
1174 return _check_title_hold_is_possible(
1175 $titleid, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou);
1177 } elsif( $hold_type eq OILS_HOLD_TYPE_METARECORD ) {
1179 my $maps = $e->search_metabib_source_map({metarecord=>$mrid});
1180 my @recs = map { $_->source } @$maps;
1181 for my $rec (@recs) {
1182 return 1 if (_check_title_hold_is_possible(
1183 $rec, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou));
1191 sub _check_metarecord_hold_is_possible {
1192 my( $mrid, $rangelib, $depth, $request_lib, $patron, $requestor, $pickup_lib ) = @_;
1194 my $e = new_editor();
1196 # this monster will grab the id and circ_lib of all of the "holdable" copies for the given metarecord
1197 my $copies = $e->json_query(
1199 select => { acp => ['id', 'circ_lib'] },
1204 fkey => 'call_number',
1209 filter => { metarecord => $mrid }
1213 acpl => { field => 'id', filter => { holdable => 't'}, fkey => 'location' },
1214 ccs => { field => 'id', filter => { holdable => 't'}, fkey => 'status' }
1218 '+acp' => { circulate => 't', deleted => 'f', holdable => 't' }
1223 return $e->event unless defined $copies;
1224 $logger->info("metarecord possible found ".scalar(@$copies)." potential copies");
1225 return 0 unless @$copies;
1227 # -----------------------------------------------------------------------
1228 # sort the copies into buckets based on their circ_lib proximity to
1229 # the patron's home_ou.
1230 # -----------------------------------------------------------------------
1232 my $home_org = $patron->home_ou;
1233 my $req_org = $request_lib->id;
1235 $prox_cache{$home_org} =
1236 $e->search_actor_org_unit_proximity({from_org => $home_org})
1237 unless $prox_cache{$home_org};
1238 my $home_prox = $prox_cache{$home_org};
1241 my %hash = map { ($_->to_org => $_->prox) } @$home_prox;
1242 push( @{$buckets{ $hash{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
1244 my @keys = sort { $a <=> $b } keys %buckets;
1247 if( $home_org ne $req_org ) {
1248 # -----------------------------------------------------------------------
1249 # shove the copies close to the request_lib into the primary buckets
1250 # directly before the farthest away copies. That way, they are not
1251 # given priority, but they are checked before the farthest copies.
1252 # -----------------------------------------------------------------------
1254 $prox_cache{$req_org} =
1255 $e->search_actor_org_unit_proximity({from_org => $req_org})
1256 unless $prox_cache{$req_org};
1257 my $req_prox = $prox_cache{$req_org};
1260 my %hash2 = map { ($_->to_org => $_->prox) } @$req_prox;
1261 push( @{$buckets2{ $hash2{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
1263 my $highest_key = $keys[@keys - 1]; # the farthest prox in the exising buckets
1264 my $new_key = $highest_key - 0.5; # right before the farthest prox
1265 my @keys2 = sort { $a <=> $b } keys %buckets2;
1266 for my $key (@keys2) {
1267 last if $key >= $highest_key;
1268 push( @{$buckets{$new_key}}, $_ ) for @{$buckets2{$key}};
1272 @keys = sort { $a <=> $b } keys %buckets;
1275 for my $key (@keys) {
1276 my @cps = @{$buckets{$key}};
1278 $logger->info("looking at " . scalar(@{$buckets{$key}}). " copies in proximity bucket $key");
1280 for my $copyid (@cps) {
1282 next if $seen{$copyid};
1283 $seen{$copyid} = 1; # there could be dupes given the merged buckets
1284 my $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
1285 $logger->debug("looking at bucket_key=$key, copy $copyid : circ_lib = " . $copy->circ_lib);
1287 my $vol = $e->retrieve_asset_call_number(
1288 [ $copy->call_number, { flesh => 1, flesh_fields => { acn => ['record'] } } ] );
1290 return 1 if verify_copy_for_hold(
1291 $patron, $requestor, $vol->record, $copy, $pickup_lib, $request_lib );
1299 sub create_ranged_org_filter {
1300 my($e, $selection_ou, $depth) = @_;
1302 # find the orgs from which this hold may be fulfilled,
1303 # based on the selection_ou and depth
1305 my $top_org = $e->search_actor_org_unit([
1306 {parent_ou => undef},
1307 {flesh=>1, flesh_fields=>{aou=>['ou_type']}}])->[0];
1310 return () if $depth == $top_org->ou_type->depth;
1312 my $org_list = $U->storagereq('open-ils.storage.actor.org_unit.descendants.atomic', $selection_ou, $depth);
1313 %org_filter = (circ_lib => []);
1314 push(@{$org_filter{circ_lib}}, $_->id) for @$org_list;
1316 $logger->info("hold org filter at depth $depth and selection_ou ".
1317 "$selection_ou created list of @{$org_filter{circ_lib}}");
1323 sub _check_title_hold_is_possible {
1324 my( $titleid, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou ) = @_;
1326 my $e = new_editor();
1327 my %org_filter = create_ranged_org_filter($e, $selection_ou, $depth);
1329 # this monster will grab the id and circ_lib of all of the "holdable" copies for the given record
1330 my $copies = $e->json_query(
1332 select => { acp => ['id', 'circ_lib'] },
1337 fkey => 'call_number',
1341 filter => { id => $titleid },
1346 acpl => { field => 'id', filter => { holdable => 't'}, fkey => 'location' },
1347 ccs => { field => 'id', filter => { holdable => 't'}, fkey => 'status' }
1351 '+acp' => { circulate => 't', deleted => 'f', holdable => 't', %org_filter }
1356 return $e->event unless defined $copies;
1357 $logger->info("title possible found ".scalar(@$copies)." potential copies");
1358 return 0 unless @$copies;
1360 # -----------------------------------------------------------------------
1361 # sort the copies into buckets based on their circ_lib proximity to
1362 # the patron's home_ou.
1363 # -----------------------------------------------------------------------
1365 my $home_org = $patron->home_ou;
1366 my $req_org = $request_lib->id;
1368 $logger->info("prox cache $home_org " . $prox_cache{$home_org});
1370 $prox_cache{$home_org} =
1371 $e->search_actor_org_unit_proximity({from_org => $home_org})
1372 unless $prox_cache{$home_org};
1373 my $home_prox = $prox_cache{$home_org};
1376 my %hash = map { ($_->to_org => $_->prox) } @$home_prox;
1377 push( @{$buckets{ $hash{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
1379 my @keys = sort { $a <=> $b } keys %buckets;
1382 if( $home_org ne $req_org ) {
1383 # -----------------------------------------------------------------------
1384 # shove the copies close to the request_lib into the primary buckets
1385 # directly before the farthest away copies. That way, they are not
1386 # given priority, but they are checked before the farthest copies.
1387 # -----------------------------------------------------------------------
1388 $prox_cache{$req_org} =
1389 $e->search_actor_org_unit_proximity({from_org => $req_org})
1390 unless $prox_cache{$req_org};
1391 my $req_prox = $prox_cache{$req_org};
1395 my %hash2 = map { ($_->to_org => $_->prox) } @$req_prox;
1396 push( @{$buckets2{ $hash2{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
1398 my $highest_key = $keys[@keys - 1]; # the farthest prox in the exising buckets
1399 my $new_key = $highest_key - 0.5; # right before the farthest prox
1400 my @keys2 = sort { $a <=> $b } keys %buckets2;
1401 for my $key (@keys2) {
1402 last if $key >= $highest_key;
1403 push( @{$buckets{$new_key}}, $_ ) for @{$buckets2{$key}};
1407 @keys = sort { $a <=> $b } keys %buckets;
1411 for my $key (@keys) {
1412 my @cps = @{$buckets{$key}};
1414 $logger->info("looking at " . scalar(@{$buckets{$key}}). " copies in proximity bucket $key");
1416 for my $copyid (@cps) {
1418 next if $seen{$copyid};
1419 $seen{$copyid} = 1; # there could be dupes given the merged buckets
1420 my $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
1421 $logger->debug("looking at bucket_key=$key, copy $copyid : circ_lib = " . $copy->circ_lib);
1423 unless($title) { # grab the title if we don't already have it
1424 my $vol = $e->retrieve_asset_call_number(
1425 [ $copy->call_number, { flesh => 1, flesh_fields => { acn => ['record'] } } ] );
1426 $title = $vol->record;
1429 return 1 if verify_copy_for_hold(
1430 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib );
1439 sub _check_volume_hold_is_possible {
1440 my( $vol, $title, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou ) = @_;
1441 my %org_filter = create_ranged_org_filter(new_editor(), $selection_ou, $depth);
1442 my $copies = new_editor->search_asset_copy({call_number => $vol->id, %org_filter});
1443 $logger->info("checking possibility of volume hold for volume ".$vol->id);
1444 for my $copy ( @$copies ) {
1445 return 1 if verify_copy_for_hold(
1446 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib );
1453 sub verify_copy_for_hold {
1454 my( $patron, $requestor, $title, $copy, $pickup_lib, $request_lib ) = @_;
1455 $logger->info("checking possibility of copy in hold request for copy ".$copy->id);
1456 return 1 if OpenILS::Utils::PermitHold::permit_copy_hold(
1457 { patron => $patron,
1458 requestor => $requestor,
1461 title_descriptor => $title->fixed_fields, # this is fleshed into the title object
1462 pickup_lib => $pickup_lib,
1463 request_lib => $request_lib,
1472 sub find_nearest_permitted_hold {
1475 my $editor = shift; # CStoreEditor object
1476 my $copy = shift; # copy to target
1477 my $user = shift; # staff
1478 my $check_only = shift; # do no updates, just see if the copy could fulfill a hold
1479 my $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND');
1481 my $bc = $copy->barcode;
1483 # find any existing holds that already target this copy
1484 my $old_holds = $editor->search_action_hold_request(
1485 { current_copy => $copy->id,
1486 cancel_time => undef,
1487 capture_time => undef
1491 # hold->type "R" means we need this copy
1492 for my $h (@$old_holds) { return ($h) if $h->hold_type eq 'R'; }
1495 my $hold_stall_interval = $U->ou_ancestor_setting_value($user->ws_ou, OILS_SETTING_HOLD_SOFT_STALL);
1497 $logger->info("circulator: searching for best hold at org ".$user->ws_ou.
1498 " and copy $bc with a hold stalling interval of ". ($hold_stall_interval || "(none)"));
1500 # search for what should be the best holds for this copy to fulfill
1501 my $best_holds = $U->storagereq(
1502 "open-ils.storage.action.hold_request.nearest_hold.atomic",
1503 $user->ws_ou, $copy->id, 10, $hold_stall_interval );
1505 unless(@$best_holds) {
1507 if( my $hold = $$old_holds[0] ) {
1508 $logger->info("circulator: using existing pre-targeted hold ".$hold->id." in hold search");
1512 $logger->info("circulator: no suitable holds found for copy $bc");
1513 return (undef, $evt);
1519 # for each potential hold, we have to run the permit script
1520 # to make sure the hold is actually permitted.
1521 for my $holdid (@$best_holds) {
1522 next unless $holdid;
1523 $logger->info("circulator: checking if hold $holdid is permitted for copy $bc");
1525 my $hold = $editor->retrieve_action_hold_request($holdid) or next;
1526 my $reqr = $editor->retrieve_actor_user($hold->requestor) or next;
1527 my $rlib = $editor->retrieve_actor_org_unit($hold->request_lib) or next;
1529 # see if this hold is permitted
1530 my $permitted = OpenILS::Utils::PermitHold::permit_copy_hold(
1531 { patron_id => $hold->usr,
1534 pickup_lib => $hold->pickup_lib,
1535 request_lib => $rlib,
1546 unless( $best_hold ) { # no "good" permitted holds were found
1547 if( my $hold = $$old_holds[0] ) { # can we return a pre-targeted hold?
1548 $logger->info("circulator: using existing pre-targeted hold ".$hold->id." in hold search");
1553 $logger->info("circulator: no suitable holds found for copy $bc");
1554 return (undef, $evt);
1557 $logger->info("circulator: best hold ".$best_hold->id." found for copy $bc");
1559 # indicate a permitted hold was found
1560 return $best_hold if $check_only;
1562 # we've found a permitted hold. we need to "grab" the copy
1563 # to prevent re-targeted holds (next part) from re-grabbing the copy
1564 $best_hold->current_copy($copy->id);
1565 $editor->update_action_hold_request($best_hold)
1566 or return (undef, $editor->event);
1571 # re-target any other holds that already target this copy
1572 for my $old_hold (@$old_holds) {
1573 next if $old_hold->id eq $best_hold->id; # don't re-target the hold we want
1574 $logger->info("circulator: clearing current_copy and prev_check_time on hold ".
1575 $old_hold->id." after a better hold [".$best_hold->id."] was found");
1576 $old_hold->clear_current_copy;
1577 $old_hold->clear_prev_check_time;
1578 $editor->update_action_hold_request($old_hold)
1579 or return (undef, $editor->event);
1583 return ($best_hold, undef, $retarget);
1591 __PACKAGE__->register_method(
1592 method => 'all_rec_holds',
1593 api_name => 'open-ils.circ.holds.retrieve_all_from_title',
1597 my( $self, $conn, $auth, $title_id, $args ) = @_;
1599 my $e = new_editor(authtoken=>$auth);
1600 $e->checkauth or return $e->event;
1601 $e->allowed('VIEW_HOLD') or return $e->event;
1603 $args ||= { fulfillment_time => undef };
1604 $args->{cancel_time} = undef;
1606 my $resp = { volume_holds => [], copy_holds => [] };
1608 $resp->{title_holds} = $e->search_action_hold_request(
1610 hold_type => OILS_HOLD_TYPE_TITLE,
1611 target => $title_id,
1615 my $vols = $e->search_asset_call_number(
1616 { record => $title_id, deleted => 'f' }, {idlist=>1});
1618 return $resp unless @$vols;
1620 $resp->{volume_holds} = $e->search_action_hold_request(
1622 hold_type => OILS_HOLD_TYPE_VOLUME,
1627 my $copies = $e->search_asset_copy(
1628 { call_number => $vols, deleted => 'f' }, {idlist=>1});
1630 return $resp unless @$copies;
1632 $resp->{copy_holds} = $e->search_action_hold_request(
1634 hold_type => OILS_HOLD_TYPE_COPY,
1646 __PACKAGE__->register_method(
1647 method => 'uber_hold',
1649 api_name => 'open-ils.circ.hold.details.retrieve'
1653 my($self, $client, $auth, $hold_id) = @_;
1654 my $e = new_editor(authtoken=>$auth);
1655 $e->checkauth or return $e->event;
1656 $e->allowed('VIEW_HOLD') or return $e->event;
1660 my $hold = $e->retrieve_action_hold_request(
1665 flesh_fields => { ahr => [ 'current_copy', 'usr' ] }
1668 ) or return $e->event;
1670 my $user = $hold->usr;
1671 $hold->usr($user->id);
1673 my $card = $e->retrieve_actor_card($user->card)
1674 or return $e->event;
1676 my( $mvr, $volume, $copy ) = find_hold_mvr($e, $hold);
1678 flesh_hold_notices([$hold], $e);
1679 flesh_hold_transits([$hold]);
1686 status => _hold_status($e, $hold),
1687 patron_first => $user->first_given_name,
1688 patron_last => $user->family_name,
1689 patron_barcode => $card->barcode,
1695 # -----------------------------------------------------
1696 # Returns the MVR object that represents what the
1698 # -----------------------------------------------------
1700 my( $e, $hold ) = @_;
1706 if( $hold->hold_type eq OILS_HOLD_TYPE_METARECORD ) {
1707 my $mr = $e->retrieve_metabib_metarecord($hold->target)
1708 or return $e->event;
1709 $tid = $mr->master_record;
1711 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_TITLE ) {
1712 $tid = $hold->target;
1714 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_VOLUME ) {
1715 $volume = $e->retrieve_asset_call_number($hold->target)
1716 or return $e->event;
1717 $tid = $volume->record;
1719 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_COPY ) {
1720 $copy = $e->retrieve_asset_copy($hold->target)
1721 or return $e->event;
1722 $volume = $e->retrieve_asset_call_number($copy->call_number)
1723 or return $e->event;
1724 $tid = $volume->record;
1727 if(!$copy and ref $hold->current_copy ) {
1728 $copy = $hold->current_copy;
1729 $hold->current_copy($copy->id);
1732 if(!$volume and $copy) {
1733 $volume = $e->retrieve_asset_call_number($copy->call_number);
1736 my $title = $e->retrieve_biblio_record_entry($tid);
1737 return ( $U->record_to_mvr($title), $volume, $copy );