Bug 17709 - Article request broken - 'internal server error'
[koha.git] / Koha / Biblio.pm
1 package Koha::Biblio;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use C4::Biblio qw( GetRecordValue GetMarcBiblio GetFrameworkCode );
25
26 use Koha::Database;
27
28 use base qw(Koha::Object);
29
30 use Koha::Items;
31 use Koha::Biblioitems;
32 use Koha::ArticleRequests;
33 use Koha::ArticleRequest::Status;
34 use Koha::IssuingRules;
35
36 =head1 NAME
37
38 Koha::Biblio - Koha Biblio Object class
39
40 =head1 API
41
42 =head2 Class Methods
43
44 =cut
45
46 =head3 subtitles
47
48 my @subtitles = $biblio->subtitles();
49
50 Returns list of subtitles for a record.
51
52 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
53
54 =cut
55
56 sub subtitles {
57     my ( $self ) = @_;
58
59     return map { $_->{subfield} } @{ GetRecordValue( 'subtitle', GetMarcBiblio( $self->id ), $self->frameworkcode ) };
60 }
61
62 =head3 can_article_request
63
64 my $bool = $biblio->can_article_request( $borrower );
65
66 Returns true if article requests can be made for this record
67
68 $borrower must be a Koha::Patron object
69
70 =cut
71
72 sub can_article_request {
73     my ( $self, $borrower ) = @_;
74
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';
78
79     return q{};
80 }
81
82 =head3 article_request_type
83
84 my $type = $biblio->article_request_type( $borrower );
85
86 Returns the article request type based on items, or on the record
87 itself if there are no items.
88
89 $borrower must be a Koha::Patron object
90
91 =cut
92
93 sub article_request_type {
94     my ( $self, $borrower ) = @_;
95
96     return q{} unless $borrower;
97
98     my $rule = $self->article_request_type_for_items( $borrower );
99     return $rule if $rule;
100
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;
104
105     return q{};
106 }
107
108 =head3 article_request_type_for_bib
109
110 my $type = $biblio->article_request_type_for_bib
111
112 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
113
114 =cut
115
116 sub article_request_type_for_bib {
117     my ( $self, $borrower ) = @_;
118
119     return q{} unless $borrower;
120
121     my $borrowertype = $borrower->categorycode;
122     my $itemtype     = $self->itemtype();
123
124     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
125
126     return q{} unless $issuing_rule;
127     return $issuing_rule->article_requests || q{}
128 }
129
130 =head3 article_request_type_for_items
131
132 my $type = $biblio->article_request_type_for_items
133
134 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
135
136 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
137
138 =cut
139
140 sub article_request_type_for_items {
141     my ( $self, $borrower ) = @_;
142
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}++;
148     }
149
150     return 'item_only' if $counts->{item_only};
151     return 'yes'       if $counts->{yes};
152     return 'no'        if $counts->{no};
153     return q{};
154 }
155
156 =head3 article_requests
157
158 my @requests = $biblio->article_requests
159
160 Returns the article requests associated with this Biblio
161
162 =cut
163
164 sub article_requests {
165     my ( $self, $borrower ) = @_;
166
167     $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
168
169     return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
170 }
171
172 =head3 article_requests_current
173
174 my @requests = $biblio->article_requests_current
175
176 Returns the article requests associated with this Biblio that are incomplete
177
178 =cut
179
180 sub article_requests_current {
181     my ( $self, $borrower ) = @_;
182
183     $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
184         {
185             biblionumber => $self->biblionumber(),
186             -or          => [
187                 { status => Koha::ArticleRequest::Status::Pending },
188                 { status => Koha::ArticleRequest::Status::Processing }
189             ]
190         }
191     );
192
193     return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
194 }
195
196 =head3 article_requests_finished
197
198 my @requests = $biblio->article_requests_finished
199
200 Returns the article requests associated with this Biblio that are completed
201
202 =cut
203
204 sub article_requests_finished {
205     my ( $self, $borrower ) = @_;
206
207     $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
208         {
209             biblionumber => $self->biblionumber(),
210             -or          => [
211                 { status => Koha::ArticleRequest::Status::Completed },
212                 { status => Koha::ArticleRequest::Status::Canceled }
213             ]
214         }
215     );
216
217     return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
218 }
219
220 =head3 items
221
222 my @items = $biblio->items();
223 my $items = $biblio->items();
224
225 Returns the related Koha::Items object for this biblio in scalar context,
226 or list of Koha::Item objects in list context.
227
228 =cut
229
230 sub items {
231     my ($self) = @_;
232
233     $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
234
235     return wantarray ? $self->{_items}->as_list : $self->{_items};
236 }
237
238 =head3 itemtype
239
240 my $itemtype = $biblio->itemtype();
241
242 Returns the itemtype for this record.
243
244 =cut
245
246 sub itemtype {
247     my ( $self ) = @_;
248
249     return $self->biblioitem()->itemtype();
250 }
251
252 =head3 biblioitem
253
254 my $field = $self->biblioitem()->itemtype
255
256 Returns the related Koha::Biblioitem object for this Biblio object
257
258 =cut
259
260 sub biblioitem {
261     my ($self) = @_;
262
263     $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
264
265     return $self->{_biblioitem};
266 }
267
268 =head3 type
269
270 =cut
271
272 sub _type {
273     return 'Biblio';
274 }
275
276 =head1 AUTHOR
277
278 Kyle M Hall <kyle@bywatersolutions.com>
279
280 =cut
281
282 1;