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
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.
23 use List::MoreUtils qw(any);
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;
40 use Koha::Subscriptions;
44 Koha::Biblio - Koha Biblio Object class
54 Overloaded I<store> method to set default values
61 $self->datecreated( dt_from_string ) unless $self->datecreated;
63 return $self->SUPER::store;
68 my $metadata = $biblio->metadata();
70 Returns a Koha::Biblio::Metadata object
77 $self->{_metadata} ||= Koha::Biblio::Metadatas->find( { biblionumber => $self->id } );
79 return $self->{_metadata};
84 my @subtitles = $biblio->subtitles();
86 Returns list of subtitles for a record.
88 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
95 return map { $_->{subfield} } @{
96 C4::Biblio::GetRecordValue(
98 C4::Biblio::GetMarcBiblio({ biblionumber => $self->id }),
99 $self->frameworkcode ) };
102 =head3 can_article_request
104 my $bool = $biblio->can_article_request( $borrower );
106 Returns true if article requests can be made for this record
108 $borrower must be a Koha::Patron object
112 sub can_article_request {
113 my ( $self, $borrower ) = @_;
115 my $rule = $self->article_request_type($borrower);
116 return q{} if $rule eq 'item_only' && !$self->items()->count();
117 return 1 if $rule && $rule ne 'no';
122 =head3 can_be_transferred
124 $biblio->can_be_transferred({ to => $to_library, from => $from_library })
126 Checks if at least one item of a biblio can be transferred to given library.
128 This feature is controlled by two system preferences:
129 UseBranchTransferLimits to enable / disable the feature
130 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
131 for setting the limitations
133 Performance-wise, it is recommended to use this method for a biblio instead of
134 iterating each item of a biblio with Koha::Item->can_be_transferred().
136 Takes HASHref that can have the following parameters:
137 MANDATORY PARAMETERS:
140 $from : Koha::Library # if given, only items from that
141 # holdingbranch are considered
143 Returns 1 if at least one of the item of a biblio can be transferred
144 to $to_library, otherwise 0.
148 sub can_be_transferred {
149 my ($self, $params) = @_;
151 my $to = $params->{to};
152 my $from = $params->{from};
154 return 1 unless C4::Context->preference('UseBranchTransferLimits');
155 my $limittype = C4::Context->preference('BranchTransferLimitsType');
158 foreach my $item_of_bib ($self->items) {
159 next unless $item_of_bib->holdingbranch;
160 next if $from && $from->branchcode ne $item_of_bib->holdingbranch;
161 return 1 if $item_of_bib->holdingbranch eq $to->branchcode;
162 my $code = $limittype eq 'itemtype'
163 ? $item_of_bib->effective_itemtype
164 : $item_of_bib->ccode;
165 return 1 unless $code;
166 $items->{$code}->{$item_of_bib->holdingbranch} = 1;
169 # At this point we will have a HASHref containing each itemtype/ccode that
170 # this biblio has, inside which are all of the holdingbranches where those
171 # items are located at. Then, we will query Koha::Item::Transfer::Limits to
172 # find out whether a transfer limits for such $limittype from any of the
173 # listed holdingbranches to the given $to library exist. If at least one
174 # holdingbranch for that $limittype does not have a transfer limit to given
175 # $to library, then we know that the transfer is possible.
176 foreach my $code (keys %{$items}) {
177 my @holdingbranches = keys %{$items->{$code}};
178 return 1 if Koha::Item::Transfer::Limits->search({
179 toBranch => $to->branchcode,
180 fromBranch => { 'in' => \@holdingbranches },
183 group_by => [qw/fromBranch/]
184 })->count == scalar(@holdingbranches) ? 0 : 1;
190 =head3 hidden_in_opac
192 my $bool = $biblio->hidden_in_opac({ [ rules => $rules ] })
194 Returns true if the biblio matches the hidding criteria defined in $rules.
195 Returns false otherwise.
197 Takes HASHref that can have the following parameters:
199 $rules : { <field> => [ value_1, ... ], ... }
201 Note: $rules inherits its structure from the parsed YAML from reading
202 the I<OpacHiddenItems> system preference.
207 my ( $self, $params ) = @_;
209 my $rules = $params->{rules} // {};
211 return !(any { !$_->hidden_in_opac({ rules => $rules }) } $self->items);
214 =head3 article_request_type
216 my $type = $biblio->article_request_type( $borrower );
218 Returns the article request type based on items, or on the record
219 itself if there are no items.
221 $borrower must be a Koha::Patron object
225 sub article_request_type {
226 my ( $self, $borrower ) = @_;
228 return q{} unless $borrower;
230 my $rule = $self->article_request_type_for_items( $borrower );
231 return $rule if $rule;
233 # If the record has no items that are requestable, go by the record itemtype
234 $rule = $self->article_request_type_for_bib($borrower);
235 return $rule if $rule;
240 =head3 article_request_type_for_bib
242 my $type = $biblio->article_request_type_for_bib
244 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
248 sub article_request_type_for_bib {
249 my ( $self, $borrower ) = @_;
251 return q{} unless $borrower;
253 my $borrowertype = $borrower->categorycode;
254 my $itemtype = $self->itemtype();
256 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
258 return q{} unless $issuing_rule;
259 return $issuing_rule->article_requests || q{}
262 =head3 article_request_type_for_items
264 my $type = $biblio->article_request_type_for_items
266 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
268 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
272 sub article_request_type_for_items {
273 my ( $self, $borrower ) = @_;
276 foreach my $item ( $self->items()->as_list() ) {
277 my $rule = $item->article_request_type($borrower);
278 return $rule if $rule eq 'bib_only'; # we don't need to go any further
282 return 'item_only' if $counts->{item_only};
283 return 'yes' if $counts->{yes};
284 return 'no' if $counts->{no};
288 =head3 article_requests
290 my @requests = $biblio->article_requests
292 Returns the article requests associated with this Biblio
296 sub article_requests {
297 my ( $self, $borrower ) = @_;
299 $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
301 return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
304 =head3 article_requests_current
306 my @requests = $biblio->article_requests_current
308 Returns the article requests associated with this Biblio that are incomplete
312 sub article_requests_current {
313 my ( $self, $borrower ) = @_;
315 $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
317 biblionumber => $self->biblionumber(),
319 { status => Koha::ArticleRequest::Status::Pending },
320 { status => Koha::ArticleRequest::Status::Processing }
325 return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
328 =head3 article_requests_finished
330 my @requests = $biblio->article_requests_finished
332 Returns the article requests associated with this Biblio that are completed
336 sub article_requests_finished {
337 my ( $self, $borrower ) = @_;
339 $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
341 biblionumber => $self->biblionumber(),
343 { status => Koha::ArticleRequest::Status::Completed },
344 { status => Koha::ArticleRequest::Status::Canceled }
349 return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
354 my @items = $biblio->items();
355 my $items = $biblio->items();
357 Returns the related Koha::Items object for this biblio in scalar context,
358 or list of Koha::Item objects in list context.
365 $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
367 return wantarray ? $self->{_items}->as_list : $self->{_items};
372 my $itemtype = $biblio->itemtype();
374 Returns the itemtype for this record.
381 return $self->biblioitem()->itemtype();
386 my $holds = $biblio->holds();
388 return the current holds placed on this record
393 my ( $self, $params, $attributes ) = @_;
394 $attributes->{order_by} = 'priority' unless exists $attributes->{order_by};
395 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
396 return Koha::Holds->_new_from_dbic($hold_rs);
401 my $holds = $biblio->current_holds
403 Return the holds placed on this bibliographic record.
404 It does not include future holds.
410 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
412 { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
417 my $field = $self->biblioitem()->itemtype
419 Returns the related Koha::Biblioitem object for this Biblio object
426 $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
428 return $self->{_biblioitem};
433 my $subscriptions = $self->subscriptions
435 Returns the related Koha::Subscriptions object for this Biblio object
442 $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
444 return $self->{_subscriptions};
447 =head3 has_items_waiting_or_intransit
449 my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
451 Tells if this bibliographic record has items waiting or in transit.
455 sub has_items_waiting_or_intransit {
458 if ( Koha::Holds->search({ biblionumber => $self->id,
459 found => ['W', 'T'] })->count ) {
463 foreach my $item ( $self->items ) {
464 return 1 if $item->get_transfer;
480 Kyle M Hall <kyle@bywatersolutions.com>