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

(-)a/Koha/ItemType.pm (-5 / +10 lines)
Lines 90-104 sub translated_descriptions { Link Here
90
}
90
}
91
91
92
92
93
=head3 get_items_and_biblioitems
93
=head3 is_used
94
my $overalltotal = $ItemType->is_used($itemtype_code)
95
96
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.
97
98
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.
94
99
95
=cut
100
=cut
96
101
97
sub get_items_and_biblioitems {
102
sub is_used {
98
    my $itemtype_code = $_[1];
103
    my ($self, $itemtype_code) = @_;
99
    my $schema = Koha::Database->new()->schema();
104
    my $schema = Koha::Database->new()->schema();
100
    my $itemtotal = $schema->resultset('Item')->search({ 'itype' => $itemtype_code})->count;
105
    my $itemtotal = Koha::Items->search({ 'itype' => $itemtype_code})->count;
101
    my $bibliototal = $schema->resultset('Biblioitem')->search({ 'itemtype' => $itemtype_code})->count;
106
    my $bibliototal = Koha::Biblioitems->search({ 'itemtype' => $itemtype_code})->count;
102
    my $overalltotal = $itemtotal + $bibliototal;
107
    my $overalltotal = $itemtotal + $bibliototal;
103
    return $overalltotal;
108
    return $overalltotal;
104
}
109
}
(-)a/admin/itemtypes.pl (-2 / +2 lines)
Lines 139-146 if ( $op eq 'add_form' ) { Link Here
139
    $op          = 'list';
139
    $op          = 'list';
140
140
141
 } elsif ( $op eq 'delete_confirm' ) {
141
 } elsif ( $op eq 'delete_confirm' ) {
142
142
    my $ItemType = Koha::ItemType->new();
143
    my $overalltotal = Koha::ItemType->get_items_and_biblioitems($itemtype_code);
143
    my $overalltotal = $ItemType->is_used($itemtype_code);
144
    if ($overalltotal) {
144
    if ($overalltotal) {
145
        push @messages, { type => 'error', code => 'cannot_be_deleted', total => $overalltotal };
145
        push @messages, { type => 'error', code => 'cannot_be_deleted', total => $overalltotal };
146
        $op = 'list';
146
        $op = 'list';
(-)a/t/db_dependent/Koha/ItemTypes.t (-1 / +42 lines)
Lines 23-28 use Test::More tests => 20; Link Here
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        => 'type2',
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
subtest is_used => sub{
159
    plan test => 2;
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
for my $itemtype_code ('type1', 'type 2') {
168
        subtest "is_used $itemtype_code", \&is_used, $range;
169
}
170
129
$schema->txn_rollback;
171
$schema->txn_rollback;
130
- 

Return to bug 17944