Cancel volume/copy holds on delete
authorBill Erickson <berick@esilibrary.com>
Mon, 6 Jun 2011 18:26:42 +0000 (14:26 -0400)
committerJason Etheridge <jason@esilibrary.com>
Fri, 24 Jun 2011 15:15:43 +0000 (11:15 -0400)
Cancel all holds that directly target a copy or volume when the
copy/volume is being deleted.

Sends async message to A/T for each hold to create and run the
necessary events (e.g.  send cancellation notices).

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Jason Etheridge <jason@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm

index 5ac2a1d..11ecdfc 100644 (file)
@@ -347,6 +347,9 @@ sub delete_copy {
                        or return $editor->event;
        }
 
+    my $evt = $class->cancel_copy_holds($editor, $copy);
+    return $evt if $evt;
+
     $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
 
     return undef if $skip_empty_cleanup;
@@ -355,6 +358,62 @@ sub delete_copy {
 }
 
 
+# deletes all holds that specifically target the deleted copy
+sub cancel_copy_holds {
+    my($class, $editor, $copy) = @_;
+
+    my $holds = $editor->search_action_hold_request({   
+        target              => $copy->id, 
+        hold_type           => [qw/C R F/],
+        cancel_time         => undef, 
+        fulfillment_time    => undef 
+    });
+
+    return $class->cancel_hold_list($editor, $holds);
+}
+
+# deletes all holds that specifically target the deleted volume
+sub cancel_volume_holds {
+    my($class, $editor, $volume) = @_;
+
+    my $holds = $editor->search_action_hold_request({   
+        target              => $volume->id, 
+        hold_type           => 'V',
+        cancel_time         => undef, 
+        fulfillment_time    => undef 
+    });
+
+    return $class->cancel_hold_list($editor, $holds);
+}
+
+sub cancel_hold_list {
+    my($class, $editor, $holds) = @_;
+
+    for my $hold (@$holds) {
+
+        $hold->cancel_time('now');
+        $hold->cancel_cause(1); # un-targeted expiration.  Do we need an alternate "target deleted" cause?
+        $editor->update_action_hold_request($hold) or return $editor->die_event;
+
+        # delete the copy maps.  
+        my $maps = $editor->search_action_hold_copy_map({hold => $hold->id});
+        for(@$maps) {
+            $editor->delete_action_hold_copy_map($_) 
+                or return $editor->die_event;
+        }
+
+        # tell A/T the hold was cancelled.  Don't wait for a response..
+        my $at_ses = OpenSRF::AppSession->create('open-ils.trigger');
+        $at_ses->request(
+            'open-ils.trigger.event.autocreate',
+            'hold_request.cancel.expire_no_target', 
+            $hold, $hold->pickup_lib);
+    }
+
+    return undef;
+}
+
+
 
 sub create_volume {
        my($class, $override, $editor, $vol) = @_;
@@ -548,6 +607,9 @@ sub delete_volume {
     $vol->editor($editor->requestor->id);
     $editor->update_asset_call_number($vol) or return $editor->die_event;
 
+    $evt = $class->cancel_volume_holds($editor, $vol);
+    return $evt if $evt;
+
     # handle the case where this is the last volume on the record
        return $class->remove_empty_objects($editor, $override, $vol);
 }