Bug 14610 - Add and update modules
[koha.git] / Koha / Biblio.pm
1 package Koha::Biblio;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use C4::Biblio qw( GetRecordValue GetMarcBiblio GetFrameworkCode );
25
26 use Koha::Database;
27
28 use base qw(Koha::Object);
29
30 use C4::Circulation qw(GetIssuingRule);
31 use Koha::Items;
32 use Koha::Biblioitems;
33 use Koha::ArticleRequests;
34 use Koha::ArticleRequest::Status;
35
36 =head1 NAME
37
38 Koha::Biblio - Koha Biblio Object class
39
40 =head1 API
41
42 =head2 Class Methods
43
44 =cut
45
46 =head3 subtitles
47
48 my @subtitles = $biblio->subtitles();
49
50 Returns list of subtitles for a record.
51
52 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
53
54 =cut
55
56 sub subtitles {
57     my ( $self ) = @_;
58
59     return map { $_->{subfield} } @{ GetRecordValue( 'subtitle', GetMarcBiblio( $self->id ), $self->frameworkcode ) };
60 }
61
62 =head3 can_article_request
63
64 my $bool = $biblio->can_article_request( $borrower );
65
66 Returns true if article requests can be made for this record
67
68 $borrower must be a Koha::Patron object
69
70 =cut
71
72 sub can_article_request {
73     my ( $self, $borrower ) = @_;
74
75     my $rule = $self->article_request_type($borrower);
76     return q{} if $rule eq 'item_only' && !$self->items()->count();
77     return 1 if $rule && $rule ne 'no';
78
79     return q{};
80 }
81
82 =head3 article_request_type
83
84 my $type = $biblio->article_request_type( $borrower );
85
86 Returns the article request type based on items, or on the record
87 itself if there are no items.
88
89 $borrower must be a Koha::Patron object
90
91 =cut
92
93 sub article_request_type {
94     my ( $self, $borrower ) = @_;
95
96     return q{} unless $borrower;
97
98     my $rule = $self->article_request_type_for_items( $borrower );
99     return $rule if $rule;
100
101     # If the record has no items that are requestable, go by the record itemtype
102     $rule = $self->article_request_type_for_bib($borrower);
103     return $rule if $rule;
104
105     return q{};
106 }
107
108 =head3 article_request_type_for_bib
109
110 my $type = $biblio->article_request_type_for_bib
111
112 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
113
114 =cut
115
116 sub article_request_type_for_bib {
117     my ( $self, $borrower ) = @_;
118
119     return q{} unless $borrower;
120
121     my $borrowertype = $borrower->categorycode;
122     my $itemtype     = $self->itemtype();
123
124     my $rules        = C4::Circulation::GetIssuingRule( $borrowertype, $itemtype );
125
126     return $rules->{article_requests} || q{};
127 }
128
129 =head3 article_request_type_for_items
130
131 my $type = $biblio->article_request_type_for_items
132
133 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
134
135 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
136
137 =cut
138
139 sub article_request_type_for_items {
140     my ( $self, $borrower ) = @_;
141
142     my $counts;
143     foreach my $item ( $self->items()->as_list() ) {
144         my $rule = $item->article_request_type($borrower);
145         return $rule if $rule eq 'bib_only';    # we don't need to go any further
146         $counts->{$rule}++;
147     }
148
149     return 'item_only' if $counts->{item_only};
150     return 'yes'       if $counts->{yes};
151     return 'no'        if $counts->{no};
152     return q{};
153 }
154
155 =head3 article_requests
156
157 my @requests = $biblio->article_requests
158
159 Returns the article requests associated with this Biblio
160
161 =cut
162
163 sub article_requests {
164     my ( $self, $borrower ) = @_;
165
166     $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
167
168     return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
169 }
170
171 =head3 article_requests_current
172
173 my @requests = $biblio->article_requests_current
174
175 Returns the article requests associated with this Biblio that are incomplete
176
177 =cut
178
179 sub article_requests_current {
180     my ( $self, $borrower ) = @_;
181
182     $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
183         {
184             biblionumber => $self->biblionumber(),
185             -or          => [
186                 { status => Koha::ArticleRequest::Status::Pending },
187                 { status => Koha::ArticleRequest::Status::Processing }
188             ]
189         }
190     );
191
192     return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
193 }
194
195 =head3 article_requests_finished
196
197 my @requests = $biblio->article_requests_finished
198
199 Returns the article requests associated with this Biblio that are completed
200
201 =cut
202
203 sub article_requests_finished {
204     my ( $self, $borrower ) = @_;
205
206     $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
207         {
208             biblionumber => $self->biblionumber(),
209             -or          => [
210                 { status => Koha::ArticleRequest::Status::Completed },
211                 { status => Koha::ArticleRequest::Status::Canceled }
212             ]
213         }
214     );
215
216     return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
217 }
218
219 =head3 items
220
221 =head3 items
222
223 my @items = $biblio->items();
224 my $items = $biblio->items();
225
226 Returns the related Koha::Items object for this biblio in scalar context,
227 or list of Koha::Item objects in list context.
228
229 =cut
230
231 sub items {
232     my ($self) = @_;
233
234     $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
235
236     return wantarray ? $self->{_items}->as_list : $self->{_items};
237 }
238
239 =head3 itemtype
240
241 my $itemtype = $biblio->itemtype();
242
243 Returns the itemtype for this record.
244
245 =cut
246
247 sub itemtype {
248     my ( $self ) = @_;
249
250     return $self->_biblioitem()->itemtype();
251 }
252
253 =head3 _biblioitem
254
255 my $field = $self->_biblioitem()->itemtype
256
257 Returns the related Koha::Biblioitem object for this Biblio object
258
259 =cut
260
261 sub _biblioitem {
262     my ($self) = @_;
263
264     $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
265
266     return $self->{_biblioitem};
267 }
268
269 =head3 type
270
271 =cut
272
273 sub _type {
274     return 'Biblio';
275 }
276
277 =head1 AUTHOR
278
279 Kyle M Hall <kyle@bywatersolutions.com>
280
281 =cut
282
283 1;