From 336cd8df619a9c44262ad3b7f84824acbf4a9777 Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Sun, 22 Jan 2017 01:52:50 +0000 Subject: [PATCH] Bug 17944 - Removed SQl query from itemtypes.pl and replaced it with can_be_deleted method in Koha/ItemType.pm In Koha/ItemTypes.t I have reworded unit test output and added test for item deletion. In addition to updating admin script by removing warn and changing if statement check --- Koha/ItemType.pm | 16 +++++++++++++ admin/itemtypes.pl | 26 ++++++++-------------- .../prog/en/modules/admin/itemtypes.tt | 2 +- t/db_dependent/Koha/ItemTypes.t | 24 +++++++++++++++++++- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/Koha/ItemType.pm b/Koha/ItemType.pm index 8f2279b..0b84e80 100644 --- a/Koha/ItemType.pm +++ b/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 diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl index c8c224e..756910d 100755 --- a/admin/itemtypes.pl +++ b/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,27 +137,19 @@ 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::ItemTypes->find($itemtype_code); + 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); - $template->param( itemtype => $itemtype, ); + $template->param( itemtype => $ItemType, ); } } elsif ( $op eq 'delete_confirmed' ) { + my $itemtype = Koha::ItemTypes->find($itemtype_code); my $deleted = eval { $itemtype->delete }; if ( $@ or not $deleted ) { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt index 040b52e..0d07187 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt +++ b/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 %] diff --git a/t/db_dependent/Koha/ItemTypes.t b/t/db_dependent/Koha/ItemTypes.t index e1ae330..082d733 100755 --- a/t/db_dependent/Koha/ItemTypes.t +++ b/t/db_dependent/Koha/ItemTypes.t @@ -19,10 +19,13 @@ use Modern::Perl; -use Test::More tests => 20; +use Test::More tests => 26; 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,23 @@ 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 type that is used by an item and a biblioitem cannot be deleted' ); + +is ( $item->delete, 1, 'An item has been deleted' ); + +is ( $biblio->delete, 1, 'A biblioitem has been deleted' ); + +is ( $item_type->can_be_deleted, 1, 'The item type that was being used by the removed item and biblioitem can now be deleted' ); + $schema->txn_rollback; -- 2.1.4