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.
25 use Koha
::DateUtils
qw( dt_from_string );
29 use Koha
::IssuingRules
;
30 use Koha
::Item
::Transfer
;
34 use base
qw(Koha::Object);
38 Koha::Item - Koha Item object class
46 =head3 effective_itemtype
48 Returns the itemtype for the item based on whether item level itemtypes are set or not.
52 sub effective_itemtype
{
55 return $self->_result()->effective_itemtype();
65 $self->{_home_branch
} ||= Koha
::Libraries
->find( $self->homebranch() );
67 return $self->{_home_branch
};
77 $self->{_holding_branch
} ||= Koha
::Libraries
->find( $self->holdingbranch() );
79 return $self->{_holding_branch
};
84 my $biblio = $item->biblio;
86 Return the bibliographic record of this item
92 my $biblio_rs = $self->_result->biblio;
93 return Koha
::Biblio
->_new_from_dbic( $biblio_rs );
98 my $biblioitem = $item->biblioitem;
100 Return the biblioitem record of this item
106 my $biblioitem_rs = $self->_result->biblioitem;
107 return Koha
::Biblioitem
->_new_from_dbic( $biblioitem_rs );
112 my $checkout = $item->checkout;
114 Return the checkout for this item
120 my $checkout_rs = $self->_result->issue;
121 return unless $checkout_rs;
122 return Koha
::Checkout
->_new_from_dbic( $checkout_rs );
127 my $transfer = $item->get_transfer;
129 Return the transfer if the item is in transit or undef
135 my $transfer_rs = $self->_result->branchtransfers->search({ datearrived
=> undef })->first;
136 return unless $transfer_rs;
137 return Koha
::Item
::Transfer
->_new_from_dbic( $transfer_rs );
140 =head3 last_returned_by
142 Gets and sets the last borrower to return an item.
144 Accepts and returns Koha::Patron objects
146 $item->last_returned_by( $borrowernumber );
148 $last_returned_by = $item->last_returned_by();
152 sub last_returned_by
{
153 my ( $self, $borrower ) = @_;
155 my $items_last_returned_by_rs = Koha
::Database
->new()->schema()->resultset('ItemsLastBorrower');
158 return $items_last_returned_by_rs->update_or_create(
159 { borrowernumber
=> $borrower->borrowernumber, itemnumber
=> $self->id } );
162 unless ( $self->{_last_returned_by
} ) {
163 my $result = $items_last_returned_by_rs->single( { itemnumber
=> $self->id } );
165 $self->{_last_returned_by
} = Koha
::Patrons
->find( $result->get_column('borrowernumber') );
169 return $self->{_last_returned_by
};
173 =head3 can_article_request
175 my $bool = $item->can_article_request( $borrower )
177 Returns true if item can be specifically requested
179 $borrower must be a Koha::Patron object
183 sub can_article_request
{
184 my ( $self, $borrower ) = @_;
186 my $rule = $self->article_request_type($borrower);
188 return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
192 =head3 article_request_type
194 my $type = $item->article_request_type( $borrower )
196 returns 'yes', 'no', 'bib_only', or 'item_only'
198 $borrower must be a Koha::Patron object
202 sub article_request_type
{
203 my ( $self, $borrower ) = @_;
205 my $branch_control = C4
::Context
->preference('HomeOrHoldingBranch');
207 $branch_control eq 'homebranch' ?
$self->homebranch
208 : $branch_control eq 'holdingbranch' ?
$self->holdingbranch
210 my $borrowertype = $borrower->categorycode;
211 my $itemtype = $self->effective_itemtype();
212 my $issuing_rule = Koha
::IssuingRules
->get_effective_issuing_rule({ categorycode
=> $borrowertype, itemtype
=> $itemtype, branchcode
=> $branchcode });
214 return q{} unless $issuing_rule;
215 return $issuing_rule->article_requests || q{}
224 my $attributes = { order_by
=> 'priority' };
225 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
227 itemnumber
=> $self->itemnumber,
230 reservedate
=> { '<=' => $dtf->format_date(dt_from_string
) },
231 waitingdate
=> { '!=' => undef },
234 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
235 return Koha
::Holds
->_new_from_dbic($hold_rs);
248 Kyle M Hall <kyle@bywatersolutions.com>