Bug 21526: uri escape TT variables when used in 'a href'
[koha.git] / Koha / Item.pm
blobf3799eff2013359e818b6ba548708d8c1b39a17c
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;
25 use Koha::DateUtils qw( dt_from_string );
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 use Koha::StockRotationItem;
35 use Koha::StockRotationRotas;
37 use base qw(Koha::Object);
39 =head1 NAME
41 Koha::Item - Koha Item object class
43 =head1 API
45 =head2 Class methods
47 =cut
49 =head3 effective_itemtype
51 Returns the itemtype for the item based on whether item level itemtypes are set or not.
53 =cut
55 sub effective_itemtype {
56 my ( $self ) = @_;
58 return $self->_result()->effective_itemtype();
61 =head3 home_branch
63 =cut
65 sub home_branch {
66 my ($self) = @_;
68 $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
70 return $self->{_home_branch};
73 =head3 holding_branch
75 =cut
77 sub holding_branch {
78 my ($self) = @_;
80 $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
82 return $self->{_holding_branch};
85 =head3 biblio
87 my $biblio = $item->biblio;
89 Return the bibliographic record of this item
91 =cut
93 sub biblio {
94 my ( $self ) = @_;
95 my $biblio_rs = $self->_result->biblio;
96 return Koha::Biblio->_new_from_dbic( $biblio_rs );
99 =head3 biblioitem
101 my $biblioitem = $item->biblioitem;
103 Return the biblioitem record of this item
105 =cut
107 sub biblioitem {
108 my ( $self ) = @_;
109 my $biblioitem_rs = $self->_result->biblioitem;
110 return Koha::Biblioitem->_new_from_dbic( $biblioitem_rs );
113 =head3 checkout
115 my $checkout = $item->checkout;
117 Return the checkout for this item
119 =cut
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 );
128 =head3 get_transfer
130 my $transfer = $item->get_transfer;
132 Return the transfer if the item is in transit or undef
134 =cut
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 );
143 =head3 last_returned_by
145 Gets and sets the last borrower to return an item.
147 Accepts and returns Koha::Patron objects
149 $item->last_returned_by( $borrowernumber );
151 $last_returned_by = $item->last_returned_by();
153 =cut
155 sub last_returned_by {
156 my ( $self, $borrower ) = @_;
158 my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
160 if ($borrower) {
161 return $items_last_returned_by_rs->update_or_create(
162 { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
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') );
172 return $self->{_last_returned_by};
176 =head3 can_article_request
178 my $bool = $item->can_article_request( $borrower )
180 Returns true if item can be specifically requested
182 $borrower must be a Koha::Patron object
184 =cut
186 sub can_article_request {
187 my ( $self, $borrower ) = @_;
189 my $rule = $self->article_request_type($borrower);
191 return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
192 return q{};
195 =head3 can_be_transferred
197 $item->can_be_transferred({ to => $to_library, from => $from_library })
198 Checks if an item can be transferred to given library.
200 This feature is controlled by two system preferences:
201 UseBranchTransferLimits to enable / disable the feature
202 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
203 for setting the limitations
205 Takes HASHref that can have the following parameters:
206 MANDATORY PARAMETERS:
207 $to : Koha::Library
208 OPTIONAL PARAMETERS:
209 $from : Koha::Library # if not given, item holdingbranch
210 # will be used instead
212 Returns 1 if item can be transferred to $to_library, otherwise 0.
214 To find out whether at least one item of a Koha::Biblio can be transferred, please
215 see Koha::Biblio->can_be_transferred() instead of using this method for
216 multiple items of the same biblio.
218 =cut
220 sub can_be_transferred {
221 my ($self, $params) = @_;
223 my $to = $params->{to};
224 my $from = $params->{from};
226 $to = $to->branchcode;
227 $from = defined $from ? $from->branchcode : $self->holdingbranch;
229 return 1 if $from eq $to; # Transfer to current branch is allowed
230 return 1 unless C4::Context->preference('UseBranchTransferLimits');
232 my $limittype = C4::Context->preference('BranchTransferLimitsType');
233 return Koha::Item::Transfer::Limits->search({
234 toBranch => $to,
235 fromBranch => $from,
236 $limittype => $limittype eq 'itemtype'
237 ? $self->effective_itemtype : $self->ccode
238 })->count ? 0 : 1;
241 =head3 article_request_type
243 my $type = $item->article_request_type( $borrower )
245 returns 'yes', 'no', 'bib_only', or 'item_only'
247 $borrower must be a Koha::Patron object
249 =cut
251 sub article_request_type {
252 my ( $self, $borrower ) = @_;
254 my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
255 my $branchcode =
256 $branch_control eq 'homebranch' ? $self->homebranch
257 : $branch_control eq 'holdingbranch' ? $self->holdingbranch
258 : undef;
259 my $borrowertype = $borrower->categorycode;
260 my $itemtype = $self->effective_itemtype();
261 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
263 return q{} unless $issuing_rule;
264 return $issuing_rule->article_requests || q{}
267 =head3 current_holds
269 =cut
271 sub current_holds {
272 my ( $self ) = @_;
273 my $attributes = { order_by => 'priority' };
274 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
275 my $params = {
276 itemnumber => $self->itemnumber,
277 suspend => 0,
278 -or => [
279 reservedate => { '<=' => $dtf->format_date(dt_from_string) },
280 waitingdate => { '!=' => undef },
283 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
284 return Koha::Holds->_new_from_dbic($hold_rs);
287 =head3 stockrotationitem
289 my $sritem = Koha::Item->stockrotationitem;
291 Returns the stock rotation item associated with the current item.
293 =cut
295 sub stockrotationitem {
296 my ( $self ) = @_;
297 my $rs = $self->_result->stockrotationitem;
298 return 0 if !$rs;
299 return Koha::StockRotationItem->_new_from_dbic( $rs );
302 =head3 add_to_rota
304 my $item = $item->add_to_rota($rota_id);
306 Add this item to the rota identified by $ROTA_ID, which means associating it
307 with the first stage of that rota. Should this item already be associated
308 with a rota, then we will move it to the new rota.
310 =cut
312 sub add_to_rota {
313 my ( $self, $rota_id ) = @_;
314 Koha::StockRotationRotas->find($rota_id)->add_item($self->itemnumber);
315 return $self;
318 =head2 Internal methods
320 =head3 _type
322 =cut
324 sub _type {
325 return 'Item';
328 =head1 AUTHOR
330 Kyle M Hall <kyle@bywatersolutions.com>
332 =cut