Bug 17944 - Add Koha::ItemType->can_be_deleted and use it from admin/itemtypes.pl
[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 subtitles
49
50 my @subtitles = $biblio->subtitles();
51
52 Returns list of subtitles for a record.
53
54 Keyword to MARC mapping for subtitle must be set for this method to return any possible values.
55
56 =cut
57
58 sub subtitles {
59     my ( $self ) = @_;
60
61     return map { $_->{subfield} } @{ C4::Biblio::GetRecordValue( 'subtitle', C4::Biblio::GetMarcBiblio( $self->id ), $self->frameworkcode ) };
62 }
63
64 =head3 can_article_request
65
66 my $bool = $biblio->can_article_request( $borrower );
67
68 Returns true if article requests can be made for this record
69
70 $borrower must be a Koha::Patron object
71
72 =cut
73
74 sub can_article_request {
75     my ( $self, $borrower ) = @_;
76
77     my $rule = $self->article_request_type($borrower);
78     return q{} if $rule eq 'item_only' && !$self->items()->count();
79     return 1 if $rule && $rule ne 'no';
80
81     return q{};
82 }
83
84 =head3 article_request_type
85
86 my $type = $biblio->article_request_type( $borrower );
87
88 Returns the article request type based on items, or on the record
89 itself if there are no items.
90
91 $borrower must be a Koha::Patron object
92
93 =cut
94
95 sub article_request_type {
96     my ( $self, $borrower ) = @_;
97
98     return q{} unless $borrower;
99
100     my $rule = $self->article_request_type_for_items( $borrower );
101     return $rule if $rule;
102
103     # If the record has no items that are requestable, go by the record itemtype
104     $rule = $self->article_request_type_for_bib($borrower);
105     return $rule if $rule;
106
107     return q{};
108 }
109
110 =head3 article_request_type_for_bib
111
112 my $type = $biblio->article_request_type_for_bib
113
114 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record
115
116 =cut
117
118 sub article_request_type_for_bib {
119     my ( $self, $borrower ) = @_;
120
121     return q{} unless $borrower;
122
123     my $borrowertype = $borrower->categorycode;
124     my $itemtype     = $self->itemtype();
125
126     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
127
128     return q{} unless $issuing_rule;
129     return $issuing_rule->article_requests || q{}
130 }
131
132 =head3 article_request_type_for_items
133
134 my $type = $biblio->article_request_type_for_items
135
136 Returns the article request type 'yes', 'no', 'item_only', 'bib_only', for the given record's items
137
138 If there is a conflict where some items are 'bib_only' and some are 'item_only', 'bib_only' will be returned.
139
140 =cut
141
142 sub article_request_type_for_items {
143     my ( $self, $borrower ) = @_;
144
145     my $counts;
146     foreach my $item ( $self->items()->as_list() ) {
147         my $rule = $item->article_request_type($borrower);
148         return $rule if $rule eq 'bib_only';    # we don't need to go any further
149         $counts->{$rule}++;
150     }
151
152     return 'item_only' if $counts->{item_only};
153     return 'yes'       if $counts->{yes};
154     return 'no'        if $counts->{no};
155     return q{};
156 }
157
158 =head3 article_requests
159
160 my @requests = $biblio->article_requests
161
162 Returns the article requests associated with this Biblio
163
164 =cut
165
166 sub article_requests {
167     my ( $self, $borrower ) = @_;
168
169     $self->{_article_requests} ||= Koha::ArticleRequests->search( { biblionumber => $self->biblionumber() } );
170
171     return wantarray ? $self->{_article_requests}->as_list : $self->{_article_requests};
172 }
173
174 =head3 article_requests_current
175
176 my @requests = $biblio->article_requests_current
177
178 Returns the article requests associated with this Biblio that are incomplete
179
180 =cut
181
182 sub article_requests_current {
183     my ( $self, $borrower ) = @_;
184
185     $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
186         {
187             biblionumber => $self->biblionumber(),
188             -or          => [
189                 { status => Koha::ArticleRequest::Status::Pending },
190                 { status => Koha::ArticleRequest::Status::Processing }
191             ]
192         }
193     );
194
195     return wantarray ? $self->{_article_requests_current}->as_list : $self->{_article_requests_current};
196 }
197
198 =head3 article_requests_finished
199
200 my @requests = $biblio->article_requests_finished
201
202 Returns the article requests associated with this Biblio that are completed
203
204 =cut
205
206 sub article_requests_finished {
207     my ( $self, $borrower ) = @_;
208
209     $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
210         {
211             biblionumber => $self->biblionumber(),
212             -or          => [
213                 { status => Koha::ArticleRequest::Status::Completed },
214                 { status => Koha::ArticleRequest::Status::Canceled }
215             ]
216         }
217     );
218
219     return wantarray ? $self->{_article_requests_finished}->as_list : $self->{_article_requests_finished};
220 }
221
222 =head3 items
223
224 my @items = $biblio->items();
225 my $items = $biblio->items();
226
227 Returns the related Koha::Items object for this biblio in scalar context,
228 or list of Koha::Item objects in list context.
229
230 =cut
231
232 sub items {
233     my ($self) = @_;
234
235     $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
236
237     return wantarray ? $self->{_items}->as_list : $self->{_items};
238 }
239
240 =head3 itemtype
241
242 my $itemtype = $biblio->itemtype();
243
244 Returns the itemtype for this record.
245
246 =cut
247
248 sub itemtype {
249     my ( $self ) = @_;
250
251     return $self->biblioitem()->itemtype();
252 }
253
254 =head3 holds
255
256 my $holds = $biblio->holds();
257
258 return the current holds placed on this record
259
260 =cut
261
262 sub holds {
263     my ( $self, $params, $attributes ) = @_;
264     $attributes->{order_by} = 'priority' unless exists $attributes->{order_by};
265     my $hold_rs = $self->_result->reserves->search( $params, $attributes );
266     return Koha::Holds->_new_from_dbic($hold_rs);
267 }
268
269 =head3 current_holds
270
271 my $holds = $biblio->current_holds
272
273 Return the holds placed on this bibliographic record.
274 It does not include future holds.
275
276 =cut
277
278 sub current_holds {
279     my ($self) = @_;
280     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
281     return $self->holds(
282         { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
283 }
284
285 =head3 biblioitem
286
287 my $field = $self->biblioitem()->itemtype
288
289 Returns the related Koha::Biblioitem object for this Biblio object
290
291 =cut
292
293 sub biblioitem {
294     my ($self) = @_;
295
296     $self->{_biblioitem} ||= Koha::Biblioitems->find( { biblionumber => $self->biblionumber() } );
297
298     return $self->{_biblioitem};
299 }
300
301 =head3 subscriptions
302
303 my $subscriptions = $self->subscriptions
304
305 Returns the related Koha::Subscriptions object for this Biblio object
306
307 =cut
308
309 sub subscriptions {
310     my ($self) = @_;
311
312     $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
313
314     return $self->{_subscriptions};
315 }
316
317
318 =head3 type
319
320 =cut
321
322 sub _type {
323     return 'Biblio';
324 }
325
326 =head1 AUTHOR
327
328 Kyle M Hall <kyle@bywatersolutions.com>
329
330 =cut
331
332 1;