1 # ---------------------------------------------------------------
2 # Copyright (C) 2005 Georgia Public Library Service
3 # Bill Erickson <highfalutin@gmail.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
17 package OpenILS::Application::Circ::Holds;
18 use base qw/OpenILS::Application/;
19 use strict; use warnings;
20 use OpenILS::Application::AppUtils;
23 use OpenSRF::EX qw(:try);
27 use OpenSRF::Utils::Logger qw(:logger);
28 use OpenILS::Utils::CStoreEditor q/:funcs/;
29 use OpenILS::Utils::PermitHold;
30 use OpenSRF::Utils::SettingsClient;
31 use OpenILS::Const qw/:const/;
32 use OpenILS::Application::Circ::Transit;
33 use OpenILS::Application::Actor::Friends;
35 my $apputils = "OpenILS::Application::AppUtils";
41 __PACKAGE__->register_method(
42 method => "create_hold",
43 api_name => "open-ils.circ.holds.create",
45 Create a new hold for an item. From a permissions perspective,
46 the login session is used as the 'requestor' of the hold.
47 The hold recipient is determined by the 'usr' setting within
50 First we verify the requestion has holds request permissions.
51 Then we verify that the recipient is allowed to make the given hold.
52 If not, we see if the requestor has "override" capabilities. If not,
53 a permission exception is returned. If permissions allow, we cycle
54 through the set of holds objects and create.
56 If the recipient does not have permission to place multiple holds
57 on a single title and said operation is attempted, a permission
62 __PACKAGE__->register_method(
63 method => "create_hold",
64 api_name => "open-ils.circ.holds.create.override",
66 If the recipient is not allowed to receive the requested hold,
67 call this method to attempt the override
68 @see open-ils.circ.holds.create
73 my( $self, $conn, $auth, @holds ) = @_;
74 my $e = new_editor(authtoken=>$auth, xact=>1);
75 return $e->event unless $e->checkauth;
77 my $override = 1 if $self->api_name =~ /override/;
79 my $holds = (ref($holds[0] eq 'ARRAY')) ? $holds[0] : [@holds];
83 for my $hold (@$holds) {
88 my $requestor = $e->requestor;
89 my $recipient = $requestor;
92 if( $requestor->id ne $hold->usr ) {
93 # Make sure the requestor is allowed to place holds for
94 # the recipient if they are not the same people
95 $recipient = $e->retrieve_actor_user($hold->usr) or return $e->event;
96 $e->allowed('REQUEST_HOLDS', $recipient->home_ou) or return $e->event;
99 # Now make sure the recipient is allowed to receive the specified hold
101 my $porg = $recipient->home_ou;
102 my $rid = $e->requestor->id;
103 my $t = $hold->hold_type;
105 # See if a duplicate hold already exists
107 usr => $recipient->id,
109 fulfillment_time => undef,
110 target => $hold->target,
111 cancel_time => undef,
114 $sargs->{holdable_formats} = $hold->holdable_formats if $t eq 'M';
116 my $existing = $e->search_action_hold_request($sargs);
117 push( @events, OpenILS::Event->new('HOLD_EXISTS')) if @$existing;
119 if( $t eq OILS_HOLD_TYPE_METARECORD )
120 { $pevt = $e->event unless $e->allowed('MR_HOLDS', $porg); }
122 if( $t eq OILS_HOLD_TYPE_TITLE )
123 { $pevt = $e->event unless $e->allowed('TITLE_HOLDS', $porg); }
125 if( $t eq OILS_HOLD_TYPE_VOLUME )
126 { $pevt = $e->event unless $e->allowed('VOLUME_HOLDS', $porg); }
128 if( $t eq OILS_HOLD_TYPE_COPY )
129 { $pevt = $e->event unless $e->allowed('COPY_HOLDS', $porg); }
131 return $pevt if $pevt;
135 for my $evt (@events) {
137 my $name = $evt->{textcode};
138 return $e->event unless $e->allowed("$name.override", $porg);
145 # set the configured expire time
146 unless($hold->expire_time) {
147 my $interval = $U->ou_ancestor_setting_value($recipient->home_ou, OILS_SETTING_HOLD_EXPIRE);
149 my $date = DateTime->now->add(seconds => OpenSRF::Utils::interval_to_seconds($interval));
150 $hold->expire_time($U->epoch2ISO8601($date->epoch));
154 $hold->requestor($e->requestor->id);
155 $hold->request_lib($e->requestor->ws_ou);
156 $hold->selection_ou($hold->pickup_lib) unless $hold->selection_ou;
157 $hold = $e->create_action_hold_request($hold) or return $e->event;
162 $conn->respond_complete(1);
165 next if $U->is_true($_->frozen);
167 'open-ils.storage.action.hold_request.copy_targeter',
175 my( $self, $client, $login_session, @holds) = @_;
177 if(!@holds){return 0;}
178 my( $user, $evt ) = $apputils->checkses($login_session);
182 if(ref($holds[0]) eq 'ARRAY') {
184 } else { $holds = [ @holds ]; }
186 $logger->debug("Iterating over holds requests...");
188 for my $hold (@$holds) {
191 my $type = $hold->hold_type;
193 $logger->activity("User " . $user->id .
194 " creating new hold of type $type for user " . $hold->usr);
197 if($user->id ne $hold->usr) {
198 ( $recipient, $evt ) = $apputils->fetch_user($hold->usr);
208 # am I allowed to place holds for this user?
209 if($hold->requestor ne $hold->usr) {
210 $perm = _check_request_holds_perm($user->id, $user->home_ou);
211 if($perm) { return $perm; }
214 # is this user allowed to have holds of this type?
215 $perm = _check_holds_perm($type, $hold->requestor, $recipient->home_ou);
216 return $perm if $perm;
218 #enforce the fact that the login is the one requesting the hold
219 $hold->requestor($user->id);
220 $hold->selection_ou($recipient->home_ou) unless $hold->selection_ou;
222 my $resp = $apputils->simplereq(
224 'open-ils.storage.direct.action.hold_request.create', $hold );
227 return OpenSRF::EX::ERROR ("Error creating hold");
234 # makes sure that a user has permission to place the type of requested hold
235 # returns the Perm exception if not allowed, returns undef if all is well
236 sub _check_holds_perm {
237 my($type, $user_id, $org_id) = @_;
241 if($evt = $apputils->check_perms(
242 $user_id, $org_id, "MR_HOLDS")) {
246 } elsif ($type eq "T") {
247 if($evt = $apputils->check_perms(
248 $user_id, $org_id, "TITLE_HOLDS")) {
252 } elsif($type eq "V") {
253 if($evt = $apputils->check_perms(
254 $user_id, $org_id, "VOLUME_HOLDS")) {
258 } elsif($type eq "C") {
259 if($evt = $apputils->check_perms(
260 $user_id, $org_id, "COPY_HOLDS")) {
268 # tests if the given user is allowed to place holds on another's behalf
269 sub _check_request_holds_perm {
272 if(my $evt = $apputils->check_perms(
273 $user_id, $org_id, "REQUEST_HOLDS")) {
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",
318 api_name => "open-ils.circ.holds.id_list.retrieve",
320 Retrieves all the hold ids for the specified
321 user id. The login session is the requestor and if the requestor is
322 different from the user, then the requestor must have VIEW_HOLD permissions.
326 my($self, $client, $auth, $user_id, $options) = @_;
328 my $e = new_editor(authtoken=>$auth);
329 return $e->event unless $e->checkauth;
330 $user_id = $e->requestor->id unless defined $user_id;
333 unless($user_id == $e->requestor->id) {
334 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
335 unless($e->allowed('VIEW_HOLD', $user->home_ou)) {
336 my $allowed = OpenILS::Application::Actor::Friends->friend_perm_allowed(
337 $e, $user_id, $e->requestor->id, 'hold.view');
338 return $e->event unless $allowed;
342 my $holds = $e->search_action_hold_request([
344 fulfillment_time => undef,
345 cancel_time => undef,
347 {order_by => {ahr => "request_time"}}
350 if($$options{canceled}) {
351 my $count = $$options{cancel_count} ||
352 $U->ou_ancestor_setting_value($e->requestor->ws_ou,
353 'circ.canceled_hold_display_count', $e) || 5;
355 my $canceled = $e->search_action_hold_request([
357 fulfillment_time => undef,
358 cancel_time => {'!=' => undef},
360 {order_by => {ahr => "cancel_time desc"}, limit => $count}
362 push(@$holds, @$canceled);
365 if( ! $self->api_name =~ /id_list/ ) {
366 for my $hold ( @$holds ) {
368 $e->search_action_hold_transit_copy([
370 {order_by => {ahtc => 'id desc'}, limit => 1}])->[0]
375 if( $self->api_name =~ /id_list/ ) {
376 return [ map { $_->id } @$holds ];
383 __PACKAGE__->register_method(
384 method => 'user_hold_count',
385 api_name => 'open-ils.circ.hold.user.count');
387 sub user_hold_count {
388 my( $self, $conn, $auth, $userid ) = @_;
389 my $e = new_editor(authtoken=>$auth);
390 return $e->event unless $e->checkauth;
391 my $patron = $e->retrieve_actor_user($userid)
393 return $e->event unless $e->allowed('VIEW_HOLD', $patron->home_ou);
394 return __user_hold_count($self, $e, $userid);
397 sub __user_hold_count {
398 my( $self, $e, $userid ) = @_;
399 my $holds = $e->search_action_hold_request(
401 fulfillment_time => undef,
402 cancel_time => undef,
407 return scalar(@$holds);
411 __PACKAGE__->register_method(
412 method => "retrieve_holds_by_pickup_lib",
413 api_name => "open-ils.circ.holds.retrieve_by_pickup_lib",
415 Retrieves all the holds, with hold transits attached, for the specified
419 __PACKAGE__->register_method(
420 method => "retrieve_holds_by_pickup_lib",
421 api_name => "open-ils.circ.holds.id_list.retrieve_by_pickup_lib",
423 Retrieves all the hold ids for the specified
427 sub retrieve_holds_by_pickup_lib {
428 my($self, $client, $login_session, $ou_id) = @_;
430 #FIXME -- put an appropriate permission check here
431 #my( $user, $target, $evt ) = $apputils->checkses_requestor(
432 # $login_session, $user_id, 'VIEW_HOLD' );
433 #return $evt if $evt;
435 my $holds = $apputils->simplereq(
437 "open-ils.cstore.direct.action.hold_request.search.atomic",
439 pickup_lib => $ou_id ,
440 fulfillment_time => undef,
443 { order_by => { ahr => "request_time" } });
446 if( ! $self->api_name =~ /id_list/ ) {
447 flesh_hold_transits($holds);
450 if( $self->api_name =~ /id_list/ ) {
451 return [ map { $_->id } @$holds ];
458 __PACKAGE__->register_method(
459 method => "uncancel_hold",
460 api_name => "open-ils.circ.hold.uncancel"
464 my($self, $client, $auth, $hold_id) = @_;
465 my $e = new_editor(authtoken=>$auth, xact=>1);
466 return $e->event unless $e->checkauth;
468 my $hold = $e->retrieve_action_hold_request($hold_id)
469 or return $e->die_event;
470 return $e->die_event unless $e->allowed('CANCEL_HOLDS', $hold->request_lib);
472 return 0 if $hold->fulfillment_time;
473 return 1 unless $hold->cancel_time;
475 # if configured to reset the request time, also reset the expire time
476 if($U->ou_ancestor_setting_value(
477 $hold->request_lib, 'circ.hold_reset_request_time_on_uncancel', $e)) {
479 $hold->request_time('now');
480 my $interval = $U->ou_ancestor_setting_value($hold->request_lib, OILS_SETTING_HOLD_EXPIRE);
482 my $date = DateTime->now->add(seconds => OpenSRF::Utils::interval_to_seconds($interval));
483 $hold->expire_time($U->epoch2ISO8601($date->epoch));
487 $hold->clear_cancel_time;
488 $e->update_action_hold_request($hold) or return $e->die_event;
491 $U->storagereq('open-ils.storage.action.hold_request.copy_targeter', undef, $hold_id);
497 __PACKAGE__->register_method(
498 method => "cancel_hold",
499 api_name => "open-ils.circ.hold.cancel",
501 Cancels the specified hold. The login session
502 is the requestor and if the requestor is different from the usr field
503 on the hold, the requestor must have CANCEL_HOLDS permissions.
504 the hold may be either the hold object or the hold id
508 my($self, $client, $auth, $holdid, $cause, $note) = @_;
510 my $e = new_editor(authtoken=>$auth, xact=>1);
511 return $e->event unless $e->checkauth;
513 my $hold = $e->retrieve_action_hold_request($holdid)
516 if( $e->requestor->id ne $hold->usr ) {
517 return $e->event unless $e->allowed('CANCEL_HOLDS');
520 return 1 if $hold->cancel_time;
522 # If the hold is captured, reset the copy status
523 if( $hold->capture_time and $hold->current_copy ) {
525 my $copy = $e->retrieve_asset_copy($hold->current_copy)
528 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
529 $logger->info("canceling hold $holdid whose item is on the holds shelf");
530 # $logger->info("setting copy to status 'reshelving' on hold cancel");
531 # $copy->status(OILS_COPY_STATUS_RESHELVING);
532 # $copy->editor($e->requestor->id);
533 # $copy->edit_date('now');
534 # $e->update_asset_copy($copy) or return $e->event;
536 } elsif( $copy->status == OILS_COPY_STATUS_IN_TRANSIT ) {
539 $logger->warn("! canceling hold [$hid] that is in transit");
540 my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id},{idlist=>1})->[0];
543 my $trans = $e->retrieve_action_transit_copy($transid);
544 # Leave the transit alive, but set the copy status to
545 # reshelving so it will be properly reshelved when it gets back home
547 $trans->copy_status( OILS_COPY_STATUS_RESHELVING );
548 $e->update_action_transit_copy($trans) or return $e->die_event;
554 $hold->cancel_time('now');
555 $hold->cancel_cause($cause);
556 $hold->cancel_note($note);
557 $e->update_action_hold_request($hold)
560 delete_hold_copy_maps($self, $e, $hold->id);
566 sub delete_hold_copy_maps {
571 my $maps = $editor->search_action_hold_copy_map({hold=>$holdid});
573 $editor->delete_action_hold_copy_map($_)
574 or return $editor->event;
580 __PACKAGE__->register_method(
581 method => "update_hold",
582 api_name => "open-ils.circ.hold.update",
584 Updates the specified hold. The login session
585 is the requestor and if the requestor is different from the usr field
586 on the hold, the requestor must have UPDATE_HOLDS permissions.
590 my($self, $client, $auth, $hold) = @_;
592 my $e = new_editor(authtoken=>$auth, xact=>1);
593 return $e->die_event unless $e->checkauth;
595 my $orig_hold = $e->retrieve_action_hold_request($hold->id)
596 or return $e->die_event;
598 # don't allow the user to be changed
599 return OpenILS::Event->new('BAD_PARAMS') if $hold->usr != $orig_hold->usr;
601 if($hold->usr ne $e->requestor->id) {
602 # if the hold is for a different user, make sure the
603 # requestor has the appropriate permissions
604 my $usr = $e->retrieve_actor_user($hold->usr)
605 or return $e->die_event;
606 return $e->die_event unless $e->allowed('UPDATE_HOLD', $usr->home_ou);
609 # --------------------------------------------------------------
610 # if the hold is on the holds shelf and the pickup lib changes,
611 # we need to create a new transit
612 # --------------------------------------------------------------
613 if( ($orig_hold->pickup_lib ne $hold->pickup_lib) and (_hold_status($e, $hold) == 4)) {
614 return $e->die_event unless $e->allowed('UPDATE_PICKUP_LIB_FROM_HOLDS_SHELF', $orig_hold->pickup_lib);
615 return $e->die_event unless $e->allowed('UPDATE_PICKUP_LIB_FROM_HOLDS_SHELF', $hold->pickup_lib);
616 my $evt = transit_hold($e, $orig_hold, $hold,
617 $e->retrieve_asset_copy($hold->current_copy));
621 update_hold_if_frozen($self, $e, $hold, $orig_hold);
622 $e->update_action_hold_request($hold) or return $e->die_event;
628 my($e, $orig_hold, $hold, $copy) = @_;
629 my $src = $orig_hold->pickup_lib;
630 my $dest = $hold->pickup_lib;
632 $logger->info("putting hold into transit on pickup_lib update");
634 my $transit = Fieldmapper::action::transit_copy->new;
635 $transit->source($src);
636 $transit->dest($dest);
637 $transit->target_copy($copy->id);
638 $transit->source_send_time('now');
639 $transit->copy_status(OILS_COPY_STATUS_ON_HOLDS_SHELF);
641 $copy->status(OILS_COPY_STATUS_IN_TRANSIT);
642 $copy->editor($e->requestor->id);
643 $copy->edit_date('now');
645 $e->create_action_transit_copy($transit) or return $e->die_event;
646 $e->update_asset_copy($copy) or return $e->die_event;
650 # if the hold is frozen, this method ensures that the hold is not "targeted",
651 # that is, it clears the current_copy and prev_check_time to essentiallly
652 # reset the hold. If it is being activated, it runs the targeter in the background
653 sub update_hold_if_frozen {
654 my($self, $e, $hold, $orig_hold) = @_;
655 return if $hold->capture_time;
657 if($U->is_true($hold->frozen)) {
658 $logger->info("clearing current_copy and check_time for frozen hold ".$hold->id);
659 $hold->clear_current_copy;
660 $hold->clear_prev_check_time;
663 if($U->is_true($orig_hold->frozen)) {
664 $logger->info("Running targeter on activated hold ".$hold->id);
665 $U->storagereq( 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
669 __PACKAGE__->register_method(
670 method => "hold_note_CUD",
671 api_name => "open-ils.circ.hold_request.note.cud");
674 my($self, $conn, $auth, $note) = @_;
676 my $e = new_editor(authtoken => $auth, xact => 1);
677 return $e->die_event unless $e->checkauth;
679 my $hold = $e->retrieve_action_hold_request($note->hold)
680 or return $e->die_event;
682 if($hold->usr ne $e->requestor->id) {
683 my $usr = $e->retrieve_actor_user($hold->usr);
684 return $e->die_event unless $e->allowed('UPDATE_HOLD', $usr->home_ou);
688 $e->create_action_hold_request_note($note) or return $e->die_event;
689 } elsif($note->ischanged) {
690 $e->update_action_hold_request_note($note) or return $e->die_event;
691 } elsif($note->isdeleted) {
692 $e->delete_action_hold_request_note($note) or return $e->die_event;
701 __PACKAGE__->register_method(
702 method => "retrieve_hold_status",
703 api_name => "open-ils.circ.hold.status.retrieve",
705 Calculates the current status of the hold.
706 the requestor must have VIEW_HOLD permissions if the hold is for a user
707 other than the requestor.
708 Returns -1 on error (for now)
709 Returns 1 for 'waiting for copy to become available'
710 Returns 2 for 'waiting for copy capture'
711 Returns 3 for 'in transit'
712 Returns 4 for 'arrived'
715 sub retrieve_hold_status {
716 my($self, $client, $auth, $hold_id) = @_;
718 my $e = new_editor(authtoken => $auth);
719 return $e->event unless $e->checkauth;
720 my $hold = $e->retrieve_action_hold_request($hold_id)
723 if( $e->requestor->id != $hold->usr ) {
724 return $e->event unless $e->allowed('VIEW_HOLD');
727 return _hold_status($e, $hold);
733 return 1 unless $hold->current_copy;
734 return 2 unless $hold->capture_time;
736 my $copy = $hold->current_copy;
737 unless( ref $copy ) {
738 $copy = $e->retrieve_asset_copy($hold->current_copy)
742 return 3 if $copy->status == OILS_COPY_STATUS_IN_TRANSIT;
743 return 4 if $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF;
750 __PACKAGE__->register_method(
751 method => "retrieve_hold_queue_stats",
752 api_name => "open-ils.circ.hold.queue_stats.retrieve",
755 Returns object with total_holds count, queue_position, potential_copies count, and status code
760 sub retrieve_hold_queue_stats {
761 my($self, $conn, $auth, $hold_id) = @_;
762 my $e = new_editor(authtoken => $auth);
763 return $e->event unless $e->checkauth;
764 my $hold = $e->retrieve_action_hold_request($hold_id) or return $e->event;
765 if($e->requestor->id != $hold->usr) {
766 return $e->event unless $e->allowed('VIEW_HOLD');
768 return retrieve_hold_queue_status_impl($e, $hold);
771 sub retrieve_hold_queue_status_impl {
775 my $hold_ids = $e->search_action_hold_request(
777 { target => $hold->target,
778 hold_type => $hold->hold_type,
779 cancel_time => undef,
780 fulfillment_time => undef
782 {order_by => {ahr => 'request_time asc'}}
788 for my $hid (@$hold_ids) {
789 last if $hid == $hold->id;
793 my $potentials = $e->search_action_hold_copy_map({hold => $hold->id}, {idlist => 1});
794 my $num_potentials = scalar(@$potentials);
796 my $user_org = $e->json_query({select => {au => 'home_ou'}, from => 'au', where => {id => $hold->usr}})->[0]->{home_ou};
797 my $default_hold_interval = $U->ou_ancestor_setting_value($user_org, OILS_SETTING_HOLD_ESIMATE_WAIT_INTERVAL);
798 my $estimated_wait = $qpos * ($default_hold_interval / $num_potentials) if $default_hold_interval;
801 total_holds => scalar(@$hold_ids),
802 queue_position => $qpos,
803 potential_copies => $num_potentials,
804 status => _hold_status($e, $hold),
805 estimated_wait => int($estimated_wait)
810 sub fetch_open_hold_by_current_copy {
813 my $hold = $apputils->simplereq(
815 'open-ils.cstore.direct.action.hold_request.search.atomic',
816 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
817 return $hold->[0] if ref($hold);
821 sub fetch_related_holds {
824 return $apputils->simplereq(
826 'open-ils.cstore.direct.action.hold_request.search.atomic',
827 { current_copy => $copyid , cancel_time => undef, fulfillment_time => undef });
831 __PACKAGE__->register_method (
832 method => "hold_pull_list",
833 api_name => "open-ils.circ.hold_pull_list.retrieve",
835 Returns a list of holds that need to be "pulled"
840 __PACKAGE__->register_method (
841 method => "hold_pull_list",
842 api_name => "open-ils.circ.hold_pull_list.id_list.retrieve",
844 Returns a list of hold ID's that need to be "pulled"
851 my( $self, $conn, $authtoken, $limit, $offset ) = @_;
852 my( $reqr, $evt ) = $U->checkses($authtoken);
855 my $org = $reqr->ws_ou || $reqr->home_ou;
856 # the perm locaiton shouldn't really matter here since holds
857 # will exist all over and VIEW_HOLDS should be universal
858 $evt = $U->check_perms($reqr->id, $org, 'VIEW_HOLD');
861 if( $self->api_name =~ /id_list/ ) {
862 return $U->storagereq(
863 'open-ils.storage.direct.action.hold_request.pull_list.id_list.current_copy_circ_lib.status_filtered.atomic',
864 $org, $limit, $offset );
866 return $U->storagereq(
867 'open-ils.storage.direct.action.hold_request.pull_list.search.current_copy_circ_lib.status_filtered.atomic',
868 $org, $limit, $offset );
872 __PACKAGE__->register_method (
873 method => 'fetch_hold_notify',
874 api_name => 'open-ils.circ.hold_notification.retrieve_by_hold',
876 Returns a list of hold notification objects based on hold id.
877 @param authtoken The loggin session key
878 @param holdid The id of the hold whose notifications we want to retrieve
879 @return An array of hold notification objects, event on error.
883 sub fetch_hold_notify {
884 my( $self, $conn, $authtoken, $holdid ) = @_;
885 my( $requestor, $evt ) = $U->checkses($authtoken);
888 ($hold, $evt) = $U->fetch_hold($holdid);
890 ($patron, $evt) = $U->fetch_user($hold->usr);
893 $evt = $U->check_perms($requestor->id, $patron->home_ou, 'VIEW_HOLD_NOTIFICATION');
896 $logger->info("User ".$requestor->id." fetching hold notifications for hold $holdid");
897 return $U->cstorereq(
898 'open-ils.cstore.direct.action.hold_notification.search.atomic', {hold => $holdid} );
902 __PACKAGE__->register_method (
903 method => 'create_hold_notify',
904 api_name => 'open-ils.circ.hold_notification.create',
906 Creates a new hold notification object
907 @param authtoken The login session key
908 @param notification The hold notification object to create
909 @return ID of the new object on success, Event on error
913 sub create_hold_notify {
914 my( $self, $conn, $auth, $note ) = @_;
915 my $e = new_editor(authtoken=>$auth, xact=>1);
916 return $e->die_event unless $e->checkauth;
918 my $hold = $e->retrieve_action_hold_request($note->hold)
919 or return $e->die_event;
920 my $patron = $e->retrieve_actor_user($hold->usr)
921 or return $e->die_event;
923 return $e->die_event unless
924 $e->allowed('CREATE_HOLD_NOTIFICATION', $patron->home_ou);
926 $note->notify_staff($e->requestor->id);
927 $e->create_action_hold_notification($note) or return $e->die_event;
933 __PACKAGE__->register_method(
934 method => 'reset_hold',
935 api_name => 'open-ils.circ.hold.reset',
937 Un-captures and un-targets a hold, essentially returning
938 it to the state it was in directly after it was placed,
939 then attempts to re-target the hold
940 @param authtoken The login session key
941 @param holdid The id of the hold
947 my( $self, $conn, $auth, $holdid ) = @_;
949 my ($hold, $evt) = $U->fetch_hold($holdid);
951 ($reqr, $evt) = $U->checksesperm($auth, 'UPDATE_HOLD');
953 $evt = _reset_hold($self, $reqr, $hold);
959 __PACKAGE__->register_method(
960 method => 'reset_hold_batch',
961 api_name => 'open-ils.circ.hold.reset.batch'
964 sub reset_hold_batch {
965 my($self, $conn, $auth, $hold_ids) = @_;
967 my $e = new_editor(authtoken => $auth);
968 return $e->event unless $e->checkauth;
970 for my $hold_id ($hold_ids) {
972 my $hold = $e->retrieve_action_hold_request(
973 [$hold_id, {flesh => 1, flesh_fields => {ahr => ['usr']}}])
976 next unless $e->allowed('UPDATE_HOLD', $hold->usr->home_ou);
977 _reset_hold($self, $e->requestor, $hold);
985 my ($self, $reqr, $hold) = @_;
987 my $e = new_editor(xact =>1, requestor => $reqr);
989 $logger->info("reseting hold ".$hold->id);
993 if( $hold->capture_time and $hold->current_copy ) {
995 my $copy = $e->retrieve_asset_copy($hold->current_copy)
998 if( $copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF ) {
999 $logger->info("setting copy to status 'reshelving' on hold retarget");
1000 $copy->status(OILS_COPY_STATUS_RESHELVING);
1001 $copy->editor($e->requestor->id);
1002 $copy->edit_date('now');
1003 $e->update_asset_copy($copy) or return $e->event;
1005 } elsif( $copy->status == OILS_COPY_STATUS_IN_TRANSIT ) {
1007 # We don't want the copy to remain "in transit"
1008 $copy->status(OILS_COPY_STATUS_RESHELVING);
1009 $logger->warn("! reseting hold [$hid] that is in transit");
1010 my $transid = $e->search_action_hold_transit_copy({hold=>$hold->id},{idlist=>1})->[0];
1013 my $trans = $e->retrieve_action_transit_copy($transid);
1015 $logger->info("Aborting transit [$transid] on hold [$hid] reset...");
1016 my $evt = OpenILS::Application::Circ::Transit::__abort_transit($e, $trans, $copy, 1);
1017 $logger->info("Transit abort completed with result $evt");
1018 return $evt unless "$evt" eq 1;
1024 $hold->clear_capture_time;
1025 $hold->clear_current_copy;
1027 $e->update_action_hold_request($hold) or return $e->event;
1031 'open-ils.storage.action.hold_request.copy_targeter', undef, $hold->id );
1037 __PACKAGE__->register_method(
1038 method => 'fetch_open_title_holds',
1039 api_name => 'open-ils.circ.open_holds.retrieve',
1041 Returns a list ids of un-fulfilled holds for a given title id
1042 @param authtoken The login session key
1043 @param id the id of the item whose holds we want to retrieve
1044 @param type The hold type - M, T, V, C
1048 sub fetch_open_title_holds {
1049 my( $self, $conn, $auth, $id, $type, $org ) = @_;
1050 my $e = new_editor( authtoken => $auth );
1051 return $e->event unless $e->checkauth;
1054 $org ||= $e->requestor->ws_ou;
1056 # return $e->search_action_hold_request(
1057 # { target => $id, hold_type => $type, fulfillment_time => undef }, {idlist=>1});
1059 # XXX make me return IDs in the future ^--
1060 my $holds = $e->search_action_hold_request(
1063 cancel_time => undef,
1065 fulfillment_time => undef
1069 flesh_hold_transits($holds);
1074 sub flesh_hold_transits {
1076 for my $hold ( @$holds ) {
1078 $apputils->simplereq(
1080 "open-ils.cstore.direct.action.hold_transit_copy.search.atomic",
1081 { hold => $hold->id },
1082 { order_by => { ahtc => 'id desc' }, limit => 1 }
1088 sub flesh_hold_notices {
1089 my( $holds, $e ) = @_;
1090 $e ||= new_editor();
1092 for my $hold (@$holds) {
1093 my $notices = $e->search_action_hold_notification(
1095 { hold => $hold->id },
1096 { order_by => { anh => 'notify_time desc' } },
1101 $hold->notify_count(scalar(@$notices));
1103 my $n = $e->retrieve_action_hold_notification($$notices[0])
1104 or return $e->event;
1105 $hold->notify_time($n->notify_time);
1111 __PACKAGE__->register_method(
1112 method => 'fetch_captured_holds',
1113 api_name => 'open-ils.circ.captured_holds.on_shelf.retrieve',
1116 Returns a list of un-fulfilled holds for a given title id
1117 @param authtoken The login session key
1118 @param org The org id of the location in question
1122 __PACKAGE__->register_method(
1123 method => 'fetch_captured_holds',
1124 api_name => 'open-ils.circ.captured_holds.id_list.on_shelf.retrieve',
1127 Returns a list ids of un-fulfilled holds for a given title id
1128 @param authtoken The login session key
1129 @param org The org id of the location in question
1133 sub fetch_captured_holds {
1134 my( $self, $conn, $auth, $org ) = @_;
1136 my $e = new_editor(authtoken => $auth);
1137 return $e->event unless $e->checkauth;
1138 return $e->event unless $e->allowed('VIEW_HOLD'); # XXX rely on editor perm
1140 $org ||= $e->requestor->ws_ou;
1142 my $hold_ids = $e->json_query(
1144 select => { ahr => ['id'] },
1149 fkey => 'current_copy'
1154 '+acp' => { status => OILS_COPY_STATUS_ON_HOLDS_SHELF },
1156 capture_time => { "!=" => undef },
1157 current_copy => { "!=" => undef },
1158 fulfillment_time => undef,
1160 cancel_time => undef,
1166 for my $hold_id (@$hold_ids) {
1167 if($self->api_name =~ /id_list/) {
1168 $conn->respond($hold_id->{id});
1172 $e->retrieve_action_hold_request([
1176 flesh_fields => {ahr => ['notifications', 'transit', 'notes']},
1177 order_by => {anh => 'notify_time desc'}
1186 __PACKAGE__->register_method(
1187 method => "check_title_hold",
1188 api_name => "open-ils.circ.title_hold.is_possible",
1190 Determines if a hold were to be placed by a given user,
1191 whether or not said hold would have any potential copies
1193 @param authtoken The login session key
1194 @param params A hash of named params including:
1195 patronid - the id of the hold recipient
1196 titleid (brn) - the id of the title to be held
1197 depth - the hold range depth (defaults to 0)
1200 sub check_title_hold {
1201 my( $self, $client, $authtoken, $params ) = @_;
1203 my %params = %$params;
1204 my $titleid = $params{titleid} ||"";
1205 my $volid = $params{volume_id};
1206 my $copyid = $params{copy_id};
1207 my $mrid = $params{mrid} ||"";
1208 my $depth = $params{depth} || 0;
1209 my $pickup_lib = $params{pickup_lib};
1210 my $hold_type = $params{hold_type} || 'T';
1211 my $selection_ou = $params{selection_ou} || $pickup_lib;
1213 my $e = new_editor(authtoken=>$authtoken);
1214 return $e->event unless $e->checkauth;
1215 my $patron = $e->retrieve_actor_user($params{patronid})
1216 or return $e->event;
1218 if( $e->requestor->id ne $patron->id ) {
1219 return $e->event unless
1220 $e->allowed('VIEW_HOLD_PERMIT', $patron->home_ou);
1223 return OpenILS::Event->new('PATRON_BARRED') if $U->is_true($patron->barred);
1225 my $request_lib = $e->retrieve_actor_org_unit($e->requestor->ws_ou)
1226 or return $e->event;
1228 my $soft_boundary = $U->ou_ancestor_setting_value($selection_ou, OILS_SETTING_HOLD_SOFT_BOUNDARY);
1229 my $hard_boundary = $U->ou_ancestor_setting_value($selection_ou, OILS_SETTING_HOLD_HARD_BOUNDARY);
1231 if(defined $soft_boundary and $$params{depth} < $soft_boundary) {
1232 # work up the tree and as soon as we find a potential copy, use that depth
1233 # also, make sure we don't go past the hard boundary if it exists
1235 # our min boundary is the greater of user-specified boundary or hard boundary
1236 my $min_depth = (defined $hard_boundary and $hard_boundary > $$params{depth}) ?
1237 $hard_boundary : $$params{depth};
1239 my $depth = $soft_boundary;
1240 while($depth >= $min_depth) {
1241 $logger->info("performing hold possibility check with soft boundary $depth");
1242 return {success => 1, depth => $depth}
1243 if do_possibility_checks($e, $patron, $request_lib, $depth, %params);
1246 return {success => 0};
1248 } elsif(defined $hard_boundary and $$params{depth} < $hard_boundary) {
1249 # there is no soft boundary, enforce the hard boundary if it exists
1250 $logger->info("performing hold possibility check with hard boundary $hard_boundary");
1251 if(do_possibility_checks($e, $patron, $request_lib, $hard_boundary, %params)) {
1252 return {success => 1, depth => $hard_boundary}
1254 return {success => 0};
1258 # no boundaries defined, fall back to user specifed boundary or no boundary
1259 $logger->info("performing hold possibility check with no boundary");
1260 if(do_possibility_checks($e, $patron, $request_lib, $params{depth}, %params)) {
1261 return {success => 1, depth => $hard_boundary};
1263 return {success => 0};
1268 sub do_possibility_checks {
1269 my($e, $patron, $request_lib, $depth, %params) = @_;
1271 my $titleid = $params{titleid} ||"";
1272 my $volid = $params{volume_id};
1273 my $copyid = $params{copy_id};
1274 my $mrid = $params{mrid} ||"";
1275 my $pickup_lib = $params{pickup_lib};
1276 my $hold_type = $params{hold_type} || 'T';
1277 my $selection_ou = $params{selection_ou} || $pickup_lib;
1284 if( $hold_type eq OILS_HOLD_TYPE_COPY ) {
1286 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
1287 $volume = $e->retrieve_asset_call_number($copy->call_number)
1288 or return $e->event;
1289 $title = $e->retrieve_biblio_record_entry($volume->record)
1290 or return $e->event;
1291 return verify_copy_for_hold(
1292 $patron, $e->requestor, $title, $copy, $pickup_lib, $request_lib );
1294 } elsif( $hold_type eq OILS_HOLD_TYPE_VOLUME ) {
1296 $volume = $e->retrieve_asset_call_number($volid)
1297 or return $e->event;
1298 $title = $e->retrieve_biblio_record_entry($volume->record)
1299 or return $e->event;
1301 return _check_volume_hold_is_possible(
1302 $volume, $title, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou);
1304 } elsif( $hold_type eq OILS_HOLD_TYPE_TITLE ) {
1306 return _check_title_hold_is_possible(
1307 $titleid, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou);
1309 } elsif( $hold_type eq OILS_HOLD_TYPE_METARECORD ) {
1311 my $maps = $e->search_metabib_source_map({metarecord=>$mrid});
1312 my @recs = map { $_->source } @$maps;
1313 for my $rec (@recs) {
1314 return 1 if (_check_title_hold_is_possible(
1315 $rec, $depth, $request_lib, $patron, $e->requestor, $pickup_lib, $selection_ou));
1323 sub _check_metarecord_hold_is_possible {
1324 my( $mrid, $rangelib, $depth, $request_lib, $patron, $requestor, $pickup_lib ) = @_;
1326 my $e = new_editor();
1328 # this monster will grab the id and circ_lib of all of the "holdable" copies for the given metarecord
1329 my $copies = $e->json_query(
1331 select => { acp => ['id', 'circ_lib'] },
1336 fkey => 'call_number',
1341 filter => { metarecord => $mrid }
1345 acpl => { field => 'id', filter => { holdable => 't'}, fkey => 'location' },
1346 ccs => { field => 'id', filter => { holdable => 't'}, fkey => 'status' }
1350 '+acp' => { circulate => 't', deleted => 'f', holdable => 't' }
1355 return $e->event unless defined $copies;
1356 $logger->info("metarecord possible found ".scalar(@$copies)." potential copies");
1357 return 0 unless @$copies;
1359 # -----------------------------------------------------------------------
1360 # sort the copies into buckets based on their circ_lib proximity to
1361 # the patron's home_ou.
1362 # -----------------------------------------------------------------------
1364 my $home_org = $patron->home_ou;
1365 my $req_org = $request_lib->id;
1367 $prox_cache{$home_org} =
1368 $e->search_actor_org_unit_proximity({from_org => $home_org})
1369 unless $prox_cache{$home_org};
1370 my $home_prox = $prox_cache{$home_org};
1373 my %hash = map { ($_->to_org => $_->prox) } @$home_prox;
1374 push( @{$buckets{ $hash{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
1376 my @keys = sort { $a <=> $b } keys %buckets;
1379 if( $home_org ne $req_org ) {
1380 # -----------------------------------------------------------------------
1381 # shove the copies close to the request_lib into the primary buckets
1382 # directly before the farthest away copies. That way, they are not
1383 # given priority, but they are checked before the farthest copies.
1384 # -----------------------------------------------------------------------
1386 $prox_cache{$req_org} =
1387 $e->search_actor_org_unit_proximity({from_org => $req_org})
1388 unless $prox_cache{$req_org};
1389 my $req_prox = $prox_cache{$req_org};
1392 my %hash2 = map { ($_->to_org => $_->prox) } @$req_prox;
1393 push( @{$buckets2{ $hash2{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
1395 my $highest_key = $keys[@keys - 1]; # the farthest prox in the exising buckets
1396 my $new_key = $highest_key - 0.5; # right before the farthest prox
1397 my @keys2 = sort { $a <=> $b } keys %buckets2;
1398 for my $key (@keys2) {
1399 last if $key >= $highest_key;
1400 push( @{$buckets{$new_key}}, $_ ) for @{$buckets2{$key}};
1404 @keys = sort { $a <=> $b } keys %buckets;
1407 for my $key (@keys) {
1408 my @cps = @{$buckets{$key}};
1410 $logger->info("looking at " . scalar(@{$buckets{$key}}). " copies in proximity bucket $key");
1412 for my $copyid (@cps) {
1414 next if $seen{$copyid};
1415 $seen{$copyid} = 1; # there could be dupes given the merged buckets
1416 my $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
1417 $logger->debug("looking at bucket_key=$key, copy $copyid : circ_lib = " . $copy->circ_lib);
1419 my $vol = $e->retrieve_asset_call_number(
1420 [ $copy->call_number, { flesh => 1, flesh_fields => { acn => ['record'] } } ] );
1422 return 1 if verify_copy_for_hold(
1423 $patron, $requestor, $vol->record, $copy, $pickup_lib, $request_lib );
1431 sub create_ranged_org_filter {
1432 my($e, $selection_ou, $depth) = @_;
1434 # find the orgs from which this hold may be fulfilled,
1435 # based on the selection_ou and depth
1437 my $top_org = $e->search_actor_org_unit([
1438 {parent_ou => undef},
1439 {flesh=>1, flesh_fields=>{aou=>['ou_type']}}])->[0];
1442 return () if $depth == $top_org->ou_type->depth;
1444 my $org_list = $U->storagereq('open-ils.storage.actor.org_unit.descendants.atomic', $selection_ou, $depth);
1445 %org_filter = (circ_lib => []);
1446 push(@{$org_filter{circ_lib}}, $_->id) for @$org_list;
1448 $logger->info("hold org filter at depth $depth and selection_ou ".
1449 "$selection_ou created list of @{$org_filter{circ_lib}}");
1455 sub _check_title_hold_is_possible {
1456 my( $titleid, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou ) = @_;
1458 my $e = new_editor();
1459 my %org_filter = create_ranged_org_filter($e, $selection_ou, $depth);
1461 # this monster will grab the id and circ_lib of all of the "holdable" copies for the given record
1462 my $copies = $e->json_query(
1464 select => { acp => ['id', 'circ_lib'] },
1469 fkey => 'call_number',
1473 filter => { id => $titleid },
1478 acpl => { field => 'id', filter => { holdable => 't'}, fkey => 'location' },
1479 ccs => { field => 'id', filter => { holdable => 't'}, fkey => 'status' }
1483 '+acp' => { circulate => 't', deleted => 'f', holdable => 't', %org_filter }
1488 return $e->event unless defined $copies;
1489 $logger->info("title possible found ".scalar(@$copies)." potential copies");
1490 return 0 unless @$copies;
1492 # -----------------------------------------------------------------------
1493 # sort the copies into buckets based on their circ_lib proximity to
1494 # the patron's home_ou.
1495 # -----------------------------------------------------------------------
1497 my $home_org = $patron->home_ou;
1498 my $req_org = $request_lib->id;
1500 $logger->info("prox cache $home_org " . $prox_cache{$home_org});
1502 $prox_cache{$home_org} =
1503 $e->search_actor_org_unit_proximity({from_org => $home_org})
1504 unless $prox_cache{$home_org};
1505 my $home_prox = $prox_cache{$home_org};
1508 my %hash = map { ($_->to_org => $_->prox) } @$home_prox;
1509 push( @{$buckets{ $hash{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
1511 my @keys = sort { $a <=> $b } keys %buckets;
1514 if( $home_org ne $req_org ) {
1515 # -----------------------------------------------------------------------
1516 # shove the copies close to the request_lib into the primary buckets
1517 # directly before the farthest away copies. That way, they are not
1518 # given priority, but they are checked before the farthest copies.
1519 # -----------------------------------------------------------------------
1520 $prox_cache{$req_org} =
1521 $e->search_actor_org_unit_proximity({from_org => $req_org})
1522 unless $prox_cache{$req_org};
1523 my $req_prox = $prox_cache{$req_org};
1527 my %hash2 = map { ($_->to_org => $_->prox) } @$req_prox;
1528 push( @{$buckets2{ $hash2{$_->{circ_lib}} } }, $_->{id} ) for @$copies;
1530 my $highest_key = $keys[@keys - 1]; # the farthest prox in the exising buckets
1531 my $new_key = $highest_key - 0.5; # right before the farthest prox
1532 my @keys2 = sort { $a <=> $b } keys %buckets2;
1533 for my $key (@keys2) {
1534 last if $key >= $highest_key;
1535 push( @{$buckets{$new_key}}, $_ ) for @{$buckets2{$key}};
1539 @keys = sort { $a <=> $b } keys %buckets;
1543 for my $key (@keys) {
1544 my @cps = @{$buckets{$key}};
1546 $logger->info("looking at " . scalar(@{$buckets{$key}}). " copies in proximity bucket $key");
1548 for my $copyid (@cps) {
1550 next if $seen{$copyid};
1551 $seen{$copyid} = 1; # there could be dupes given the merged buckets
1552 my $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
1553 $logger->debug("looking at bucket_key=$key, copy $copyid : circ_lib = " . $copy->circ_lib);
1555 unless($title) { # grab the title if we don't already have it
1556 my $vol = $e->retrieve_asset_call_number(
1557 [ $copy->call_number, { flesh => 1, flesh_fields => { acn => ['record'] } } ] );
1558 $title = $vol->record;
1561 return 1 if verify_copy_for_hold(
1562 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib );
1571 sub _check_volume_hold_is_possible {
1572 my( $vol, $title, $depth, $request_lib, $patron, $requestor, $pickup_lib, $selection_ou ) = @_;
1573 my %org_filter = create_ranged_org_filter(new_editor(), $selection_ou, $depth);
1574 my $copies = new_editor->search_asset_copy({call_number => $vol->id, %org_filter});
1575 $logger->info("checking possibility of volume hold for volume ".$vol->id);
1576 for my $copy ( @$copies ) {
1577 return 1 if verify_copy_for_hold(
1578 $patron, $requestor, $title, $copy, $pickup_lib, $request_lib );
1585 sub verify_copy_for_hold {
1586 my( $patron, $requestor, $title, $copy, $pickup_lib, $request_lib ) = @_;
1587 $logger->info("checking possibility of copy in hold request for copy ".$copy->id);
1588 return 1 if OpenILS::Utils::PermitHold::permit_copy_hold(
1589 { patron => $patron,
1590 requestor => $requestor,
1593 title_descriptor => $title->fixed_fields, # this is fleshed into the title object
1594 pickup_lib => $pickup_lib,
1595 request_lib => $request_lib,
1604 sub find_nearest_permitted_hold {
1607 my $editor = shift; # CStoreEditor object
1608 my $copy = shift; # copy to target
1609 my $user = shift; # staff
1610 my $check_only = shift; # do no updates, just see if the copy could fulfill a hold
1611 my $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND');
1613 my $bc = $copy->barcode;
1615 # find any existing holds that already target this copy
1616 my $old_holds = $editor->search_action_hold_request(
1617 { current_copy => $copy->id,
1618 cancel_time => undef,
1619 capture_time => undef
1623 # hold->type "R" means we need this copy
1624 for my $h (@$old_holds) { return ($h) if $h->hold_type eq 'R'; }
1627 my $hold_stall_interval = $U->ou_ancestor_setting_value($user->ws_ou, OILS_SETTING_HOLD_SOFT_STALL);
1629 $logger->info("circulator: searching for best hold at org ".$user->ws_ou.
1630 " and copy $bc with a hold stalling interval of ". ($hold_stall_interval || "(none)"));
1632 # search for what should be the best holds for this copy to fulfill
1633 my $best_holds = $U->storagereq(
1634 "open-ils.storage.action.hold_request.nearest_hold.atomic",
1635 $user->ws_ou, $copy->id, 10, $hold_stall_interval );
1637 unless(@$best_holds) {
1639 if( my $hold = $$old_holds[0] ) {
1640 $logger->info("circulator: using existing pre-targeted hold ".$hold->id." in hold search");
1644 $logger->info("circulator: no suitable holds found for copy $bc");
1645 return (undef, $evt);
1651 # for each potential hold, we have to run the permit script
1652 # to make sure the hold is actually permitted.
1653 for my $holdid (@$best_holds) {
1654 next unless $holdid;
1655 $logger->info("circulator: checking if hold $holdid is permitted for copy $bc");
1657 my $hold = $editor->retrieve_action_hold_request($holdid) or next;
1658 my $reqr = $editor->retrieve_actor_user($hold->requestor) or next;
1659 my $rlib = $editor->retrieve_actor_org_unit($hold->request_lib) or next;
1661 # see if this hold is permitted
1662 my $permitted = OpenILS::Utils::PermitHold::permit_copy_hold(
1663 { patron_id => $hold->usr,
1666 pickup_lib => $hold->pickup_lib,
1667 request_lib => $rlib,
1678 unless( $best_hold ) { # no "good" permitted holds were found
1679 if( my $hold = $$old_holds[0] ) { # can we return a pre-targeted hold?
1680 $logger->info("circulator: using existing pre-targeted hold ".$hold->id." in hold search");
1685 $logger->info("circulator: no suitable holds found for copy $bc");
1686 return (undef, $evt);
1689 $logger->info("circulator: best hold ".$best_hold->id." found for copy $bc");
1691 # indicate a permitted hold was found
1692 return $best_hold if $check_only;
1694 # we've found a permitted hold. we need to "grab" the copy
1695 # to prevent re-targeted holds (next part) from re-grabbing the copy
1696 $best_hold->current_copy($copy->id);
1697 $editor->update_action_hold_request($best_hold)
1698 or return (undef, $editor->event);
1703 # re-target any other holds that already target this copy
1704 for my $old_hold (@$old_holds) {
1705 next if $old_hold->id eq $best_hold->id; # don't re-target the hold we want
1706 $logger->info("circulator: clearing current_copy and prev_check_time on hold ".
1707 $old_hold->id." after a better hold [".$best_hold->id."] was found");
1708 $old_hold->clear_current_copy;
1709 $old_hold->clear_prev_check_time;
1710 $editor->update_action_hold_request($old_hold)
1711 or return (undef, $editor->event);
1712 push(@retarget, $old_hold->id);
1715 return ($best_hold, undef, (@retarget) ? \@retarget : undef);
1723 __PACKAGE__->register_method(
1724 method => 'all_rec_holds',
1725 api_name => 'open-ils.circ.holds.retrieve_all_from_title',
1729 my( $self, $conn, $auth, $title_id, $args ) = @_;
1731 my $e = new_editor(authtoken=>$auth);
1732 $e->checkauth or return $e->event;
1733 $e->allowed('VIEW_HOLD') or return $e->event;
1736 $args->{fulfillment_time} = undef; # we don't want to see old fulfilled holds
1737 $args->{cancel_time} = undef;
1739 my $resp = { volume_holds => [], copy_holds => [], metarecord_holds => [] };
1741 my $mr_map = $e->search_metabib_metarecord_source_map({source => $title_id})->[0];
1743 $resp->{metarecord_holds} = $e->search_action_hold_request(
1744 { hold_type => OILS_HOLD_TYPE_METARECORD,
1745 target => $mr_map->metarecord,
1751 $resp->{title_holds} = $e->search_action_hold_request(
1753 hold_type => OILS_HOLD_TYPE_TITLE,
1754 target => $title_id,
1758 my $vols = $e->search_asset_call_number(
1759 { record => $title_id, deleted => 'f' }, {idlist=>1});
1761 return $resp unless @$vols;
1763 $resp->{volume_holds} = $e->search_action_hold_request(
1765 hold_type => OILS_HOLD_TYPE_VOLUME,
1770 my $copies = $e->search_asset_copy(
1771 { call_number => $vols, deleted => 'f' }, {idlist=>1});
1773 return $resp unless @$copies;
1775 $resp->{copy_holds} = $e->search_action_hold_request(
1777 hold_type => OILS_HOLD_TYPE_COPY,
1789 __PACKAGE__->register_method(
1790 method => 'uber_hold',
1792 api_name => 'open-ils.circ.hold.details.retrieve'
1796 my($self, $client, $auth, $hold_id) = @_;
1797 my $e = new_editor(authtoken=>$auth);
1798 $e->checkauth or return $e->event;
1799 $e->allowed('VIEW_HOLD') or return $e->event;
1803 my $hold = $e->retrieve_action_hold_request(
1808 flesh_fields => { ahr => [ 'current_copy', 'usr', 'notes' ] }
1811 ) or return $e->event;
1813 my $user = $hold->usr;
1814 $hold->usr($user->id);
1816 my $card = $e->retrieve_actor_card($user->card)
1817 or return $e->event;
1819 my( $mvr, $volume, $copy ) = find_hold_mvr($e, $hold);
1821 flesh_hold_notices([$hold], $e);
1822 flesh_hold_transits([$hold]);
1824 my $details = retrieve_hold_queue_status_impl($e, $hold);
1831 patron_first => $user->first_given_name,
1832 patron_last => $user->family_name,
1833 patron_barcode => $card->barcode,
1840 # -----------------------------------------------------
1841 # Returns the MVR object that represents what the
1843 # -----------------------------------------------------
1845 my( $e, $hold ) = @_;
1851 if( $hold->hold_type eq OILS_HOLD_TYPE_METARECORD ) {
1852 my $mr = $e->retrieve_metabib_metarecord($hold->target)
1853 or return $e->event;
1854 $tid = $mr->master_record;
1856 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_TITLE ) {
1857 $tid = $hold->target;
1859 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_VOLUME ) {
1860 $volume = $e->retrieve_asset_call_number($hold->target)
1861 or return $e->event;
1862 $tid = $volume->record;
1864 } elsif( $hold->hold_type eq OILS_HOLD_TYPE_COPY ) {
1865 $copy = $e->retrieve_asset_copy($hold->target)
1866 or return $e->event;
1867 $volume = $e->retrieve_asset_call_number($copy->call_number)
1868 or return $e->event;
1869 $tid = $volume->record;
1872 if(!$copy and ref $hold->current_copy ) {
1873 $copy = $hold->current_copy;
1874 $hold->current_copy($copy->id);
1877 if(!$volume and $copy) {
1878 $volume = $e->retrieve_asset_call_number($copy->call_number);
1881 my $title = $e->retrieve_biblio_record_entry($tid);
1882 return ( $U->record_to_mvr($title), $volume, $copy );