Bug 17944 - Add Koha::ItemType->can_be_deleted and use it from admin/itemtypes.pl

Removed the sql code from Itemtypes.pm and replaced  it with DBIx
database query in the itemtypes.pl administrative script

Test plan:
1. In the staff interface, stage and manage MARC records for import

2. Try to delete an itemtype. If there are items of that itemtype in the
   database then a message telling you the number of items of that
   itemtype there are will be displayed.

3. Record that number

4. View the admin/itemtpes.pl script and confirm that there is sql code
   written in this file.

5. Apply this patch

6. View the admin/itemtypes.pl script and observe that there is no sql
   in this file. There is however DBIx code, for example
   $schema->resultset('Item')->search({ 'itype' => $itemtype_code} );
   which is searching for items with the itype value matching
   $itemtype_code value.

7. In the staff interface try to delete the same itemtype

8. Record the number of items there are with that itemtype in the
   resulting message

9. The numbers recorded in steps 3 and 8 should match showing that the
   DBIx code is working as intended

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Alex Buckley 2017-01-22 01:52:50 +00:00 committed by Jonathan Druart
parent 281e125377
commit db3ebe47e8
4 changed files with 48 additions and 18 deletions

View File

@ -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

View File

@ -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 ) {

View File

@ -110,7 +110,7 @@ Item types administration
[% CASE 'already_exists' %]
This item type already exists.
[% CASE 'cannot_be_deleted' %]
Cannot delete this item type. <p><strong>This record is used [% m.total %] times</strong>. Deletion is not possible.</p>
Cannot delete this item type. <p><strong>This record is in use</strong>. Deletion is not possible.</p>
[% CASE %]
[% m.code %]
[% END %]

View File

@ -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;