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 );
28 use Koha::IssuingRules;
29 use Koha::Item::Transfer;
33 use base qw(Koha::Object);
37 Koha::Item - Koha Item object class
45 =head3 effective_itemtype
47 Returns the itemtype for the item based on whether item level itemtypes are set or not.
51 sub effective_itemtype {
54 return $self->_result()->effective_itemtype();
64 $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
66 return $self->{_home_branch};
76 $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
78 return $self->{_holding_branch};
83 my $biblio = $item->biblio;
85 Return the bibliographic record of this item
91 my $biblio_rs = $self->_result->biblio;
92 return Koha::Biblio->_new_from_dbic( $biblio_rs );
97 my $transfer = $item->get_transfer;
99 Return the transfer if the item is in transit or undef
105 my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
106 return unless $transfer_rs;
107 return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
110 =head3 last_returned_by
112 Gets and sets the last borrower to return an item.
114 Accepts and returns Koha::Patron objects
116 $item->last_returned_by( $borrowernumber );
118 $last_returned_by = $item->last_returned_by();
122 sub last_returned_by {
123 my ( $self, $borrower ) = @_;
125 my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
128 return $items_last_returned_by_rs->update_or_create(
129 { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
132 unless ( $self->{_last_returned_by} ) {
133 my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
135 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
139 return $self->{_last_returned_by};
143 =head3 can_article_request
145 my $bool = $item->can_article_request( $borrower )
147 Returns true if item can be specifically requested
149 $borrower must be a Koha::Patron object
153 sub can_article_request {
154 my ( $self, $borrower ) = @_;
156 my $rule = $self->article_request_type($borrower);
158 return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
162 =head3 article_request_type
164 my $type = $item->article_request_type( $borrower )
166 returns 'yes', 'no', 'bib_only', or 'item_only'
168 $borrower must be a Koha::Patron object
172 sub article_request_type {
173 my ( $self, $borrower ) = @_;
175 my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
177 $branch_control eq 'homebranch' ? $self->homebranch
178 : $branch_control eq 'holdingbranch' ? $self->holdingbranch
180 my $borrowertype = $borrower->categorycode;
181 my $itemtype = $self->effective_itemtype();
182 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
184 return q{} unless $issuing_rule;
185 return $issuing_rule->article_requests || q{}
188 =head3 holds_placed_before_today
192 sub holds_placed_before_today {
194 my $attributes = { order_by => 'priority' };
195 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
197 itemnumber => $self->itemnumber,
200 reservedate => { '<=' => $dtf->format_date(dt_from_string) },
201 waitingdate => { '!=' => undef },
204 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
205 return Koha::Holds->_new_from_dbic($hold_rs);
218 Kyle M Hall <kyle@bywatersolutions.com>