Bug 23697: (follow-up) Add autocompletion search in patron module
[koha.git] / Koha / Item.pm
blob552e977e9752d30363bb176b3b1f0f7e8ad34f72
1 package Koha::Item;
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;
23 use List::MoreUtils qw(any);
25 use Koha::Database;
26 use Koha::DateUtils qw( dt_from_string );
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;
38 use base qw(Koha::Object);
40 =head1 NAME
42 Koha::Item - Koha Item object class
44 =head1 API
46 =head2 Class methods
48 =cut
50 =head3 effective_itemtype
52 Returns the itemtype for the item based on whether item level itemtypes are set or not.
54 =cut
56 sub effective_itemtype {
57 my ( $self ) = @_;
59 return $self->_result()->effective_itemtype();
62 =head3 home_branch
64 =cut
66 sub home_branch {
67 my ($self) = @_;
69 $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
71 return $self->{_home_branch};
74 =head3 holding_branch
76 =cut
78 sub holding_branch {
79 my ($self) = @_;
81 $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
83 return $self->{_holding_branch};
86 =head3 biblio
88 my $biblio = $item->biblio;
90 Return the bibliographic record of this item
92 =cut
94 sub biblio {
95 my ( $self ) = @_;
96 my $biblio_rs = $self->_result->biblio;
97 return Koha::Biblio->_new_from_dbic( $biblio_rs );
100 =head3 biblioitem
102 my $biblioitem = $item->biblioitem;
104 Return the biblioitem record of this item
106 =cut
108 sub biblioitem {
109 my ( $self ) = @_;
110 my $biblioitem_rs = $self->_result->biblioitem;
111 return Koha::Biblioitem->_new_from_dbic( $biblioitem_rs );
114 =head3 checkout
116 my $checkout = $item->checkout;
118 Return the checkout for this item
120 =cut
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 );
129 =head3 holds
131 my $holds = $item->holds();
132 my $holds = $item->holds($params);
133 my $holds = $item->holds({ found => 'W'});
135 Return holds attached to an item, optionally accept a hashref of params to pass to search
137 =cut
139 sub holds {
140 my ( $self,$params ) = @_;
141 my $transfer_rs = $self->_result->reserves->search($params);
142 return unless $transfer_rs->count;
143 return Koha::Holds->_new_from_dbic( $transfer_rs );
146 =head3 get_transfer
148 my $transfer = $item->get_transfer;
150 Return the transfer if the item is in transit or undef
152 =cut
154 sub get_transfer {
155 my ( $self ) = @_;
156 my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
157 return unless $transfer_rs;
158 return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
161 =head3 last_returned_by
163 Gets and sets the last borrower to return an item.
165 Accepts and returns Koha::Patron objects
167 $item->last_returned_by( $borrowernumber );
169 $last_returned_by = $item->last_returned_by();
171 =cut
173 sub last_returned_by {
174 my ( $self, $borrower ) = @_;
176 my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
178 if ($borrower) {
179 return $items_last_returned_by_rs->update_or_create(
180 { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
182 else {
183 unless ( $self->{_last_returned_by} ) {
184 my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
185 if ($result) {
186 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
190 return $self->{_last_returned_by};
194 =head3 can_article_request
196 my $bool = $item->can_article_request( $borrower )
198 Returns true if item can be specifically requested
200 $borrower must be a Koha::Patron object
202 =cut
204 sub can_article_request {
205 my ( $self, $borrower ) = @_;
207 my $rule = $self->article_request_type($borrower);
209 return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
210 return q{};
213 =head3 hidden_in_opac
215 my $bool = $item->hidden_in_opac({ [ rules => $rules ] })
217 Returns true if item fields match the hidding criteria defined in $rules.
218 Returns false otherwise.
220 Takes HASHref that can have the following parameters:
221 OPTIONAL PARAMETERS:
222 $rules : { <field> => [ value_1, ... ], ... }
224 Note: $rules inherits its structure from the parsed YAML from reading
225 the I<OpacHiddenItems> system preference.
227 =cut
229 sub hidden_in_opac {
230 my ( $self, $params ) = @_;
232 my $rules = $params->{rules} // {};
234 return 1
235 if C4::Context->preference('hidelostitems') and
236 $self->itemlost > 0;
238 my $hidden_in_opac = 0;
240 foreach my $field ( keys %{$rules} ) {
242 if ( any { $self->$field eq $_ } @{ $rules->{$field} } ) {
243 $hidden_in_opac = 1;
244 last;
248 return $hidden_in_opac;
251 =head3 can_be_transferred
253 $item->can_be_transferred({ to => $to_library, from => $from_library })
254 Checks if an item can be transferred to given library.
256 This feature is controlled by two system preferences:
257 UseBranchTransferLimits to enable / disable the feature
258 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
259 for setting the limitations
261 Takes HASHref that can have the following parameters:
262 MANDATORY PARAMETERS:
263 $to : Koha::Library
264 OPTIONAL PARAMETERS:
265 $from : Koha::Library # if not given, item holdingbranch
266 # will be used instead
268 Returns 1 if item can be transferred to $to_library, otherwise 0.
270 To find out whether at least one item of a Koha::Biblio can be transferred, please
271 see Koha::Biblio->can_be_transferred() instead of using this method for
272 multiple items of the same biblio.
274 =cut
276 sub can_be_transferred {
277 my ($self, $params) = @_;
279 my $to = $params->{to};
280 my $from = $params->{from};
282 $to = $to->branchcode;
283 $from = defined $from ? $from->branchcode : $self->holdingbranch;
285 return 1 if $from eq $to; # Transfer to current branch is allowed
286 return 1 unless C4::Context->preference('UseBranchTransferLimits');
288 my $limittype = C4::Context->preference('BranchTransferLimitsType');
289 return Koha::Item::Transfer::Limits->search({
290 toBranch => $to,
291 fromBranch => $from,
292 $limittype => $limittype eq 'itemtype'
293 ? $self->effective_itemtype : $self->ccode
294 })->count ? 0 : 1;
297 =head3 article_request_type
299 my $type = $item->article_request_type( $borrower )
301 returns 'yes', 'no', 'bib_only', or 'item_only'
303 $borrower must be a Koha::Patron object
305 =cut
307 sub article_request_type {
308 my ( $self, $borrower ) = @_;
310 my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
311 my $branchcode =
312 $branch_control eq 'homebranch' ? $self->homebranch
313 : $branch_control eq 'holdingbranch' ? $self->holdingbranch
314 : undef;
315 my $borrowertype = $borrower->categorycode;
316 my $itemtype = $self->effective_itemtype();
317 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
319 return q{} unless $issuing_rule;
320 return $issuing_rule->article_requests || q{}
323 =head3 current_holds
325 =cut
327 sub current_holds {
328 my ( $self ) = @_;
329 my $attributes = { order_by => 'priority' };
330 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
331 my $params = {
332 itemnumber => $self->itemnumber,
333 suspend => 0,
334 -or => [
335 reservedate => { '<=' => $dtf->format_date(dt_from_string) },
336 waitingdate => { '!=' => undef },
339 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
340 return Koha::Holds->_new_from_dbic($hold_rs);
343 =head3 stockrotationitem
345 my $sritem = Koha::Item->stockrotationitem;
347 Returns the stock rotation item associated with the current item.
349 =cut
351 sub stockrotationitem {
352 my ( $self ) = @_;
353 my $rs = $self->_result->stockrotationitem;
354 return 0 if !$rs;
355 return Koha::StockRotationItem->_new_from_dbic( $rs );
358 =head3 add_to_rota
360 my $item = $item->add_to_rota($rota_id);
362 Add this item to the rota identified by $ROTA_ID, which means associating it
363 with the first stage of that rota. Should this item already be associated
364 with a rota, then we will move it to the new rota.
366 =cut
368 sub add_to_rota {
369 my ( $self, $rota_id ) = @_;
370 Koha::StockRotationRotas->find($rota_id)->add_item($self->itemnumber);
371 return $self;
374 =head3 has_pending_hold
376 my $is_pending_hold = $item->has_pending_hold();
378 This method checks the tmp_holdsqueue to see if this item has been selected for a hold, but not filled yet and returns true or false
380 =cut
382 sub has_pending_hold {
383 my ( $self ) = @_;
384 my $pending_hold = $self->_result->tmp_holdsqueues;
385 return !C4::Context->preference('AllowItemsOnHoldCheckout') && $pending_hold->count ? 1: 0;
388 =head2 Internal methods
390 =head3 _type
392 =cut
394 sub _type {
395 return 'Item';
398 =head1 AUTHOR
400 Kyle M Hall <kyle@bywatersolutions.com>
402 =cut