LP#1386347: Clear hold-copy-map efficiently
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Circ / Holds.pm
index 794f2dd..1ec6066 100644 (file)
@@ -826,8 +826,6 @@ sub cancel_hold {
     $e->update_action_hold_request($hold)
         or return $e->die_event;
 
-    delete_hold_copy_maps($self, $e, $hold->id);
-
     $e->commit;
 
     # re-fetch the hold to pick up the real cancel_time (not "now") for A/T
@@ -844,20 +842,6 @@ sub cancel_hold {
     return 1;
 }
 
-sub delete_hold_copy_maps {
-    my $class  = shift;
-    my $editor = shift;
-    my $holdid = shift;
-
-    my $maps = $editor->search_action_hold_copy_map({hold=>$holdid});
-    for(@$maps) {
-        $editor->delete_action_hold_copy_map($_)
-            or return $editor->event;
-    }
-    return undef;
-}
-
-
 my $update_hold_desc = 'The login session is the requestor. '       .
    'If the requestor is different from the usr field on the hold, ' .
    'the requestor must have UPDATE_HOLDS permissions. '             .
@@ -1555,6 +1539,25 @@ __PACKAGE__->register_method(
     }
 );
 
+__PACKAGE__->register_method(
+    method    => "hold_pull_list",
+    stream => 1,
+    # TODO: tag with api_level 2 once fully supported
+    api_name  => "open-ils.circ.hold_pull_list.fleshed.stream",
+    signature => {
+        desc   => q/Returns a stream of fleshed holds  that need to be 
+                    "pulled" by a given location.  The location is 
+                    determined by the login session.  
+                    This API calls always run in authoritative mode./,
+        params => [
+            { desc => 'Limit (optional)',  type => 'number'},
+            { desc => 'Offset (optional)', type => 'number'},
+        ],
+        return => {
+            desc => 'Stream of holds holds, or event on failure',
+        }
+    }
+);
 
 sub hold_pull_list {
     my( $self, $conn, $authtoken, $limit, $offset ) = @_;
@@ -1577,12 +1580,24 @@ sub hold_pull_list {
         return $count;
 
     } elsif( $self->api_name =~ /id_list/ ) {
-        return $U->storagereq(
+        $U->storagereq(
             'open-ils.storage.direct.action.hold_request.pull_list.id_list.current_copy_circ_lib.status_filtered.atomic',
             $org, $limit, $offset );
 
+    } elsif ($self->api_name =~ /fleshed/) {
+
+        my $ids = $U->storagereq(
+            'open-ils.storage.direct.action.hold_request.pull_list.id_list.current_copy_circ_lib.status_filtered.atomic',
+            $org, $limit, $offset );
+
+        my $e = new_editor(xact => 1, requestor => $reqr);
+        $conn->respond(uber_hold_impl($e, $_, {flesh_acpl => 1})) for @$ids;
+        $e->rollback;
+        $conn->respond_complete;
+        return;
+
     } else {
-        return $U->storagereq(
+        $U->storagereq(
             'open-ils.storage.direct.action.hold_request.pull_list.search.current_copy_circ_lib.status_filtered.atomic',
             $org, $limit, $offset );
     }
@@ -2148,7 +2163,7 @@ sub fetch_captured_holds {
     };
     if($self->api_name =~ /expired/) {
         $query->{'where'}->{'+alhr'}->{'-or'} = {
-                shelf_expire_time => { '<' => 'now'},
+                shelf_expire_time => { '<' => 'today'},
                 cancel_time => { '!=' => undef },
         };
     }
@@ -3384,6 +3399,10 @@ sub uber_hold_impl {
         %$details
     };
 
+    $resp->{copy}->location(
+        $e->retrieve_asset_copy_location($resp->{copy}->location))
+        if $resp->{copy} and $args->{flesh_acpl};
+
     unless($args->{suppress_patron_details}) {
         my $card = $e->retrieve_actor_card($user->card) or return $e->event;
         $resp->{patron_first}   = $user->first_given_name,
@@ -3604,7 +3623,6 @@ sub clear_shelf_process {
             $hold->cancel_time('now');
             $hold->cancel_cause(2); # Hold Shelf expiration
             $e->update_action_hold_request($hold) or return $e->die_event;
-            delete_hold_copy_maps($self, $e, $hold->id) and return $e->die_event;
             push(@canceled_holds, $hold_id);
         }
 
@@ -4081,15 +4099,15 @@ __PACKAGE__->register_method(
     }
 );
 
-# XXX Need to add type I (and, soon, type P) holds to these counts
+# XXX Need to add type I holds to these counts
 sub rec_hold_count {
     my($self, $conn, $target_id, $args) = @_;
     $args ||= {};
 
     my $mmr_join = {
         mmrsm => {
-            field => 'id',
-            fkey => 'source',
+            field => 'source',
+            fkey => 'id',
             filter => {metarecord => $target_id}
         }
     };
@@ -4147,6 +4165,17 @@ sub rec_hold_count {
                     },
                     {
                         '-and' => {
+                            hold_type => 'P',
+                            target => {
+                                in => {
+                                    select => {bmp => ['id']},
+                                    from => {bmp => $bre_join}
+                                }
+                            }
+                        }
+                    },
+                    {
+                        '-and' => {
                             hold_type => 'T',
                             target => $target_id
                         }
@@ -4157,7 +4186,7 @@ sub rec_hold_count {
     };
 
     if($self->api_name =~ /mmr/) {
-        $query->{where}->{'+ahr'}->{'-or'}->[2] = {
+        $query->{where}->{'+ahr'}->{'-or'}->[3] = {
             '-and' => {
                 hold_type => 'T',
                 target => {
@@ -4169,7 +4198,7 @@ sub rec_hold_count {
             }
         };
 
-        $query->{where}->{'+ahr'}->{'-or'}->[3] = {
+        $query->{where}->{'+ahr'}->{'-or'}->[4] = {
             '-and' => {
                 hold_type => 'M',
                 target => $target_id