Bug 18089 - Unit test
[koha.git] / Koha / Biblio.pm
blobd5ca37045b1226185525d314f147a5eaff707a4e
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( GetRecordValue GetMarcBiblio GetFrameworkCode );
26 use Koha::Database;
28 use base qw(Koha::Object);
30 use Koha::Items;
31 use Koha::Biblioitems;
32 use Koha::ArticleRequests;
33 use Koha::ArticleRequest::Status;
34 use Koha::IssuingRules;
36 =head1 NAME
38 Koha::Biblio - Koha Biblio Object class
40 =head1 API
42 =head2 Class Methods
44 =cut
46 =head3 subtitles
48 my @subtitles = $biblio->subtitles();
50 Returns list of subtitles for a record.
52 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
54 =cut
56 sub subtitles {
57 my ( $self ) = @_;
59 return map { $_->{subfield} } @{ GetRecordValue( 'subtitle', GetMarcBiblio( $self->id ), $self->frameworkcode ) };
62 =head3 can_article_request
64 my $bool = $biblio->can_article_request( $borrower );
66 Returns true if article requests can be made for this record
68 $borrower must be a Koha::Patron object
70 =cut
72 sub can_article_request {
73 my ( $self, $borrower ) = @_;
75 my $rule = $self->article_request_type($borrower);
76 return q{} if $rule eq 'item_only' && !$self->items()->count();
77 return 1 if $rule && $rule ne 'no';
79 return q{};
82 =head3 article_request_type
84 my $type = $biblio->article_request_type( $borrower );
86 Returns the article request type based on items, or on the record
87 itself if there are no items.
89 $borrower must be a Koha::Patron object
91 =cut
93 sub article_request_type {
94 my ( $self, $borrower ) = @_;
96 return q{} unless $borrower;
98 my $rule = $self->article_request_type_for_items( $borrower );
99 return $rule if $rule;
101 # If the record has no items that are requestable, go by the record itemtype
102 $rule = $self->article_request_type_for_bib($borrower);
103 return $rule if $rule;
105 return q{};
108 =head3 article_request_type_for_bib
110 my $type = $biblio->article_request_type_for_bib
112 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
114 =cut
116 sub article_request_type_for_bib {
117 my ( $self, $borrower ) = @_;
119 return q{} unless $borrower;
121 my $borrowertype = $borrower->categorycode;
122 my $itemtype = $self->itemtype();
124 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
126 return q{} unless $issuing_rule;
127 return $issuing_rule->article_requests || q{}
130 =head3 article_request_type_for_items
132 my $type = $biblio->article_request_type_for_items
134 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
136 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
138 =cut
140 sub article_request_type_for_items {
141 my ( $self, $borrower ) = @_;
143 my $counts;
144 foreach my $item ( $self->items()->as_list() ) {
145 my $rule = $item->article_request_type($borrower);
146 return $rule if $rule eq 'bib_only'; # we don't need to go any further
147 $counts->{$rule}++;
150 return 'item_only' if $counts->{item_only};
151 return 'yes' if $counts->{yes};
152 return 'no' if $counts->{no};
153 return q{};
156 =head3 article_requests
158 my @requests = $biblio->article_requests
160 Returns the article requests associated with this Biblio
162 =cut
164 sub article_requests {
165 my ( $self, $borrower ) = @_;
167 $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
169 return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
172 =head3 article_requests_current
174 my @requests = $biblio->article_requests_current
176 Returns the article requests associated with this Biblio that are incomplete
178 =cut
180 sub article_requests_current {
181 my ( $self, $borrower ) = @_;
183 $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
185 biblionumber => $self->biblionumber(),
186 -or => [
187 { status => Koha::ArticleRequest::Status::Pending },
188 { status => Koha::ArticleRequest::Status::Processing }
193 return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
196 =head3 article_requests_finished
198 my @requests = $biblio->article_requests_finished
200 Returns the article requests associated with this Biblio that are completed
202 =cut
204 sub article_requests_finished {
205 my ( $self, $borrower ) = @_;
207 $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
209 biblionumber => $self->biblionumber(),
210 -or => [
211 { status => Koha::ArticleRequest::Status::Completed },
212 { status => Koha::ArticleRequest::Status::Canceled }
217 return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
220 =head3 items
222 my @items = $biblio->items();
223 my $items = $biblio->items();
225 Returns the related Koha::Items object for this biblio in scalar context,
226 or list of Koha::Item objects in list context.
228 =cut
230 sub items {
231 my ($self) = @_;
233 $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
235 return wantarray ? $self->{_items}->as_list : $self->{_items};
238 =head3 itemtype
240 my $itemtype = $biblio->itemtype();
242 Returns the itemtype for this record.
244 =cut
246 sub itemtype {
247 my ( $self ) = @_;
249 return $self->biblioitem()->itemtype();
252 =head3 holds
254 my $holds = $biblio->holds();
256 return the current holds placed on this record
258 =cut
260 sub holds {
261 my ( $self ) = @_;
263 my $holds_rs = $self->_result->reserves;
264 return Koha::Holds->_new_from_dbic( $holds_rs );
267 =head3 biblioitem
269 my $field = $self->biblioitem()->itemtype
271 Returns the related Koha::Biblioitem object for this Biblio object
273 =cut
275 sub biblioitem {
276 my ($self) = @_;
278 $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
280 return $self->{_biblioitem};
283 =head3 type
285 =cut
287 sub _type {
288 return 'Biblio';
291 =head1 AUTHOR
293 Kyle M Hall <kyle@bywatersolutions.com>
295 =cut