From bfd45a57a8d75af0c7dfd86f4adef375e1da29f8 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Fri, 28 Feb 2020 10:32:16 -0500
Subject: [PATCH] Bug 24857: Delete a volume when the last item on that volume
 is deleted

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>

Signed-off-by: Rebecca  Coert <rcoert@arlingtonva.us>
---
 Koha/Item.pm | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/Koha/Item.pm b/Koha/Item.pm
index f45010299b..9959e35d7d 100644
--- a/Koha/Item.pm
+++ b/Koha/Item.pm
@@ -232,6 +232,18 @@ sub delete {
     $indexer->index_records( $self->biblionumber, "specialUpdate", "biblioserver" )
         unless $params->{skip_record_index};
 
+    if ( C4::Context->preference('EnableVolumes') ) {
+        my $volume_item =
+          Koha::Biblio::Volume::Items->find( { itemnumber => $self->itemnumber } );
+        my $volume_id = $volume_item ? $volume_item->volume_id : undef;
+
+        # If this item is the last item on a volume, delete the volume as well
+        if ($volume_id) {
+            my $volume = Koha::Biblio::Volumes->find($volume_id);
+            $volume->delete unless $volume->items->count > 1;
+        }
+    }
+
     $self->_after_item_action_hooks({ action => 'delete' });
 
     logaction( "CATALOGUING", "DELETE", $self->itemnumber, "item" )
-- 
2.24.3 (Apple Git-128)