Bug 18639: (follow-up) Populate replacement price from list price during ordering
[koha.git] / Koha / Item.pm
blob964359ecf8e773e9fede6b42e76072953b1fb0a8
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;
24 use Koha::Database;
25 use Koha::DateUtils qw( dt_from_string );
27 use C4::Context;
28 use Koha::Checkouts;
29 use Koha::IssuingRules;
30 use Koha::Item::Transfer::Limits;
31 use Koha::Item::Transfers;
32 use Koha::Patrons;
33 use Koha::Libraries;
35 use base qw(Koha::Object);
37 =head1 NAME
39 Koha::Item - Koha Item object class
41 =head1 API
43 =head2 Class Methods
45 =cut
47 =head3 effective_itemtype
49 Returns the itemtype for the item based on whether item level itemtypes are set or not.
51 =cut
53 sub effective_itemtype {
54 my ( $self ) = @_;
56 return $self->_result()->effective_itemtype();
59 =head3 home_branch
61 =cut
63 sub home_branch {
64 my ($self) = @_;
66 $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
68 return $self->{_home_branch};
71 =head3 holding_branch
73 =cut
75 sub holding_branch {
76 my ($self) = @_;
78 $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
80 return $self->{_holding_branch};
83 =head3 biblio
85 my $biblio = $item->biblio;
87 Return the bibliographic record of this item
89 =cut
91 sub biblio {
92 my ( $self ) = @_;
93 my $biblio_rs = $self->_result->biblio;
94 return Koha::Biblio->_new_from_dbic( $biblio_rs );
97 =head3 biblioitem
99 my $biblioitem = $item->biblioitem;
101 Return the biblioitem record of this item
103 =cut
105 sub biblioitem {
106 my ( $self ) = @_;
107 my $biblioitem_rs = $self->_result->biblioitem;
108 return Koha::Biblioitem->_new_from_dbic( $biblioitem_rs );
111 =head3 checkout
113 my $checkout = $item->checkout;
115 Return the checkout for this item
117 =cut
119 sub checkout {
120 my ( $self ) = @_;
121 my $checkout_rs = $self->_result->issue;
122 return unless $checkout_rs;
123 return Koha::Checkout->_new_from_dbic( $checkout_rs );
126 =head3 get_transfer
128 my $transfer = $item->get_transfer;
130 Return the transfer if the item is in transit or undef
132 =cut
134 sub get_transfer {
135 my ( $self ) = @_;
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();
151 =cut
153 sub last_returned_by {
154 my ( $self, $borrower ) = @_;
156 my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
158 if ($borrower) {
159 return $items_last_returned_by_rs->update_or_create(
160 { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
162 else {
163 unless ( $self->{_last_returned_by} ) {
164 my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
165 if ($result) {
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
182 =cut
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';
190 return q{};
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:
205 $to : Koha::Library
206 OPTIONAL 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.
216 =cut
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({
232 toBranch => $to,
233 fromBranch => $from,
234 $limittype => $limittype eq 'itemtype'
235 ? $self->effective_itemtype : $self->ccode
236 })->count ? 0 : 1;
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
247 =cut
249 sub article_request_type {
250 my ( $self, $borrower ) = @_;
252 my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
253 my $branchcode =
254 $branch_control eq 'homebranch' ? $self->homebranch
255 : $branch_control eq 'holdingbranch' ? $self->holdingbranch
256 : undef;
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{}
265 =head3 current_holds
267 =cut
269 sub current_holds {
270 my ( $self ) = @_;
271 my $attributes = { order_by => 'priority' };
272 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
273 my $params = {
274 itemnumber => $self->itemnumber,
275 suspend => 0,
276 -or => [
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);
285 =head3 type
287 =cut
289 sub _type {
290 return 'Item';
293 =head1 AUTHOR
295 Kyle M Hall <kyle@bywatersolutions.com>
297 =cut