Bug 20912: Rental Fees based on Time Period
[koha.git] / Koha / Biblio.pm
blobd2e4d4d321582784470d56db5a57b8d2c0fa8586
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;
24 use C4::Biblio qw();
26 use Koha::Database;
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;
37 use Koha::Items;
38 use Koha::Libraries;
39 use Koha::Subscriptions;
41 =head1 NAME
43 Koha::Biblio - Koha Biblio Object class
45 =head1 API
47 =head2 Class Methods
49 =cut
51 =head3 store
53 Overloaded I<store> method to set default values
55 =cut
57 sub store {
58 my ( $self ) = @_;
60 $self->datecreated( dt_from_string ) unless $self->datecreated;
62 return $self->SUPER::store;
65 =head3 metadata
67 my $metadata = $biblio->metadata();
69 Returns a Koha::Biblio::Metadata object
71 =cut
73 sub metadata {
74 my ( $self ) = @_;
76 $self->{_metadata} ||= Koha::Biblio::Metadatas->find( { biblionumber => $self->id } );
78 return $self->{_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) {
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 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
198 =cut
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;
212 return q{};
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
221 =cut
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.
245 =cut
247 sub article_request_type_for_items {
248 my ( $self, $borrower ) = @_;
250 my $counts;
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
254 $counts->{$rule}++;
257 return 'item_only' if $counts->{item_only};
258 return 'yes' if $counts->{yes};
259 return 'no' if $counts->{no};
260 return q{};
263 =head3 article_requests
265 my @requests = $biblio->article_requests
267 Returns the article requests associated with this Biblio
269 =cut
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
285 =cut
287 sub article_requests_current {
288 my ( $self, $borrower ) = @_;
290 $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
292 biblionumber => $self->biblionumber(),
293 -or => [
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
309 =cut
311 sub article_requests_finished {
312 my ( $self, $borrower ) = @_;
314 $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
316 biblionumber => $self->biblionumber(),
317 -or => [
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};
327 =head3 items
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.
335 =cut
337 sub items {
338 my ($self) = @_;
340 $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
342 return wantarray ? $self->{_items}->as_list : $self->{_items};
345 =head3 itemtype
347 my $itemtype = $biblio->itemtype();
349 Returns the itemtype for this record.
351 =cut
353 sub itemtype {
354 my ( $self ) = @_;
356 return $self->biblioitem()->itemtype();
359 =head3 holds
361 my $holds = $biblio->holds();
363 return the current holds placed on this record
365 =cut
367 sub holds {
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);
374 =head3 current_holds
376 my $holds = $biblio->current_holds
378 Return the holds placed on this bibliographic record.
379 It does not include future holds.
381 =cut
383 sub current_holds {
384 my ($self) = @_;
385 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
386 return $self->holds(
387 { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
390 =head3 biblioitem
392 my $field = $self->biblioitem()->itemtype
394 Returns the related Koha::Biblioitem object for this Biblio object
396 =cut
398 sub biblioitem {
399 my ($self) = @_;
401 $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
403 return $self->{_biblioitem};
406 =head3 subscriptions
408 my $subscriptions = $self->subscriptions
410 Returns the related Koha::Subscriptions object for this Biblio object
412 =cut
414 sub subscriptions {
415 my ($self) = @_;
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.
428 =cut
430 sub has_items_waiting_or_intransit {
431 my ( $self ) = @_;
433 if ( Koha::Holds->search({ biblionumber => $self->id,
434 found => ['W', 'T'] })->count ) {
435 return 1;
438 foreach my $item ( $self->items ) {
439 return 1 if $item->get_transfer;
442 return 0;
445 =head3 type
447 =cut
449 sub _type {
450 return 'Biblio';
453 =head1 AUTHOR
455 Kyle M Hall <kyle@bywatersolutions.com>
457 =cut