when a copy is manually edited and is either deleted or changed into a non-holdable...
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 25 Jun 2009 03:03:32 +0000 (03:03 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 25 Jun 2009 03:03:32 +0000 (03:03 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@13464 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
Open-ILS/src/perlmods/OpenILS/Application/Cat/AssetCommon.pm

index 56d5dc0..99f289d 100644 (file)
@@ -449,18 +449,31 @@ sub fleshed_copy_update {
        return $evt if $evt;
        my $editor = new_editor(requestor => $reqr, xact => 1);
        my $override = $self->api_name =~ /override/;
+    my $retarget_holds = [];
        $evt = OpenILS::Application::Cat::AssetCommon->update_fleshed_copies(
-        $editor, $override, undef, $copies, $delete_stats);
+        $editor, $override, undef, $copies, $delete_stats, $retarget_holds);
+
        if( $evt ) { 
                $logger->info("fleshed copy update failed with event: ".OpenSRF::Utils::JSON->perl2JSON($evt));
                $editor->rollback; 
                return $evt; 
        }
+
        $editor->commit;
        $logger->info("fleshed copy update successfully updated ".scalar(@$copies)." copies");
+    reset_hold_list($auth, $retarget_holds);
+
        return 1;
 }
 
+sub reset_hold_list {
+    my($auth, $hold_ids) = @_;
+    return unless @$hold_ids;
+    $logger->info("reseting holds after copy status change: @$hold_ids");
+    my $ses = OpenSRF::AppSession->create('open-ils.circ');
+    $ses->request('open-ils.circ.hold.reset.batch', $auth, $hold_ids);
+}
+
 
 __PACKAGE__->register_method(
        method => 'merge',
@@ -572,6 +585,7 @@ sub fleshed_volume_update {
 
        my $override = ($self->api_name =~ /override/);
        my $editor = new_editor( requestor => $reqr, xact => 1 );
+    my $retarget_holds = [];
 
        for my $vol (@$volumes) {
                $logger->info("vol-update: investigating volume ".$vol->id);
@@ -613,12 +627,13 @@ sub fleshed_volume_update {
                if( $copies and @$copies and !$vol->isdeleted ) {
                        $_->call_number($vol->id) for @$copies;
                        $evt = OpenILS::Application::Cat::AssetCommon->update_fleshed_copies(
-                $editor, $override, $vol, $copies, $delete_stats);
+                $editor, $override, $vol, $copies, $delete_stats, $retarget_holds);
                        return $evt if $evt;
                }
        }
 
        $editor->finish;
+    reset_hold_list($auth, $retarget_holds);
        return scalar(@$volumes);
 }
 
index a82b846..a150a07 100644 (file)
@@ -135,7 +135,7 @@ sub update_copy_stat_entries {
 
 
 sub update_copy {
-       my($class, $editor, $override, $vol, $copy) = @_;
+       my($class, $editor, $override, $vol, $copy, $retarget_holds) = @_;
 
        my $evt;
        my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
@@ -152,15 +152,47 @@ sub update_copy {
                if ref $copy->age_protect;
 
        $class->fix_copy_price($copy);
+    $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
 
        return $editor->event unless $editor->update_asset_copy($copy);
        return $class->remove_empty_objects($editor, $override, $orig_vol);
 }
 
+sub check_hold_retarget {
+    my($class, $editor, $copy, $orig_copy, $retarget_holds) = @_;
+    return unless $retarget_holds;
+
+    if( !($copy->isdeleted or $U->is_true($copy->deleted)) ) {
+        # see if a status change warrants a retarget
+
+        $orig_copy = $editor->retrieve_asset_copy($copy->id) unless $orig_copy;
+
+        if($orig_copy->status == $copy->status) {
+            # no status change, no retarget
+            return;
+        }
+
+        my $stat = $editor->retrieve_config_copy_status($copy->status);
+
+        # new status is holdable, no retarget. Later add logic to find potential 
+        # holds and retarget those to pick up the newly available copy
+        return if $U->is_true($stat->holdable); 
+    }
+
+    my $hold_ids = $editor->search_action_hold_request(
+        {   current_copy        => $copy->id, 
+            cancel_time         => undef, 
+            fulfillment_time    => undef 
+        }, {idlist => 1}
+    );
+
+    push(@$retarget_holds, @$hold_ids);
+}
+
 
 # this does the actual work
 sub update_fleshed_copies {
-       my($class, $editor, $override, $vol, $copies, $delete_stats) = @_;
+       my($class, $editor, $override, $vol, $copies, $delete_stats, $retarget_holds) = @_;
 
        my $evt;
        my $fetchvol = ($vol) ? 0 : 1;
@@ -193,7 +225,7 @@ sub update_fleshed_copies {
                $copy->clear_stat_cat_entries;
 
                if( $copy->isdeleted ) {
-                       $evt = $class->delete_copy($editor, $override, $vol, $copy);
+                       $evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds);
                        return $evt if $evt;
 
                } elsif( $copy->isnew ) {
@@ -202,7 +234,7 @@ sub update_fleshed_copies {
 
                } elsif( $copy->ischanged ) {
 
-                       $evt = $class->update_copy( $editor, $override, $vol, $copy );
+                       $evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds );
                        return $evt if $evt;
                }
 
@@ -218,7 +250,7 @@ sub update_fleshed_copies {
 
 
 sub delete_copy {
-       my($class, $editor, $override, $vol, $copy ) = @_;
+       my($class, $editor, $override, $vol, $copy, $retarget_holds ) = @_;
 
    return $editor->event unless 
       $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
@@ -249,6 +281,8 @@ sub delete_copy {
                        or return $editor->event;
        }
 
+    $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
+
        return $class->remove_empty_objects($editor, $override, $vol);
 }