Bug 18072: Add Koha::Item->can_be_transferred
[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 use Koha::DateUtils qw( dt_from_string );
26
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;
34
35 use Koha::Exceptions::Library;
36
37 use base qw(Koha::Object);
38
39 =head1 NAME
40
41 Koha::Item - Koha Item object class
42
43 =head1 API
44
45 =head2 Class Methods
46
47 =cut
48
49 =head3 effective_itemtype
50
51 Returns the itemtype for the item based on whether item level itemtypes are set or not.
52
53 =cut
54
55 sub effective_itemtype {
56     my ( $self ) = @_;
57
58     return $self->_result()->effective_itemtype();
59 }
60
61 =head3 home_branch
62
63 =cut
64
65 sub home_branch {
66     my ($self) = @_;
67
68     $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
69
70     return $self->{_home_branch};
71 }
72
73 =head3 holding_branch
74
75 =cut
76
77 sub holding_branch {
78     my ($self) = @_;
79
80     $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
81
82     return $self->{_holding_branch};
83 }
84
85 =head3 biblio
86
87 my $biblio = $item->biblio;
88
89 Return the bibliographic record of this item
90
91 =cut
92
93 sub biblio {
94     my ( $self ) = @_;
95     my $biblio_rs = $self->_result->biblio;
96     return Koha::Biblio->_new_from_dbic( $biblio_rs );
97 }
98
99 =head3 biblioitem
100
101 my $biblioitem = $item->biblioitem;
102
103 Return the biblioitem record of this item
104
105 =cut
106
107 sub biblioitem {
108     my ( $self ) = @_;
109     my $biblioitem_rs = $self->_result->biblioitem;
110     return Koha::Biblioitem->_new_from_dbic( $biblioitem_rs );
111 }
112
113 =head3 checkout
114
115 my $checkout = $item->checkout;
116
117 Return the checkout for this item
118
119 =cut
120
121 sub checkout {
122     my ( $self ) = @_;
123     my $checkout_rs = $self->_result->issue;
124     return unless $checkout_rs;
125     return Koha::Checkout->_new_from_dbic( $checkout_rs );
126 }
127
128 =head3 get_transfer
129
130 my $transfer = $item->get_transfer;
131
132 Return the transfer if the item is in transit or undef
133
134 =cut
135
136 sub get_transfer {
137     my ( $self ) = @_;
138     my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
139     return unless $transfer_rs;
140     return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
141 }
142
143 =head3 last_returned_by
144
145 Gets and sets the last borrower to return an item.
146
147 Accepts and returns Koha::Patron objects
148
149 $item->last_returned_by( $borrowernumber );
150
151 $last_returned_by = $item->last_returned_by();
152
153 =cut
154
155 sub last_returned_by {
156     my ( $self, $borrower ) = @_;
157
158     my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
159
160     if ($borrower) {
161         return $items_last_returned_by_rs->update_or_create(
162             { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
163     }
164     else {
165         unless ( $self->{_last_returned_by} ) {
166             my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
167             if ($result) {
168                 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
169             }
170         }
171
172         return $self->{_last_returned_by};
173     }
174 }
175
176 =head3 can_article_request
177
178 my $bool = $item->can_article_request( $borrower )
179
180 Returns true if item can be specifically requested
181
182 $borrower must be a Koha::Patron object
183
184 =cut
185
186 sub can_article_request {
187     my ( $self, $borrower ) = @_;
188
189     my $rule = $self->article_request_type($borrower);
190
191     return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
192     return q{};
193 }
194
195 =head3 can_be_transferred
196
197 Checks if an item can be transferred to given library.
198
199 This feature is controlled by two system preferences:
200 UseBranchTransferLimits to enable / disable the feature
201 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
202                          for setting the limitations
203
204     MANDATORY PARAMETERS:
205     $to   : Koha::Library or branchcode string
206     OPTIONAL PARAMETERS:
207     $from : Koha::Library or branchcode string  # if not given, item holdingbranch
208                                                 # will be used instead
209
210 =cut
211
212 sub can_be_transferred {
213     my ($self, $to, $from) = @_;
214
215     if (ref($to) ne 'Koha::Library') {
216         my $tobranchcode = defined $to ? $to : '';
217         $to = Koha::Libraries->find($tobranchcode);
218         unless ($to) {
219             Koha::Exceptions::Library::NotFound->throw(
220                 error => "Library '$tobranchcode' not found.",
221             );
222         }
223     }
224     if (defined $from && ref($from) ne 'Koha::Library') {
225         my $frombranchcode = defined $from ? $from : '';
226         $from = Koha::Libraries->find($frombranchcode);
227         unless ($from) {
228             Koha::Exceptions::Library::NotFound->throw(
229                 error => "Library '$frombranchcode' not found.",
230             );
231         }
232     }
233
234     $to = $to->branchcode;
235     $from = defined $from ? $from->branchcode : $self->holdingbranch;
236
237     return 1 if $from eq $to; # Transfer to current branch is allowed
238     return 1 unless C4::Context->preference('UseBranchTransferLimits');
239
240     my $limittype = C4::Context->preference('BranchTransferLimitsType');
241     return Koha::Item::Transfer::Limits->search({
242         toBranch => $to,
243         fromBranch => $from,
244         $limittype => $limittype eq 'itemtype'
245                         ? $self->effective_itemtype : $self->ccode
246     })->count ? 0 : 1;
247 }
248
249 =head3 article_request_type
250
251 my $type = $item->article_request_type( $borrower )
252
253 returns 'yes', 'no', 'bib_only', or 'item_only'
254
255 $borrower must be a Koha::Patron object
256
257 =cut
258
259 sub article_request_type {
260     my ( $self, $borrower ) = @_;
261
262     my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
263     my $branchcode =
264         $branch_control eq 'homebranch'    ? $self->homebranch
265       : $branch_control eq 'holdingbranch' ? $self->holdingbranch
266       :                                      undef;
267     my $borrowertype = $borrower->categorycode;
268     my $itemtype = $self->effective_itemtype();
269     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
270
271     return q{} unless $issuing_rule;
272     return $issuing_rule->article_requests || q{}
273 }
274
275 =head3 current_holds
276
277 =cut
278
279 sub current_holds {
280     my ( $self ) = @_;
281     my $attributes = { order_by => 'priority' };
282     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
283     my $params = {
284         itemnumber => $self->itemnumber,
285         suspend => 0,
286         -or => [
287             reservedate => { '<=' => $dtf->format_date(dt_from_string) },
288             waitingdate => { '!=' => undef },
289         ],
290     };
291     my $hold_rs = $self->_result->reserves->search( $params, $attributes );
292     return Koha::Holds->_new_from_dbic($hold_rs);
293 }
294
295 =head3 type
296
297 =cut
298
299 sub _type {
300     return 'Item';
301 }
302
303 =head1 AUTHOR
304
305 Kyle M Hall <kyle@bywatersolutions.com>
306
307 =cut
308
309 1;