From 154ed30a131a76b0e542708fa653083b93f0439a Mon Sep 17 00:00:00 2001
From: phasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Thu, 29 Apr 2010 22:07:17 +0000
Subject: [PATCH] The associated behavior for the
 cat.bib.delete_on_no_copy_via_acq_lineitem_cancel org unit setting.

I notice the "alert on empty" setting does not seem to work with bib deletion via lineitem cancel

git-svn-id: svn://svn.open-ils.org/ILS/trunk@16356 dcc99617-32d9-48b4-a31d-7c20da2025e4
---
 .../src/perlmods/OpenILS/Application/Acq/Order.pm  |  6 ++++--
 Open-ILS/src/perlmods/OpenILS/Application/Cat.pm   |  4 ++--
 .../OpenILS/Application/Cat/AssetCommon.pm         | 22 +++++++++++-----------
 3 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm
index d815d341ea..2a3136ccb1 100644
--- a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm
+++ b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm
@@ -2486,13 +2486,15 @@ sub cancel_lineitem {
     }
 
     # Attempt to delete the gathered copies (this will also handle volume deletion and bib deletion)
-    # Another edge case, if we have a bib but not copies, are we supposed to delete the bib?
+    # Delete empty bibs according org unit setting
+    my $force_delete_empty_bib = $U->ou_ancestor_setting_value(
+        $mgr->editor->requestor->ws_ou, 'cat.bib.delete_on_no_copy_via_acq_lineitem_cancel', $mgr->editor);
     if (scalar(@$copies)>0) {
         my $override = 1;
         my $delete_stats = undef;
         my $retarget_holds = [];
         my $cat_evt = OpenILS::Application::Cat::AssetCommon->update_fleshed_copies(
-            $mgr->editor, $override, undef, $copies, $delete_stats, $retarget_holds);
+            $mgr->editor, $override, undef, $copies, $delete_stats, $retarget_holds,$force_delete_empty_bib);
 
         if( $cat_evt ) {
             $logger->info("fleshed copy update failed with event: ".OpenSRF::Utils::JSON->perl2JSON($cat_evt));
diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm b/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
index 7aa2d54d1b..abb7f3e005 100644
--- a/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
+++ b/Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
@@ -514,7 +514,7 @@ sub fleshed_copy_update {
 	my $override = $self->api_name =~ /override/;
     my $retarget_holds = [];
 	$evt = OpenILS::Application::Cat::AssetCommon->update_fleshed_copies(
-        $editor, $override, undef, $copies, $delete_stats, $retarget_holds);
+        $editor, $override, undef, $copies, $delete_stats, $retarget_holds, undef);
 
 	if( $evt ) { 
 		$logger->info("fleshed copy update failed with event: ".OpenSRF::Utils::JSON->perl2JSON($evt));
@@ -646,7 +646,7 @@ sub fleshed_volume_update {
 		if( $copies and @$copies and !$vol->isdeleted ) {
 			$_->call_number($vol->id) for @$copies;
 			$evt = OpenILS::Application::Cat::AssetCommon->update_fleshed_copies(
-                $editor, $override, $vol, $copies, $delete_stats, $retarget_holds);
+                $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, undef);
 			return $evt if $evt;
 		}
 	}
diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Cat/AssetCommon.pm b/Open-ILS/src/perlmods/OpenILS/Application/Cat/AssetCommon.pm
index ed5aa4a037..75f96e6027 100644
--- a/Open-ILS/src/perlmods/OpenILS/Application/Cat/AssetCommon.pm
+++ b/Open-ILS/src/perlmods/OpenILS/Application/Cat/AssetCommon.pm
@@ -135,7 +135,7 @@ sub update_copy_stat_entries {
 
 
 sub update_copy {
-	my($class, $editor, $override, $vol, $copy, $retarget_holds) = @_;
+	my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_;
 
 	my $evt;
 	my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
@@ -155,7 +155,7 @@ sub update_copy {
     $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
 
 	return $editor->event unless $editor->update_asset_copy($copy);
-	return $class->remove_empty_objects($editor, $override, $orig_vol);
+	return $class->remove_empty_objects($editor, $override, $orig_vol, $force_delete_empty_bib);
 }
 
 sub check_hold_retarget {
@@ -192,7 +192,7 @@ sub check_hold_retarget {
 
 # this does the actual work
 sub update_fleshed_copies {
-	my($class, $editor, $override, $vol, $copies, $delete_stats, $retarget_holds) = @_;
+	my($class, $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, $force_delete_empty_bib) = @_;
 
 	my $evt;
 	my $fetchvol = ($vol) ? 0 : 1;
@@ -225,7 +225,7 @@ sub update_fleshed_copies {
 		$copy->clear_stat_cat_entries;
 
 		if( $copy->isdeleted ) {
-			$evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds);
+			$evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
 			return $evt if $evt;
 
 		} elsif( $copy->isnew ) {
@@ -234,7 +234,7 @@ sub update_fleshed_copies {
 
 		} elsif( $copy->ischanged ) {
 
-			$evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds );
+			$evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
 			return $evt if $evt;
 		}
 
@@ -250,7 +250,7 @@ sub update_fleshed_copies {
 
 
 sub delete_copy {
-	my($class, $editor, $override, $vol, $copy, $retarget_holds ) = @_;
+	my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_;
 
    return $editor->event unless 
       $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
@@ -283,7 +283,7 @@ sub delete_copy {
 
     $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
 
-	return $class->remove_empty_objects($editor, $override, $vol);
+	return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib);
 }
 
 
@@ -397,7 +397,7 @@ sub create_copy_note {
 
 
 sub remove_empty_objects {
-	my($class, $editor, $override, $vol) = @_; 
+	my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_; 
 
     my $koe = $U->ou_ancestor_setting_value(
         $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
@@ -415,10 +415,10 @@ sub remove_empty_objects {
         }
 
         return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record ) 
-            if $aoe and not $override;
+            if $aoe and not $override and not $force_delete_empty_bib;
 
-        unless($koe) {
-            # delete the bib record if the keep-on-empty setting is not set
+        unless($koe and not $force_delete_empty_bib) {
+            # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
             my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
             return $evt if $evt;
         }
-- 
2.11.0