with scan missing pieces, retarget affected holds prior to recirculating to previous...
authorJason Etheridge <jason@esilibrary.com>
Fri, 24 Jun 2011 19:40:32 +0000 (15:40 -0400)
committerBill Erickson <berick@esilibrary.com>
Mon, 27 Jun 2011 13:53:17 +0000 (09:53 -0400)
Signed-off-by: Jason Etheridge <jason@esilibrary.com>
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Circ.pm

index df81b27..f5cc14c 100644 (file)
@@ -1306,79 +1306,100 @@ __PACKAGE__->register_method(
 sub mark_item_missing_pieces {
        my( $self, $conn, $auth, $copy_id, $args ) = @_;
     ### FIXME: We're starting a transaction here, but we're doing a lot of things outside of the transaction
-       my $e = new_editor(authtoken=>$auth, xact =>1);
-       return $e->die_event unless $e->checkauth;
+    ### FIXME: Even better, we're going to use two transactions, the first to affect pertinent holds before checkout can
+
+       my $e2 = new_editor(authtoken=>$auth, xact =>1);
+       return $e2->die_event unless $e2->checkauth;
     $args ||= {};
 
-    my $copy = $e->retrieve_asset_copy([
+    my $copy = $e2->retrieve_asset_copy([
         $copy_id,
         {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
-            or return $e->die_event;
+            or return $e2->die_event;
 
     my $owning_lib = 
         ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ? 
             $copy->circ_lib : $copy->call_number->owning_lib;
 
-    return $e->die_event unless $e->allowed('MARK_ITEM_MISSING_PIECES', $owning_lib);
+    return $e2->die_event unless $e2->allowed('MARK_ITEM_MISSING_PIECES', $owning_lib);
 
     #### grab the last circulation
-    my $circ = $e->search_action_circulation([
+    my $circ = $e2->search_action_circulation([
         {   target_copy => $copy->id}, 
         {   limit => 1, 
             order_by => {circ => "xact_start DESC"}
         }
     ])->[0];
 
-    if ($circ) {
-        if (! $circ->checkin_time) { # if circ active, attempt renew
-            my ($res) = $self->method_lookup('open-ils.circ.renew')->run($e->authtoken,{'copy_id'=>$circ->target_copy});
-            if (ref $res ne 'ARRAY') { $res = [ $res ]; }
-            if ( $res->[0]->{textcode} eq 'SUCCESS' ) {
-                $circ = $res->[0]->{payload}{'circ'};
-                $circ->target_copy( $copy->id );
-                $logger->info('open-ils.circ.mark_item_missing_pieces: successful renewal');
-            } else {
-                $logger->info('open-ils.circ.mark_item_missing_pieces: non-successful renewal');
-            }
-        } else {
+    if (!$circ) {
+        $logger->info('open-ils.circ.mark_item_missing_pieces: no previous checkout');
+        $e2->rollback;
+        return OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND',{'copy'=>$copy});
+    }
 
-            my $co_params = {
-                'copy_id'=>$circ->target_copy,
-                'patron_id'=>$circ->usr,
-                'skip_deposit_fee'=>1,
-                'skip_rental_fee'=>1
-            };
+       my $holds = $e2->search_action_hold_request(
+               { 
+                       current_copy => $copy->id,
+                       fulfillment_time => undef,
+                       cancel_time => undef,
+               }
+       );
 
-            if ($U->ou_ancestor_setting_value($e->requestor->ws_ou, 'circ.block_renews_for_holds')) {
+    $logger->debug("resetting holds that target the marked copy");
+    OpenILS::Application::Circ::Holds->_reset_hold($e2->requestor, $_) for @$holds;
 
-                my ($hold, undef, $retarget) = $holdcode->find_nearest_permitted_hold(
-                    $e, $copy, $e->requestor, 1 );
+    
+       if (! $e2->commit) {
+        return $e2->die_event;
+    }
 
-                if ($hold) { # needed for hold? then due now
+       my $e = new_editor(authtoken=>$auth, xact =>1);
+       return $e->die_event unless $e->checkauth;
 
-                    $logger->info('open-ils.circ.mark_item_missing_pieces: item needed for hold, shortening due date');
-                    my $due_date = DateTime->now(time_zone => 'local');
-                    $co_params->{'due_date'} = cleanse_ISO8601( $due_date->strftime('%FT%T%z') );
-                } else {
-                    $logger->info('open-ils.circ.mark_item_missing_pieces: item not needed for hold');
-                }
-            }
+    if (! $circ->checkin_time) { # if circ active, attempt renew
+        my ($res) = $self->method_lookup('open-ils.circ.renew')->run($e->authtoken,{'copy_id'=>$circ->target_copy});
+        if (ref $res ne 'ARRAY') { $res = [ $res ]; }
+        if ( $res->[0]->{textcode} eq 'SUCCESS' ) {
+            $circ = $res->[0]->{payload}{'circ'};
+            $circ->target_copy( $copy->id );
+            $logger->info('open-ils.circ.mark_item_missing_pieces: successful renewal');
+        } else {
+            $logger->info('open-ils.circ.mark_item_missing_pieces: non-successful renewal');
+        }
+    } else {
+
+        my $co_params = {
+            'copy_id'=>$circ->target_copy,
+            'patron_id'=>$circ->usr,
+            'skip_deposit_fee'=>1,
+            'skip_rental_fee'=>1
+        };
+
+        if ($U->ou_ancestor_setting_value($e->requestor->ws_ou, 'circ.block_renews_for_holds')) {
+
+            my ($hold, undef, $retarget) = $holdcode->find_nearest_permitted_hold(
+                $e, $copy, $e->requestor, 1 );
 
-            my ($res) = $self->method_lookup('open-ils.circ.checkout.full.override')->run($e->authtoken,$co_params);
-            if (ref $res ne 'ARRAY') { $res = [ $res ]; }
-            if ( $res->[0]->{textcode} eq 'SUCCESS' ) {
-                $logger->info('open-ils.circ.mark_item_missing_pieces: successful checkout');
-                $circ = $res->[0]->{payload}{'circ'};
+            if ($hold) { # needed for hold? then due now
+
+                $logger->info('open-ils.circ.mark_item_missing_pieces: item needed for hold, shortening due date');
+                my $due_date = DateTime->now(time_zone => 'local');
+                $co_params->{'due_date'} = cleanse_ISO8601( $due_date->strftime('%FT%T%z') );
             } else {
-                $logger->info('open-ils.circ.mark_item_missing_pieces: non-successful checkout');
-                $e->rollback;
-                return $res;
+                $logger->info('open-ils.circ.mark_item_missing_pieces: item not needed for hold');
             }
         }
-    } else {
-        $logger->info('open-ils.circ.mark_item_missing_pieces: no previous checkout');
-        $e->rollback;
-        return OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND',{'copy'=>$copy});
+
+        my ($res) = $self->method_lookup('open-ils.circ.checkout.full.override')->run($e->authtoken,$co_params);
+        if (ref $res ne 'ARRAY') { $res = [ $res ]; }
+        if ( $res->[0]->{textcode} eq 'SUCCESS' ) {
+            $logger->info('open-ils.circ.mark_item_missing_pieces: successful checkout');
+            $circ = $res->[0]->{payload}{'circ'};
+        } else {
+            $logger->info('open-ils.circ.mark_item_missing_pieces: non-successful checkout');
+            $e->rollback;
+            return $res;
+        }
     }
 
     ### Update the item status
@@ -1396,17 +1417,6 @@ sub mark_item_missing_pieces {
 
        $e->update_asset_copy($copy) or return $e->die_event;
 
-       my $holds = $e->search_action_hold_request(
-               { 
-                       current_copy => $copy->id,
-                       fulfillment_time => undef,
-                       cancel_time => undef,
-               }
-       );
-
-    $logger->debug("resetting holds that target the marked copy");
-    OpenILS::Application::Circ::Holds->_reset_hold($e->requestor, $_) for @$holds;
-
        if ($e->commit) {
 
         my $ses = OpenSRF::AppSession->create('open-ils.trigger');