Bug 6758: DBRev 17.05.00.008
[koha.git] / Koha / Item.pm
blobbea05f83179e5d755f02f82095e20981bc5394da
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;
31 use Koha::Patrons;
32 use Koha::Libraries;
34 use base qw(Koha::Object);
36 =head1 NAME
38 Koha::Item - Koha Item object class
40 =head1 API
42 =head2 Class Methods
44 =cut
46 =head3 effective_itemtype
48 Returns the itemtype for the item based on whether item level itemtypes are set or not.
50 =cut
52 sub effective_itemtype {
53 my ( $self ) = @_;
55 return $self->_result()->effective_itemtype();
58 =head3 home_branch
60 =cut
62 sub home_branch {
63 my ($self) = @_;
65 $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
67 return $self->{_home_branch};
70 =head3 holding_branch
72 =cut
74 sub holding_branch {
75 my ($self) = @_;
77 $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
79 return $self->{_holding_branch};
82 =head3 biblio
84 my $biblio = $item->biblio;
86 Return the bibliographic record of this item
88 =cut
90 sub biblio {
91 my ( $self ) = @_;
92 my $biblio_rs = $self->_result->biblio;
93 return Koha::Biblio->_new_from_dbic( $biblio_rs );
96 =head3 biblioitem
98 my $biblioitem = $item->biblioitem;
100 Return the biblioitem record of this item
102 =cut
104 sub biblioitem {
105 my ( $self ) = @_;
106 my $biblioitem_rs = $self->_result->biblioitem;
107 return Koha::Biblioitem->_new_from_dbic( $biblioitem_rs );
110 =head3 checkout
112 my $checkout = $item->checkout;
114 Return the checkout for this item
116 =cut
118 sub checkout {
119 my ( $self ) = @_;
120 my $checkout_rs = $self->_result->issue;
121 return unless $checkout_rs;
122 return Koha::Checkout->_new_from_dbic( $checkout_rs );
125 =head3 get_transfer
127 my $transfer = $item->get_transfer;
129 Return the transfer if the item is in transit or undef
131 =cut
133 sub get_transfer {
134 my ( $self ) = @_;
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();
150 =cut
152 sub last_returned_by {
153 my ( $self, $borrower ) = @_;
155 my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
157 if ($borrower) {
158 return $items_last_returned_by_rs->update_or_create(
159 { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
161 else {
162 unless ( $self->{_last_returned_by} ) {
163 my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
164 if ($result) {
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
181 =cut
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';
189 return q{};
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
200 =cut
202 sub article_request_type {
203 my ( $self, $borrower ) = @_;
205 my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
206 my $branchcode =
207 $branch_control eq 'homebranch' ? $self->homebranch
208 : $branch_control eq 'holdingbranch' ? $self->holdingbranch
209 : undef;
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{}
218 =head3 current_holds
220 =cut
222 sub current_holds {
223 my ( $self ) = @_;
224 my $attributes = { order_by => 'priority' };
225 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
226 my $params = {
227 itemnumber => $self->itemnumber,
228 suspend => 0,
229 -or => [
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);
238 =head3 type
240 =cut
242 sub _type {
243 return 'Item';
246 =head1 AUTHOR
248 Kyle M Hall <kyle@bywatersolutions.com>
250 =cut