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.
27 use Koha::DateUtils qw( dt_from_string );
29 use base qw(Koha::Object);
32 use Koha::Biblioitems;
33 use Koha::ArticleRequests;
34 use Koha::ArticleRequest::Status;
35 use Koha::IssuingRules;
36 use Koha::Item::Transfer::Limits;
38 use Koha::Subscriptions;
40 use Koha::Exceptions::Library;
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 @subtitles = $biblio->subtitles();
70 Returns list of subtitles for a record.
72 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
79 return map { $_->{subfield} } @{
80 C4::Biblio::GetRecordValue(
82 C4::Biblio::GetMarcBiblio({ biblionumber => $self->id }),
83 $self->frameworkcode ) };
86 =head3 can_article_request
88 my $bool = $biblio->can_article_request( $borrower );
90 Returns true if article requests can be made for this record
92 $borrower must be a Koha::Patron object
96 sub can_article_request {
97 my ( $self, $borrower ) = @_;
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';
106 =head3 can_be_transferred
108 $biblio->can_be_transferred({ to => $to_library, from => $from_library })
110 Checks if at least one item of a biblio can be transferred to given library.
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
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().
120 Takes HASHref that can have the following parameters:
121 MANDATORY PARAMETERS:
122 $to : Koha::Library or branchcode string
124 $from : Koha::Library or branchcode string # if given, only items from that
125 # holdingbranch are considered
127 Returns 1 if at least one of the item of a biblio can be transferred
128 to $to_library, otherwise 0.
132 sub can_be_transferred {
133 my ($self, $params) = @_;
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);
141 Koha::Exceptions::Library::NotFound->throw(
142 error => "Library '$tobranchcode' not found.",
146 if ($from && ref($from) ne 'Koha::Library') {
147 my $frombranchcode = defined $from ? $from : '';
148 $from = Koha::Libraries->find($frombranchcode);
150 Koha::Exceptions::Library::NotFound->throw(
151 error => "Library '$frombranchcode' not found.",
156 return 1 unless C4::Context->preference('UseBranchTransferLimits');
157 my $limittype = C4::Context->preference('BranchTransferLimitsType');
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;
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 },
185 group_by => [qw/fromBranch/]
186 })->count == scalar(@holdingbranches) ? 0 : 1;
192 =head3 article_request_type
194 my $type = $biblio->article_request_type( $borrower );
196 Returns the article request type based on items, or on the record
197 itself if there are no items.
199 $borrower must be a Koha::Patron object
203 sub article_request_type {
204 my ( $self, $borrower ) = @_;
206 return q{} unless $borrower;
208 my $rule = $self->article_request_type_for_items( $borrower );
209 return $rule if $rule;
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;
218 =head3 article_request_type_for_bib
220 my $type = $biblio->article_request_type_for_bib
222 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
226 sub article_request_type_for_bib {
227 my ( $self, $borrower ) = @_;
229 return q{} unless $borrower;
231 my $borrowertype = $borrower->categorycode;
232 my $itemtype = $self->itemtype();
234 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
236 return q{} unless $issuing_rule;
237 return $issuing_rule->article_requests || q{}
240 =head3 article_request_type_for_items
242 my $type = $biblio->article_request_type_for_items
244 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
246 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
250 sub article_request_type_for_items {
251 my ( $self, $borrower ) = @_;
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
260 return 'item_only' if $counts->{item_only};
261 return 'yes' if $counts->{yes};
262 return 'no' if $counts->{no};
266 =head3 article_requests
268 my @requests = $biblio->article_requests
270 Returns the article requests associated with this Biblio
274 sub article_requests {
275 my ( $self, $borrower ) = @_;
277 $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
279 return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
282 =head3 article_requests_current
284 my @requests = $biblio->article_requests_current
286 Returns the article requests associated with this Biblio that are incomplete
290 sub article_requests_current {
291 my ( $self, $borrower ) = @_;
293 $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
295 biblionumber => $self->biblionumber(),
297 { status => Koha::ArticleRequest::Status::Pending },
298 { status => Koha::ArticleRequest::Status::Processing }
303 return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
306 =head3 article_requests_finished
308 my @requests = $biblio->article_requests_finished
310 Returns the article requests associated with this Biblio that are completed
314 sub article_requests_finished {
315 my ( $self, $borrower ) = @_;
317 $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
319 biblionumber => $self->biblionumber(),
321 { status => Koha::ArticleRequest::Status::Completed },
322 { status => Koha::ArticleRequest::Status::Canceled }
327 return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
332 my @items = $biblio->items();
333 my $items = $biblio->items();
335 Returns the related Koha::Items object for this biblio in scalar context,
336 or list of Koha::Item objects in list context.
343 $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
345 return wantarray ? $self->{_items}->as_list : $self->{_items};
350 my $itemtype = $biblio->itemtype();
352 Returns the itemtype for this record.
359 return $self->biblioitem()->itemtype();
364 my $holds = $biblio->holds();
366 return the current holds placed on this record
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);
379 my $holds = $biblio->current_holds
381 Return the holds placed on this bibliographic record.
382 It does not include future holds.
388 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
390 { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
395 my $field = $self->biblioitem()->itemtype
397 Returns the related Koha::Biblioitem object for this Biblio object
404 $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
406 return $self->{_biblioitem};
411 my $subscriptions = $self->subscriptions
413 Returns the related Koha::Subscriptions object for this Biblio object
420 $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
422 return $self->{_subscriptions};
425 =head3 has_items_waiting_or_intransit
427 my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
429 Tells if this bibliographic record has items waiting or in transit.
433 sub has_items_waiting_or_intransit {
436 if ( Koha::Holds->search({ biblionumber => $self->id,
437 found => ['W', 'T'] })->count ) {
441 foreach my $item ( $self->items ) {
442 return 1 if $item->get_transfer;
458 Kyle M Hall <kyle@bywatersolutions.com>