Bug 20920: Add missing dependency on CGI::Compile
[koha.git] / Koha / Biblio.pm
blobd226304267e5039ceac194b40f9aa670c01b6931
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::Items;
32 use Koha::Biblioitems;
33 use Koha::ArticleRequests;
34 use Koha::ArticleRequest::Status;
35 use Koha::IssuingRules;
36 use Koha::Subscriptions;
38 =head1 NAME
40 Koha::Biblio - Koha Biblio Object class
42 =head1 API
44 =head2 Class Methods
46 =cut
48 =head3 store
50 Overloaded I<store> method to set default values
52 =cut
54 sub store {
55 my ( $self ) = @_;
57 $self->datecreated( dt_from_string ) unless $self->datecreated;
59 return $self->SUPER::store;
62 =head3 subtitles
64 my @subtitles = $biblio->subtitles();
66 Returns list of subtitles for a record.
68 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
70 =cut
72 sub subtitles {
73 my ( $self ) = @_;
75 return map { $_->{subfield} } @{
76 C4::Biblio::GetRecordValue(
77 'subtitle',
78 C4::Biblio::GetMarcBiblio({ biblionumber => $self->id }),
79 $self->frameworkcode ) };
82 =head3 can_article_request
84 my $bool = $biblio->can_article_request( $borrower );
86 Returns true if article requests can be made for this record
88 $borrower must be a Koha::Patron object
90 =cut
92 sub can_article_request {
93 my ( $self, $borrower ) = @_;
95 my $rule = $self->article_request_type($borrower);
96 return q{} if $rule eq 'item_only' && !$self->items()->count();
97 return 1 if $rule && $rule ne 'no';
99 return q{};
102 =head3 article_request_type
104 my $type = $biblio->article_request_type( $borrower );
106 Returns the article request type based on items, or on the record
107 itself if there are no items.
109 $borrower must be a Koha::Patron object
111 =cut
113 sub article_request_type {
114 my ( $self, $borrower ) = @_;
116 return q{} unless $borrower;
118 my $rule = $self->article_request_type_for_items( $borrower );
119 return $rule if $rule;
121 # If the record has no items that are requestable, go by the record itemtype
122 $rule = $self->article_request_type_for_bib($borrower);
123 return $rule if $rule;
125 return q{};
128 =head3 article_request_type_for_bib
130 my $type = $biblio->article_request_type_for_bib
132 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
134 =cut
136 sub article_request_type_for_bib {
137 my ( $self, $borrower ) = @_;
139 return q{} unless $borrower;
141 my $borrowertype = $borrower->categorycode;
142 my $itemtype = $self->itemtype();
144 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
146 return q{} unless $issuing_rule;
147 return $issuing_rule->article_requests || q{}
150 =head3 article_request_type_for_items
152 my $type = $biblio->article_request_type_for_items
154 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
156 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
158 =cut
160 sub article_request_type_for_items {
161 my ( $self, $borrower ) = @_;
163 my $counts;
164 foreach my $item ( $self->items()->as_list() ) {
165 my $rule = $item->article_request_type($borrower);
166 return $rule if $rule eq 'bib_only'; # we don't need to go any further
167 $counts->{$rule}++;
170 return 'item_only' if $counts->{item_only};
171 return 'yes' if $counts->{yes};
172 return 'no' if $counts->{no};
173 return q{};
176 =head3 article_requests
178 my @requests = $biblio->article_requests
180 Returns the article requests associated with this Biblio
182 =cut
184 sub article_requests {
185 my ( $self, $borrower ) = @_;
187 $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
189 return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
192 =head3 article_requests_current
194 my @requests = $biblio->article_requests_current
196 Returns the article requests associated with this Biblio that are incomplete
198 =cut
200 sub article_requests_current {
201 my ( $self, $borrower ) = @_;
203 $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
205 biblionumber => $self->biblionumber(),
206 -or => [
207 { status => Koha::ArticleRequest::Status::Pending },
208 { status => Koha::ArticleRequest::Status::Processing }
213 return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
216 =head3 article_requests_finished
218 my @requests = $biblio->article_requests_finished
220 Returns the article requests associated with this Biblio that are completed
222 =cut
224 sub article_requests_finished {
225 my ( $self, $borrower ) = @_;
227 $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
229 biblionumber => $self->biblionumber(),
230 -or => [
231 { status => Koha::ArticleRequest::Status::Completed },
232 { status => Koha::ArticleRequest::Status::Canceled }
237 return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
240 =head3 items
242 my @items = $biblio->items();
243 my $items = $biblio->items();
245 Returns the related Koha::Items object for this biblio in scalar context,
246 or list of Koha::Item objects in list context.
248 =cut
250 sub items {
251 my ($self) = @_;
253 $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
255 return wantarray ? $self->{_items}->as_list : $self->{_items};
258 =head3 itemtype
260 my $itemtype = $biblio->itemtype();
262 Returns the itemtype for this record.
264 =cut
266 sub itemtype {
267 my ( $self ) = @_;
269 return $self->biblioitem()->itemtype();
272 =head3 holds
274 my $holds = $biblio->holds();
276 return the current holds placed on this record
278 =cut
280 sub holds {
281 my ( $self, $params, $attributes ) = @_;
282 $attributes->{order_by} = 'priority' unless exists $attributes->{order_by};
283 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
284 return Koha::Holds->_new_from_dbic($hold_rs);
287 =head3 current_holds
289 my $holds = $biblio->current_holds
291 Return the holds placed on this bibliographic record.
292 It does not include future holds.
294 =cut
296 sub current_holds {
297 my ($self) = @_;
298 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
299 return $self->holds(
300 { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
303 =head3 biblioitem
305 my $field = $self->biblioitem()->itemtype
307 Returns the related Koha::Biblioitem object for this Biblio object
309 =cut
311 sub biblioitem {
312 my ($self) = @_;
314 $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
316 return $self->{_biblioitem};
319 =head3 subscriptions
321 my $subscriptions = $self->subscriptions
323 Returns the related Koha::Subscriptions object for this Biblio object
325 =cut
327 sub subscriptions {
328 my ($self) = @_;
330 $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
332 return $self->{_subscriptions};
335 =head3 has_items_waiting_or_intransit
337 my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
339 Tells if this bibliographic record has items waiting or in transit.
341 =cut
343 sub has_items_waiting_or_intransit {
344 my ( $self ) = @_;
346 if ( Koha::Holds->search({ biblionumber => $self->id,
347 found => ['W', 'T'] })->count ) {
348 return 1;
351 foreach my $item ( $self->items ) {
352 return 1 if $item->get_transfer;
355 return 0;
358 =head3 type
360 =cut
362 sub _type {
363 return 'Biblio';
366 =head1 AUTHOR
368 Kyle M Hall <kyle@bywatersolutions.com>
370 =cut