Bug 17974 (QA Followup)
[koha.git] / Koha / Item.pm
1 package Koha::Item;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25
26 use C4::Context;
27 use Koha::IssuingRules;
28 use Koha::Item::Transfer;
29 use Koha::Patrons;
30 use Koha::Libraries;
31
32 use base qw(Koha::Object);
33
34 =head1 NAME
35
36 Koha::Item - Koha Item object class
37
38 =head1 API
39
40 =head2 Class Methods
41
42 =cut
43
44 =head3 effective_itemtype
45
46 Returns the itemtype for the item based on whether item level itemtypes are set or not.
47
48 =cut
49
50 sub effective_itemtype {
51     my ( $self ) = @_;
52
53     return $self->_result()->effective_itemtype();
54 }
55
56 =head3 home_branch
57
58 =cut
59
60 sub home_branch {
61     my ($self) = @_;
62
63     $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
64
65     return $self->{_home_branch};
66 }
67
68 =head3 holding_branch
69
70 =cut
71
72 sub holding_branch {
73     my ($self) = @_;
74
75     $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
76
77     return $self->{_holding_branch};
78 }
79
80 =head3 biblio
81
82 my $biblio = $item->biblio;
83
84 Return the bibliographic record of this item
85
86 =cut
87
88 sub biblio {
89     my ( $self ) = @_;
90     my $biblio_rs = $self->_result->biblio;
91     return Koha::Biblio->_new_from_dbic( $biblio_rs );
92 }
93
94 =head3 get_transfer
95
96 my $transfer = $item->get_transfer;
97
98 Return the transfer if the item is in transit or undef
99
100 =cut
101
102 sub get_transfer {
103     my ( $self ) = @_;
104     my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
105     return unless $transfer_rs;
106     return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
107 }
108
109 =head3 last_returned_by
110
111 Gets and sets the last borrower to return an item.
112
113 Accepts and returns Koha::Patron objects
114
115 $item->last_returned_by( $borrowernumber );
116
117 $last_returned_by = $item->last_returned_by();
118
119 =cut
120
121 sub last_returned_by {
122     my ( $self, $borrower ) = @_;
123
124     my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
125
126     if ($borrower) {
127         return $items_last_returned_by_rs->update_or_create(
128             { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
129     }
130     else {
131         unless ( $self->{_last_returned_by} ) {
132             my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
133             if ($result) {
134                 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
135             }
136         }
137
138         return $self->{_last_returned_by};
139     }
140 }
141
142 =head3 can_article_request
143
144 my $bool = $item->can_article_request( $borrower )
145
146 Returns true if item can be specifically requested
147
148 $borrower must be a Koha::Patron object
149
150 =cut
151
152 sub can_article_request {
153     my ( $self, $borrower ) = @_;
154
155     my $rule = $self->article_request_type($borrower);
156
157     return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
158     return q{};
159 }
160
161 =head3 article_request_type
162
163 my $type = $item->article_request_type( $borrower )
164
165 returns 'yes', 'no', 'bib_only', or 'item_only'
166
167 $borrower must be a Koha::Patron object
168
169 =cut
170
171 sub article_request_type {
172     my ( $self, $borrower ) = @_;
173
174     my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
175     my $branchcode =
176         $branch_control eq 'homebranch'    ? $self->homebranch
177       : $branch_control eq 'holdingbranch' ? $self->holdingbranch
178       :                                      undef;
179     my $borrowertype = $borrower->categorycode;
180     my $itemtype = $self->effective_itemtype();
181     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
182
183     return q{} unless $issuing_rule;
184     return $issuing_rule->article_requests || q{}
185 }
186
187 =head3 type
188
189 =cut
190
191 sub _type {
192     return 'Item';
193 }
194
195 =head1 AUTHOR
196
197 Kyle M Hall <kyle@bywatersolutions.com>
198
199 =cut
200
201 1;