Bug 24857: Add Koha Objects
[koha.git] / Koha / Biblio / ItemGroup.pm
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;