@@ -, +, @@ it with can_be_deleted method in Koha/ItemType.pm --- Koha/ItemType.pm | 16 ++++++++++++++ admin/itemtypes.pl | 25 ++++++++-------------- .../prog/en/modules/admin/itemtypes.tt | 2 +- t/db_dependent/Koha/ItemTypes.t | 20 ++++++++++++++++- 4 files changed, 45 insertions(+), 18 deletions(-) --- a/Koha/ItemType.pm +++ a/Koha/ItemType.pm @@ -23,6 +23,7 @@ use C4::Koha; use C4::Languages; use Koha::Database; use Koha::Localizations; +use Koha::Exceptions; use base qw(Koha::Object); @@ -89,6 +90,21 @@ sub translated_descriptions { } @translated_descriptions ]; } + +=head3 can_be_deleted +my $overalltotal = Koha::ItemType->can_be_deleted(); + +Counts up the number of biblioitems and items with itemtype (code) and hands back the combined number of biblioitems and items with the itemtype + +=cut + +sub can_be_deleted { + my ($self) = @_; + my $nb_items = Koha::Items->search( { 'itype' => $self->itemtype} )->count; + my $nb_biblioitems = Koha::Biblioitems->search( { 'itemtype' => $self->itemtype} )->count; + return $nb_items + $nb_biblioitems == 0 ? 1 : 0; +} + =head3 type =cut --- a/admin/itemtypes.pl +++ a/admin/itemtypes.pl @@ -31,8 +31,8 @@ use C4::Koha; use C4::Context; use C4::Auth; use C4::Output; - use Koha::ItemTypes; +use Koha::ItemType; use Koha::Localizations; my $input = new CGI; @@ -137,20 +137,12 @@ if ( $op eq 'add_form' ) { $searchfield = ''; $op = 'list'; -} elsif ( $op eq 'delete_confirm' ) { - - # Check both items and biblioitems - my ($total) = $dbh->selectrow_array( ' - SELECT COUNT(*) AS total FROM ( - SELECT itemtype AS t FROM biblioitems - UNION ALL - SELECT itype AS t FROM items - ) AS tmp - WHERE tmp.t=? - ', {}, $itemtype_code ); - - if ($total) { - push @messages, { type => 'error', code => 'cannot_be_deleted', total => $total }; + + } elsif ( $op eq 'delete_confirm' ) { + my $ItemType = Koha::ItemType->new(); + my $overalltotal = $ItemType->can_be_deleted(); + if ($overalltotal == 0) { + push @messages, { type => 'error', code => 'cannot_be_deleted'}; $op = 'list'; } else { my $itemtype = Koha::ItemTypes->find($itemtype_code); @@ -158,8 +150,9 @@ if ( $op eq 'add_form' ) { } } elsif ( $op eq 'delete_confirmed' ) { + my $itemtype = Koha::ItemTypes->find($itemtype_code); - my $deleted = eval { $itemtype->delete }; + my $deleted = eval{ $itemtype->delete };; if ( $@ or not $deleted ) { push @messages, { type => 'error', code => 'error_on_delete' }; } else { --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt @@ -110,7 +110,7 @@ Item types administration [% CASE 'already_exists' %] This item type already exists. [% CASE 'cannot_be_deleted' %] - Cannot delete this item type.

This record is used [% m.total %] times. Deletion is not possible.

+ Cannot delete this item type.

This record is in use. Deletion is not possible.

[% CASE %] [% m.code %] [% END %] --- a/t/db_dependent/Koha/ItemTypes.t +++ a/t/db_dependent/Koha/ItemTypes.t @@ -19,10 +19,13 @@ use Modern::Perl; -use Test::More tests => 20; +use Test::More tests => 24; use Data::Dumper; use Koha::Database; use t::lib::Mocks; +use Koha::Items; +use Koha::Biblioitems; +use t::lib::TestBuilder; BEGIN { use_ok('Koha::ItemType'); @@ -126,4 +129,19 @@ is( 'item types should be sorted by translated description' ); +my $builder = t::lib::TestBuilder->new; +my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' }); + +is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted'); + +my $item = $builder->build_object({ class => 'Koha::Items', value => { itype => $item_type->itemtype }}); + +is( $item_type->can_be_deleted, 0, 'An item type that is used by an item cannot be deleted' ); + +my $biblio = $builder->build_object({ class => 'Koha::Biblioitems', value => { itemtype => $item_type->itemtype }}); + +is ( $item_type->can_be_deleted, 0, 'An item that is used by an item and a biblioitem cannot be deleted' ); + +is ( $item_type->delete, 1, 'An item has been deleted' ); + $schema->txn_rollback; --