From 1e1dca7d4f9294b1f8a5e242861e9e6ad8d348e3 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Fri, 17 Mar 2017 12:06:24 -0400 Subject: [PATCH] LP#1619703 Transfer ACQ lineitem to alternate bib WIP Signed-off-by: Bill Erickson --- .../perlmods/lib/OpenILS/Application/Acq/Order.pm | 55 ++++++++++++++++++++-- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm index b157a1d35a..9adc310e37 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm @@ -4218,9 +4218,6 @@ sub transfer_lineitem { where => {lineitem => $li_id} }); - # TODO: Create or transfer volumes to the new bib record. Create if - # some copies will remain linked to the old volume, merge if all - # copies are moving. # TODO: Transfer monograph parts. my $copies = $e->search_asset_copy( @@ -4242,11 +4239,63 @@ sub transfer_lineitem { return $evt if $evt; } + # Transfer parts as needed to the target bib record. + my $part_maps = $e->search_asset_copy_part_map( + [ {target_copy => [map {$_->id} @$copies]}, + {flesh => 1, flesh_fields => {acpm => ['part']}} + ], {substream => 1} + ); + + for my $map (@$part_maps) { + my $evt = transfer_order_copy_parts($e, $bib_id, $map); + return $evt if $evt; + } + $e->commit; return $li; } +# Transfer copy part map from the source bib to the target bib +# If a part exists on the target bib with the same label, use it. +# Otherwise, create a new part linked to the target bib to be used +# by the migrated copy. +# +# Returns undef on success, event on error. +sub transfer_order_copy_parts { + my ($e, $bib_id, $map) = @_; + + # See if the same part exists on the target bib. + my $existing = $e->search_biblio_monograph_part({ + record => $bib_id, + label => $map->part->label, + deleted => 'f' + })->[0]; + + if ($existing) { + # matching part exists on target record. + # Teach existing copy part map to use it. + + $map->part($existing->id); + + } else { + # No matching part exists on the target record. + # Create one and teach the existing copy part map to use it. + + my $new_part = Fieldmapper::biblio::monograph_part->new; + $new_part->record($bib_id); + $new_part->label($map->part->label); + $new_part->label_sortkey($map->part->label_sortkey); + $e->create_biblio_monograph_part($new_part) or return $e->die_event; + + $map->part($new_part->id); + } + + $e->update_asset_copy_part_map($map) or return $e->die_event; + + return undef; +} + # 1. If every copy linked to the CN is represented by ordered copies # -- the ones we're processing here -- then transfer the call number # wholesale to the new bib record. -- 2.11.0