From 6a57826c0602a6f958936b7911fa04df8198ea3f Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Sun, 22 Jan 2017 01:52:50 +0000 Subject: [PATCH] Bug 17944 - Overridden Koha::Object->delete with Koha::ItemType->delete and implemented exception to be shown if the itemtype cannot be deleted. --- Koha/ItemType.pm | 38 +++++++++++++++++++++++++++++++++ admin/itemtypes.pl | 25 ++++++++-------------- t/db_dependent/Koha/ItemTypes.t | 47 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 17 deletions(-) diff --git a/Koha/ItemType.pm b/Koha/ItemType.pm index 8f2279b..daac3f1 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,43 @@ sub translated_descriptions { } @translated_descriptions ]; } + +=head3 is_used +my $overalltotal = $ItemType->is_used($itemtype_code) + +Seperately retrieves and counts the number of items and biblioitems with an itemtype code matching the argument $itemtype_code (which is the itemtype code of the itemtype the user is trying to delete). The sum of the two numbers is added together and stored in the $overalltotal variable. + +True (a number greater than 0 which is the sum of items and biblioitems) is returned to admin/itemtypes.pl if there is more than one item and/or biblioitem with a matching itemtype code. False (0) is returned if there are no items and/or biblioitems with a matching itemtype code. + +=cut + +sub can_be_deleted { + my ($self, $itemtype_code) = @_; + my $schema = Koha::Database->new()->schema(); + my $itemtotal = Koha::Items->search({ 'itype' => $itemtype_code})->count; + my $bibliototal = Koha::Biblioitems->search({ 'itemtype' => $itemtype_code})->count; + my $overalltotal = $itemtotal + $bibliototal; + return $overalltotal; +} + +=head3 delete + +=cut + +sub delete{ + my ( $self, $itemtype ) = @_; + my $delete_allowed = can_be_deleted($itemtype); + if ($delete_allowed) { + my $deleted = $self->SUPER::delete($self); + logaction( 'ITEMTYPE', 'DELETE', $self->reserve_id, Dumper($self->unblessed) ) + if C4::Context->preference('ItemTypeLog'); + return $deleted; + } else { + Koha::Exceptions::CannotDeleteDefault->throw('itemtype could not be deleted'); + } +} + + =head3 type =cut diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl index c8c224e..25c96fa 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,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 = Koha::ItemType->can_be_deleted($itemtype_code); + if ($overalltotal) { + push @messages, { type => 'error', code => 'cannot_be_deleted', total => $overalltotal }; $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 { Koha::ItemType->delete($itemtype_code) }; if ( $@ or not $deleted ) { push @messages, { type => 'error', code => 'error_on_delete' }; } else { diff --git a/t/db_dependent/Koha/ItemTypes.t b/t/db_dependent/Koha/ItemTypes.t index e1ae330..0d44f27 100755 --- a/t/db_dependent/Koha/ItemTypes.t +++ b/t/db_dependent/Koha/ItemTypes.t @@ -19,10 +19,12 @@ use Modern::Perl; -use Test::More tests => 20; +use Test::More tests => 22; use Data::Dumper; use Koha::Database; use t::lib::Mocks; +use Koha::Items; +use Koha::Biblioitems; BEGIN { use_ok('Koha::ItemType'); @@ -95,6 +97,33 @@ Koha::Localization->new( } )->store; +Koha::Biblio->new( + { + biblionumber => 1, + } +)->store; + +Koha::Biblioitem->new( + { + biblioitemnumber => 1, + biblionumber => 1, + itemtype => 'type1' + } +)->store; + + +Koha::Item->new( + { + itemnumber => 1, + biblionumber => 1, + biblioitemnumber =>1, + itype => 'type1', + notforloan => 1, + itemlost => 0, + } +)->store; + + my $type = Koha::ItemTypes->find('type1'); ok( defined($type), 'first result' ); is( $type->itemtype, 'type1', 'itemtype/code' ); @@ -126,4 +155,20 @@ is( 'item types should be sorted by translated description' ); +sub is_used { + require Test::More; + my $itemtype_code = shift; + my $schema = Koha::Database->new()->schema(); + my $itemtotal = Koha::Items->search({ 'itype' => $itemtype_code })->count; + my $bibliototal = Koha::Biblioitems->search({ 'itemtype' => $itemtype_code})->count; + my $overalltotal = $itemtotal + $bibliototal; + return $overalltotal; +} + +my $passcount = is_used('type1'); +is ( $passcount,2,'Exists in the database, test is successful'); + +my $failcount = is_used('type2'); +is ( $failcount,0,'Doesnt exist in database, test is success'); + $schema->txn_rollback; -- 2.1.4