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
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.
23 use List
::MoreUtils
qw(any);
26 use Koha
::DateUtils
qw( dt_from_string );
30 use Koha
::IssuingRules
;
31 use Koha
::Item
::Transfer
::Limits
;
32 use Koha
::Item
::Transfers
;
35 use Koha
::StockRotationItem
;
36 use Koha
::StockRotationRotas
;
38 use base
qw(Koha::Object);
42 Koha::Item - Koha Item object class
50 =head3 effective_itemtype
52 Returns the itemtype for the item based on whether item level itemtypes are set or not.
56 sub effective_itemtype
{
59 return $self->_result()->effective_itemtype();
69 $self->{_home_branch
} ||= Koha
::Libraries
->find( $self->homebranch() );
71 return $self->{_home_branch
};
81 $self->{_holding_branch
} ||= Koha
::Libraries
->find( $self->holdingbranch() );
83 return $self->{_holding_branch
};
88 my $biblio = $item->biblio;
90 Return the bibliographic record of this item
96 my $biblio_rs = $self->_result->biblio;
97 return Koha
::Biblio
->_new_from_dbic( $biblio_rs );
102 my $biblioitem = $item->biblioitem;
104 Return the biblioitem record of this item
110 my $biblioitem_rs = $self->_result->biblioitem;
111 return Koha
::Biblioitem
->_new_from_dbic( $biblioitem_rs );
116 my $checkout = $item->checkout;
118 Return the checkout for this item
124 my $checkout_rs = $self->_result->issue;
125 return unless $checkout_rs;
126 return Koha
::Checkout
->_new_from_dbic( $checkout_rs );
131 my $transfer = $item->get_transfer;
133 Return the transfer if the item is in transit or undef
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 );
144 =head3 last_returned_by
146 Gets and sets the last borrower to return an item.
148 Accepts and returns Koha::Patron objects
150 $item->last_returned_by( $borrowernumber );
152 $last_returned_by = $item->last_returned_by();
156 sub last_returned_by
{
157 my ( $self, $borrower ) = @_;
159 my $items_last_returned_by_rs = Koha
::Database
->new()->schema()->resultset('ItemsLastBorrower');
162 return $items_last_returned_by_rs->update_or_create(
163 { borrowernumber
=> $borrower->borrowernumber, itemnumber
=> $self->id } );
166 unless ( $self->{_last_returned_by
} ) {
167 my $result = $items_last_returned_by_rs->single( { itemnumber
=> $self->id } );
169 $self->{_last_returned_by
} = Koha
::Patrons
->find( $result->get_column('borrowernumber') );
173 return $self->{_last_returned_by
};
177 =head3 can_article_request
179 my $bool = $item->can_article_request( $borrower )
181 Returns true if item can be specifically requested
183 $borrower must be a Koha::Patron object
187 sub can_article_request
{
188 my ( $self, $borrower ) = @_;
190 my $rule = $self->article_request_type($borrower);
192 return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
196 =head3 hidden_in_opac
198 my $bool = $item->hidden_in_opac({ [ rules => $rules ] })
200 Returns true if item fields match the hidding criteria defined in $rules.
201 Returns false otherwise.
203 Takes HASHref that can have the following parameters:
205 $rules : { <field> => [ value_1, ... ], ... }
207 Note: $rules inherits its structure from the parsed YAML from reading
208 the I<OpacHiddenItems> system preference.
213 my ( $self, $params ) = @_;
215 my $rules = $params->{rules
} // {};
218 if C4
::Context
->preference('hidelostitems') and
221 my $hidden_in_opac = 0;
223 foreach my $field ( keys %{$rules} ) {
225 if ( any
{ $self->$field eq $_ } @
{ $rules->{$field} } ) {
231 return $hidden_in_opac;
234 =head3 can_be_transferred
236 $item->can_be_transferred({ to => $to_library, from => $from_library })
237 Checks if an item can be transferred to given library.
239 This feature is controlled by two system preferences:
240 UseBranchTransferLimits to enable / disable the feature
241 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
242 for setting the limitations
244 Takes HASHref that can have the following parameters:
245 MANDATORY PARAMETERS:
248 $from : Koha::Library # if not given, item holdingbranch
249 # will be used instead
251 Returns 1 if item can be transferred to $to_library, otherwise 0.
253 To find out whether at least one item of a Koha::Biblio can be transferred, please
254 see Koha::Biblio->can_be_transferred() instead of using this method for
255 multiple items of the same biblio.
259 sub can_be_transferred
{
260 my ($self, $params) = @_;
262 my $to = $params->{to
};
263 my $from = $params->{from
};
265 $to = $to->branchcode;
266 $from = defined $from ?
$from->branchcode : $self->holdingbranch;
268 return 1 if $from eq $to; # Transfer to current branch is allowed
269 return 1 unless C4
::Context
->preference('UseBranchTransferLimits');
271 my $limittype = C4
::Context
->preference('BranchTransferLimitsType');
272 return Koha
::Item
::Transfer
::Limits
->search({
275 $limittype => $limittype eq 'itemtype'
276 ?
$self->effective_itemtype : $self->ccode
280 =head3 article_request_type
282 my $type = $item->article_request_type( $borrower )
284 returns 'yes', 'no', 'bib_only', or 'item_only'
286 $borrower must be a Koha::Patron object
290 sub article_request_type
{
291 my ( $self, $borrower ) = @_;
293 my $branch_control = C4
::Context
->preference('HomeOrHoldingBranch');
295 $branch_control eq 'homebranch' ?
$self->homebranch
296 : $branch_control eq 'holdingbranch' ?
$self->holdingbranch
298 my $borrowertype = $borrower->categorycode;
299 my $itemtype = $self->effective_itemtype();
300 my $issuing_rule = Koha
::IssuingRules
->get_effective_issuing_rule({ categorycode
=> $borrowertype, itemtype
=> $itemtype, branchcode
=> $branchcode });
302 return q{} unless $issuing_rule;
303 return $issuing_rule->article_requests || q{}
312 my $attributes = { order_by
=> 'priority' };
313 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
315 itemnumber
=> $self->itemnumber,
318 reservedate
=> { '<=' => $dtf->format_date(dt_from_string
) },
319 waitingdate
=> { '!=' => undef },
322 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
323 return Koha
::Holds
->_new_from_dbic($hold_rs);
326 =head3 stockrotationitem
328 my $sritem = Koha::Item->stockrotationitem;
330 Returns the stock rotation item associated with the current item.
334 sub stockrotationitem
{
336 my $rs = $self->_result->stockrotationitem;
338 return Koha
::StockRotationItem
->_new_from_dbic( $rs );
343 my $item = $item->add_to_rota($rota_id);
345 Add this item to the rota identified by $ROTA_ID, which means associating it
346 with the first stage of that rota. Should this item already be associated
347 with a rota, then we will move it to the new rota.
352 my ( $self, $rota_id ) = @_;
353 Koha
::StockRotationRotas
->find($rota_id)->add_item($self->itemnumber);
357 =head2 Internal methods
369 Kyle M Hall <kyle@bywatersolutions.com>