From: Jason Stephenson <jason@sigio.com>
Date: Sun, 31 Oct 2021 20:14:10 +0000 (-0400)
Subject: LP1883171 & LP1940663: Replace circ method to update latest inventory
X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=f3811352554f09dc43cebe3f0929c8678dbf99ba;p=evergreen%2Fmasslnc.git

LP1883171 & LP1940663: Replace circ method to update latest inventory

Replace the open-ils.circ.circulation.update_latest_inventory method
with open-ils.circ.circulation.update_copy_inventory.

Beyond renaming the method and changing the tables it operates on, the
following changes are also made:

1. Use savepoints to allow some updates to succeed when others fail.

2. Modify the return value to include success and failure counts so that
these could potentially be used in client feedback.

This work was sponsored by NOBLE.

Signed-off-by: Jason Stephenson <jason@sigio.com>
Signed-off-by: Michele Morgan <mmorgan@noblenet.org>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
---

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ.pm
index bb4412b6d9..6436f79e4d 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ.pm
@@ -371,35 +371,39 @@ sub new_set_circ_lost {
 }
 
 __PACKAGE__->register_method(
-    method    => "update_latest_inventory",
-    api_name  => "open-ils.circ.circulation.update_latest_inventory");
+    method    => "update_copy_inventory",
+    api_name  => "open-ils.circ.circulation.update_copy_inventory");
 
-sub update_latest_inventory {
+sub update_copy_inventory {
     my( $self, $conn, $auth, $args ) = @_;
-    my $e = new_editor(authtoken=>$auth, xact=>1);
-    return $e->die_event unless $e->checkauth;
+    my $e = new_editor(authtoken=>$auth);
+    return [0, 0, $e->die_event] unless $e->checkauth;
 
+    my ($success,$failure) = (0,0);
+    my $spname = "aci_update_savepoint";
+    $e->xact_begin();
     my $copies = $$args{copy_list};
     foreach my $copyid (@$copies) {
-        my $copy = $e->retrieve_asset_copy($copyid);
-        my $alci = $e->search_asset_latest_inventory({copy => $copyid})->[0];
-
-        if($alci) {
-            $alci->inventory_date('now');
-            $alci->inventory_workstation($e->requestor->wsid);
-            $e->update_asset_latest_inventory($alci) or return $e->die_event;
+        my $aci = Fieldmapper::asset::copy_inventory->new;
+        $aci->inventory_date('now');
+        $aci->inventory_workstation($e->requestor->wsid);
+        $aci->copy($copyid);
+        $e->set_savepoint($spname);
+        if ($e->create_asset_copy_inventory($aci)) {
+            $success++;
         } else {
-            my $alci = Fieldmapper::asset::latest_inventory->new;
-            $alci->inventory_date('now');
-            $alci->inventory_workstation($e->requestor->wsid);
-            $alci->copy($copy->id);
-            $e->create_asset_latest_inventory($alci) or return $e->die_event;
+            $failure++;
+            $e->rollback_savepoint($spname);
         }
-
-        $copy->latest_inventory($alci);
+        $e->release_savepoint($spname);
     }
-    $e->commit;
-    return 1;
+    if ($success) {
+        $e->commit();
+    } else {
+        $e->rollback();
+    }
+
+    return [ $success, $failure ];
 }
 
 __PACKAGE__->register_method(