Bug 15492: Add KOHA_VERSION to included .css and .js
[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();
25
26 use Koha::Database;
27 use Koha::DateUtils qw( dt_from_string );
28
29 use base qw(Koha::Object);
30
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;
37
38 =head1 NAME
39
40 Koha::Biblio - Koha Biblio Object class
41
42 =head1 API
43
44 =head2 Class Methods
45
46 =cut
47
48 =head3 store
49
50 Overloaded I<store> method to set default values
51
52 =cut
53
54 sub store {
55     my ( $self ) = @_;
56
57     $self->datecreated( dt_from_string ) unless $self->datecreated;
58
59     return $self->SUPER::store;
60 }
61
62 =head3 subtitles
63
64 my @subtitles = $biblio->subtitles();
65
66 Returns list of subtitles for a record.
67
68 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
69
70 =cut
71
72 sub subtitles {
73     my ( $self ) = @_;
74
75     return map { $_->{subfield} } @{
76         C4::Biblio::GetRecordValue(
77             'subtitle',
78             C4::Biblio::GetMarcBiblio({ biblionumber => $self->id }),
79             $self->frameworkcode ) };
80 }
81
82 =head3 can_article_request
83
84 my $bool = $biblio->can_article_request( $borrower );
85
86 Returns true if article requests can be made for this record
87
88 $borrower must be a Koha::Patron object
89
90 =cut
91
92 sub can_article_request {
93     my ( $self, $borrower ) = @_;
94
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';
98
99     return q{};
100 }
101
102 =head3 article_request_type
103
104 my $type = $biblio->article_request_type( $borrower );
105
106 Returns the article request type based on items, or on the record
107 itself if there are no items.
108
109 $borrower must be a Koha::Patron object
110
111 =cut
112
113 sub article_request_type {
114     my ( $self, $borrower ) = @_;
115
116     return q{} unless $borrower;
117
118     my $rule = $self->article_request_type_for_items( $borrower );
119     return $rule if $rule;
120
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;
124
125     return q{};
126 }
127
128 =head3 article_request_type_for_bib
129
130 my $type = $biblio->article_request_type_for_bib
131
132 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
133
134 =cut
135
136 sub article_request_type_for_bib {
137     my ( $self, $borrower ) = @_;
138
139     return q{} unless $borrower;
140
141     my $borrowertype = $borrower->categorycode;
142     my $itemtype     = $self->itemtype();
143
144     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
145
146     return q{} unless $issuing_rule;
147     return $issuing_rule->article_requests || q{}
148 }
149
150 =head3 article_request_type_for_items
151
152 my $type = $biblio->article_request_type_for_items
153
154 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
155
156 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
157
158 =cut
159
160 sub article_request_type_for_items {
161     my ( $self, $borrower ) = @_;
162
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}++;
168     }
169
170     return 'item_only' if $counts->{item_only};
171     return 'yes'       if $counts->{yes};
172     return 'no'        if $counts->{no};
173     return q{};
174 }
175
176 =head3 article_requests
177
178 my @requests = $biblio->article_requests
179
180 Returns the article requests associated with this Biblio
181
182 =cut
183
184 sub article_requests {
185     my ( $self, $borrower ) = @_;
186
187     $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
188
189     return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
190 }
191
192 =head3 article_requests_current
193
194 my @requests = $biblio->article_requests_current
195
196 Returns the article requests associated with this Biblio that are incomplete
197
198 =cut
199
200 sub article_requests_current {
201     my ( $self, $borrower ) = @_;
202
203     $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
204         {
205             biblionumber => $self->biblionumber(),
206             -or          => [
207                 { status => Koha::ArticleRequest::Status::Pending },
208                 { status => Koha::ArticleRequest::Status::Processing }
209             ]
210         }
211     );
212
213     return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
214 }
215
216 =head3 article_requests_finished
217
218 my @requests = $biblio->article_requests_finished
219
220 Returns the article requests associated with this Biblio that are completed
221
222 =cut
223
224 sub article_requests_finished {
225     my ( $self, $borrower ) = @_;
226
227     $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
228         {
229             biblionumber => $self->biblionumber(),
230             -or          => [
231                 { status => Koha::ArticleRequest::Status::Completed },
232                 { status => Koha::ArticleRequest::Status::Canceled }
233             ]
234         }
235     );
236
237     return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
238 }
239
240 =head3 items
241
242 my @items = $biblio->items();
243 my $items = $biblio->items();
244
245 Returns the related Koha::Items object for this biblio in scalar context,
246 or list of Koha::Item objects in list context.
247
248 =cut
249
250 sub items {
251     my ($self) = @_;
252
253     $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
254
255     return wantarray ? $self->{_items}->as_list : $self->{_items};
256 }
257
258 =head3 itemtype
259
260 my $itemtype = $biblio->itemtype();
261
262 Returns the itemtype for this record.
263
264 =cut
265
266 sub itemtype {
267     my ( $self ) = @_;
268
269     return $self->biblioitem()->itemtype();
270 }
271
272 =head3 holds
273
274 my $holds = $biblio->holds();
275
276 return the current holds placed on this record
277
278 =cut
279
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);
285 }
286
287 =head3 current_holds
288
289 my $holds = $biblio->current_holds
290
291 Return the holds placed on this bibliographic record.
292 It does not include future holds.
293
294 =cut
295
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) } } );
301 }
302
303 =head3 biblioitem
304
305 my $field = $self->biblioitem()->itemtype
306
307 Returns the related Koha::Biblioitem object for this Biblio object
308
309 =cut
310
311 sub biblioitem {
312     my ($self) = @_;
313
314     $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
315
316     return $self->{_biblioitem};
317 }
318
319 =head3 subscriptions
320
321 my $subscriptions = $self->subscriptions
322
323 Returns the related Koha::Subscriptions object for this Biblio object
324
325 =cut
326
327 sub subscriptions {
328     my ($self) = @_;
329
330     $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
331
332     return $self->{_subscriptions};
333 }
334
335 =head3 has_items_waiting_or_intransit
336
337 my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
338
339 Tells if this bibliographic record has items waiting or in transit.
340
341 =cut
342
343 sub has_items_waiting_or_intransit {
344     my ( $self ) = @_;
345
346     if ( Koha::Holds->search({ biblionumber => $self->id,
347                                found => ['W', 'T'] })->count ) {
348         return 1;
349     }
350
351     foreach my $item ( $self->items ) {
352         return 1 if $item->get_transfer;
353     }
354
355     return 0;
356 }
357
358 =head3 type
359
360 =cut
361
362 sub _type {
363     return 'Biblio';
364 }
365
366 =head1 AUTHOR
367
368 Kyle M Hall <kyle@bywatersolutions.com>
369
370 =cut
371
372 1;