From 887764989e6342dd59fbc18358298fb06b0c0409 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

---
 Koha/Item.pm | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/Koha/Item.pm b/Koha/Item.pm
index fa1ac262e1..c613c341fe 100644
--- a/Koha/Item.pm
+++ b/Koha/Item.pm
@@ -186,6 +186,19 @@ sub delete {
 
     C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" );
 
+    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);
+            my @volume_items = $volume->items();
+            $volume->delete unless @volume_items;
+        }
+    }
+
     $self->_after_item_action_hooks({ action => 'delete' });
 
     logaction( "CATALOGUING", "DELETE", $self->itemnumber, "item" )
-- 
2.21.1 (Apple Git-122.3)