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);
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
;
39 use Koha
::Subscriptions
;
43 Koha::Biblio - Koha Biblio Object class
53 Overloaded I<store> method to set default values
60 $self->datecreated( dt_from_string
) unless $self->datecreated;
62 return $self->SUPER::store
;
67 my $metadata = $biblio->metadata();
69 Returns a Koha::Biblio::Metadata object
76 $self->{_metadata
} ||= Koha
::Biblio
::Metadatas
->find( { biblionumber
=> $self->id } );
78 return $self->{_metadata
};
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.
94 return map { $_->{subfield
} } @
{
95 C4
::Biblio
::GetRecordValue
(
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
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';
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:
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.
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');
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;
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 },
182 group_by
=> [qw
/fromBranch/]
183 })->count == scalar(@holdingbranches) ?
0 : 1;
189 =head3 article_request_type
191 my $type = $biblio->article_request_type( $borrower );
193 Returns the article request type based on items, or on the record
194 itself if there are no items.
196 $borrower must be a Koha::Patron object
200 sub article_request_type
{
201 my ( $self, $borrower ) = @_;
203 return q{} unless $borrower;
205 my $rule = $self->article_request_type_for_items( $borrower );
206 return $rule if $rule;
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;
215 =head3 article_request_type_for_bib
217 my $type = $biblio->article_request_type_for_bib
219 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
223 sub article_request_type_for_bib
{
224 my ( $self, $borrower ) = @_;
226 return q{} unless $borrower;
228 my $borrowertype = $borrower->categorycode;
229 my $itemtype = $self->itemtype();
231 my $issuing_rule = Koha
::IssuingRules
->get_effective_issuing_rule({ categorycode
=> $borrowertype, itemtype
=> $itemtype });
233 return q{} unless $issuing_rule;
234 return $issuing_rule->article_requests || q{}
237 =head3 article_request_type_for_items
239 my $type = $biblio->article_request_type_for_items
241 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
243 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
247 sub article_request_type_for_items
{
248 my ( $self, $borrower ) = @_;
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
257 return 'item_only' if $counts->{item_only
};
258 return 'yes' if $counts->{yes
};
259 return 'no' if $counts->{no};
263 =head3 article_requests
265 my @requests = $biblio->article_requests
267 Returns the article requests associated with this Biblio
271 sub article_requests
{
272 my ( $self, $borrower ) = @_;
274 $self->{_article_requests
} ||= Koha
::ArticleRequests
->search( { biblionumber
=> $self->biblionumber() } );
276 return wantarray ?
$self->{_article_requests
}->as_list : $self->{_article_requests
};
279 =head3 article_requests_current
281 my @requests = $biblio->article_requests_current
283 Returns the article requests associated with this Biblio that are incomplete
287 sub article_requests_current
{
288 my ( $self, $borrower ) = @_;
290 $self->{_article_requests_current
} ||= Koha
::ArticleRequests
->search(
292 biblionumber
=> $self->biblionumber(),
294 { status
=> Koha
::ArticleRequest
::Status
::Pending
},
295 { status
=> Koha
::ArticleRequest
::Status
::Processing
}
300 return wantarray ?
$self->{_article_requests_current
}->as_list : $self->{_article_requests_current
};
303 =head3 article_requests_finished
305 my @requests = $biblio->article_requests_finished
307 Returns the article requests associated with this Biblio that are completed
311 sub article_requests_finished
{
312 my ( $self, $borrower ) = @_;
314 $self->{_article_requests_finished
} ||= Koha
::ArticleRequests
->search(
316 biblionumber
=> $self->biblionumber(),
318 { status
=> Koha
::ArticleRequest
::Status
::Completed
},
319 { status
=> Koha
::ArticleRequest
::Status
::Canceled
}
324 return wantarray ?
$self->{_article_requests_finished
}->as_list : $self->{_article_requests_finished
};
329 my @items = $biblio->items();
330 my $items = $biblio->items();
332 Returns the related Koha::Items object for this biblio in scalar context,
333 or list of Koha::Item objects in list context.
340 $self->{_items
} ||= Koha
::Items
->search( { biblionumber
=> $self->biblionumber() } );
342 return wantarray ?
$self->{_items
}->as_list : $self->{_items
};
347 my $itemtype = $biblio->itemtype();
349 Returns the itemtype for this record.
356 return $self->biblioitem()->itemtype();
361 my $holds = $biblio->holds();
363 return the current holds placed on this record
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);
376 my $holds = $biblio->current_holds
378 Return the holds placed on this bibliographic record.
379 It does not include future holds.
385 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
387 { reservedate
=> { '<=' => $dtf->format_date(dt_from_string
) } } );
392 my $field = $self->biblioitem()->itemtype
394 Returns the related Koha::Biblioitem object for this Biblio object
401 $self->{_biblioitem
} ||= Koha
::Biblioitems
->find( { biblionumber
=> $self->biblionumber() } );
403 return $self->{_biblioitem
};
408 my $subscriptions = $self->subscriptions
410 Returns the related Koha::Subscriptions object for this Biblio object
417 $self->{_subscriptions
} ||= Koha
::Subscriptions
->search( { biblionumber
=> $self->biblionumber } );
419 return $self->{_subscriptions
};
422 =head3 has_items_waiting_or_intransit
424 my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
426 Tells if this bibliographic record has items waiting or in transit.
430 sub has_items_waiting_or_intransit
{
433 if ( Koha
::Holds
->search({ biblionumber
=> $self->id,
434 found
=> ['W', 'T'] })->count ) {
438 foreach my $item ( $self->items ) {
439 return 1 if $item->get_transfer;
455 Kyle M Hall <kyle@bywatersolutions.com>