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;
42 Koha::Biblio - Koha Biblio Object class
52 Overloaded I<store> method to set default values
59 $self->datecreated( dt_from_string ) unless $self->datecreated;
61 return $self->SUPER::store;
66 my @subtitles = $biblio->subtitles();
68 Returns list of subtitles for a record.
70 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
77 return map { $_->{subfield} } @{
78 C4::Biblio::GetRecordValue(
80 C4::Biblio::GetMarcBiblio({ biblionumber => $self->id }),
81 $self->frameworkcode ) };
84 =head3 can_article_request
86 my $bool = $biblio->can_article_request( $borrower );
88 Returns true if article requests can be made for this record
90 $borrower must be a Koha::Patron object
94 sub can_article_request {
95 my ( $self, $borrower ) = @_;
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';
104 =head3 can_be_transferred
106 $biblio->can_be_transferred({ to => $to_library, from => $from_library })
108 Checks if at least one item of a biblio can be transferred to given library.
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
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().
118 Takes HASHref that can have the following parameters:
119 MANDATORY PARAMETERS:
122 $from : Koha::Library # if given, only items from that
123 # holdingbranch are considered
125 Returns 1 if at least one of the item of a biblio can be transferred
126 to $to_library, otherwise 0.
130 sub can_be_transferred {
131 my ($self, $params) = @_;
133 my $to = $params->{to};
134 my $from = $params->{from};
136 return 1 unless C4::Context->preference('UseBranchTransferLimits');
137 my $limittype = C4::Context->preference('BranchTransferLimitsType');
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;
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 },
165 group_by => [qw/fromBranch/]
166 })->count == scalar(@holdingbranches) ? 0 : 1;
172 =head3 may_article_request
174 Returns true if it is likely possible to make an article request for
175 a given item type (or the default item type from biblioitems).
178 my $boolean = Koha::Biblio->may_article_request({ itemtype => 'BK' });
179 # As instance method:
180 my $boolean = Koha::Biblios->find($biblionumber)->may_article_request;
184 sub may_article_request {
185 my ( $class_or_self, $params ) = @_;
186 return q{} if !C4::Context->preference('ArticleRequests');
187 my $itemtype = ref($class_or_self)
188 ? $class_or_self->itemtype
189 : $params->{itemtype};
190 my $category = $params->{categorycode};
192 my $guess = Koha::IssuingRules->guess_article_requestable_itemtypes({
193 $category ? ( categorycode => $category ) : (),
195 return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{};
198 =head3 article_request_type
200 my $type = $biblio->article_request_type( $borrower );
202 Returns the article request type based on items, or on the record
203 itself if there are no items.
205 $borrower must be a Koha::Patron object
209 sub article_request_type {
210 my ( $self, $borrower ) = @_;
212 return q{} unless $borrower;
214 my $rule = $self->article_request_type_for_items( $borrower );
215 return $rule if $rule;
217 # If the record has no items that are requestable, go by the record itemtype
218 $rule = $self->article_request_type_for_bib($borrower);
219 return $rule if $rule;
224 =head3 article_request_type_for_bib
226 my $type = $biblio->article_request_type_for_bib
228 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
232 sub article_request_type_for_bib {
233 my ( $self, $borrower ) = @_;
235 return q{} unless $borrower;
237 my $borrowertype = $borrower->categorycode;
238 my $itemtype = $self->itemtype();
240 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
242 return q{} unless $issuing_rule;
243 return $issuing_rule->article_requests || q{}
246 =head3 article_request_type_for_items
248 my $type = $biblio->article_request_type_for_items
250 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
252 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
256 sub article_request_type_for_items {
257 my ( $self, $borrower ) = @_;
260 foreach my $item ( $self->items()->as_list() ) {
261 my $rule = $item->article_request_type($borrower);
262 return $rule if $rule eq 'bib_only'; # we don't need to go any further
266 return 'item_only' if $counts->{item_only};
267 return 'yes' if $counts->{yes};
268 return 'no' if $counts->{no};
272 =head3 article_requests
274 my @requests = $biblio->article_requests
276 Returns the article requests associated with this Biblio
280 sub article_requests {
281 my ( $self, $borrower ) = @_;
283 $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
285 return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
288 =head3 article_requests_current
290 my @requests = $biblio->article_requests_current
292 Returns the article requests associated with this Biblio that are incomplete
296 sub article_requests_current {
297 my ( $self, $borrower ) = @_;
299 $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
301 biblionumber => $self->biblionumber(),
303 { status => Koha::ArticleRequest::Status::Pending },
304 { status => Koha::ArticleRequest::Status::Processing }
309 return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
312 =head3 article_requests_finished
314 my @requests = $biblio->article_requests_finished
316 Returns the article requests associated with this Biblio that are completed
320 sub article_requests_finished {
321 my ( $self, $borrower ) = @_;
323 $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
325 biblionumber => $self->biblionumber(),
327 { status => Koha::ArticleRequest::Status::Completed },
328 { status => Koha::ArticleRequest::Status::Canceled }
333 return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
338 my @items = $biblio->items();
339 my $items = $biblio->items();
341 Returns the related Koha::Items object for this biblio in scalar context,
342 or list of Koha::Item objects in list context.
349 $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
351 return wantarray ? $self->{_items}->as_list : $self->{_items};
356 my $itemtype = $biblio->itemtype();
358 Returns the itemtype for this record.
365 return $self->biblioitem()->itemtype();
370 my $holds = $biblio->holds();
372 return the current holds placed on this record
377 my ( $self, $params, $attributes ) = @_;
378 $attributes->{order_by} = 'priority' unless exists $attributes->{order_by};
379 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
380 return Koha::Holds->_new_from_dbic($hold_rs);
385 my $holds = $biblio->current_holds
387 Return the holds placed on this bibliographic record.
388 It does not include future holds.
394 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
396 { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
401 my $field = $self->biblioitem()->itemtype
403 Returns the related Koha::Biblioitem object for this Biblio object
410 $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
412 return $self->{_biblioitem};
417 my $subscriptions = $self->subscriptions
419 Returns the related Koha::Subscriptions object for this Biblio object
426 $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
428 return $self->{_subscriptions};
431 =head3 has_items_waiting_or_intransit
433 my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
435 Tells if this bibliographic record has items waiting or in transit.
439 sub has_items_waiting_or_intransit {
442 if ( Koha::Holds->search({ biblionumber => $self->id,
443 found => ['W', 'T'] })->count ) {
447 foreach my $item ( $self->items ) {
448 return 1 if $item->get_transfer;
464 Kyle M Hall <kyle@bywatersolutions.com>