Bug 20402: Implement OAuth2 authentication for REST API
[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;
31 use Koha::Patrons;
32 use Koha::Libraries;
33
34 use base qw(Koha::Object);
35
36 =head1 NAME
37
38 Koha::Item - Koha Item object class
39
40 =head1 API
41
42 =head2 Class Methods
43
44 =cut
45
46 =head3 effective_itemtype
47
48 Returns the itemtype for the item based on whether item level itemtypes are set or not.
49
50 =cut
51
52 sub effective_itemtype {
53     my ( $self ) = @_;
54
55     return $self->_result()->effective_itemtype();
56 }
57
58 =head3 home_branch
59
60 =cut
61
62 sub home_branch {
63     my ($self) = @_;
64
65     $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
66
67     return $self->{_home_branch};
68 }
69
70 =head3 holding_branch
71
72 =cut
73
74 sub holding_branch {
75     my ($self) = @_;
76
77     $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
78
79     return $self->{_holding_branch};
80 }
81
82 =head3 biblio
83
84 my $biblio = $item->biblio;
85
86 Return the bibliographic record of this item
87
88 =cut
89
90 sub biblio {
91     my ( $self ) = @_;
92     my $biblio_rs = $self->_result->biblio;
93     return Koha::Biblio->_new_from_dbic( $biblio_rs );
94 }
95
96 =head3 biblioitem
97
98 my $biblioitem = $item->biblioitem;
99
100 Return the biblioitem record of this item
101
102 =cut
103
104 sub biblioitem {
105     my ( $self ) = @_;
106     my $biblioitem_rs = $self->_result->biblioitem;
107     return Koha::Biblioitem->_new_from_dbic( $biblioitem_rs );
108 }
109
110 =head3 checkout
111
112 my $checkout = $item->checkout;
113
114 Return the checkout for this item
115
116 =cut
117
118 sub checkout {
119     my ( $self ) = @_;
120     my $checkout_rs = $self->_result->issue;
121     return unless $checkout_rs;
122     return Koha::Checkout->_new_from_dbic( $checkout_rs );
123 }
124
125 =head3 get_transfer
126
127 my $transfer = $item->get_transfer;
128
129 Return the transfer if the item is in transit or undef
130
131 =cut
132
133 sub get_transfer {
134     my ( $self ) = @_;
135     my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
136     return unless $transfer_rs;
137     return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
138 }
139
140 =head3 last_returned_by
141
142 Gets and sets the last borrower to return an item.
143
144 Accepts and returns Koha::Patron objects
145
146 $item->last_returned_by( $borrowernumber );
147
148 $last_returned_by = $item->last_returned_by();
149
150 =cut
151
152 sub last_returned_by {
153     my ( $self, $borrower ) = @_;
154
155     my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
156
157     if ($borrower) {
158         return $items_last_returned_by_rs->update_or_create(
159             { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
160     }
161     else {
162         unless ( $self->{_last_returned_by} ) {
163             my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
164             if ($result) {
165                 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
166             }
167         }
168
169         return $self->{_last_returned_by};
170     }
171 }
172
173 =head3 can_article_request
174
175 my $bool = $item->can_article_request( $borrower )
176
177 Returns true if item can be specifically requested
178
179 $borrower must be a Koha::Patron object
180
181 =cut
182
183 sub can_article_request {
184     my ( $self, $borrower ) = @_;
185
186     my $rule = $self->article_request_type($borrower);
187
188     return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
189     return q{};
190 }
191
192 =head3 article_request_type
193
194 my $type = $item->article_request_type( $borrower )
195
196 returns 'yes', 'no', 'bib_only', or 'item_only'
197
198 $borrower must be a Koha::Patron object
199
200 =cut
201
202 sub article_request_type {
203     my ( $self, $borrower ) = @_;
204
205     my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
206     my $branchcode =
207         $branch_control eq 'homebranch'    ? $self->homebranch
208       : $branch_control eq 'holdingbranch' ? $self->holdingbranch
209       :                                      undef;
210     my $borrowertype = $borrower->categorycode;
211     my $itemtype = $self->effective_itemtype();
212     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
213
214     return q{} unless $issuing_rule;
215     return $issuing_rule->article_requests || q{}
216 }
217
218 =head3 current_holds
219
220 =cut
221
222 sub current_holds {
223     my ( $self ) = @_;
224     my $attributes = { order_by => 'priority' };
225     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
226     my $params = {
227         itemnumber => $self->itemnumber,
228         suspend => 0,
229         -or => [
230             reservedate => { '<=' => $dtf->format_date(dt_from_string) },
231             waitingdate => { '!=' => undef },
232         ],
233     };
234     my $hold_rs = $self->_result->reserves->search( $params, $attributes );
235     return Koha::Holds->_new_from_dbic($hold_rs);
236 }
237
238 =head3 type
239
240 =cut
241
242 sub _type {
243     return 'Item';
244 }
245
246 =head1 AUTHOR
247
248 Kyle M Hall <kyle@bywatersolutions.com>
249
250 =cut
251
252 1;