Bug 4319: [OPAC] Allow holds on waiting/transit items
[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();
25
26 use Koha::Database;
27 use Koha::DateUtils qw( dt_from_string );
28
29 use base qw(Koha::Object);
30
31 use Koha::Items;
32 use Koha::Biblioitems;
33 use Koha::ArticleRequests;
34 use Koha::ArticleRequest::Status;
35 use Koha::IssuingRules;
36 use Koha::Subscriptions;
37
38 =head1 NAME
39
40 Koha::Biblio - Koha Biblio Object class
41
42 =head1 API
43
44 =head2 Class Methods
45
46 =cut
47
48 =head3 subtitles
49
50 my @subtitles = $biblio->subtitles();
51
52 Returns list of subtitles for a record.
53
54 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
55
56 =cut
57
58 sub subtitles {
59     my ( $self ) = @_;
60
61     return map { $_->{subfield} } @{
62         C4::Biblio::GetRecordValue(
63             'subtitle',
64             C4::Biblio::GetMarcBiblio({ biblionumber => $self->id }),
65             $self->frameworkcode ) };
66 }
67
68 =head3 can_article_request
69
70 my $bool = $biblio->can_article_request( $borrower );
71
72 Returns true if article requests can be made for this record
73
74 $borrower must be a Koha::Patron object
75
76 =cut
77
78 sub can_article_request {
79     my ( $self, $borrower ) = @_;
80
81     my $rule = $self->article_request_type($borrower);
82     return q{} if $rule eq 'item_only' && !$self->items()->count();
83     return 1 if $rule && $rule ne 'no';
84
85     return q{};
86 }
87
88 =head3 article_request_type
89
90 my $type = $biblio->article_request_type( $borrower );
91
92 Returns the article request type based on items, or on the record
93 itself if there are no items.
94
95 $borrower must be a Koha::Patron object
96
97 =cut
98
99 sub article_request_type {
100     my ( $self, $borrower ) = @_;
101
102     return q{} unless $borrower;
103
104     my $rule = $self->article_request_type_for_items( $borrower );
105     return $rule if $rule;
106
107     # If the record has no items that are requestable, go by the record itemtype
108     $rule = $self->article_request_type_for_bib($borrower);
109     return $rule if $rule;
110
111     return q{};
112 }
113
114 =head3 article_request_type_for_bib
115
116 my $type = $biblio->article_request_type_for_bib
117
118 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
119
120 =cut
121
122 sub article_request_type_for_bib {
123     my ( $self, $borrower ) = @_;
124
125     return q{} unless $borrower;
126
127     my $borrowertype = $borrower->categorycode;
128     my $itemtype     = $self->itemtype();
129
130     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
131
132     return q{} unless $issuing_rule;
133     return $issuing_rule->article_requests || q{}
134 }
135
136 =head3 article_request_type_for_items
137
138 my $type = $biblio->article_request_type_for_items
139
140 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
141
142 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
143
144 =cut
145
146 sub article_request_type_for_items {
147     my ( $self, $borrower ) = @_;
148
149     my $counts;
150     foreach my $item ( $self->items()->as_list() ) {
151         my $rule = $item->article_request_type($borrower);
152         return $rule if $rule eq 'bib_only';    # we don't need to go any further
153         $counts->{$rule}++;
154     }
155
156     return 'item_only' if $counts->{item_only};
157     return 'yes'       if $counts->{yes};
158     return 'no'        if $counts->{no};
159     return q{};
160 }
161
162 =head3 article_requests
163
164 my @requests = $biblio->article_requests
165
166 Returns the article requests associated with this Biblio
167
168 =cut
169
170 sub article_requests {
171     my ( $self, $borrower ) = @_;
172
173     $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
174
175     return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
176 }
177
178 =head3 article_requests_current
179
180 my @requests = $biblio->article_requests_current
181
182 Returns the article requests associated with this Biblio that are incomplete
183
184 =cut
185
186 sub article_requests_current {
187     my ( $self, $borrower ) = @_;
188
189     $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
190         {
191             biblionumber => $self->biblionumber(),
192             -or          => [
193                 { status => Koha::ArticleRequest::Status::Pending },
194                 { status => Koha::ArticleRequest::Status::Processing }
195             ]
196         }
197     );
198
199     return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
200 }
201
202 =head3 article_requests_finished
203
204 my @requests = $biblio->article_requests_finished
205
206 Returns the article requests associated with this Biblio that are completed
207
208 =cut
209
210 sub article_requests_finished {
211     my ( $self, $borrower ) = @_;
212
213     $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
214         {
215             biblionumber => $self->biblionumber(),
216             -or          => [
217                 { status => Koha::ArticleRequest::Status::Completed },
218                 { status => Koha::ArticleRequest::Status::Canceled }
219             ]
220         }
221     );
222
223     return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
224 }
225
226 =head3 items
227
228 my @items = $biblio->items();
229 my $items = $biblio->items();
230
231 Returns the related Koha::Items object for this biblio in scalar context,
232 or list of Koha::Item objects in list context.
233
234 =cut
235
236 sub items {
237     my ($self) = @_;
238
239     $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
240
241     return wantarray ? $self->{_items}->as_list : $self->{_items};
242 }
243
244 =head3 itemtype
245
246 my $itemtype = $biblio->itemtype();
247
248 Returns the itemtype for this record.
249
250 =cut
251
252 sub itemtype {
253     my ( $self ) = @_;
254
255     return $self->biblioitem()->itemtype();
256 }
257
258 =head3 holds
259
260 my $holds = $biblio->holds();
261
262 return the current holds placed on this record
263
264 =cut
265
266 sub holds {
267     my ( $self, $params, $attributes ) = @_;
268     $attributes->{order_by} = 'priority' unless exists $attributes->{order_by};
269     my $hold_rs = $self->_result->reserves->search( $params, $attributes );
270     return Koha::Holds->_new_from_dbic($hold_rs);
271 }
272
273 =head3 current_holds
274
275 my $holds = $biblio->current_holds
276
277 Return the holds placed on this bibliographic record.
278 It does not include future holds.
279
280 =cut
281
282 sub current_holds {
283     my ($self) = @_;
284     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
285     return $self->holds(
286         { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
287 }
288
289 =head3 biblioitem
290
291 my $field = $self->biblioitem()->itemtype
292
293 Returns the related Koha::Biblioitem object for this Biblio object
294
295 =cut
296
297 sub biblioitem {
298     my ($self) = @_;
299
300     $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
301
302     return $self->{_biblioitem};
303 }
304
305 =head3 subscriptions
306
307 my $subscriptions = $self->subscriptions
308
309 Returns the related Koha::Subscriptions object for this Biblio object
310
311 =cut
312
313 sub subscriptions {
314     my ($self) = @_;
315
316     $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
317
318     return $self->{_subscriptions};
319 }
320
321 =head3 hasItemswaitingOrInTransit
322
323 =cut
324
325 sub hasItemswaitingOrInTransit {
326     my ( $self ) = @_;
327
328     if ( Koha::Holds->search({ biblionumber => $self->id,
329                                found => ['W', 'T'] })->count ) {
330         return 1;
331     }
332
333     foreach my $item ( $self->items ) {
334         return 1 if $item->get_transfer;
335     }
336
337     return 0;
338 }
339
340 =head3 type
341
342 =cut
343
344 sub _type {
345     return 'Biblio';
346 }
347
348 =head1 AUTHOR
349
350 Kyle M Hall <kyle@bywatersolutions.com>
351
352 =cut
353
354 1;