From 95c60c6c80d0ac6c17384598bc6ba419f58a5434 Mon Sep 17 00:00:00 2001 From: Jason Boyer Date: Tue, 14 Feb 2023 13:12:55 -0500 Subject: [PATCH] LP2007284: Don't fail when assigning a new part to multiple items Signed-off-by: Jason Boyer --- .../lib/OpenILS/Application/Cat/AssetCommon.pm | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm index 079fed631e..9d7acc5a62 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm @@ -13,7 +13,6 @@ use OpenILS::Utils::Penalty; use OpenILS::Application::Circ::CircCommon; my $U = 'OpenILS::Application::AppUtils'; - # --------------------------------------------------------------------------- # Shared copy mangling code. Do not publish methods from here. # --------------------------------------------------------------------------- @@ -417,6 +416,8 @@ sub update_fleshed_copies { my %cache; $cache{$vol->id} = $vol if $vol; + my @new_parts = (); + for my $copy (@$copies) { my $copyid = $copy->id; @@ -470,6 +471,44 @@ sub update_fleshed_copies { $copy->stat_cat_entries( $sc_entries ); $evt = $class->update_copy_stat_entries($editor, $copy, $delete_stats); + + # Have to watch out for multiple items creating the same new part or the whole update will fail + # Twiddling knobs inside an $x inside a for $x in @y has to be done carefully + for my $part (@$parts) { + next unless $part->isnew; + + my @existing = grep { $_->label eq $part->label && $_->record == $part->record } @new_parts; + if (@existing) { + # We've created this part previously, don't want to do that again. + my $oldnewthing = $existing[0]; + if ($oldnewthing->id) { + $part->id($oldnewthing->id); + $part->isnew(0); + $part->ischanged(0); + } else { + # Grab the id of the previously created part so we can keep track of it + my $results = $editor->search_biblio_monograph_part( + { + label => $part->label, + record => $part->record, + deleted => 'f' + } + ); + + # There *should* always be a result, but... + if ($results) { + $oldnewthing->id($results->[0]->id); + $part->id($oldnewthing->id); + $part->isnew(0); + $part->ischanged(0); + } + } + } else { + # haven't seen this new part before + push(@new_parts, $part); + } + } + $copy->parts( $parts ); # probably okay to use $delete_stats here for simplicity $evt = $class->update_copy_parts($editor, $copy, $delete_stats, $create_parts); -- 2.11.0