Bug 14610 [QA Followup] - Fix publishercode issue
[koha.git] / Koha / Biblio.pm
blobb33a8639cb9f89fb70a72e9a4b36c37440555228
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 C4::Circulation qw(GetIssuingRule);
31 use Koha::Items;
32 use Koha::Biblioitems;
33 use Koha::ArticleRequests;
34 use Koha::ArticleRequest::Status;
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 $rules = C4::Circulation::GetIssuingRule( $borrowertype, $itemtype );
126 return $rules->{article_requests} || q{};
129 =head3 article_request_type_for_items
131 my $type = $biblio->article_request_type_for_items
133 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
135 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
137 =cut
139 sub article_request_type_for_items {
140 my ( $self, $borrower ) = @_;
142 my $counts;
143 foreach my $item ( $self->items()->as_list() ) {
144 my $rule = $item->article_request_type($borrower);
145 return $rule if $rule eq 'bib_only'; # we don't need to go any further
146 $counts->{$rule}++;
149 return 'item_only' if $counts->{item_only};
150 return 'yes' if $counts->{yes};
151 return 'no' if $counts->{no};
152 return q{};
155 =head3 article_requests
157 my @requests = $biblio->article_requests
159 Returns the article requests associated with this Biblio
161 =cut
163 sub article_requests {
164 my ( $self, $borrower ) = @_;
166 $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
168 return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
171 =head3 article_requests_current
173 my @requests = $biblio->article_requests_current
175 Returns the article requests associated with this Biblio that are incomplete
177 =cut
179 sub article_requests_current {
180 my ( $self, $borrower ) = @_;
182 $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
184 biblionumber => $self->biblionumber(),
185 -or => [
186 { status => Koha::ArticleRequest::Status::Pending },
187 { status => Koha::ArticleRequest::Status::Processing }
192 return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
195 =head3 article_requests_finished
197 my @requests = $biblio->article_requests_finished
199 Returns the article requests associated with this Biblio that are completed
201 =cut
203 sub article_requests_finished {
204 my ( $self, $borrower ) = @_;
206 $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
208 biblionumber => $self->biblionumber(),
209 -or => [
210 { status => Koha::ArticleRequest::Status::Completed },
211 { status => Koha::ArticleRequest::Status::Canceled }
216 return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
219 =head3 items
221 =head3 items
223 my @items = $biblio->items();
224 my $items = $biblio->items();
226 Returns the related Koha::Items object for this biblio in scalar context,
227 or list of Koha::Item objects in list context.
229 =cut
231 sub items {
232 my ($self) = @_;
234 $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
236 return wantarray ? $self->{_items}->as_list : $self->{_items};
239 =head3 itemtype
241 my $itemtype = $biblio->itemtype();
243 Returns the itemtype for this record.
245 =cut
247 sub itemtype {
248 my ( $self ) = @_;
250 return $self->_biblioitem()->itemtype();
253 =head3 _biblioitem
255 my $field = $self->_biblioitem()->itemtype
257 Returns the related Koha::Biblioitem object for this Biblio object
259 =cut
261 sub biblioitem {
262 my ($self) = @_;
264 $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
266 return $self->{_biblioitem};
269 =head3 type
271 =cut
273 sub _type {
274 return 'Biblio';
277 =head1 AUTHOR
279 Kyle M Hall <kyle@bywatersolutions.com>
281 =cut