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