Bug 17909: Add tests for merging with another authtype
[koha.git] / Koha / Item.pm
blob52a77001f9f878f601a2d92a9051b15b7fa3487f
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;
26 use C4::Context;
27 use Koha::IssuingRules;
28 use Koha::Item::Transfer;
29 use Koha::Patrons;
30 use Koha::Libraries;
32 use base qw(Koha::Object);
34 =head1 NAME
36 Koha::Item - Koha Item object class
38 =head1 API
40 =head2 Class Methods
42 =cut
44 =head3 effective_itemtype
46 Returns the itemtype for the item based on whether item level itemtypes are set or not.
48 =cut
50 sub effective_itemtype {
51 my ( $self ) = @_;
53 return $self->_result()->effective_itemtype();
56 =head3 home_branch
58 =cut
60 sub home_branch {
61 my ($self) = @_;
63 $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
65 return $self->{_home_branch};
68 =head3 holding_branch
70 =cut
72 sub holding_branch {
73 my ($self) = @_;
75 $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
77 return $self->{_holding_branch};
80 =head3 get_transfer
82 my $transfer = $item->get_transfer;
84 Return the transfer if the item is in transit or undef
86 =cut
88 sub get_transfer {
89 my ( $self ) = @_;
90 my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
91 return unless $transfer_rs;
92 return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
95 =head3 last_returned_by
97 Gets and sets the last borrower to return an item.
99 Accepts and returns Koha::Patron objects
101 $item->last_returned_by( $borrowernumber );
103 $last_returned_by = $item->last_returned_by();
105 =cut
107 sub last_returned_by {
108 my ( $self, $borrower ) = @_;
110 my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
112 if ($borrower) {
113 return $items_last_returned_by_rs->update_or_create(
114 { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
116 else {
117 unless ( $self->{_last_returned_by} ) {
118 my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
119 if ($result) {
120 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
124 return $self->{_last_returned_by};
128 =head3 can_article_request
130 my $bool = $item->can_article_request( $borrower )
132 Returns true if item can be specifically requested
134 $borrower must be a Koha::Patron object
136 =cut
138 sub can_article_request {
139 my ( $self, $borrower ) = @_;
141 my $rule = $self->article_request_type($borrower);
143 return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
144 return q{};
147 =head3 article_request_type
149 my $type = $item->article_request_type( $borrower )
151 returns 'yes', 'no', 'bib_only', or 'item_only'
153 $borrower must be a Koha::Patron object
155 =cut
157 sub article_request_type {
158 my ( $self, $borrower ) = @_;
160 my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
161 my $branchcode =
162 $branch_control eq 'homebranch' ? $self->homebranch
163 : $branch_control eq 'holdingbranch' ? $self->holdingbranch
164 : undef;
165 my $borrowertype = $borrower->categorycode;
166 my $itemtype = $self->effective_itemtype();
167 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
169 return q{} unless $issuing_rule;
170 return $issuing_rule->article_requests || q{}
173 =head3 type
175 =cut
177 sub _type {
178 return 'Item';
181 =head1 AUTHOR
183 Kyle M Hall <kyle@bywatersolutions.com>
185 =cut