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