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];
78 for my $hold (@$holds) {
83 my $requestor = $e->requestor;
84 my $recipient = $requestor;
87 if( $requestor->id ne $hold->usr ) {
88 # Make sure the requestor is allowed to place holds for
89 # the recipient if they are not the same people
90 $recipient = $e->retrieve_actor_user($hold->usr) or return $e->event;
91 $e->allowed('REQUEST_HOLDS', $recipient->home_ou) or return $e->event;
94 # Now make sure the recipient is allowed to receive the specified hold
96 my $porg = $recipient->home_ou;
97 my $rid = $e->requestor->id;
98 my $t = $hold->hold_type;
100 # See if a duplicate hold already exists
102 usr => $recipient->id,
104 fulfillment_time => undef,
105 target => $hold->target,
106 cancel_time => undef,
109 $sargs->{holdable_formats} = $hold->holdable_formats if $t eq 'M';
111 my $existing = $e->search_action_hold_request($sargs);
112 push( @events, OpenILS::Event->new('HOLD_EXISTS')) if @$existing;
114 if( $t eq OILS_HOLD_TYPE_METARECORD )
115 { $pevt = $e->event unless $e->checkperm($rid, $porg, 'MR_HOLDS'); }
117 if( $t eq OILS_HOLD_TYPE_TITLE )
118 { $pevt = $e->event unless $e->checkperm($rid, $porg, 'TITLE_HOLDS'); }
120 if( $t eq OILS_HOLD_TYPE_VOLUME )
121 { $pevt = $e->event unless $e->checkperm($rid, $porg, 'VOLUME_HOLDS'); }
123 if( $t eq OILS_HOLD_TYPE_COPY )
124 { $pevt = $e->event unless $e->checkperm($rid, $porg, 'COPY_HOLDS'); }
126 return $pevt if $pevt;
130 for my $evt (@events) {
132 my $name = $evt->{textcode};
133 return $e->event unless $e->allowed("$name.override", $porg);
140 $hold->requestor($e->requestor->id);
141 $hold->selection_ou($recipient->home_ou) unless $hold->selection_ou;
142 $hold = $e->create_action_hold_request($hold) or return $e->event;
143 push( @copyholds, $hold ) if $hold->hold_type eq OILS_HOLD_TYPE_COPY;
148 # Go ahead and target the copy-level holds
150 'open-ils.storage.action.hold_request.copy_targeter',
151 undef, $_->id ) for @copyholds;
157 my( $self, $client, $login_session, @holds) = @_;
159 if(!@holds){return 0;}
160 my( $user, $evt ) = $apputils->checkses($login_session);
164 if(ref($holds[0]) eq 'ARRAY') {
166 } else { $holds = [ @holds ]; }
168 $logger->debug("Iterating over holds requests...");
170 for my $hold (@$holds) {
173 my $type = $hold->hold_type;
175 $logger->activity("User " . $user->id .
176 " creating new hold of type $type for user " . $hold->usr);
179 if($user->id ne $hold->usr) {
180 ( $recipient, $evt ) = $apputils->fetch_user($hold->usr);
190 # am I allowed to place holds for this user?
191 if($hold->requestor ne $hold->usr) {
192 $perm = _check_request_holds_perm($user->id, $user->home_ou);
193 if($perm) { return $perm; }
196 # is this user allowed to have holds of this type?
197 $perm = _check_holds_perm($type, $hold->requestor, $recipient->home_ou);
199 #if there is a requestor, see if the requestor has override privelages
200 if($hold->requestor ne $hold->usr) {
201 $perm = _check_request_holds_override($user->id, $user->home_ou);
202 if($perm) {return $perm;}
209 #enforce the fact that the login is the one requesting the hold
210 $hold->requestor($user->id);
211 $hold->selection_ou($recipient->home_ou) unless $hold->selection_ou;
213 my $resp = $apputils->simplereq(
215 'open-ils.storage.direct.action.hold_request.create', $hold );
218 return OpenSRF::EX::ERROR ("Error creating hold");
225 # makes sure that a user has permission to place the type of requested hold
226 # returns the Perm exception if not allowed, returns undef if all is well
227 sub _check_holds_perm {
228 my($type, $user_id, $org_id) = @_;
232 if($evt = $apputils->check_perms(
233 $user_id, $org_id, "MR_HOLDS")) {
237 } elsif ($type eq "T") {
238 if($evt = $apputils->check_perms(
239 $user_id, $org_id, "TITLE_HOLDS")) {
243 } elsif($type eq "V") {
244 if($evt = $apputils->check_perms(
245 $user_id, $org_id, "VOLUME_HOLDS")) {
249 } elsif($type eq "C") {
250 if($evt = $apputils->check_perms(
251 $user_id, $org_id, "COPY_HOLDS")) {
259 # tests if the given user is allowed to place holds on another's behalf
260 sub _check_request_holds_perm {
263 if(my $evt = $apputils->check_perms(
264 $user_id, $org_id, "REQUEST_HOLDS")) {
269 sub _check_request_holds_override {
272 if(my $evt = $apputils->check_perms(
273 $user_id, $org_id, "REQUEST_HOLDS_OVERRIDE")) {
278 __PACKAGE__->register_method(
279 method => "retrieve_holds_by_id",
280 api_name => "open-ils.circ.holds.retrieve_by_id",
282 Retrieve the hold, with hold transits attached, for the specified id The login session is the requestor and if the requestor is
283 different from the user, then the requestor must have VIEW_HOLD permissions.
287 sub retrieve_holds_by_id {
288 my($self, $client, $auth, $hold_id) = @_;
289 my $e = new_editor(authtoken=>$auth);
290 $e->checkauth or return $e->event;
291 $e->allowed('VIEW_HOLD') or return $e->event;
293 my $holds = $e->search_action_hold_request(
295 { id => $hold_id , fulfillment_time => undef },
296 { order_by => { ahr => "request_time" } }
300 flesh_hold_transits($holds);
301 flesh_hold_notices($holds, $e);
306 __PACKAGE__->register_method(
307 method => "retrieve_holds",
308 api_name => "open-ils.circ.holds.retrieve",
310 Retrieves all the holds, with hold transits attached, for the specified
311 user id. The login session is the requestor and if the requestor is
312 different from the user, then the requestor must have VIEW_HOLD permissions.
315 __PACKAGE__->register_method(
316 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 ];
362 __PACKAGE__->register_method(
363 method => "retrieve_holds_by_pickup_lib",
364 api_name => "open-ils.circ.holds.retrieve_by_pickup_lib",
366 Retrieves all the holds, with hold transits attached, for the specified
370 __PACKAGE__->register_method(
371 method => "retrieve_holds_by_pickup_lib",
372 api_name => "open-ils.circ.holds.id_list.retrieve_by_pickup_lib",
374 Retrieves all the hold ids for the specified
378 sub retrieve_holds_by_pickup_lib {
379 my($self, $client, $login_session, $ou_id) = @_;
381 #FIXME -- put an appropriate permission check here
382 #my( $user, $target, $evt ) = $apputils->checkses_requestor(
383 # $login_session, $user_id, 'VIEW_HOLD' );
384 #return $evt if $evt;
386 my $holds = $apputils->simplereq(
388 "open-ils.cstore.direct.action.hold_request.search.atomic",
390 pickup_lib => $ou_id ,
391 fulfillment_time => undef,
394 { order_by => { ahr => "request_time" } });
397 if( ! $self->api_name =~ /id_list/ ) {
398 flesh_hold_transits($holds);
401 if( $self->api_name =~ /id_list/ ) {
402 return [ map { $_->id } @$holds ];
408 __PACKAGE__->register_method(
409 method => "cancel_hold",
410 api_name => "open-ils.circ.hold.cancel",
412 Cancels the specified hold. The login session
413 is the requestor and if the requestor is different from the usr field
414 on the hold, the requestor must have CANCEL_HOLDS permissions.
415 the hold may be either the hold object or the hold id
419 my($self, $client, $auth, $holdid) = @_;
421 my $e = new_editor(authtoken=>$auth, xact=>1);
422 return $e->event unless $e->checkauth;
424 my $hold = $e->retrieve_action_hold_request($holdid)
427 if( $e->requestor->id ne $hold->usr ) {
428 return $e->event unless $e->allowed('CANCEL_HOLDS');
431 return 1 if $hold->cancel_time;
433 # If the hold is captured, reset the copy status
434 if( $hold->capture_time and $hold->current_copy ) {
436 my $copy = $e->retrieve_asset_copy($hold->current_copy)
439 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
440 $logger->info("setting copy to status 'reshelving' on hold cancel");
441 $copy->status(OILS_COPY_STATUS_RESHELVING);
442 $copy->editor($e->requestor->id);
443 $copy->edit_date('now');
444 $e->update_asset_copy($copy) or return $e->event;
448 $hold->cancel_time('now');
449 $e->update_action_hold_request($hold)
452 $self->delete_hold_copy_maps($e, $hold->id);
458 sub delete_hold_copy_maps {
463 my $maps = $editor->search_action_hold_copy_map({hold=>$holdid});
465 $editor->delete_action_hold_copy_map($_)
466 or return $editor->event;
472 __PACKAGE__->register_method(
473 method => "update_hold",
474 api_name => "open-ils.circ.hold.update",
476 Updates the specified hold. The login session
477 is the requestor and if the requestor is different from the usr field
478 on the hold, the requestor must have UPDATE_HOLDS permissions.
482 my($self, $client, $login_session, $hold) = @_;
484 my( $requestor, $target, $evt ) = $apputils->checkses_requestor(
485 $login_session, $hold->usr, 'UPDATE_HOLD' );
488 $logger->activity('User ' . $requestor->id .
489 ' updating hold ' . $hold->id . ' for user ' . $target->id );
491 return $U->storagereq(
492 "open-ils.storage.direct.action.hold_request.update", $hold );
496 __PACKAGE__->register_method(
497 method => "retrieve_hold_status",
498 api_name => "open-ils.circ.hold.status.retrieve",
500 Calculates the current status of the hold.
501 the requestor must have VIEW_HOLD permissions if the hold is for a user
502 other than the requestor.
503 Returns -1 on error (for now)
504 Returns 1 for 'waiting for copy to become available'
505 Returns 2 for 'waiting for copy capture'
506 Returns 3 for 'in transit'
507 Returns 4 for 'arrived'
510 sub retrieve_hold_status {
511 my($self, $client, $auth, $hold_id) = @_;
513 my $e = new_editor(authtoken => $auth);
514 return $e->event unless $e->checkauth;
515 my $hold = $e->retrieve_action_hold_request($hold_id)
518 if( $e->requestor->id != $hold->usr ) {
519 return $e->event unless $e->allowed('VIEW_HOLD');
522 return 1 unless $hold->current_copy;
523 return 2 unless $hold->capture_time;
525 my $copy = $e->retrieve_asset_copy($hold->current_copy)
528 return 3 if $copy->status == OILS_COPY_STATUS_IN_TRANSIT;
529 return 4 if $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF;
539 __PACKAGE__->register_method(
540 method => "capture_copy",
541 api_name => "open-ils.circ.hold.capture_copy.barcode",
543 Captures a copy to fulfil a hold
544 Params is login session and copy barcode
545 Optional param is 'flesh'. If set, we also return the
546 relevant copy and title
547 login mus have COPY_CHECKIN permissions (since this is essentially
551 # XXX deprecate me XXX
554 my( $self, $client, $login_session, $params ) = @_;
555 my %params = %$params;
556 my $barcode = $params{barcode};
559 my( $user, $target, $copy, $hold, $evt );
561 ( $user, $evt ) = $apputils->checkses($login_session);
564 # am I allowed to checkin a copy?
565 $evt = $apputils->check_perms($user->id, $user->home_ou, "COPY_CHECKIN");
568 $logger->info("Capturing copy with barcode $barcode");
570 my $session = $apputils->start_db_session();
572 ($copy, $evt) = $apputils->fetch_copy_by_barcode($barcode);
575 $logger->debug("Capturing copy " . $copy->id);
577 #( $hold, $evt ) = _find_local_hold_for_copy($session, $copy, $user);
578 ( $hold, $evt ) = $self->find_nearest_permitted_hold($session, $copy, $user);
581 warn "Found hold " . $hold->id . "\n";
582 $logger->info("We found a hold " .$hold->id. "for capturing copy with barcode $barcode");
584 $hold->current_copy($copy->id);
585 $hold->capture_time("now");
588 my $stat = $session->request(
589 "open-ils.storage.direct.action.hold_request.update", $hold)->gather(1);
590 if(!$stat) { throw OpenSRF::EX::ERROR
591 ("Error updating hold request " . $copy->id); }
593 $copy->status(OILS_COPY_STATUS_ON_HOLDS_SHELF); #status on holds shelf
595 # if the staff member capturing this item is not at the pickup lib
596 if( $user->home_ou ne $hold->pickup_lib ) {
597 $self->_build_hold_transit( $login_session, $session, $hold, $user, $copy );
600 $copy->editor($user->id);
601 $copy->edit_date("now");
602 $stat = $session->request(
603 "open-ils.storage.direct.asset.copy.update", $copy )->gather(1);
604 if(!$stat) { throw OpenSRF::EX ("Error updating copy " . $copy->id); }
606 my $payload = { hold => $hold };
607 $payload->{copy} = $copy if $params{flesh_copy};
609 if($params{flesh_record}) {
611 ($record, $evt) = $apputils->fetch_record_by_copy( $copy->id );
613 $record = $apputils->record_to_mvr($record);
614 $payload->{record} = $record;
617 $apputils->commit_db_session($session);
619 return OpenILS::Event->new('ROUTE_ITEM',
620 route_to => $hold->pickup_lib, payload => $payload );
623 sub _build_hold_transit {
624 my( $self, $login_session, $session, $hold, $user, $copy ) = @_;
625 my $trans = Fieldmapper::action::hold_transit_copy->new;
627 $trans->hold($hold->id);
628 $trans->source($user->home_ou);
629 $trans->dest($hold->pickup_lib);
630 $trans->source_send_time("now");
631 $trans->target_copy($copy->id);
632 $trans->copy_status($copy->status);
634 my $meth = $self->method_lookup("open-ils.circ.hold_transit.create");
635 my ($stat) = $meth->run( $login_session, $trans, $session );
636 if(!$stat) { throw OpenSRF::EX ("Error creating new hold transit"); }
637 else { $copy->status(6); } #status in transit
642 __PACKAGE__->register_method(
643 method => "create_hold_transit",
644 api_name => "open-ils.circ.hold_transit.create",
646 Creates a new transit object
649 sub create_hold_transit {
650 my( $self, $client, $login_session, $transit, $session ) = @_;
652 my( $user, $evt ) = $apputils->checkses($login_session);
654 $evt = $apputils->check_perms($user->id, $user->home_ou, "CREATE_TRANSIT");
658 if($session) { $ses = $session; }
659 else { $ses = OpenSRF::AppSession->create("open-ils.storage"); }
661 return $ses->request(
662 "open-ils.storage.direct.action.hold_transit_copy.create", $transit )->gather(1);
668 sub find_local_hold {
669 my( $class, $session, $copy, $user ) = @_;
670 return $class->find_nearest_permitted_hold($session, $copy, $user);
678 sub fetch_open_hold_by_current_copy {
681 my $hold = $apputils->simplereq(
683 'open-ils.cstore.direct.action.hold_request.search.atomic',
684 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
685 return $hold->[0] if ref($hold);
689 sub fetch_related_holds {
692 return $apputils->simplereq(
694 'open-ils.cstore.direct.action.hold_request.search.atomic',
695 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
699 __PACKAGE__->register_method (
700 method => "hold_pull_list",
701 api_name => "open-ils.circ.hold_pull_list.retrieve",
703 Returns a list of holds that need to be "pulled"
708 __PACKAGE__->register_method (
709 method => "hold_pull_list",
710 api_name => "open-ils.circ.hold_pull_list.id_list.retrieve",
712 Returns a list of hold ID's that need to be "pulled"
719 my( $self, $conn, $authtoken, $limit, $offset ) = @_;
720 my( $reqr, $evt ) = $U->checkses($authtoken);
723 my $org = $reqr->ws_ou || $reqr->home_ou;
724 # the perm locaiton shouldn't really matter here since holds
725 # will exist all over and VIEW_HOLDS should be universal
726 $evt = $U->check_perms($reqr->id, $org, 'VIEW_HOLD');
729 if( $self->api_name =~ /id_list/ ) {
730 return $U->storagereq(
731 'open-ils.storage.direct.action.hold_request.pull_list.id_list.current_copy_circ_lib.atomic',
732 $org, $limit, $offset );
734 return $U->storagereq(
735 'open-ils.storage.direct.action.hold_request.pull_list.search.current_copy_circ_lib.atomic',
736 $org, $limit, $offset );
740 __PACKAGE__->register_method (
741 method => 'fetch_hold_notify',
742 api_name => 'open-ils.circ.hold_notification.retrieve_by_hold',
744 Returns a list of hold notification objects based on hold id.
745 @param authtoken The loggin session key
746 @param holdid The id of the hold whose notifications we want to retrieve
747 @return An array of hold notification objects, event on error.
751 sub fetch_hold_notify {
752 my( $self, $conn, $authtoken, $holdid ) = @_;
753 my( $requestor, $evt ) = $U->checkses($authtoken);
756 ($hold, $evt) = $U->fetch_hold($holdid);
758 ($patron, $evt) = $U->fetch_user($hold->usr);
761 $evt = $U->check_perms($requestor->id, $patron->home_ou, 'VIEW_HOLD_NOTIFICATION');
764 $logger->info("User ".$requestor->id." fetching hold notifications for hold $holdid");
765 return $U->cstorereq(
766 'open-ils.cstore.direct.action.hold_notification.search.atomic', {hold => $holdid} );
770 __PACKAGE__->register_method (
771 method => 'create_hold_notify',
772 api_name => 'open-ils.circ.hold_notification.create',
774 Creates a new hold notification object
775 @param authtoken The login session key
776 @param notification The hold notification object to create
777 @return ID of the new object on success, Event on error
780 sub create_hold_notify {
781 my( $self, $conn, $authtoken, $notification ) = @_;
782 my( $requestor, $evt ) = $U->checkses($authtoken);
785 ($hold, $evt) = $U->fetch_hold($notification->hold);
787 ($patron, $evt) = $U->fetch_user($hold->usr);
790 # XXX perm depth probably doesn't matter here -- should always be consortium level
791 $evt = $U->check_perms($requestor->id, $patron->home_ou, 'CREATE_HOLD_NOTIFICATION');
794 # Set the proper notifier
795 $notification->notify_staff($requestor->id);
796 my $id = $U->storagereq(
797 'open-ils.storage.direct.action.hold_notification.create', $notification );
798 return $U->DB_UPDATE_FAILED($notification) unless $id;
799 $logger->info("User ".$requestor->id." successfully created new hold notification $id");
804 __PACKAGE__->register_method(
805 method => 'reset_hold',
806 api_name => 'open-ils.circ.hold.reset',
808 Un-captures and un-targets a hold, essentially returning
809 it to the state it was in directly after it was placed,
810 then attempts to re-target the hold
811 @param authtoken The login session key
812 @param holdid The id of the hold
818 my( $self, $conn, $auth, $holdid ) = @_;
820 my ($hold, $evt) = $U->fetch_hold($holdid);
822 ($reqr, $evt) = $U->checksesperm($auth, 'UPDATE_HOLD'); # XXX stronger permission
824 $evt = $self->_reset_hold($reqr, $hold);
830 my ($self, $reqr, $hold) = @_;
832 my $e = new_editor(xact =>1, requestor => $reqr);
834 $logger->info("reseting hold ".$hold->id);
836 if( $hold->capture_time and $hold->current_copy ) {
838 my $copy = $e->retrieve_asset_copy($hold->current_copy)
841 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
842 $logger->info("setting copy to status 'reshelving' on hold retarget");
843 $copy->status(OILS_COPY_STATUS_RESHELVING);
844 $copy->editor($e->requestor->id);
845 $copy->edit_date('now');
846 $e->update_asset_copy($copy) or return $e->event;
848 } elsif( $copy->status == OILS_COPY_STATUS_IN_TRANSIT ) {
849 $logger->warn("reseting hold that is in transit: ".$hold->id);
854 $hold->clear_capture_time;
855 $hold->clear_current_copy;
857 $e->update_action_hold_request($hold) or return $e->event;
862 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
868 __PACKAGE__->register_method(
869 method => 'fetch_open_title_holds',
870 api_name => 'open-ils.circ.open_holds.retrieve',
872 Returns a list ids of un-fulfilled holds for a given title id
873 @param authtoken The login session key
874 @param id the id of the item whose holds we want to retrieve
875 @param type The hold type - M, T, V, C
879 sub fetch_open_title_holds {
880 my( $self, $conn, $auth, $id, $type, $org ) = @_;
881 my $e = new_editor( authtoken => $auth );
882 return $e->event unless $e->checkauth;
885 $org ||= $e->requestor->ws_ou;
887 # return $e->search_action_hold_request(
888 # { target => $id, hold_type => $type, fulfillment_time => undef }, {idlist=>1});
890 # XXX make me return IDs in the future ^--
891 my $holds = $e->search_action_hold_request(
894 cancel_time => undef,
896 fulfillment_time => undef
900 flesh_hold_transits($holds);
905 sub flesh_hold_transits {
907 for my $hold ( @$holds ) {
909 $apputils->simplereq(
911 "open-ils.cstore.direct.action.hold_transit_copy.search.atomic",
912 { hold => $hold->id },
913 { order_by => { ahtc => 'id desc' }, limit => 1 }
919 sub flesh_hold_notices {
920 my( $holds, $e ) = @_;
923 for my $hold (@$holds) {
924 my $notices = $e->search_action_hold_notification(
926 { hold => $hold->id },
927 { order_by => { anh => { 'notify_time desc' } } },
932 $hold->notify_count(scalar(@$notices));
934 my $n = $e->retrieve_action_hold_notification($$notices[0])
936 $hold->notify_time($n->notify_time);
944 __PACKAGE__->register_method(
945 method => 'fetch_captured_holds',
946 api_name => 'open-ils.circ.captured_holds.on_shelf.retrieve',
948 Returns a list of un-fulfilled holds for a given title id
949 @param authtoken The login session key
950 @param org The org id of the location in question
954 __PACKAGE__->register_method(
955 method => 'fetch_captured_holds',
956 api_name => 'open-ils.circ.captured_holds.id_list.on_shelf.retrieve',
958 Returns a list ids of un-fulfilled holds for a given title id
959 @param authtoken The login session key
960 @param org The org id of the location in question
964 sub fetch_captured_holds {
965 my( $self, $conn, $auth, $org ) = @_;
967 my $e = new_editor(authtoken => $auth);
968 return $e->event unless $e->checkauth;
969 return $e->event unless $e->allowed('VIEW_HOLD'); # XXX rely on editor perm
971 $org ||= $e->requestor->ws_ou;
973 my $holds = $e->search_action_hold_request(
975 capture_time => { "!=" => undef },
976 current_copy => { "!=" => undef },
977 fulfillment_time => undef,
979 cancel_time => undef,
984 for my $h (@$holds) {
985 my $copy = $e->retrieve_asset_copy($h->current_copy)
988 $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF;
991 if( ! $self->api_name =~ /id_list/ ) {
992 flesh_hold_transits(\@res);
993 flesh_hold_notices(\@res, $e);
996 if( $self->api_name =~ /id_list/ ) {
997 return [ map { $_->id } @res ];
1004 __PACKAGE__->register_method(
1005 method => "check_title_hold",
1006 api_name => "open-ils.circ.title_hold.is_possible",
1008 Determines if a hold were to be placed by a given user,
1009 whether or not said hold would have any potential copies
1011 @param authtoken The login session key
1012 @param params A hash of named params including:
1013 patronid - the id of the hold recipient
1014 titleid (brn) - the id of the title to be held
1015 depth - the hold range depth (defaults to 0)
1018 sub check_title_hold {
1019 my( $self, $client, $authtoken, $params ) = @_;
1021 my %params = %$params;
1022 my $titleid = $params{titleid} ||"";
1023 my $mrid = $params{mrid} ||"";
1024 my $depth = $params{depth} || 0;
1025 my $pickup_lib = $params{pickup_lib};
1026 my $hold_type = $params{hold_type} || 'T';
1028 my $e = new_editor(authtoken=>$authtoken);
1029 return $e->event unless $e->checkauth;
1030 my $patron = $e->retrieve_actor_user($params{patronid})
1031 or return $e->event;
1033 if( $e->requestor->id ne $patron->id ) {
1034 return $e->event unless
1035 $e->allowed('VIEW_HOLD_PERMIT', $patron->home_ou);
1038 return OpenILS::Event->new('PATRON_BARRED')
1039 if $patron->barred and
1040 ($patron->barred =~ /t/i or $patron->barred == 1);
1042 my $rangelib = $params{range_lib} || $patron->home_ou;
1044 my $request_lib = $e->retrieve_actor_org_unit($e->requestor->ws_ou)
1045 or return $e->event;
1047 if( $hold_type eq 'T' ) {
1048 return _check_title_hold_is_possible(
1049 $titleid, $rangelib, $depth, $request_lib, $patron, $e->requestor, $pickup_lib);
1052 if( $hold_type eq 'M' ) {
1053 my $maps = $e->search_metabib_source_map({metarecord=>$mrid});
1054 my @recs = map { $_->source } @$maps;
1055 for my $rec (@recs) {
1056 return 1 if (_check_title_hold_is_possible(
1057 $rec, $rangelib, $depth, $request_lib, $patron, $e->requestor, $pickup_lib));
1064 sub _check_title_hold_is_possible {
1065 my( $titleid, $rangelib, $depth, $request_lib, $patron, $requestor, $pickup_lib ) = @_;
1071 $logger->debug("Fetching ranged title tree for title $titleid, org $rangelib, depth $depth");
1073 while( $title = $U->storagereq(
1074 'open-ils.storage.biblio.record_entry.ranged_tree',
1075 $titleid, $rangelib, $depth, $limit, $offset ) ) {
1079 ref($title->call_numbers) and
1080 @{$title->call_numbers};
1082 for my $cn (@{$title->call_numbers}) {
1084 $logger->debug("Checking callnumber ".$cn->id." for hold fulfillment possibility");
1086 for my $copy (@{$cn->copies}) {
1088 $logger->debug("Checking copy ".$copy->id." for hold fulfillment possibility");
1090 return 1 if OpenILS::Utils::PermitHold::permit_copy_hold(
1091 { patron => $patron,
1092 requestor => $requestor,
1095 title_descriptor => $title->fixed_fields, # this is fleshed into the title object
1096 pickup_lib => $pickup_lib,
1097 request_lib => $request_lib
1101 $logger->debug("Copy ".$copy->id." for hold fulfillment possibility failed...");
1112 sub find_nearest_permitted_hold {
1115 my $session = shift;
1118 my $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND');
1120 # first see if this copy has already been selected to fulfill a hold
1121 my $hold = $session->request(
1122 "open-ils.storage.direct.action.hold_request.search_where",
1123 { current_copy => $copy->id, cancel_time => undef, capture_time => undef } )->gather(1);
1126 $logger->info("hold found which can be fulfilled by copy ".$copy->id);
1130 # We know this hold is permitted, so just return it
1131 return $hold if $hold;
1133 $logger->debug("searching for potential holds at org ".
1134 $user->ws_ou." and copy ".$copy->id);
1136 my $holds = $session->request(
1137 "open-ils.storage.action.hold_request.nearest_hold.atomic",
1138 $user->ws_ou, $copy->id, 5 )->gather(1);
1140 return (undef, $evt) unless @$holds;
1142 # for each potential hold, we have to run the permit script
1143 # to make sure the hold is actually permitted.
1145 for my $holdid (@$holds) {
1146 next unless $holdid;
1147 $logger->info("Checking if hold $holdid is permitted for user ".$user->id);
1149 my ($hold) = $U->fetch_hold($holdid);
1151 my ($reqr) = $U->fetch_user($hold->requestor);
1153 return ($hold) if OpenILS::Utils::PermitHold::permit_copy_hold(
1155 patron_id => $hold->usr,
1156 requestor => $reqr->id,
1158 pickup_lib => $hold->pickup_lib,
1159 request_lib => $hold->request_lib
1164 return (undef, $evt);
1168 __PACKAGE__->register_method(
1169 method => 'all_rec_holds',
1170 api_name => 'open-ils.circ.holds.retrieve_all_from_title',
1174 my( $self, $conn, $auth, $title_id, $args ) = @_;
1176 my $e = new_editor(authtoken=>$auth);
1177 $e->checkauth or return $e->event;
1178 $e->allowed('VIEW_HOLD') or return $e->event;
1180 $args ||= { fulfillment_time => undef };
1181 $args->{cancel_time} = undef;
1185 $resp->{title_holds} = $e->search_action_hold_request(
1187 hold_type => OILS_HOLD_TYPE_TITLE,
1188 target => $title_id,
1192 my $vols = $e->search_asset_call_number(
1193 { record => $title_id, deleted => 'f' }, {idlist=>1});
1195 $resp->{volume_holds} = (!@$vols) ? [] : $e->search_action_hold_request(
1197 hold_type => OILS_HOLD_TYPE_VOLUME,
1202 my $copies = $e->search_asset_copy(
1203 { call_number => $vols, deleted => 'f' }, {idlist=>1});
1205 $resp->{copy_holds} = (!@$copies) ? [] : $e->search_action_hold_request(
1207 hold_type => OILS_HOLD_TYPE_COPY,