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
::Limits
;
31 use Koha
::Item
::Transfers
;
35 use base
qw(Koha::Object);
39 Koha::Item - Koha Item object class
47 =head3 effective_itemtype
49 Returns the itemtype for the item based on whether item level itemtypes are set or not.
53 sub effective_itemtype
{
56 return $self->_result()->effective_itemtype();
66 $self->{_home_branch
} ||= Koha
::Libraries
->find( $self->homebranch() );
68 return $self->{_home_branch
};
78 $self->{_holding_branch
} ||= Koha
::Libraries
->find( $self->holdingbranch() );
80 return $self->{_holding_branch
};
85 my $biblio = $item->biblio;
87 Return the bibliographic record of this item
93 my $biblio_rs = $self->_result->biblio;
94 return Koha
::Biblio
->_new_from_dbic( $biblio_rs );
99 my $biblioitem = $item->biblioitem;
101 Return the biblioitem record of this item
107 my $biblioitem_rs = $self->_result->biblioitem;
108 return Koha
::Biblioitem
->_new_from_dbic( $biblioitem_rs );
113 my $checkout = $item->checkout;
115 Return the checkout for this item
121 my $checkout_rs = $self->_result->issue;
122 return unless $checkout_rs;
123 return Koha
::Checkout
->_new_from_dbic( $checkout_rs );
128 my $transfer = $item->get_transfer;
130 Return the transfer if the item is in transit or undef
136 my $transfer_rs = $self->_result->branchtransfers->search({ datearrived
=> undef })->first;
137 return unless $transfer_rs;
138 return Koha
::Item
::Transfer
->_new_from_dbic( $transfer_rs );
141 =head3 last_returned_by
143 Gets and sets the last borrower to return an item.
145 Accepts and returns Koha::Patron objects
147 $item->last_returned_by( $borrowernumber );
149 $last_returned_by = $item->last_returned_by();
153 sub last_returned_by
{
154 my ( $self, $borrower ) = @_;
156 my $items_last_returned_by_rs = Koha
::Database
->new()->schema()->resultset('ItemsLastBorrower');
159 return $items_last_returned_by_rs->update_or_create(
160 { borrowernumber
=> $borrower->borrowernumber, itemnumber
=> $self->id } );
163 unless ( $self->{_last_returned_by
} ) {
164 my $result = $items_last_returned_by_rs->single( { itemnumber
=> $self->id } );
166 $self->{_last_returned_by
} = Koha
::Patrons
->find( $result->get_column('borrowernumber') );
170 return $self->{_last_returned_by
};
174 =head3 can_article_request
176 my $bool = $item->can_article_request( $borrower )
178 Returns true if item can be specifically requested
180 $borrower must be a Koha::Patron object
184 sub can_article_request
{
185 my ( $self, $borrower ) = @_;
187 my $rule = $self->article_request_type($borrower);
189 return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
193 =head3 can_be_transferred
195 $item->can_be_transferred({ to => $to_library, from => $from_library })
196 Checks if an item can be transferred to given library.
198 This feature is controlled by two system preferences:
199 UseBranchTransferLimits to enable / disable the feature
200 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
201 for setting the limitations
203 Takes HASHref that can have the following parameters:
204 MANDATORY PARAMETERS:
207 $from : Koha::Library # if not given, item holdingbranch
208 # will be used instead
210 Returns 1 if item can be transferred to $to_library, otherwise 0.
212 To find out whether at least one item of a Koha::Biblio can be transferred, please
213 see Koha::Biblio->can_be_transferred() instead of using this method for
214 multiple items of the same biblio.
218 sub can_be_transferred
{
219 my ($self, $params) = @_;
221 my $to = $params->{to
};
222 my $from = $params->{from
};
224 $to = $to->branchcode;
225 $from = defined $from ?
$from->branchcode : $self->holdingbranch;
227 return 1 if $from eq $to; # Transfer to current branch is allowed
228 return 1 unless C4
::Context
->preference('UseBranchTransferLimits');
230 my $limittype = C4
::Context
->preference('BranchTransferLimitsType');
231 return Koha
::Item
::Transfer
::Limits
->search({
234 $limittype => $limittype eq 'itemtype'
235 ?
$self->effective_itemtype : $self->ccode
239 =head3 article_request_type
241 my $type = $item->article_request_type( $borrower )
243 returns 'yes', 'no', 'bib_only', or 'item_only'
245 $borrower must be a Koha::Patron object
249 sub article_request_type
{
250 my ( $self, $borrower ) = @_;
252 my $branch_control = C4
::Context
->preference('HomeOrHoldingBranch');
254 $branch_control eq 'homebranch' ?
$self->homebranch
255 : $branch_control eq 'holdingbranch' ?
$self->holdingbranch
257 my $borrowertype = $borrower->categorycode;
258 my $itemtype = $self->effective_itemtype();
259 my $issuing_rule = Koha
::IssuingRules
->get_effective_issuing_rule({ categorycode
=> $borrowertype, itemtype
=> $itemtype, branchcode
=> $branchcode });
261 return q{} unless $issuing_rule;
262 return $issuing_rule->article_requests || q{}
271 my $attributes = { order_by
=> 'priority' };
272 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
274 itemnumber
=> $self->itemnumber,
277 reservedate
=> { '<=' => $dtf->format_date(dt_from_string
) },
278 waitingdate
=> { '!=' => undef },
281 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
282 return Koha
::Holds
->_new_from_dbic($hold_rs);
295 Kyle M Hall <kyle@bywatersolutions.com>