View | Details | Raw Unified | Return to bug 17944
Collapse All | Expand All

(-)a/Koha/ItemType.pm (+16 lines)
Lines 23-28 use C4::Koha; Link Here
23
use C4::Languages;
23
use C4::Languages;
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::Localizations;
25
use Koha::Localizations;
26
use Koha::Exceptions;
26
27
27
use base qw(Koha::Object);
28
use base qw(Koha::Object);
28
29
Lines 89-94 sub translated_descriptions { Link Here
89
    } @translated_descriptions ];
90
    } @translated_descriptions ];
90
}
91
}
91
92
93
94
=head3 can_be_deleted
95
my $overalltotal = Koha::ItemType->can_be_deleted();
96
97
Counts up the number of biblioitems and items with itemtype (code) and hands back the combined number of biblioitems and items with the itemtype
98
99
=cut
100
101
sub can_be_deleted {
102
    my ($self) = @_;
103
    my $nb_items = Koha::Items->search( { 'itype' => $self->itemtype} )->count;
104
    my $nb_biblioitems = Koha::Biblioitems->search( { 'itemtype' => $self->itemtype} )->count;
105
    return $nb_items + $nb_biblioitems == 0 ? 1 : 0;
106
}
107
92
=head3 type
108
=head3 type
93
109
94
=cut
110
=cut
(-)a/admin/itemtypes.pl (-17 / +9 lines)
Lines 31-38 use C4::Koha; Link Here
31
use C4::Context;
31
use C4::Context;
32
use C4::Auth;
32
use C4::Auth;
33
use C4::Output;
33
use C4::Output;
34
35
use Koha::ItemTypes;
34
use Koha::ItemTypes;
35
use Koha::ItemType;
36
use Koha::Localizations;
36
use Koha::Localizations;
37
37
38
my $input         = new CGI;
38
my $input         = new CGI;
Lines 137-163 if ( $op eq 'add_form' ) { Link Here
137
137
138
    $searchfield = '';
138
    $searchfield = '';
139
    $op          = 'list';
139
    $op          = 'list';
140
} elsif ( $op eq 'delete_confirm' ) {
140
141
141
 } elsif ( $op eq 'delete_confirm' ) {
142
    # Check both items and biblioitems
142
    my $ItemType = Koha::ItemTypes->find($itemtype_code);
143
    my ($total) = $dbh->selectrow_array( '
143
    my $overalltotal = $ItemType->can_be_deleted();
144
        SELECT COUNT(*) AS total FROM (
144
    if ($overalltotal == 0) {
145
            SELECT itemtype AS t FROM biblioitems
145
        push @messages, { type => 'error', code => 'cannot_be_deleted'};
146
            UNION ALL
147
            SELECT itype AS t FROM items
148
        ) AS tmp
149
        WHERE tmp.t=?
150
    ', {}, $itemtype_code );
151
152
    if ($total) {
153
        push @messages, { type => 'error', code => 'cannot_be_deleted', total => $total };
154
        $op = 'list';
146
        $op = 'list';
155
    } else {
147
    } else {
156
        my $itemtype = Koha::ItemTypes->find($itemtype_code);
148
        $template->param( itemtype => $ItemType, );
157
        $template->param( itemtype => $itemtype, );
158
    }
149
    }
159
150
160
} elsif ( $op eq 'delete_confirmed' ) {
151
} elsif ( $op eq 'delete_confirmed' ) {
152
161
    my $itemtype = Koha::ItemTypes->find($itemtype_code);
153
    my $itemtype = Koha::ItemTypes->find($itemtype_code);
162
    my $deleted = eval { $itemtype->delete };
154
    my $deleted = eval { $itemtype->delete };
163
    if ( $@ or not $deleted ) {
155
    if ( $@ or not $deleted ) {
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt (-1 / +1 lines)
Lines 110-116 Item types administration Link Here
110
        [% CASE 'already_exists' %]
110
        [% CASE 'already_exists' %]
111
            This item type already exists.
111
            This item type already exists.
112
        [% CASE 'cannot_be_deleted' %]
112
        [% CASE 'cannot_be_deleted' %]
113
            Cannot delete this item type. <p><strong>This record is used [% m.total %] times</strong>. Deletion is not possible.</p>
113
            Cannot delete this item type. <p><strong>This record is in use</strong>. Deletion is not possible.</p>
114
        [% CASE %]
114
        [% CASE %]
115
            [% m.code %]
115
            [% m.code %]
116
        [% END %]
116
        [% END %]
(-)a/t/db_dependent/Koha/ItemTypes.t (-2 / +23 lines)
Lines 19-28 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 20;
22
use Test::More tests => 26;
23
use Data::Dumper;
23
use Data::Dumper;
24
use Koha::Database;
24
use Koha::Database;
25
use t::lib::Mocks;
25
use t::lib::Mocks;
26
use Koha::Items;
27
use Koha::Biblioitems;
28
use t::lib::TestBuilder;
26
29
27
BEGIN {
30
BEGIN {
28
    use_ok('Koha::ItemType');
31
    use_ok('Koha::ItemType');
Lines 126-129 is( Link Here
126
    'item types should be sorted by translated description'
129
    'item types should be sorted by translated description'
127
);
130
);
128
131
132
my $builder = t::lib::TestBuilder->new;
133
my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
134
135
is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted');
136
137
my $item = $builder->build_object({ class => 'Koha::Items', value => { itype => $item_type->itemtype }});
138
139
is( $item_type->can_be_deleted, 0, 'An item type that is used by an item cannot be deleted' );
140
141
my $biblio = $builder->build_object({ class => 'Koha::Biblioitems', value => { itemtype => $item_type->itemtype }});
142
143
is ( $item_type->can_be_deleted, 0, 'An item type that is used by an item and a biblioitem cannot be deleted' );
144
145
is ( $item->delete, 1, 'An item has been deleted' );
146
147
is ( $biblio->delete, 1, 'A biblioitem has been deleted' );
148
149
is ( $item_type->can_be_deleted, 1, 'The item type that was being used by the removed item and biblioitem can now be deleted' );
150
129
$schema->txn_rollback;
151
$schema->txn_rollback;
130
- 

Return to bug 17944