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/OpenSRF::Application/;
19 use strict; use warnings;
20 use OpenILS::Application::AppUtils;
22 use OpenSRF::EX qw(:try);
25 use OpenSRF::Utils::Logger qw(:logger);
26 use OpenILS::Utils::CStoreEditor q/:funcs/;
27 use OpenILS::Utils::PermitHold;
28 use OpenSRF::Utils::SettingsClient;
29 use OpenILS::Const qw/:const/;
31 my $apputils = "OpenILS::Application::AppUtils";
36 __PACKAGE__->register_method(
37 method => "create_hold",
38 api_name => "open-ils.circ.holds.create",
40 Create a new hold for an item. From a permissions perspective,
41 the login session is used as the 'requestor' of the hold.
42 The hold recipient is determined by the 'usr' setting within
45 First we verify the requestion has holds request permissions.
46 Then we verify that the recipient is allowed to make the given hold.
47 If not, we see if the requestor has "override" capabilities. If not,
48 a permission exception is returned. If permissions allow, we cycle
49 through the set of holds objects and create.
51 If the recipient does not have permission to place multiple holds
52 on a single title and said operation is attempted, a permission
57 __PACKAGE__->register_method(
58 method => "create_hold",
59 api_name => "open-ils.circ.holds.create.override",
61 If the recipient is not allowed to receive the requested hold,
62 call this method to attempt the override
63 @see open-ils.circ.holds.create
68 my( $self, $conn, $auth, @holds ) = @_;
69 my $e = new_editor(authtoken=>$auth, xact=>1);
70 return $e->event unless $e->checkauth;
72 my $override = 1 if $self->api_name =~ /override/;
74 my $holds = (ref($holds[0] eq 'ARRAY')) ? $holds[0] : [@holds];
76 for my $hold (@$holds) {
81 my $requestor = $e->requestor;
82 my $recipient = $requestor;
85 if( $requestor->id ne $hold->usr ) {
86 # Make sure the requestor is allowed to place holds for
87 # the recipient if they are not the same people
88 $recipient = $e->retrieve_actor_user($hold->usr) or return $e->event;
89 $e->allowed('REQUEST_HOLDS', $recipient->home_ou) or return $e->event;
93 # Now make sure the recipient is allowed to receive the specified hold
95 my $porg = $recipient->home_ou;
96 my $rid = $e->requestor->id;
97 my $t = $hold->hold_type;
99 # See if a duplicate hold already exists
101 usr => $recipient->id,
103 fulfillment_time => undef,
104 target => $hold->target,
105 cancel_time => undef,
108 $sargs->{holdable_formats} = $hold->holdable_formats if $t eq 'M';
110 my $existing = $e->search_action_hold_request($sargs);
111 push( @events, OpenILS::Event->new('HOLD_EXISTS')) if @$existing;
113 if( $t eq 'M' ) { $pevt = $e->event unless $e->checkperm($rid, $porg, 'MR_HOLDS'); }
114 if( $t eq 'T' ) { $pevt = $e->event unless $e->checkperm($rid, $porg, 'TITLE_HOLDS'); }
115 if( $t eq 'V' ) { $pevt = $e->event unless $e->checkperm($rid, $porg, 'VOLUME_HOLDS'); }
116 if( $t eq 'C' ) { $pevt = $e->event unless $e->checkperm($rid, $porg, 'COPY_HOLDS'); }
118 return $pevt if $pevt;
122 for my $evt (@events) {
124 my $name = $evt->{textcode};
125 return $e->event unless $e->allowed("$name.override", $porg);
135 # return $e->event unless $e->allowed('CREATE_DUPLICATE_HOLDS', $porg);
142 $hold->requestor($e->requestor->id);
143 $hold->selection_ou($recipient->home_ou) unless $hold->selection_ou;
144 $e->create_action_hold_request($hold) or return $e->event;
152 my( $self, $client, $login_session, @holds) = @_;
154 if(!@holds){return 0;}
155 my( $user, $evt ) = $apputils->checkses($login_session);
159 if(ref($holds[0]) eq 'ARRAY') {
161 } else { $holds = [ @holds ]; }
163 $logger->debug("Iterating over holds requests...");
165 for my $hold (@$holds) {
168 my $type = $hold->hold_type;
170 $logger->activity("User " . $user->id .
171 " creating new hold of type $type for user " . $hold->usr);
174 if($user->id ne $hold->usr) {
175 ( $recipient, $evt ) = $apputils->fetch_user($hold->usr);
185 # am I allowed to place holds for this user?
186 if($hold->requestor ne $hold->usr) {
187 $perm = _check_request_holds_perm($user->id, $user->home_ou);
188 if($perm) { return $perm; }
191 # is this user allowed to have holds of this type?
192 $perm = _check_holds_perm($type, $hold->requestor, $recipient->home_ou);
194 #if there is a requestor, see if the requestor has override privelages
195 if($hold->requestor ne $hold->usr) {
196 $perm = _check_request_holds_override($user->id, $user->home_ou);
197 if($perm) {return $perm;}
204 #enforce the fact that the login is the one requesting the hold
205 $hold->requestor($user->id);
206 $hold->selection_ou($recipient->home_ou) unless $hold->selection_ou;
208 my $resp = $apputils->simplereq(
210 'open-ils.storage.direct.action.hold_request.create', $hold );
213 return OpenSRF::EX::ERROR ("Error creating hold");
220 # makes sure that a user has permission to place the type of requested hold
221 # returns the Perm exception if not allowed, returns undef if all is well
222 sub _check_holds_perm {
223 my($type, $user_id, $org_id) = @_;
227 if($evt = $apputils->check_perms(
228 $user_id, $org_id, "MR_HOLDS")) {
232 } elsif ($type eq "T") {
233 if($evt = $apputils->check_perms(
234 $user_id, $org_id, "TITLE_HOLDS")) {
238 } elsif($type eq "V") {
239 if($evt = $apputils->check_perms(
240 $user_id, $org_id, "VOLUME_HOLDS")) {
244 } elsif($type eq "C") {
245 if($evt = $apputils->check_perms(
246 $user_id, $org_id, "COPY_HOLDS")) {
254 # tests if the given user is allowed to place holds on another's behalf
255 sub _check_request_holds_perm {
258 if(my $evt = $apputils->check_perms(
259 $user_id, $org_id, "REQUEST_HOLDS")) {
264 sub _check_request_holds_override {
267 if(my $evt = $apputils->check_perms(
268 $user_id, $org_id, "REQUEST_HOLDS_OVERRIDE")) {
273 __PACKAGE__->register_method(
274 method => "retrieve_holds_by_id",
275 api_name => "open-ils.circ.holds.retrieve_by_id",
277 Retrieve the hold, with hold transits attached, for the specified id The login session is the requestor and if the requestor is
278 different from the user, then the requestor must have VIEW_HOLD permissions.
282 sub retrieve_holds_by_id {
283 my($self, $client, $login_session, $hold_id) = @_;
286 #my( $user, $target, $evt ) = $apputils->checkses_requestor(
287 # $login_session, $user_id, 'VIEW_HOLD' );
288 #return $evt if $evt;
290 my $holds = $apputils->simplereq(
292 "open-ils.cstore.direct.action.hold_request.search.atomic",
293 { id => $hold_id , fulfillment_time => undef },
294 { order_by => { ahr => "request_time" } }
297 flesh_hold_transits($holds);
302 __PACKAGE__->register_method(
303 method => "retrieve_holds",
304 api_name => "open-ils.circ.holds.retrieve",
306 Retrieves all the holds, with hold transits attached, for the specified
307 user id. The login session is the requestor and if the requestor is
308 different from the user, then the requestor must have VIEW_HOLD permissions.
313 my($self, $client, $login_session, $user_id) = @_;
315 my( $user, $target, $evt ) = $apputils->checkses_requestor(
316 $login_session, $user_id, 'VIEW_HOLD' );
319 my $holds = $apputils->simplereq(
321 "open-ils.cstore.direct.action.hold_request.search.atomic",
324 fulfillment_time => undef,
325 cancel_time => undef,
327 { order_by => { ahr => "request_time" } }
330 for my $hold ( @$holds ) {
332 $apputils->simplereq(
334 "open-ils.cstore.direct.action.hold_transit_copy.search.atomic",
335 { hold => $hold->id },
336 { order_by => { ahtc => 'id desc' }, limit => 1 }
344 __PACKAGE__->register_method(
345 method => "retrieve_holds_by_pickup_lib",
346 api_name => "open-ils.circ.holds.retrieve_by_pickup_lib",
348 Retrieves all the holds, with hold transits attached, for the specified
353 sub retrieve_holds_by_pickup_lib {
354 my($self, $client, $login_session, $ou_id) = @_;
356 #FIXME -- put an appropriate permission check here
357 #my( $user, $target, $evt ) = $apputils->checkses_requestor(
358 # $login_session, $user_id, 'VIEW_HOLD' );
359 #return $evt if $evt;
361 my $holds = $apputils->simplereq(
363 "open-ils.cstore.direct.action.hold_request.search.atomic",
365 pickup_lib => $ou_id ,
366 fulfillment_time => undef,
369 { order_by => { ahr => "request_time" } });
372 flesh_hold_transits($holds);
377 __PACKAGE__->register_method(
378 method => "cancel_hold",
379 api_name => "open-ils.circ.hold.cancel",
381 Cancels the specified hold. The login session
382 is the requestor and if the requestor is different from the usr field
383 on the hold, the requestor must have CANCEL_HOLDS permissions.
384 the hold may be either the hold object or the hold id
388 my($self, $client, $auth, $holdid) = @_;
390 my $e = new_editor(authtoken=>$auth, xact=>1);
391 return $e->event unless $e->checkauth;
393 my $hold = $e->retrieve_action_hold_request($holdid)
396 if( $e->requestor->id ne $hold->usr ) {
397 return $e->event unless $e->allowed('CANCEL_HOLDS');
400 return 1 if $hold->cancel_time;
402 # If the hold is captured, reset the copy status
403 if( $hold->capture_time and $hold->current_copy ) {
405 my $copy = $e->retrieve_asset_copy($hold->current_copy)
408 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
409 $logger->info("setting copy to status 'reshelving' on hold cancel");
410 $copy->status(OILS_COPY_STATUS_RESHELVING);
411 $copy->editor($e->requestor->id);
412 $copy->edit_date('now');
413 $e->update_asset_copy($copy) or return $e->event;
417 $hold->cancel_time('now');
418 $e->update_action_hold_request($hold)
421 $self->delete_hold_copy_maps($e, $hold->id);
427 sub delete_hold_copy_maps {
432 my $maps = $editor->search_action_hold_copy_map({hold=>$holdid});
434 $editor->delete_action_hold_copy_map($_)
435 or return $editor->event;
441 __PACKAGE__->register_method(
442 method => "update_hold",
443 api_name => "open-ils.circ.hold.update",
445 Updates the specified hold. The login session
446 is the requestor and if the requestor is different from the usr field
447 on the hold, the requestor must have UPDATE_HOLDS permissions.
451 my($self, $client, $login_session, $hold) = @_;
453 my( $requestor, $target, $evt ) = $apputils->checkses_requestor(
454 $login_session, $hold->usr, 'UPDATE_HOLD' );
457 $logger->activity('User ' . $requestor->id .
458 ' updating hold ' . $hold->id . ' for user ' . $target->id );
460 return $U->storagereq(
461 "open-ils.storage.direct.action.hold_request.update", $hold );
465 __PACKAGE__->register_method(
466 method => "retrieve_hold_status",
467 api_name => "open-ils.circ.hold.status.retrieve",
469 Calculates the current status of the hold.
470 the requestor must have VIEW_HOLD permissions if the hold is for a user
471 other than the requestor.
472 Returns -1 on error (for now)
473 Returns 1 for 'waiting for copy to become available'
474 Returns 2 for 'waiting for copy capture'
475 Returns 3 for 'in transit'
476 Returns 4 for 'arrived'
479 sub retrieve_hold_status {
480 my($self, $client, $login_session, $hold_id) = @_;
483 my( $requestor, $target, $hold, $copy, $transit, $evt );
485 ( $hold, $evt ) = $apputils->fetch_hold($hold_id);
488 ( $requestor, $target, $evt ) = $apputils->checkses_requestor(
489 $login_session, $hold->usr, 'VIEW_HOLD' );
492 return 1 unless (defined($hold->current_copy));
494 ( $copy, $evt ) = $apputils->fetch_copy($hold->current_copy);
497 return 4 if ($hold->capture_time and $copy->circ_lib eq $hold->pickup_lib);
499 ( $transit, $evt ) = $apputils->fetch_hold_transit_by_hold( $hold->id );
500 return 4 if(ref($transit) and defined($transit->dest_recv_time) );
502 return 3 if defined($hold->capture_time);
512 __PACKAGE__->register_method(
513 method => "capture_copy",
514 api_name => "open-ils.circ.hold.capture_copy.barcode",
516 Captures a copy to fulfil a hold
517 Params is login session and copy barcode
518 Optional param is 'flesh'. If set, we also return the
519 relevant copy and title
520 login mus have COPY_CHECKIN permissions (since this is essentially
524 # XXX deprecate me XXX
527 my( $self, $client, $login_session, $params ) = @_;
528 my %params = %$params;
529 my $barcode = $params{barcode};
532 my( $user, $target, $copy, $hold, $evt );
534 ( $user, $evt ) = $apputils->checkses($login_session);
537 # am I allowed to checkin a copy?
538 $evt = $apputils->check_perms($user->id, $user->home_ou, "COPY_CHECKIN");
541 $logger->info("Capturing copy with barcode $barcode");
543 my $session = $apputils->start_db_session();
545 ($copy, $evt) = $apputils->fetch_copy_by_barcode($barcode);
548 $logger->debug("Capturing copy " . $copy->id);
550 #( $hold, $evt ) = _find_local_hold_for_copy($session, $copy, $user);
551 ( $hold, $evt ) = $self->find_nearest_permitted_hold($session, $copy, $user);
554 warn "Found hold " . $hold->id . "\n";
555 $logger->info("We found a hold " .$hold->id. "for capturing copy with barcode $barcode");
557 $hold->current_copy($copy->id);
558 $hold->capture_time("now");
561 my $stat = $session->request(
562 "open-ils.storage.direct.action.hold_request.update", $hold)->gather(1);
563 if(!$stat) { throw OpenSRF::EX::ERROR
564 ("Error updating hold request " . $copy->id); }
566 $copy->status(OILS_COPY_STATUS_ON_HOLDS_SHELF); #status on holds shelf
568 # if the staff member capturing this item is not at the pickup lib
569 if( $user->home_ou ne $hold->pickup_lib ) {
570 $self->_build_hold_transit( $login_session, $session, $hold, $user, $copy );
573 $copy->editor($user->id);
574 $copy->edit_date("now");
575 $stat = $session->request(
576 "open-ils.storage.direct.asset.copy.update", $copy )->gather(1);
577 if(!$stat) { throw OpenSRF::EX ("Error updating copy " . $copy->id); }
579 my $payload = { hold => $hold };
580 $payload->{copy} = $copy if $params{flesh_copy};
582 if($params{flesh_record}) {
584 ($record, $evt) = $apputils->fetch_record_by_copy( $copy->id );
586 $record = $apputils->record_to_mvr($record);
587 $payload->{record} = $record;
590 $apputils->commit_db_session($session);
592 return OpenILS::Event->new('ROUTE_ITEM',
593 route_to => $hold->pickup_lib, payload => $payload );
596 sub _build_hold_transit {
597 my( $self, $login_session, $session, $hold, $user, $copy ) = @_;
598 my $trans = Fieldmapper::action::hold_transit_copy->new;
600 $trans->hold($hold->id);
601 $trans->source($user->home_ou);
602 $trans->dest($hold->pickup_lib);
603 $trans->source_send_time("now");
604 $trans->target_copy($copy->id);
605 $trans->copy_status($copy->status);
607 my $meth = $self->method_lookup("open-ils.circ.hold_transit.create");
608 my ($stat) = $meth->run( $login_session, $trans, $session );
609 if(!$stat) { throw OpenSRF::EX ("Error creating new hold transit"); }
610 else { $copy->status(6); } #status in transit
615 __PACKAGE__->register_method(
616 method => "create_hold_transit",
617 api_name => "open-ils.circ.hold_transit.create",
619 Creates a new transit object
622 sub create_hold_transit {
623 my( $self, $client, $login_session, $transit, $session ) = @_;
625 my( $user, $evt ) = $apputils->checkses($login_session);
627 $evt = $apputils->check_perms($user->id, $user->home_ou, "CREATE_TRANSIT");
631 if($session) { $ses = $session; }
632 else { $ses = OpenSRF::AppSession->create("open-ils.storage"); }
634 return $ses->request(
635 "open-ils.storage.direct.action.hold_transit_copy.create", $transit )->gather(1);
641 sub find_local_hold {
642 my( $class, $session, $copy, $user ) = @_;
643 return $class->find_nearest_permitted_hold($session, $copy, $user);
651 sub fetch_open_hold_by_current_copy {
654 my $hold = $apputils->simplereq(
656 'open-ils.cstore.direct.action.hold_request.search.atomic',
657 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
658 return $hold->[0] if ref($hold);
662 sub fetch_related_holds {
665 return $apputils->simplereq(
667 'open-ils.cstore.direct.action.hold_request.search.atomic',
668 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
672 __PACKAGE__->register_method (
673 method => "hold_pull_list",
674 api_name => "open-ils.circ.hold_pull_list.retrieve",
676 Returns a list of hold ID's that need to be "pulled"
682 my( $self, $conn, $authtoken, $limit, $offset ) = @_;
683 my( $reqr, $evt ) = $U->checkses($authtoken);
686 my $org = $reqr->ws_ou || $reqr->home_ou;
687 # the perm locaiton shouldn't really matter here since holds
688 # will exist all over and VIEW_HOLDS should be universal
689 $evt = $U->check_perms($reqr->id, $org, 'VIEW_HOLD');
692 return $U->storagereq(
693 'open-ils.storage.direct.action.hold_request.pull_list.search.current_copy_circ_lib.atomic',
694 $org, $limit, $offset );
697 __PACKAGE__->register_method (
698 method => 'fetch_hold_notify',
699 api_name => 'open-ils.circ.hold_notification.retrieve_by_hold',
701 Returns a list of hold notification objects based on hold id.
702 @param authtoken The loggin session key
703 @param holdid The id of the hold whose notifications we want to retrieve
704 @return An array of hold notification objects, event on error.
708 sub fetch_hold_notify {
709 my( $self, $conn, $authtoken, $holdid ) = @_;
710 my( $requestor, $evt ) = $U->checkses($authtoken);
713 ($hold, $evt) = $U->fetch_hold($holdid);
715 ($patron, $evt) = $U->fetch_user($hold->usr);
718 $evt = $U->check_perms($requestor->id, $patron->home_ou, 'VIEW_HOLD_NOTIFICATION');
721 $logger->info("User ".$requestor->id." fetching hold notifications for hold $holdid");
722 return $U->cstorereq(
723 'open-ils.cstore.direct.action.hold_notification.search.atomic', {hold => $holdid} );
727 __PACKAGE__->register_method (
728 method => 'create_hold_notify',
729 api_name => 'open-ils.circ.hold_notification.create',
731 Creates a new hold notification object
732 @param authtoken The login session key
733 @param notification The hold notification object to create
734 @return ID of the new object on success, Event on error
737 sub create_hold_notify {
738 my( $self, $conn, $authtoken, $notification ) = @_;
739 my( $requestor, $evt ) = $U->checkses($authtoken);
742 ($hold, $evt) = $U->fetch_hold($notification->hold);
744 ($patron, $evt) = $U->fetch_user($hold->usr);
747 # XXX perm depth probably doesn't matter here -- should always be consortium level
748 $evt = $U->check_perms($requestor->id, $patron->home_ou, 'CREATE_HOLD_NOTIFICATION');
751 # Set the proper notifier
752 $notification->notify_staff($requestor->id);
753 my $id = $U->storagereq(
754 'open-ils.storage.direct.action.hold_notification.create', $notification );
755 return $U->DB_UPDATE_FAILED($notification) unless $id;
756 $logger->info("User ".$requestor->id." successfully created new hold notification $id");
761 __PACKAGE__->register_method(
762 method => 'reset_hold',
763 api_name => 'open-ils.circ.hold.reset',
765 Un-captures and un-targets a hold, essentially returning
766 it to the state it was in directly after it was placed,
767 then attempts to re-target the hold
768 @param authtoken The login session key
769 @param holdid The id of the hold
775 my( $self, $conn, $auth, $holdid ) = @_;
777 my ($hold, $evt) = $U->fetch_hold($holdid);
779 ($reqr, $evt) = $U->checksesperm($auth, 'UPDATE_HOLD'); # XXX stronger permission
781 $evt = $self->_reset_hold($reqr, $hold);
787 my ($self, $reqr, $hold) = @_;
789 my $e = new_editor(xact =>1, requestor => $reqr);
791 if( $hold->capture_time and $hold->current_copy ) {
793 my $copy = $e->retrieve_asset_copy($hold->current_copy)
796 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
797 $logger->info("setting copy to status 'reshelving' on hold retarget");
798 $copy->status(OILS_COPY_STATUS_RESHELVING);
799 $copy->editor($e->requestor->id);
800 $copy->edit_date('now');
801 $e->update_asset_copy($copy) or return $e->event;
805 $hold->clear_capture_time;
806 $hold->clear_current_copy;
808 $e->update_action_hold_request($hold) or return $e->event;
813 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
819 __PACKAGE__->register_method(
820 method => 'fetch_open_title_holds',
821 api_name => 'open-ils.circ.open_holds.retrieve',
823 Returns a list ids of un-fulfilled holds for a given title id
824 @param authtoken The login session key
825 @param id the id of the item whose holds we want to retrieve
826 @param type The hold type - M, T, V, C
830 sub fetch_open_title_holds {
831 my( $self, $conn, $auth, $id, $type, $org ) = @_;
832 my $e = new_editor( authtoken => $auth );
833 return $e->event unless $e->checkauth;
836 $org ||= $e->requestor->ws_ou;
838 # return $e->search_action_hold_request(
839 # { target => $id, hold_type => $type, fulfillment_time => undef }, {idlist=>1});
841 # XXX make me return IDs in the future ^--
842 my $holds = $e->search_action_hold_request(
845 cancel_time => undef,
847 fulfillment_time => undef
851 flesh_hold_transits($holds);
856 sub flesh_hold_transits {
858 for my $hold ( @$holds ) {
860 $apputils->simplereq(
862 "open-ils.cstore.direct.action.hold_transit_copy.search.atomic",
863 { hold => $hold->id },
864 { order_by => { ahtc => 'id desc' }, limit => 1 }
873 __PACKAGE__->register_method(
874 method => 'fetch_captured_holds',
875 api_name => 'open-ils.circ.captured_holds.on_shelf.retrieve',
877 Returns a list ids of un-fulfilled holds for a given title id
878 @param authtoken The login session key
879 @param org The org id of the location in question
882 sub fetch_captured_holds {
883 my( $self, $conn, $auth, $org ) = @_;
885 my $e = new_editor(authtoken => $auth);
886 return $e->event unless $e->checkauth;
887 return $e->event unless $e->allowed('VIEW_HOLD'); # XXX rely on editor perm
889 $org ||= $e->requestor->ws_ou;
891 my $holds = $e->search_action_hold_request(
893 capture_time => { "!=" => undef },
894 current_copy => { "!=" => undef },
895 fulfillment_time => undef,
897 cancel_time => undef,
902 for my $h (@$holds) {
903 my $copy = $e->retrieve_asset_copy($h->current_copy)
906 $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF;
909 flesh_hold_transits(\@res);
917 __PACKAGE__->register_method(
918 method => "check_title_hold",
919 api_name => "open-ils.circ.title_hold.is_possible",
921 Determines if a hold were to be placed by a given user,
922 whether or not said hold would have any potential copies
924 @param authtoken The login session key
925 @param params A hash of named params including:
926 patronid - the id of the hold recipient
927 titleid (brn) - the id of the title to be held
928 depth - the hold range depth (defaults to 0)
931 sub check_title_hold {
932 my( $self, $client, $authtoken, $params ) = @_;
934 my %params = %$params;
935 my $titleid = $params{titleid} ||"";
936 my $mrid = $params{mrid} ||"";
937 my $depth = $params{depth} || 0;
938 my $pickup_lib = $params{pickup_lib};
939 my $hold_type = $params{hold_type} || 'T';
941 my $e = new_editor(authtoken=>$authtoken);
942 return $e->event unless $e->checkauth;
943 my $patron = $e->retrieve_actor_user($params{patronid})
946 if( $e->requestor->id ne $patron->id ) {
947 return $e->event unless
948 $e->allowed('VIEW_HOLD_PERMIT', $patron->home_ou);
951 return OpenILS::Event->new('PATRON_BARRED')
952 if $patron->barred and
953 ($patron->barred =~ /t/i or $patron->barred == 1);
955 my $rangelib = $params{range_lib} || $patron->home_ou;
957 my $request_lib = $e->retrieve_actor_org_unit($e->requestor->ws_ou)
960 if( $hold_type eq 'T' ) {
961 return _check_title_hold_is_possible(
962 $titleid, $rangelib, $depth, $request_lib, $patron, $e->requestor, $pickup_lib);
965 if( $hold_type eq 'M' ) {
966 my $maps = $e->search_metabib_source_map({metarecord=>$mrid});
967 my @recs = map { $_->source } @$maps;
968 for my $rec (@recs) {
969 return 1 if (_check_title_hold_is_possible(
970 $rec, $rangelib, $depth, $request_lib, $patron, $e->requestor, $pickup_lib));
977 sub _check_title_hold_is_possible {
978 my( $titleid, $rangelib, $depth, $request_lib, $patron, $requestor, $pickup_lib ) = @_;
984 $logger->debug("Fetching ranged title tree for title $titleid, org $rangelib, depth $depth");
986 while( $title = $U->storagereq(
987 'open-ils.storage.biblio.record_entry.ranged_tree',
988 $titleid, $rangelib, $depth, $limit, $offset ) ) {
992 ref($title->call_numbers) and
993 @{$title->call_numbers};
995 for my $cn (@{$title->call_numbers}) {
997 $logger->debug("Checking callnumber ".$cn->id." for hold fulfillment possibility");
999 for my $copy (@{$cn->copies}) {
1001 $logger->debug("Checking copy ".$copy->id." for hold fulfillment possibility");
1003 return 1 if OpenILS::Utils::PermitHold::permit_copy_hold(
1004 { patron => $patron,
1005 requestor => $requestor,
1008 title_descriptor => $title->fixed_fields, # this is fleshed into the title object
1009 pickup_lib => $pickup_lib,
1010 request_lib => $request_lib
1014 $logger->debug("Copy ".$copy->id." for hold fulfillment possibility failed...");
1025 sub find_nearest_permitted_hold {
1028 my $session = shift;
1031 my $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND');
1033 # first see if this copy has already been selected to fulfill a hold
1034 my $hold = $session->request(
1035 "open-ils.storage.direct.action.hold_request.search_where",
1036 { current_copy => $copy->id, cancel_time => undef, capture_time => undef } )->gather(1);
1039 $logger->info("hold found which can be fulfilled by copy ".$copy->id);
1043 # We know this hold is permitted, so just return it
1044 return $hold if $hold;
1046 $logger->debug("searching for potential holds at org ".
1047 $user->ws_ou." and copy ".$copy->id);
1049 my $holds = $session->request(
1050 "open-ils.storage.action.hold_request.nearest_hold.atomic",
1051 $user->ws_ou, $copy->id, 5 )->gather(1);
1053 return (undef, $evt) unless @$holds;
1055 # for each potential hold, we have to run the permit script
1056 # to make sure the hold is actually permitted.
1058 for my $holdid (@$holds) {
1059 next unless $holdid;
1060 $logger->info("Checking if hold $holdid is permitted for user ".$user->id);
1062 my ($hold) = $U->fetch_hold($holdid);
1064 my ($reqr) = $U->fetch_user($hold->requestor);
1066 return ($hold) if OpenILS::Utils::PermitHold::permit_copy_hold(
1068 patron_id => $hold->usr,
1069 requestor => $reqr->id,
1071 pickup_lib => $hold->pickup_lib,
1072 request_lib => $hold->request_lib
1077 return (undef, $evt);