More back end improvements.
authorJason Stephenson <jason@sigio.com>
Sun, 15 Jul 2018 21:52:22 +0000 (17:52 -0400)
committerJason Stephenson <jason@sigio.com>
Fri, 26 Oct 2018 23:04:55 +0000 (19:04 -0400)
It should actually work.  Though I didn't try the previous commit, I
realized that the transaction code was wrong.  I also decided to make
a request to open-ils.circ.transit.abort rather that do all that code
"by hand."

Open-ILS/src/perlmods/lib/OpenILS/Application/Circ.pm

index bbeeae5..22c4b77 100644 (file)
@@ -1307,7 +1307,7 @@ sub mark_item {
     my( $self, $conn, $auth, $copy_id, $args ) = @_;
     $args ||= {};
 
-    my $e = new_editor(authtoken=>$auth, xact =>1);
+    my $e = new_editor(authtoken=>$auth);
     return $e->die_event unless $e->checkauth;
     my $copy = $e->retrieve_asset_copy([
         $copy_id,
@@ -1359,23 +1359,7 @@ sub mark_item {
     } elsif ($copy->status->id() == OILS_COPY_STATUS_IN_TRANSIT) {
         # Items in transit need to have the transit aborted before being marked.
         if ($args->{handle_transit}) {
-            if ($e->allowed('ABORT_TRANSIT')) {
-                my $transit = $e->search_action_transit_copy(
-                    {
-                        target_copy => $copy->id,
-                        dest_recv_time => undef,
-                        cancel_time => undef
-                    }
-                );
-                if ($transit && @{$transit}) {
-                    # Abort the transit and do not retarget holds nor commit the transaction.
-                    # NOTE: __abort_transit should probably be moved/renamed since we're reaching in to use it.
-                    my $r = OpenILS::Application::Circ::Transit::__abort_transit($e, $transit->[0], $copy, 1, 1);
-                    $evt = $r if (ref $r); #__abort_transit returns 1 or an event.
-                }
-            } else {
-                $evt = $e->event;
-            }
+            $evt = try_abort_transit($auth, $copy_id);
         } else {
             $evt = OpenILS::Event->new('ITEM_TO_MARK_IN_TRANSIT');
         }
@@ -1401,12 +1385,6 @@ sub mark_item {
         {flesh=>1, flesh_fields=>{ahr=>['eligible_copies']}}
     );
 
-    # Handle extra mark damaged charges, etc.
-    if ($self->api_name =~ /damaged/) {
-        $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
-        return $evt if $evt;
-    }
-
     # Throw event if attempting to  mark discard the only copy to fill a hold.
     if ($self->api_name =~ /discard/) {
         if (!$args->{handle_last_hold_copy}) {
@@ -1421,6 +1399,16 @@ sub mark_item {
         return $evt if $evt;
     }
 
+    # Everything below here requires a transaction and there is nothing left to interfere with it.
+    $e->xact_begin;
+
+    # Handle extra mark damaged charges, etc.
+    if ($self->api_name =~ /damaged/) {
+        $evt = handle_mark_damaged($e, $copy, $owning_lib, $args);
+        return $evt if $evt;
+    }
+
+    # Mark the copy.
     $copy->status($stat);
     $copy->edit_date('now');
     $copy->editor($e->requestor->id);
@@ -1466,6 +1454,19 @@ sub try_checkin {
     }
 }
 
+sub try_abort_transit {
+    my ($auth, $copy_id) = @_;
+
+    my $abort = $U->simplereq(
+        'open-ils.circ',
+        'open-ils.circ.transit.abort',
+        $auth, {copyid => $copy_id}
+    );
+    # Above returns 1 or an event.
+    return $abort if (ref $abort);
+    return undef;
+}
+
 sub handle_mark_damaged {
     my($e, $copy, $owning_lib, $args) = @_;