From 0a39b7e3c89d85b96bee5cc555951b77c9ea74c4 Mon Sep 17 00:00:00 2001
From: erickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Mon, 8 Mar 2010 21:29:50 +0000
Subject: [PATCH] added auto_merge_vols option to
 open-ils.cat.asset.volume.fleshed.batch.update.  when changing the label (or
 owner or bib) for a volume creates a collision against an existing volume,
 auto-merge (or override) will merge the contents of the edited volume into
 the existing volume

git-svn-id: svn://svn.open-ils.org/ILS/trunk@15741 dcc99617-32d9-48b4-a31d-7c20da2025e4
---
 Open-ILS/src/perlmods/OpenILS/Application/Cat.pm | 48 ++++++++++++++++--------
 1 file changed, 32 insertions(+), 16 deletions(-)

diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm b/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
index b67f3ca595..f05c7abb97 100644
--- a/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
+++ b/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
@@ -543,13 +543,15 @@ __PACKAGE__->register_method(
 	api_name	=> "open-ils.cat.asset.volume.fleshed.batch.update.override",);
 
 sub fleshed_volume_update {
-	my( $self, $conn, $auth, $volumes, $delete_stats ) = @_;
+	my( $self, $conn, $auth, $volumes, $delete_stats, $options ) = @_;
 	my( $reqr, $evt ) = $U->checkses($auth);
 	return $evt if $evt;
+    $options ||= {};
 
 	my $override = ($self->api_name =~ /override/);
 	my $editor = new_editor( requestor => $reqr, xact => 1 );
     my $retarget_holds = [];
+    my $auto_merge_vols = $options->{auto_merge_vols};
 
 	for my $vol (@$volumes) {
 		$logger->info("vol-update: investigating volume ".$vol->id);
@@ -583,8 +585,9 @@ sub fleshed_volume_update {
 
 		} elsif( $vol->ischanged ) {
 			$logger->info("vol-update: update volume");
-			$evt = update_volume($vol, $editor);
-			return $evt if $evt;
+			my $resp = update_volume($vol, $editor, ($override or $auto_merge_vols));
+			return $resp->{evt} if $resp->{evt};
+            $vol = $resp->{merge_vol};
 		}
 
 		# now update any attached copies
@@ -605,24 +608,37 @@ sub fleshed_volume_update {
 sub update_volume {
 	my $vol = shift;
 	my $editor = shift;
+    my $auto_merge = shift;
 	my $evt;
+    my $merge_vol;
 
-	return $evt if ( $evt = OpenILS::Application::Cat::AssetCommon->org_cannot_have_vols($editor, $vol->owning_lib) );
+	return {evt => $evt} 
+        if ( $evt = OpenILS::Application::Cat::AssetCommon->org_cannot_have_vols($editor, $vol->owning_lib) );
 
-	my $vols = $editor->search_asset_call_number( { 
-			owning_lib	=> $vol->owning_lib,
-			record		=> $vol->record,
-			label			=> $vol->label,
-			deleted		=> 'f'
-		}
-	);
+	my $vols = $editor->search_asset_call_number({ 
+        owning_lib	=> $vol->owning_lib,
+        record		=> $vol->record,
+        label		=> $vol->label,
+        deleted		=> 'f',
+        id          => {'!=' => $vol->id}
+    });
 
-	# There exists a different volume in the DB with the same properties
-	return OpenILS::Event->new('VOLUME_LABEL_EXISTS', payload => $vol->id)
-		if grep { $_->id ne $vol->id } @$vols;
+    if(@$vols) {
 
-	return $editor->event unless $editor->update_asset_call_number($vol);
-	return undef;
+        if($auto_merge) {
+
+            # If the auto-merge option is on, merge our updated volume into the existing
+            # volume with the same record + owner + label.
+            ($merge_vol, $evt) = OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $vols->[0]);
+            return {evt => $evt, merge_vol => $merge_vol};
+
+        } else {
+	        return {evt => OpenILS::Event->new('VOLUME_LABEL_EXISTS', payload => $vol->id)};
+        }
+    }
+
+	return {evt => $editor->die_event} unless $editor->update_asset_call_number($vol);
+	return {};
 }
 
 
-- 
2.11.0