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

(-)a/Koha/Biblio/ItemGroup.pm (+142 lines)
Line 0 Link Here
1
package Koha::Biblio::ItemGroup;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use base qw(Koha::Object);
21
22
use Koha::Biblio::ItemGroup::Items;
23
use Koha::Exceptions::Object;
24
use Koha::Items;
25
26
=head1 NAME
27
28
Koha::Biblio::ItemGroup - Koha ItemGroup Object class
29
30
=head1 API
31
32
=head2 Class methods
33
34
=head3 store
35
36
    $item_group->store;
37
38
Overloaded I<store> method that takes care of creation date handling.
39
40
=cut
41
42
sub store {
43
    my ($self) = @_;
44
45
    unless ( $self->in_storage ) {
46
        # new entry
47
        $self->set(
48
            {
49
                created_on => \'NOW()'
50
            }
51
        );
52
    }
53
54
    return $self->SUPER::store();
55
}
56
57
=head3 items
58
59
    my $items = $item_group->items;
60
61
Returns all the items linked to the item group.
62
63
=cut
64
65
sub items {
66
    my ($self) = @_;
67
68
    my $items_rs = $self->_result->item_group_items;
69
    my @item_ids = $items_rs->get_column('item_id')->all;
70
71
    return Koha::Items->new->empty unless @item_ids;
72
73
    return Koha::Items->search(
74
        {
75
            itemnumber => {
76
                -in => \@item_ids
77
            }
78
        }
79
    );
80
}
81
82
=head3 add_item
83
84
    $item_group->add_item({ item_id => $item_id });
85
86
=cut
87
88
sub add_item {
89
    my ($self, $params) = @_;
90
91
    my $item_id = $params->{item_id};
92
93
    my $item = Koha::Items->find( $item_id );
94
    unless ( $item->biblionumber == $self->biblio_id ) {
95
        Koha::Exceptions::Object::FKConstraint->throw(
96
            broken_fk => 'biblio_id'
97
        );
98
    }
99
100
    Koha::Biblio::ItemGroup::Item->new(
101
        {
102
            item_group_id => $self->id,
103
            item_id       => $item_id,
104
        }
105
    )->store;
106
107
    return $self;
108
}
109
110
=head3 to_api_mapping
111
112
This method returns the mapping for representing a Koha::Biblio::ItemGroup object
113
on the API.
114
115
=cut
116
117
sub to_api_mapping {
118
    return {
119
        created_on   => 'creation_date',
120
        updated_on   => 'modification_date'
121
    };
122
}
123
124
=head2 Internal methods
125
126
=head3 _type
127
128
=cut
129
130
sub _type {
131
    return 'ItemGroup';
132
}
133
134
=head3 object_class
135
136
=cut
137
138
sub object_class {
139
    return 'Koha::Biblio::ItemGroup';
140
}
141
142
1;
(-)a/Koha/Biblio/ItemGroup/Item.pm (+46 lines)
Line 0 Link Here
1
package Koha::Biblio::ItemGroup::Item;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use base qw(Koha::Object);
21
22
=head1 NAME
23
24
Koha::Biblio::ItemGroup::Item - Koha ItemGroup Item Object class
25
26
=head1 API
27
28
=head2 Internal methods
29
30
=head3 _type
31
32
=cut
33
34
sub _type {
35
    return 'ItemGroupItem';
36
}
37
38
=head3 object_class
39
40
=cut
41
42
sub object_class {
43
    return 'Koha::Biblio::ItemGroup::Item';
44
}
45
46
1;
(-)a/Koha/Biblio/ItemGroup/Items.pm (+48 lines)
Line 0 Link Here
1
package Koha::Biblio::ItemGroup::Items;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Koha::Biblio::ItemGroup::Item;
21
22
use base qw(Koha::Objects);
23
24
=head1 NAME
25
26
Koha::Biblio::ItemGroup::Items - Koha ItemGroup Items Object set class
27
28
=head1 API
29
30
=head2 Internal methods
31
32
=head3 _type
33
34
=cut
35
36
sub _type {
37
    return 'ItemGroupItem';
38
}
39
40
=head3 object_class
41
42
=cut
43
44
sub object_class {
45
    return 'Koha::Biblio::ItemGroup::Item';
46
}
47
48
1;
(-)a/Koha/Biblio/ItemGroups.pm (+48 lines)
Line 0 Link Here
1
package Koha::Biblio::ItemGroups;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Koha::Biblio::ItemGroup;
21
22
use base qw(Koha::Objects);
23
24
=head1 NAME
25
26
Koha::Biblio::ItemGroups - Koha ItemGroup Object set class
27
28
=head1 API
29
30
=head2 Internal methods
31
32
=head3 _type
33
34
=cut
35
36
sub _type {
37
    return 'ItemGroup';
38
}
39
40
=head3 object_class
41
42
=cut
43
44
sub object_class {
45
    return 'Koha::Biblio::ItemGroup';
46
}
47
48
1;
(-)a/t/db_dependent/Koha/Biblio/ItemGroups.t (-1 / +70 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 3;
21
22
use Koha::Database;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
BEGIN {
28
    use_ok('Koha::Biblio::ItemGroup');
29
    use_ok('Koha::Biblio::ItemGroups');
30
}
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
t::lib::Mocks::mock_preference('EnableItemGroups', 1);
36
37
subtest 'add_item() and items() tests' => sub {
38
39
    plan tests => 8;
40
41
    $schema->storage->txn_begin;
42
43
    my $biblio = $builder->build_sample_biblio();
44
    my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
45
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
46
47
    my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
48
49
    my $items = $item_group->items;
50
    is( $items->count, 0, 'Item group has no items');
51
52
    $item_group->add_item({ item_id => $item_1->id });
53
    my @items = $item_group->items->as_list();
54
    is( scalar(@items), 1, 'Item group has one item');
55
    is( $items[0]->id, $item_1->id, 'Item 1 is correct' );
56
57
    $item_group->add_item({ item_id => $item_2->id });
58
    @items = $item_group->items->as_list();
59
    is( scalar(@items), 2, 'Item group has two items');
60
    is( $items[0]->id, $item_1->id, 'Item 1 is correct' );
61
    is( $items[1]->id, $item_2->id, 'Item 2 is correct' );
62
63
    # Remove an item
64
    $item_1->delete;
65
    @items = $item_group->items->as_list();
66
    is( scalar(@items), 1, 'Item group now has only one item');
67
    is( $items[0]->id, $item_2->id, 'Item 2 is correct' );
68
69
    $schema->storage->txn_rollback;
70
};

Return to bug 24857