Bug 18258: (QA followup) Use Koha::Subscriptions
[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 Koha::Items;
31 use Koha::Biblioitems;
32 use Koha::ArticleRequests;
33 use Koha::ArticleRequest::Status;
34 use Koha::IssuingRules;
35 use Koha::Subscriptions;
36
37 =head1 NAME
38
39 Koha::Biblio - Koha Biblio Object class
40
41 =head1 API
42
43 =head2 Class Methods
44
45 =cut
46
47 =head3 subtitles
48
49 my @subtitles = $biblio->subtitles();
50
51 Returns list of subtitles for a record.
52
53 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
54
55 =cut
56
57 sub subtitles {
58     my ( $self ) = @_;
59
60     return map { $_->{subfield} } @{ GetRecordValue( 'subtitle', GetMarcBiblio( $self->id ), $self->frameworkcode ) };
61 }
62
63 =head3 can_article_request
64
65 my $bool = $biblio->can_article_request( $borrower );
66
67 Returns true if article requests can be made for this record
68
69 $borrower must be a Koha::Patron object
70
71 =cut
72
73 sub can_article_request {
74     my ( $self, $borrower ) = @_;
75
76     my $rule = $self->article_request_type($borrower);
77     return q{} if $rule eq 'item_only' && !$self->items()->count();
78     return 1 if $rule && $rule ne 'no';
79
80     return q{};
81 }
82
83 =head3 article_request_type
84
85 my $type = $biblio->article_request_type( $borrower );
86
87 Returns the article request type based on items, or on the record
88 itself if there are no items.
89
90 $borrower must be a Koha::Patron object
91
92 =cut
93
94 sub article_request_type {
95     my ( $self, $borrower ) = @_;
96
97     return q{} unless $borrower;
98
99     my $rule = $self->article_request_type_for_items( $borrower );
100     return $rule if $rule;
101
102     # If the record has no items that are requestable, go by the record itemtype
103     $rule = $self->article_request_type_for_bib($borrower);
104     return $rule if $rule;
105
106     return q{};
107 }
108
109 =head3 article_request_type_for_bib
110
111 my $type = $biblio->article_request_type_for_bib
112
113 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
114
115 =cut
116
117 sub article_request_type_for_bib {
118     my ( $self, $borrower ) = @_;
119
120     return q{} unless $borrower;
121
122     my $borrowertype = $borrower->categorycode;
123     my $itemtype     = $self->itemtype();
124
125     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
126
127     return q{} unless $issuing_rule;
128     return $issuing_rule->article_requests || q{}
129 }
130
131 =head3 article_request_type_for_items
132
133 my $type = $biblio->article_request_type_for_items
134
135 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
136
137 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
138
139 =cut
140
141 sub article_request_type_for_items {
142     my ( $self, $borrower ) = @_;
143
144     my $counts;
145     foreach my $item ( $self->items()->as_list() ) {
146         my $rule = $item->article_request_type($borrower);
147         return $rule if $rule eq 'bib_only';    # we don't need to go any further
148         $counts->{$rule}++;
149     }
150
151     return 'item_only' if $counts->{item_only};
152     return 'yes'       if $counts->{yes};
153     return 'no'        if $counts->{no};
154     return q{};
155 }
156
157 =head3 article_requests
158
159 my @requests = $biblio->article_requests
160
161 Returns the article requests associated with this Biblio
162
163 =cut
164
165 sub article_requests {
166     my ( $self, $borrower ) = @_;
167
168     $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
169
170     return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
171 }
172
173 =head3 article_requests_current
174
175 my @requests = $biblio->article_requests_current
176
177 Returns the article requests associated with this Biblio that are incomplete
178
179 =cut
180
181 sub article_requests_current {
182     my ( $self, $borrower ) = @_;
183
184     $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
185         {
186             biblionumber => $self->biblionumber(),
187             -or          => [
188                 { status => Koha::ArticleRequest::Status::Pending },
189                 { status => Koha::ArticleRequest::Status::Processing }
190             ]
191         }
192     );
193
194     return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
195 }
196
197 =head3 article_requests_finished
198
199 my @requests = $biblio->article_requests_finished
200
201 Returns the article requests associated with this Biblio that are completed
202
203 =cut
204
205 sub article_requests_finished {
206     my ( $self, $borrower ) = @_;
207
208     $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
209         {
210             biblionumber => $self->biblionumber(),
211             -or          => [
212                 { status => Koha::ArticleRequest::Status::Completed },
213                 { status => Koha::ArticleRequest::Status::Canceled }
214             ]
215         }
216     );
217
218     return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
219 }
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 holds
254
255 my $holds = $biblio->holds();
256
257 return the current holds placed on this record
258
259 =cut
260
261 sub holds {
262     my ( $self ) = @_;
263
264     my $holds_rs = $self->_result->reserves;
265     return Koha::Holds->_new_from_dbic( $holds_rs );
266 }
267
268 =head3 biblioitem
269
270 my $field = $self->biblioitem()->itemtype
271
272 Returns the related Koha::Biblioitem object for this Biblio object
273
274 =cut
275
276 sub biblioitem {
277     my ($self) = @_;
278
279     $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
280
281     return $self->{_biblioitem};
282 }
283
284 =head3 subscriptions
285
286 my $subscriptions = $self->subscriptions
287
288 Returns the related Koha::Subscriptions object for this Biblio object
289
290 =cut
291
292 sub subscriptions {
293     my ($self) = @_;
294
295     $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
296
297     return $self->{_subscriptions};
298 }
299
300
301 =head3 type
302
303 =cut
304
305 sub _type {
306     return 'Biblio';
307 }
308
309 =head1 AUTHOR
310
311 Kyle M Hall <kyle@bywatersolutions.com>
312
313 =cut
314
315 1;