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

(-)a/Koha/ItemType.pm (-5 / +10 lines)
Lines 82-96 sub translated_descriptions { Link Here
82
}
82
}
83
83
84
84
85
=head3 get_items_and_biblioitems
85
=head3 is_used 
86
my $overalltotal = $ItemType->is_used($itemtype_code)
87
88
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.
89
90
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.
86
91
87
=cut
92
=cut
88
93
89
sub get_items_and_biblioitems {
94
sub is_used {
90
    my $itemtype_code = $_[1];
95
    my ($self, $itemtype_code) = @_;
91
    my $schema = Koha::Database->new()->schema();
96
    my $schema = Koha::Database->new()->schema();
92
    my $itemtotal = $schema->resultset('Item')->search({ 'itype' => $itemtype_code})->count;
97
    my $itemtotal = Koha::Items->search({ 'itype' => $itemtype_code})->count;
93
    my $bibliototal = $schema->resultset('Biblioitem')->search({ 'itemtype' => $itemtype_code})->count;
98
    my $bibliototal = Koha::Biblioitems->search({ 'itemtype' => $itemtype_code})->count;
94
    my $overalltotal = $itemtotal + $bibliototal;
99
    my $overalltotal = $itemtotal + $bibliototal;
95
    return $overalltotal;
100
    return $overalltotal;
96
}
101
}
(-)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 / +43 lines)
Lines 22-27 use Modern::Perl; Link Here
22
use Test::More tests => 18;
22
use Test::More tests => 18;
23
use Data::Dumper;
23
use Data::Dumper;
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::Items;
26
use Koha::Biblioitems;
25
27
26
BEGIN {
28
BEGIN {
27
    use_ok('Koha::ItemType');
29
    use_ok('Koha::ItemType');
Lines 56-61 Koha::ItemType->new( Link Here
56
    }
58
    }
57
)->store;
59
)->store;
58
60
61
Koha::Biblio->new(
62
    {
63
        biblionumber  => 1,
64
    }
65
)->store;
66
67
Koha::Biblioitem->new(
68
    {
69
        biblioitemnumber => 1,
70
        biblionumber     => 1,
71
        itemtype         => 'type1'
72
    }
73
)->store;
74
75
76
Koha::Item->new(
77
    {
78
        itemnumber => 1,
79
        biblionumber => 1,
80
        biblioitemnumber  =>1,
81
        itype        => 'type2',
82
        notforloan   => 1,
83
        itemlost     => 0, 
84
    }
85
)->store;
86
87
59
my $type = Koha::ItemTypes->find('type1');
88
my $type = Koha::ItemTypes->find('type1');
60
ok( defined($type), 'first result' );
89
ok( defined($type), 'first result' );
61
is( $type->itemtype,       'type1',          'itemtype/code' );
90
is( $type->itemtype,       'type1',          'itemtype/code' );
Lines 76-79 is( $type->summary, 'summary', 'summary' ); Link Here
76
is( $type->checkinmsg,     'checkinmsg',     'checkinmsg' );
105
is( $type->checkinmsg,     'checkinmsg',     'checkinmsg' );
77
is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
106
is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
78
107
108
109
subtest is_used => sub{
110
    plan test => 2;
111
    my $itemtype_code = shift;
112
    my $schema = Koha::Database->new()->schema();
113
    my $itemtotal = Koha::Items->search({ 'itype' => $itemtype_code })->count;
114
    my $bibliototal = Koha::Biblioitems->search({ 'itemtype' => $itemtype_code})->count;
115
    my $overalltotal = $itemtotal + $bibliototal;
116
    return $overalltotal;
117
}
118
for my $itemtype_code ('type1', 'type 2') {
119
        subtest "is_used $itemtype_code", \&is_used, $range;
120
}
121
79
$schema->txn_rollback;
122
$schema->txn_rollback;
80
- 

Return to bug 17944