Bug 18072: Add Koha::Biblio->can_be_transferred
[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::Item::Transfer::Limits;
37 use Koha::Libraries;
38 use Koha::Subscriptions;
39
40 use Koha::Exceptions::Library;
41
42 =head1 NAME
43
44 Koha::Biblio - Koha Biblio Object class
45
46 =head1 API
47
48 =head2 Class Methods
49
50 =cut
51
52 =head3 store
53
54 Overloaded I<store> method to set default values
55
56 =cut
57
58 sub store {
59     my ( $self ) = @_;
60
61     $self->datecreated( dt_from_string ) unless $self->datecreated;
62
63     return $self->SUPER::store;
64 }
65
66 =head3 subtitles
67
68 my @subtitles = $biblio->subtitles();
69
70 Returns list of subtitles for a record.
71
72 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
73
74 =cut
75
76 sub subtitles {
77     my ( $self ) = @_;
78
79     return map { $_->{subfield} } @{
80         C4::Biblio::GetRecordValue(
81             'subtitle',
82             C4::Biblio::GetMarcBiblio({ biblionumber => $self->id }),
83             $self->frameworkcode ) };
84 }
85
86 =head3 can_article_request
87
88 my $bool = $biblio->can_article_request( $borrower );
89
90 Returns true if article requests can be made for this record
91
92 $borrower must be a Koha::Patron object
93
94 =cut
95
96 sub can_article_request {
97     my ( $self, $borrower ) = @_;
98
99     my $rule = $self->article_request_type($borrower);
100     return q{} if $rule eq 'item_only' && !$self->items()->count();
101     return 1 if $rule && $rule ne 'no';
102
103     return q{};
104 }
105
106 =head3 can_be_transferred
107
108 $biblio->can_be_transferred({ to => $to_library, from => $from_library })
109
110 Checks if at least one item of a biblio can be transferred to given library.
111
112 This feature is controlled by two system preferences:
113 UseBranchTransferLimits to enable / disable the feature
114 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
115                          for setting the limitations
116
117 Performance-wise, it is recommended to use this method for a biblio instead of
118 iterating each item of a biblio with Koha::Item->can_be_transferred().
119
120 Takes HASHref that can have the following parameters:
121     MANDATORY PARAMETERS:
122     $to   : Koha::Library or branchcode string
123     OPTIONAL PARAMETERS:
124     $from : Koha::Library or branchcode string  # if given, only items from that
125                                                 # holdingbranch are considered
126
127 Returns 1 if at least one of the item of a biblio can be transferred
128 to $to_library, otherwise 0.
129
130 =cut
131
132 sub can_be_transferred {
133     my ($self, $params) = @_;
134
135     my $to = $params->{'to'};
136     my $from = $params->{'from'};
137     if (ref($to) ne 'Koha::Library') {
138         my $tobranchcode = defined $to ? $to : '';
139         $to = Koha::Libraries->find($tobranchcode);
140         unless ($to) {
141             Koha::Exceptions::Library::NotFound->throw(
142                 error => "Library '$tobranchcode' not found.",
143             );
144         }
145     }
146     if ($from && ref($from) ne 'Koha::Library') {
147         my $frombranchcode = defined $from ? $from : '';
148         $from = Koha::Libraries->find($frombranchcode);
149         unless ($from) {
150             Koha::Exceptions::Library::NotFound->throw(
151                 error => "Library '$frombranchcode' not found.",
152             );
153         }
154     }
155
156     return 1 unless C4::Context->preference('UseBranchTransferLimits');
157     my $limittype = C4::Context->preference('BranchTransferLimitsType');
158
159     my $items;
160     foreach my $item_of_bib ($self->items) {
161         next unless $item_of_bib->holdingbranch;
162         next if $from && $from->branchcode ne $item_of_bib->holdingbranch;
163         return 1 if $item_of_bib->holdingbranch eq $to->branchcode;
164         my $code = $limittype eq 'itemtype'
165             ? $item_of_bib->effective_itemtype
166             : $item_of_bib->ccode;
167         return 1 unless $code;
168         $items->{$code}->{$item_of_bib->holdingbranch} = 1;
169     }
170
171     # At this point we will have a HASHref containing each itemtype/ccode that
172     # this biblio has, inside which are all of the holdingbranches where those
173     # items are located at. Then, we will query Koha::Item::Transfer::Limits to
174     # find out whether a transfer limits for such $limittype from any of the
175     # listed holdingbranches to the given $to library exist. If at least one
176     # holdingbranch for that $limittype does not have a transfer limit to given
177     # $to library, then we know that the transfer is possible.
178     foreach my $code (keys %{$items}) {
179         my @holdingbranches = keys %{$items->{$code}};
180         return 1 if Koha::Item::Transfer::Limits->search({
181             toBranch => $to->branchcode,
182             fromBranch => { 'in' => \@holdingbranches },
183             $limittype => $code
184         }, {
185             group_by => [qw/fromBranch/]
186         })->count == scalar(@holdingbranches) ? 0 : 1;
187     }
188
189     return 0;
190 }
191
192 =head3 article_request_type
193
194 my $type = $biblio->article_request_type( $borrower );
195
196 Returns the article request type based on items, or on the record
197 itself if there are no items.
198
199 $borrower must be a Koha::Patron object
200
201 =cut
202
203 sub article_request_type {
204     my ( $self, $borrower ) = @_;
205
206     return q{} unless $borrower;
207
208     my $rule = $self->article_request_type_for_items( $borrower );
209     return $rule if $rule;
210
211     # If the record has no items that are requestable, go by the record itemtype
212     $rule = $self->article_request_type_for_bib($borrower);
213     return $rule if $rule;
214
215     return q{};
216 }
217
218 =head3 article_request_type_for_bib
219
220 my $type = $biblio->article_request_type_for_bib
221
222 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
223
224 =cut
225
226 sub article_request_type_for_bib {
227     my ( $self, $borrower ) = @_;
228
229     return q{} unless $borrower;
230
231     my $borrowertype = $borrower->categorycode;
232     my $itemtype     = $self->itemtype();
233
234     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
235
236     return q{} unless $issuing_rule;
237     return $issuing_rule->article_requests || q{}
238 }
239
240 =head3 article_request_type_for_items
241
242 my $type = $biblio->article_request_type_for_items
243
244 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
245
246 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
247
248 =cut
249
250 sub article_request_type_for_items {
251     my ( $self, $borrower ) = @_;
252
253     my $counts;
254     foreach my $item ( $self->items()->as_list() ) {
255         my $rule = $item->article_request_type($borrower);
256         return $rule if $rule eq 'bib_only';    # we don't need to go any further
257         $counts->{$rule}++;
258     }
259
260     return 'item_only' if $counts->{item_only};
261     return 'yes'       if $counts->{yes};
262     return 'no'        if $counts->{no};
263     return q{};
264 }
265
266 =head3 article_requests
267
268 my @requests = $biblio->article_requests
269
270 Returns the article requests associated with this Biblio
271
272 =cut
273
274 sub article_requests {
275     my ( $self, $borrower ) = @_;
276
277     $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
278
279     return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
280 }
281
282 =head3 article_requests_current
283
284 my @requests = $biblio->article_requests_current
285
286 Returns the article requests associated with this Biblio that are incomplete
287
288 =cut
289
290 sub article_requests_current {
291     my ( $self, $borrower ) = @_;
292
293     $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
294         {
295             biblionumber => $self->biblionumber(),
296             -or          => [
297                 { status => Koha::ArticleRequest::Status::Pending },
298                 { status => Koha::ArticleRequest::Status::Processing }
299             ]
300         }
301     );
302
303     return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
304 }
305
306 =head3 article_requests_finished
307
308 my @requests = $biblio->article_requests_finished
309
310 Returns the article requests associated with this Biblio that are completed
311
312 =cut
313
314 sub article_requests_finished {
315     my ( $self, $borrower ) = @_;
316
317     $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
318         {
319             biblionumber => $self->biblionumber(),
320             -or          => [
321                 { status => Koha::ArticleRequest::Status::Completed },
322                 { status => Koha::ArticleRequest::Status::Canceled }
323             ]
324         }
325     );
326
327     return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
328 }
329
330 =head3 items
331
332 my @items = $biblio->items();
333 my $items = $biblio->items();
334
335 Returns the related Koha::Items object for this biblio in scalar context,
336 or list of Koha::Item objects in list context.
337
338 =cut
339
340 sub items {
341     my ($self) = @_;
342
343     $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
344
345     return wantarray ? $self->{_items}->as_list : $self->{_items};
346 }
347
348 =head3 itemtype
349
350 my $itemtype = $biblio->itemtype();
351
352 Returns the itemtype for this record.
353
354 =cut
355
356 sub itemtype {
357     my ( $self ) = @_;
358
359     return $self->biblioitem()->itemtype();
360 }
361
362 =head3 holds
363
364 my $holds = $biblio->holds();
365
366 return the current holds placed on this record
367
368 =cut
369
370 sub holds {
371     my ( $self, $params, $attributes ) = @_;
372     $attributes->{order_by} = 'priority' unless exists $attributes->{order_by};
373     my $hold_rs = $self->_result->reserves->search( $params, $attributes );
374     return Koha::Holds->_new_from_dbic($hold_rs);
375 }
376
377 =head3 current_holds
378
379 my $holds = $biblio->current_holds
380
381 Return the holds placed on this bibliographic record.
382 It does not include future holds.
383
384 =cut
385
386 sub current_holds {
387     my ($self) = @_;
388     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
389     return $self->holds(
390         { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
391 }
392
393 =head3 biblioitem
394
395 my $field = $self->biblioitem()->itemtype
396
397 Returns the related Koha::Biblioitem object for this Biblio object
398
399 =cut
400
401 sub biblioitem {
402     my ($self) = @_;
403
404     $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
405
406     return $self->{_biblioitem};
407 }
408
409 =head3 subscriptions
410
411 my $subscriptions = $self->subscriptions
412
413 Returns the related Koha::Subscriptions object for this Biblio object
414
415 =cut
416
417 sub subscriptions {
418     my ($self) = @_;
419
420     $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
421
422     return $self->{_subscriptions};
423 }
424
425 =head3 has_items_waiting_or_intransit
426
427 my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
428
429 Tells if this bibliographic record has items waiting or in transit.
430
431 =cut
432
433 sub has_items_waiting_or_intransit {
434     my ( $self ) = @_;
435
436     if ( Koha::Holds->search({ biblionumber => $self->id,
437                                found => ['W', 'T'] })->count ) {
438         return 1;
439     }
440
441     foreach my $item ( $self->items ) {
442         return 1 if $item->get_transfer;
443     }
444
445     return 0;
446 }
447
448 =head3 type
449
450 =cut
451
452 sub _type {
453     return 'Biblio';
454 }
455
456 =head1 AUTHOR
457
458 Kyle M Hall <kyle@bywatersolutions.com>
459
460 =cut
461
462 1;