Bug 20421: (follow-up) Fix styling
[koha.git] / Koha / Biblio.pm
blob3cfce721921c4abcaebc67a224b092ad80cef298
1 package Koha::Biblio;
3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use Carp;
23 use List::MoreUtils qw(any);
25 use C4::Biblio qw();
27 use Koha::Database;
28 use Koha::DateUtils qw( dt_from_string );
30 use base qw(Koha::Object);
32 use Koha::ArticleRequest::Status;
33 use Koha::ArticleRequests;
34 use Koha::Biblio::Metadatas;
35 use Koha::Biblioitems;
36 use Koha::IssuingRules;
37 use Koha::Item::Transfer::Limits;
38 use Koha::Items;
39 use Koha::Libraries;
40 use Koha::Subscriptions;
42 =head1 NAME
44 Koha::Biblio - Koha Biblio Object class
46 =head1 API
48 =head2 Class Methods
50 =cut
52 =head3 store
54 Overloaded I<store> method to set default values
56 =cut
58 sub store {
59 my ( $self ) = @_;
61 $self->datecreated( dt_from_string ) unless $self->datecreated;
63 return $self->SUPER::store;
66 =head3 metadata
68 my $metadata = $biblio->metadata();
70 Returns a Koha::Biblio::Metadata object
72 =cut
74 sub metadata {
75 my ( $self ) = @_;
77 my $metadata = $self->_result->metadata;
78 return Koha::Biblio::Metadata->_new_from_dbic($metadata);
81 =head3 subtitles
83 my @subtitles = $biblio->subtitles();
85 Returns list of subtitles for a record.
87 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
89 =cut
91 sub subtitles {
92 my ( $self ) = @_;
94 return map { $_->{subfield} } @{
95 C4::Biblio::GetRecordValue(
96 'subtitle',
97 C4::Biblio::GetMarcBiblio({ biblionumber => $self->id }),
98 $self->frameworkcode ) };
101 =head3 can_article_request
103 my $bool = $biblio->can_article_request( $borrower );
105 Returns true if article requests can be made for this record
107 $borrower must be a Koha::Patron object
109 =cut
111 sub can_article_request {
112 my ( $self, $borrower ) = @_;
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';
118 return q{};
121 =head3 can_be_transferred
123 $biblio->can_be_transferred({ to => $to_library, from => $from_library })
125 Checks if at least one item of a biblio can be transferred to given library.
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
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().
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
142 Returns 1 if at least one of the item of a biblio can be transferred
143 to $to_library, otherwise 0.
145 =cut
147 sub can_be_transferred {
148 my ($self, $params) = @_;
150 my $to = $params->{to};
151 my $from = $params->{from};
153 return 1 unless C4::Context->preference('UseBranchTransferLimits');
154 my $limittype = C4::Context->preference('BranchTransferLimitsType');
156 my $items;
157 foreach my $item_of_bib ($self->items->as_list) {
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;
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;
186 return 0;
189 =head3 hidden_in_opac
191 my $bool = $biblio->hidden_in_opac({ [ rules => $rules ] })
193 Returns true if the biblio matches the hidding criteria defined in $rules.
194 Returns false otherwise.
196 Takes HASHref that can have the following parameters:
197 OPTIONAL PARAMETERS:
198 $rules : { <field> => [ value_1, ... ], ... }
200 Note: $rules inherits its structure from the parsed YAML from reading
201 the I<OpacHiddenItems> system preference.
203 =cut
205 sub hidden_in_opac {
206 my ( $self, $params ) = @_;
208 my $rules = $params->{rules} // {};
210 return !(any { !$_->hidden_in_opac({ rules => $rules }) } $self->items->as_list);
213 =head3 article_request_type
215 my $type = $biblio->article_request_type( $borrower );
217 Returns the article request type based on items, or on the record
218 itself if there are no items.
220 $borrower must be a Koha::Patron object
222 =cut
224 sub article_request_type {
225 my ( $self, $borrower ) = @_;
227 return q{} unless $borrower;
229 my $rule = $self->article_request_type_for_items( $borrower );
230 return $rule if $rule;
232 # If the record has no items that are requestable, go by the record itemtype
233 $rule = $self->article_request_type_for_bib($borrower);
234 return $rule if $rule;
236 return q{};
239 =head3 article_request_type_for_bib
241 my $type = $biblio->article_request_type_for_bib
243 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
245 =cut
247 sub article_request_type_for_bib {
248 my ( $self, $borrower ) = @_;
250 return q{} unless $borrower;
252 my $borrowertype = $borrower->categorycode;
253 my $itemtype = $self->itemtype();
255 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
257 return q{} unless $issuing_rule;
258 return $issuing_rule->article_requests || q{}
261 =head3 article_request_type_for_items
263 my $type = $biblio->article_request_type_for_items
265 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
267 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
269 =cut
271 sub article_request_type_for_items {
272 my ( $self, $borrower ) = @_;
274 my $counts;
275 foreach my $item ( $self->items()->as_list() ) {
276 my $rule = $item->article_request_type($borrower);
277 return $rule if $rule eq 'bib_only'; # we don't need to go any further
278 $counts->{$rule}++;
281 return 'item_only' if $counts->{item_only};
282 return 'yes' if $counts->{yes};
283 return 'no' if $counts->{no};
284 return q{};
287 =head3 article_requests
289 my @requests = $biblio->article_requests
291 Returns the article requests associated with this Biblio
293 =cut
295 sub article_requests {
296 my ( $self, $borrower ) = @_;
298 $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
300 return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
303 =head3 article_requests_current
305 my @requests = $biblio->article_requests_current
307 Returns the article requests associated with this Biblio that are incomplete
309 =cut
311 sub article_requests_current {
312 my ( $self, $borrower ) = @_;
314 $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
316 biblionumber => $self->biblionumber(),
317 -or => [
318 { status => Koha::ArticleRequest::Status::Pending },
319 { status => Koha::ArticleRequest::Status::Processing }
324 return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
327 =head3 article_requests_finished
329 my @requests = $biblio->article_requests_finished
331 Returns the article requests associated with this Biblio that are completed
333 =cut
335 sub article_requests_finished {
336 my ( $self, $borrower ) = @_;
338 $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
340 biblionumber => $self->biblionumber(),
341 -or => [
342 { status => Koha::ArticleRequest::Status::Completed },
343 { status => Koha::ArticleRequest::Status::Canceled }
348 return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
351 =head3 items
353 my $items = $biblio->items();
355 Returns the related Koha::Items object for this biblio
357 =cut
359 sub items {
360 my ($self) = @_;
362 my $items_rs = $self->_result->items;
364 return Koha::Items->_new_from_dbic( $items_rs );
367 =head3 itemtype
369 my $itemtype = $biblio->itemtype();
371 Returns the itemtype for this record.
373 =cut
375 sub itemtype {
376 my ( $self ) = @_;
378 return $self->biblioitem()->itemtype();
381 =head3 holds
383 my $holds = $biblio->holds();
385 return the current holds placed on this record
387 =cut
389 sub holds {
390 my ( $self, $params, $attributes ) = @_;
391 $attributes->{order_by} = 'priority' unless exists $attributes->{order_by};
392 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
393 return Koha::Holds->_new_from_dbic($hold_rs);
396 =head3 current_holds
398 my $holds = $biblio->current_holds
400 Return the holds placed on this bibliographic record.
401 It does not include future holds.
403 =cut
405 sub current_holds {
406 my ($self) = @_;
407 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
408 return $self->holds(
409 { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
412 =head3 biblioitem
414 my $field = $self->biblioitem()->itemtype
416 Returns the related Koha::Biblioitem object for this Biblio object
418 =cut
420 sub biblioitem {
421 my ($self) = @_;
423 $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
425 return $self->{_biblioitem};
428 =head3 subscriptions
430 my $subscriptions = $self->subscriptions
432 Returns the related Koha::Subscriptions object for this Biblio object
434 =cut
436 sub subscriptions {
437 my ($self) = @_;
439 $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
441 return $self->{_subscriptions};
444 =head3 has_items_waiting_or_intransit
446 my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
448 Tells if this bibliographic record has items waiting or in transit.
450 =cut
452 sub has_items_waiting_or_intransit {
453 my ( $self ) = @_;
455 if ( Koha::Holds->search({ biblionumber => $self->id,
456 found => ['W', 'T'] })->count ) {
457 return 1;
460 foreach my $item ( $self->items->as_list ) {
461 return 1 if $item->get_transfer;
464 return 0;
467 =head3 type
469 =cut
471 sub _type {
472 return 'Biblio';
475 =head1 AUTHOR
477 Kyle M Hall <kyle@bywatersolutions.com>
479 =cut