66e0f9c73baf33ccbcacdaf50e3f189a0ffa37da
[koha.git] / Koha / Item.pm
1 package Koha::Item;
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 use List::MoreUtils qw(any);
24
25 use Koha::Database;
26 use Koha::DateUtils qw( dt_from_string );
27
28 use C4::Context;
29 use Koha::Checkouts;
30 use Koha::IssuingRules;
31 use Koha::Item::Transfer::Limits;
32 use Koha::Item::Transfers;
33 use Koha::Patrons;
34 use Koha::Libraries;
35 use Koha::StockRotationItem;
36 use Koha::StockRotationRotas;
37
38 use base qw(Koha::Object);
39
40 =head1 NAME
41
42 Koha::Item - Koha Item object class
43
44 =head1 API
45
46 =head2 Class methods
47
48 =cut
49
50 =head3 effective_itemtype
51
52 Returns the itemtype for the item based on whether item level itemtypes are set or not.
53
54 =cut
55
56 sub effective_itemtype {
57     my ( $self ) = @_;
58
59     return $self->_result()->effective_itemtype();
60 }
61
62 =head3 home_branch
63
64 =cut
65
66 sub home_branch {
67     my ($self) = @_;
68
69     $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
70
71     return $self->{_home_branch};
72 }
73
74 =head3 holding_branch
75
76 =cut
77
78 sub holding_branch {
79     my ($self) = @_;
80
81     $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
82
83     return $self->{_holding_branch};
84 }
85
86 =head3 biblio
87
88 my $biblio = $item->biblio;
89
90 Return the bibliographic record of this item
91
92 =cut
93
94 sub biblio {
95     my ( $self ) = @_;
96     my $biblio_rs = $self->_result->biblio;
97     return Koha::Biblio->_new_from_dbic( $biblio_rs );
98 }
99
100 =head3 biblioitem
101
102 my $biblioitem = $item->biblioitem;
103
104 Return the biblioitem record of this item
105
106 =cut
107
108 sub biblioitem {
109     my ( $self ) = @_;
110     my $biblioitem_rs = $self->_result->biblioitem;
111     return Koha::Biblioitem->_new_from_dbic( $biblioitem_rs );
112 }
113
114 =head3 checkout
115
116 my $checkout = $item->checkout;
117
118 Return the checkout for this item
119
120 =cut
121
122 sub checkout {
123     my ( $self ) = @_;
124     my $checkout_rs = $self->_result->issue;
125     return unless $checkout_rs;
126     return Koha::Checkout->_new_from_dbic( $checkout_rs );
127 }
128
129 =head3 get_transfer
130
131 my $transfer = $item->get_transfer;
132
133 Return the transfer if the item is in transit or undef
134
135 =cut
136
137 sub get_transfer {
138     my ( $self ) = @_;
139     my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
140     return unless $transfer_rs;
141     return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
142 }
143
144 =head3 last_returned_by
145
146 Gets and sets the last borrower to return an item.
147
148 Accepts and returns Koha::Patron objects
149
150 $item->last_returned_by( $borrowernumber );
151
152 $last_returned_by = $item->last_returned_by();
153
154 =cut
155
156 sub last_returned_by {
157     my ( $self, $borrower ) = @_;
158
159     my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
160
161     if ($borrower) {
162         return $items_last_returned_by_rs->update_or_create(
163             { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
164     }
165     else {
166         unless ( $self->{_last_returned_by} ) {
167             my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
168             if ($result) {
169                 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
170             }
171         }
172
173         return $self->{_last_returned_by};
174     }
175 }
176
177 =head3 can_article_request
178
179 my $bool = $item->can_article_request( $borrower )
180
181 Returns true if item can be specifically requested
182
183 $borrower must be a Koha::Patron object
184
185 =cut
186
187 sub can_article_request {
188     my ( $self, $borrower ) = @_;
189
190     my $rule = $self->article_request_type($borrower);
191
192     return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
193     return q{};
194 }
195
196 =head3 hidden_in_opac
197
198 my $bool = $item->hidden_in_opac({ [ rules => $rules ] })
199
200 Returns true if item fields match the hidding criteria defined in $rules.
201 Returns false otherwise.
202
203 Takes HASHref that can have the following parameters:
204     OPTIONAL PARAMETERS:
205     $rules : { <field> => [ value_1, ... ], ... }
206
207 Note: $rules inherits its structure from the parsed YAML from reading
208 the I<OpacHiddenItems> system preference.
209
210 =cut
211
212 sub hidden_in_opac {
213     my ( $self, $params ) = @_;
214
215     my $rules = $params->{rules} // {};
216
217     my $hidden_in_opac = 0;
218
219     foreach my $field ( keys %{$rules} ) {
220
221         if ( any { $self->$field eq $_ } @{ $rules->{$field} } ) {
222             $hidden_in_opac = 1;
223             last;
224         }
225     }
226
227     return $hidden_in_opac;
228 }
229
230 =head3 can_be_transferred
231
232 $item->can_be_transferred({ to => $to_library, from => $from_library })
233 Checks if an item can be transferred to given library.
234
235 This feature is controlled by two system preferences:
236 UseBranchTransferLimits to enable / disable the feature
237 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
238                          for setting the limitations
239
240 Takes HASHref that can have the following parameters:
241     MANDATORY PARAMETERS:
242     $to   : Koha::Library
243     OPTIONAL PARAMETERS:
244     $from : Koha::Library  # if not given, item holdingbranch
245                            # will be used instead
246
247 Returns 1 if item can be transferred to $to_library, otherwise 0.
248
249 To find out whether at least one item of a Koha::Biblio can be transferred, please
250 see Koha::Biblio->can_be_transferred() instead of using this method for
251 multiple items of the same biblio.
252
253 =cut
254
255 sub can_be_transferred {
256     my ($self, $params) = @_;
257
258     my $to   = $params->{to};
259     my $from = $params->{from};
260
261     $to   = $to->branchcode;
262     $from = defined $from ? $from->branchcode : $self->holdingbranch;
263
264     return 1 if $from eq $to; # Transfer to current branch is allowed
265     return 1 unless C4::Context->preference('UseBranchTransferLimits');
266
267     my $limittype = C4::Context->preference('BranchTransferLimitsType');
268     return Koha::Item::Transfer::Limits->search({
269         toBranch => $to,
270         fromBranch => $from,
271         $limittype => $limittype eq 'itemtype'
272                         ? $self->effective_itemtype : $self->ccode
273     })->count ? 0 : 1;
274 }
275
276 =head3 article_request_type
277
278 my $type = $item->article_request_type( $borrower )
279
280 returns 'yes', 'no', 'bib_only', or 'item_only'
281
282 $borrower must be a Koha::Patron object
283
284 =cut
285
286 sub article_request_type {
287     my ( $self, $borrower ) = @_;
288
289     my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
290     my $branchcode =
291         $branch_control eq 'homebranch'    ? $self->homebranch
292       : $branch_control eq 'holdingbranch' ? $self->holdingbranch
293       :                                      undef;
294     my $borrowertype = $borrower->categorycode;
295     my $itemtype = $self->effective_itemtype();
296     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
297
298     return q{} unless $issuing_rule;
299     return $issuing_rule->article_requests || q{}
300 }
301
302 =head3 current_holds
303
304 =cut
305
306 sub current_holds {
307     my ( $self ) = @_;
308     my $attributes = { order_by => 'priority' };
309     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
310     my $params = {
311         itemnumber => $self->itemnumber,
312         suspend => 0,
313         -or => [
314             reservedate => { '<=' => $dtf->format_date(dt_from_string) },
315             waitingdate => { '!=' => undef },
316         ],
317     };
318     my $hold_rs = $self->_result->reserves->search( $params, $attributes );
319     return Koha::Holds->_new_from_dbic($hold_rs);
320 }
321
322 =head3 stockrotationitem
323
324   my $sritem = Koha::Item->stockrotationitem;
325
326 Returns the stock rotation item associated with the current item.
327
328 =cut
329
330 sub stockrotationitem {
331     my ( $self ) = @_;
332     my $rs = $self->_result->stockrotationitem;
333     return 0 if !$rs;
334     return Koha::StockRotationItem->_new_from_dbic( $rs );
335 }
336
337 =head3 add_to_rota
338
339   my $item = $item->add_to_rota($rota_id);
340
341 Add this item to the rota identified by $ROTA_ID, which means associating it
342 with the first stage of that rota.  Should this item already be associated
343 with a rota, then we will move it to the new rota.
344
345 =cut
346
347 sub add_to_rota {
348     my ( $self, $rota_id ) = @_;
349     Koha::StockRotationRotas->find($rota_id)->add_item($self->itemnumber);
350     return $self;
351 }
352
353 =head2 Internal methods
354
355 =head3 _type
356
357 =cut
358
359 sub _type {
360     return 'Item';
361 }
362
363 =head1 AUTHOR
364
365 Kyle M Hall <kyle@bywatersolutions.com>
366
367 =cut
368
369 1;