From 95206809441fee6bc01d166539a4c42e0ea2f40b Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 2 Jun 2011 14:44:28 -0400 Subject: [PATCH] Option to force-delete copies during volume delete Added a "force_delete_copies" option to open-ils.cat.asset.volume.fleshed.batch.update* which forces deletion of all copies attached to a volume if the volume is being deleted. There are still scenarios that will result in copies not being deleted (e.g. copy is checked out), in which case the override option will behave here the same way it behaves in open-ils.cat.asset.copy.fleshed.batch.update* Signed-off-by: Bill Erickson Signed-off-by: Jason Etheridge --- .../src/perlmods/lib/OpenILS/Application/Cat.pm | 20 ++++----- .../lib/OpenILS/Application/Cat/AssetCommon.pm | 47 ++++++++++++++++++---- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm index 974390e40..8e311f6b7 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm @@ -26,6 +26,7 @@ use OpenSRF::AppSession; my $U = "OpenILS::Application::AppUtils"; my $conf; my %marctemplates; +my $assetcom = 'OpenILS::Application::Cat::AssetCommon'; __PACKAGE__->register_method( method => "retrieve_marc_template", @@ -868,21 +869,20 @@ sub fleshed_volume_update { if( $vol->isdeleted ) { $logger->info("vol-update: deleting volume"); - return $editor->event unless + return $editor->die_event unless $editor->allowed('UPDATE_VOLUME', $vol->owning_lib); - my $cs = $editor->search_asset_copy( - { call_number => $vol->id, deleted => 'f' } ); - return OpenILS::Event->new( - 'VOLUME_NOT_EMPTY', payload => $vol->id ) if @$cs; - $vol->deleted('t'); - return $editor->event unless + if(my $evt = $assetcom->delete_volume($editor, $vol, $override, $$options{force_delete_copies})) { + $editor->rollback; + return $evt; + } + + return $editor->die_event unless $editor->update_asset_call_number($vol); - } elsif( $vol->isnew ) { $logger->info("vol-update: creating volume"); - $evt = OpenILS::Application::Cat::AssetCommon->create_volume( $override, $editor, $vol ); + $evt = $assetcom->create_volume( $override, $editor, $vol ); return $evt if $evt; } elsif( $vol->ischanged ) { @@ -895,7 +895,7 @@ sub fleshed_volume_update { # now update any attached copies if( $copies and @$copies and !$vol->isdeleted ) { $_->call_number($vol->id) for @$copies; - $evt = OpenILS::Application::Cat::AssetCommon->update_fleshed_copies( + $evt = $assetcom->update_fleshed_copies( $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, undef); return $evt if $evt; } 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 c24a9f3d5..555e4b791 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm @@ -60,7 +60,7 @@ sub create_copy { my $evt; my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib; - return $evt if ($evt = OpenILS::Application::Cat::AssetCommon->org_cannot_have_vols($editor, $org)); + return $evt if ($evt = $class->org_cannot_have_vols($editor, $org)); $copy->clear_id; $copy->editor($editor->requestor->id); @@ -196,7 +196,7 @@ sub update_copy { my $evt; my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib; - return $evt if ( $evt = OpenILS::Application::Cat::AssetCommon->org_cannot_have_vols($editor, $org) ); + return $evt if ( $evt = $class->org_cannot_have_vols($editor, $org) ); $logger->info("vol-update: updating copy ".$copy->id); my $orig_copy = $editor->retrieve_asset_copy($copy->id); @@ -313,7 +313,7 @@ sub update_fleshed_copies { sub delete_copy { - my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_; + my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib, $skip_empty_cleanup) = @_; return $editor->event unless $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy)); @@ -346,6 +346,8 @@ sub delete_copy { $class->check_hold_retarget($editor, $copy, undef, $retarget_holds); + return undef if $skip_empty_cleanup; + return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib); } @@ -447,7 +449,7 @@ sub find_or_create_volume { $vol->suffix($suffix); $vol->record($record_id); - my $evt = OpenILS::Application::Cat::AssetCommon->create_volume(0, $e, $vol); + my $evt = $class->create_volume(0, $e, $vol); return (undef, $evt) if $evt; return ($vol); @@ -479,10 +481,8 @@ sub remove_empty_objects { # delete this volume if it's not already marked as deleted unless( $U->is_true($vol->deleted) || $vol->isdeleted ) { - $vol->deleted('t'); - $vol->editor($editor->requestor->id); - $vol->edit_date('now'); - $editor->update_asset_call_number($vol) or return $editor->event; + my $evt = $class->delete_volume($editor, $vol, $override, 0, 1); + return $evt if $evt; } return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record ) @@ -498,6 +498,37 @@ sub remove_empty_objects { return undef; } +# Deletes a volume. Returns undef on success, event on error +# force : deletes all attached copies +# skip_copy_check : assumes caller has verified no copies need deleting first +sub delete_volume { + my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_; + my $evt; + + unless($skip_copy_checks) { + my $cs = $editor->search_asset_copy( + [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1}); + + return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id) + if @$cs and !$delete_copies; + + my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'}); + + for my $copy (@$copies) { + $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1); + return $evt if $evt; + } + } + + $vol->deleted('t'); + $vol->edit_date('now'); + $vol->editor($editor->requestor->id); + $editor->update_asset_call_number($vol) or return $editor->die_event; + + # handle the case where this is the last volume on the record + return $class->remove_empty_objects($editor, $override, $vol); +} + sub copy_perm_org { my($class, $vol, $copy) = @_; -- 2.11.0