LP1883171 & LP1940663: Modify do_checkin for asset.copy_inventory
authorJason Stephenson <jason@sigio.com>
Sun, 31 Oct 2021 19:18:22 +0000 (15:18 -0400)
committerMike Rylander <mrylander@gmail.com>
Thu, 24 Mar 2022 21:25:44 +0000 (17:25 -0400)
Modify the do_chekin method of Circulate.pm to create a new
asset::copy_inventory object when the do inventory modifier is used.

This change necessitates moving the check for a floating copy up to
where the do inventory code is run and subsequent modifications to the
transit and noop checkin code.

Set checkin_changed to true when adding an asset.copy_inventory entry
during check-in so that even if there would otherwise be a NO_CHANGE
response, the row will still get created.

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>

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

index 40fe4b0..c2047e3 100644 (file)
@@ -407,7 +407,6 @@ my @AUTOLOAD_FIELDS = qw/
     backdate
     reservation
     do_inventory_update
-    latest_inventory
     copy
     copy_id
     copy_barcode
@@ -2716,19 +2715,32 @@ sub do_checkin {
         $self->dont_change_lost_zero($dont_change_lost_zero);
     }
 
-    my $latest_inventory = Fieldmapper::asset::latest_inventory->new;
-
-    if ($self->do_inventory_update) {
-        $latest_inventory->inventory_date('now');
-        $latest_inventory->inventory_workstation($self->editor->requestor->wsid);
-        $latest_inventory->copy($self->copy->id());
-    } else {
-        my $alci = $self->editor->search_asset_latest_inventory(
-            {copy => $self->copy->id}
+    # Check if the copy can float to here. We need this for inventory
+    # and to see if the copy needs to transit or stay here later.
+    my $can_float = 0;
+    if ($self->copy->floating) {
+        my $res = $self->editor->json_query(
+            {   from =>
+                [
+                    'evergreen.can_float',
+                    $self->copy->floating->id,
+                    $self->copy->circ_lib,
+                    $self->circ_lib
+                ]
+            }
         );
-        $latest_inventory = $alci->[0]
+        $can_float = $U->is_true($res->[0]->{'evergreen.can_float'}) if $res;
+    }
+
+    # Do copy inventory if necessary.
+    if ($self->do_inventory_update && ($self->circ_lib == $self->copy->circ_lib || $can_float)) {
+        my $aci = Fieldmapper::asset::copy_inventory->new();
+        $aci->inventory_date('now');
+        $aci->inventory_workstation($self->editor->requestor->wsid);
+        $aci->copy($self->copy->id());
+        $self->editor->create_asset_copy_inventory($aci);
+        $self->checkin_changed(1);
     }
-    $self->latest_inventory($latest_inventory);
 
     if( $self->checkin_check_holds_shelf() ) {
         $self->bail_on_events(OpenILS::Event->new('NO_CHANGE'));
@@ -2940,20 +2952,7 @@ sub do_checkin {
     
             } else {
                 # copy needs to transit "home", or stick here if it's a floating copy
-                my $can_float = 0;
-                if ($self->copy->floating && ($self->manual_float || !$U->is_true($self->copy->floating->manual)) && !$self->remote_hold) { # copy is potentially floating?
-                    my $res = $self->editor->json_query(
-                        {   from => [
-                                'evergreen.can_float',
-                                $self->copy->floating->id,
-                                $self->copy->circ_lib,
-                                $self->circ_lib
-                            ]
-                        }
-                    );
-                    $can_float = $U->is_true($res->[0]->{'evergreen.can_float'}) if $res; 
-                }
-                if ($can_float) { # Yep, floating, stick here
+                if ($can_float && ($self->manual_float || !$U->is_true($self->copy->floating->manual)) && !$self->remote_hold) { # Yep, floating, stick here
                     $self->checkin_changed(1);
                     $self->copy->circ_lib( $self->circ_lib );
                     $self->update_copy;
@@ -2967,22 +2966,11 @@ sub do_checkin {
             }
         }
     } else { # no-op checkin
-        if ($self->copy->floating) { # XXX floating items still stick where they are even with no-op checkin?
-            my $res = $self->editor->json_query(
-                {
-                    from => [
-                        'evergreen.can_float',
-                        $self->copy->floating->id,
-                        $self->copy->circ_lib,
-                        $self->circ_lib
-                    ]
-                }
-            );
-            if ($res && @$res && $U->is_true($res->[0]->{'evergreen.can_float'})) {
-                $self->checkin_changed(1);
-                $self->copy->circ_lib( $self->circ_lib );
-                $self->update_copy;
-            }
+        # XXX floating items still stick where they are even with no-op checkin?
+        if ($self->copy->floating && $can_float) {
+            $self->checkin_changed(1);
+            $self->copy->circ_lib( $self->circ_lib );
+            $self->update_copy;
         }
     }
 
@@ -4019,22 +4007,17 @@ sub checkin_flesh_events {
         );
     }
 
-    if ($self->latest_inventory) {
-        # flesh some workstation fields before returning
-        $self->latest_inventory->inventory_workstation(
-            $self->editor->retrieve_actor_workstation([$self->latest_inventory->inventory_workstation])
-        );
-    }
-
-    if($self->latest_inventory && !$self->latest_inventory->id) {
-        my $alci = $self->editor->search_asset_latest_inventory(
-            {copy => $self->latest_inventory->copy}
-        );
-        if($alci->[0]) {
-            $self->latest_inventory->id($alci->[0]->id);
-        }
+    # Flesh the latest inventory.
+    # NB: This survives the unflesh_copy below. Let's keep it that way.
+    my $alci = $self->editor->search_asset_latest_inventory([
+        {copy=>$self->copy->id},
+        {flesh => 1,
+         flesh_fields => {
+             alci => ['inventory_workstation']
+         }}]);
+    if ($alci && $alci->[0]) {
+        $self->copy->latest_inventory($alci->[0]);
     }
-    $self->copy->latest_inventory($self->latest_inventory);
 
     for my $evt (@{$self->events}) {
 
@@ -4049,8 +4032,6 @@ sub checkin_flesh_events {
         $payload->{patron}  = $self->patron;
         $payload->{reservation} = $self->reservation
             unless (not $self->reservation or $self->reservation->cancel_time);
-        $payload->{latest_inventory} = $self->latest_inventory;
-        if ($self->do_inventory_update) { $payload->{do_inventory_update} = 1; }
 
         $evt->{payload}     = $payload;
     }