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

(-)a/Koha/ItemType.pm (+38 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 is_used
95
my $overalltotal = $ItemType->is_used($itemtype_code)
96
97
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.
98
99
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.
100
101
=cut
102
103
sub can_be_deleted {
104
    my ($self, $itemtype_code) = @_;
105
    my $schema = Koha::Database->new()->schema();
106
    my $itemtotal = Koha::Items->search({ 'itype' => $itemtype_code})->count;
107
    my $bibliototal = Koha::Biblioitems->search({ 'itemtype' => $itemtype_code})->count;
108
    my $overalltotal = $itemtotal + $bibliototal;
109
    return $overalltotal;
110
}
111
112
=head3 delete
113
114
=cut
115
116
sub delete{
117
    my ( $self, $itemtype ) = @_;
118
    my $delete_allowed = can_be_deleted($itemtype);
119
    if ($delete_allowed) {
120
        my $deleted = $self->SUPER::delete($self);
121
        logaction( 'ITEMTYPE', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
122
        if C4::Context->preference('ItemTypeLog');
123
        return $deleted;
124
    }  else {
125
            Koha::Exceptions::CannotDeleteDefault->throw('itemtype could not be deleted');
126
    }
127
}
128
129
92
=head3 type
130
=head3 type
93
131
94
=cut
132
=cut
(-)a/admin/itemtypes.pl (-16 / +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-156 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::ItemType->new();
143
    my ($total) = $dbh->selectrow_array( '
143
    my $overalltotal = Koha::ItemType->can_be_deleted($itemtype_code);
144
        SELECT COUNT(*) AS total FROM (
144
    if ($overalltotal) {
145
            SELECT itemtype AS t FROM biblioitems
145
        push @messages, { type => 'error', code => 'cannot_be_deleted', total => $overalltotal };
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
        my $itemtype = Koha::ItemTypes->find($itemtype_code);
Lines 158-165 if ( $op eq 'add_form' ) { Link Here
158
    }
150
    }
159
151
160
} elsif ( $op eq 'delete_confirmed' ) {
152
} elsif ( $op eq 'delete_confirmed' ) {
153
161
    my $itemtype = Koha::ItemTypes->find($itemtype_code);
154
    my $itemtype = Koha::ItemTypes->find($itemtype_code);
162
    my $deleted = eval { $itemtype->delete };
155
    my $deleted = eval { Koha::ItemType->delete($itemtype_code) };
163
    if ( $@ or not $deleted ) {
156
    if ( $@ or not $deleted ) {
164
        push @messages, { type => 'error', code => 'error_on_delete' };
157
        push @messages, { type => 'error', code => 'error_on_delete' };
165
    } else {
158
    } else {
(-)a/t/db_dependent/Koha/ItemTypes.t (-2 / +46 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 => 22;
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;
26
28
27
BEGIN {
29
BEGIN {
28
    use_ok('Koha::ItemType');
30
    use_ok('Koha::ItemType');
Lines 95-100 Koha::Localization->new( Link Here
95
    }
97
    }
96
)->store;
98
)->store;
97
99
100
Koha::Biblio->new(
101
    {
102
        biblionumber  => 1,
103
    }
104
)->store;
105
106
Koha::Biblioitem->new(
107
    {
108
        biblioitemnumber => 1,
109
        biblionumber     => 1,
110
        itemtype         => 'type1'
111
    }
112
)->store;
113
114
115
Koha::Item->new(
116
    {
117
        itemnumber => 1,
118
        biblionumber => 1,
119
        biblioitemnumber  =>1,
120
        itype        => 'type1',
121
        notforloan   => 1,
122
        itemlost     => 0,
123
    }
124
)->store;
125
126
98
my $type = Koha::ItemTypes->find('type1');
127
my $type = Koha::ItemTypes->find('type1');
99
ok( defined($type), 'first result' );
128
ok( defined($type), 'first result' );
100
is( $type->itemtype,       'type1',          'itemtype/code' );
129
is( $type->itemtype,       'type1',          'itemtype/code' );
Lines 126-129 is( Link Here
126
    'item types should be sorted by translated description'
155
    'item types should be sorted by translated description'
127
);
156
);
128
157
158
sub is_used {
159
    require Test::More;
160
    my $itemtype_code = shift;
161
    my $schema = Koha::Database->new()->schema();
162
    my $itemtotal = Koha::Items->search({ 'itype' => $itemtype_code })->count;
163
    my $bibliototal = Koha::Biblioitems->search({ 'itemtype' => $itemtype_code})->count;
164
    my $overalltotal = $itemtotal + $bibliototal;
165
    return $overalltotal;
166
}
167
168
my $passcount = is_used('type1');
169
is ( $passcount,2,'Exists in the database, test is successful');
170
171
my $failcount = is_used('type2');
172
is ( $failcount,0,'Doesnt exist in database, test is success');
173
129
$schema->txn_rollback;
174
$schema->txn_rollback;
130
- 

Return to bug 17944