serials: Respect the "Previous Issuance Copy Location" YAOUS
authorMike Rylander <mrylander@gmail.com>
Mon, 10 Jul 2017 18:41:19 +0000 (14:41 -0400)
committerMike Rylander <mrylander@gmail.com>
Mon, 10 Jul 2017 18:41:31 +0000 (14:41 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Serial.pm

index 73c8d5b..f58c805 100644 (file)
@@ -1340,6 +1340,7 @@ sub unitize_items {
     }
     my %found_stream_ids;
     my %found_types;
+    my $prev_loc_setting_map = {};
 
     my %stream_ids_by_unit_id;
 
@@ -1443,6 +1444,57 @@ sub unitize_items {
 
         my $evt = _update_sitem($editor, undef, $item);
         return $evt if $evt;
+
+        if ($mode eq 'receive') {
+            my $sdists = $editor->search_serial_distribution([
+                {"+sstr" => {"id" => $stream_id}},
+                {
+                    "join" => {"sstr" => {}},
+                    "flesh" => 1,
+                    "flesh_fields" => {"sdist" => ["subscription"]}
+                }]);
+
+            #-------------------------------------------------------------------------
+            # The following is copied from open-ils.serial.receive_items.one_unit_per
+    
+            # Fetch a list of issuances with received copies already existing
+            # on this distribution (and with the same holding type on the
+            # issuance).  This will be used in up to two places: once when building
+            # a summary, once when changing the copy location of the previous
+            # issuance's copy.
+            my $issuances_received = _issuances_received($editor, $item);
+            if ($U->event_code($issuances_received)) {
+                $editor->rollback;
+                return $issuances_received;
+            }
+    
+            # Find out if we need to to deal with previous copy location changing.
+            my $ou = $sdists->[0]->holding_lib;
+            unless (exists $prev_loc_setting_map->{$ou}) {
+                $prev_loc_setting_map->{$ou} = $U->ou_ancestor_setting_value(
+                    $ou, "serial.prev_issuance_copy_location", $editor
+                );
+            }
+    
+            # If there is a previous copy location setting, we need the previous
+            # issuance, from which we can in turn look up the item attached to the
+            # same stream we're on now.
+            if ($prev_loc_setting_map->{$ou}) {
+                if (my $prev_iss =
+                    _previous_issuance($issuances_received, $item->issuance)) {
+    
+                    # Now we can change the copy location of the previous unit,
+                    # if needed.
+                    return $editor->event if defined $U->event_code(
+                        move_previous_unit(
+                            $editor, $prev_iss, $item, $prev_loc_setting_map->{$ou}
+                        )
+                    );
+                }
+            }
+            #-------------------------------------------------------------------------
+        }
+
     }
 
     # cleanup 'dead' units (units which are now emptied of their items)