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