@@ -, +, @@ item deletion --- Koha/Item.pm | 10 ++++++++++ t/db_dependent/Items_DelItemCheck.t | 21 +++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) --- a/Koha/Item.pm +++ a/Koha/Item.pm @@ -210,6 +210,8 @@ returns 1 if the item is safe to delete, "linked_analytics" if the item has linked analytic records. +"last_item_for_hold" if the item is the last one on a record on which a biblio-level hold is placed + =cut sub safe_to_delete { @@ -230,6 +232,14 @@ sub safe_to_delete { return "linked_analytics" if C4::Items::GetAnalyticsCount( $self->itemnumber ) > 0; + return "last_item_for_hold" + if $self->biblio->items->count == 1 + && $self->biblio->holds->search( + { + itemnumber => undef, + } + )->count; + return 1; } --- a/t/db_dependent/Items_DelItemCheck.t +++ a/t/db_dependent/Items_DelItemCheck.t @@ -24,7 +24,7 @@ use Koha::Items; use t::lib::TestBuilder; use t::lib::Mocks; -use Test::More tests => 9; +use Test::More tests => 10; use Test::MockModule; BEGIN { @@ -71,20 +71,13 @@ my $patron = $builder->build( } ); -my $biblio = $builder->build( - { - source => 'Biblio', - value => { - branchcode => $branch->{branchcode}, - }, - } -); +my $biblio = $builder->build_sample_biblio({branchcode => $branch->{branchcode}}); my $item = $builder->build_object( { class => 'Koha::Items', value => { - biblionumber => $biblio->{biblionumber}, + biblionumber => $biblio->biblionumber, homebranch => $branch->{branchcode}, holdingbranch => $branch->{branchcode}, withdrawn => 0, # randomly assigned value may block return. @@ -153,6 +146,14 @@ $item->set( { homebranch => $branch->{branchcode}, holdingbranch => $branch->{br } +{ # last_item_for_hold + C4::Reserves::AddReserve($branch->{branchcode}, $patron->{borrowernumber}, $item->biblionumber ); + is( $item->safe_to_delete, 'last_item_for_hold', 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio' ); + + # With another item attached to the biblio, the item can be deleted + $builder->build_sample_item({ biblionumber => $item->biblionumber }); +} + is( $item->safe_to_delete, 1, --