Bug 23413: (follow-up) Rename variables
[koha.git] / Koha / Item.pm
blobb5b3ce06f47dd1a3641007d0f57e9c2bf6009df1
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;
23 use List::MoreUtils qw(any);
25 use Koha::Database;
26 use Koha::DateUtils qw( dt_from_string );
28 use C4::Context;
30 use Koha::Checkouts;
31 use Koha::IssuingRules;
32 use Koha::Item::Transfer::Limits;
33 use Koha::Item::Transfers;
34 use Koha::Patrons;
35 use Koha::Libraries;
36 use Koha::StockRotationItem;
37 use Koha::StockRotationRotas;
39 use base qw(Koha::Object);
41 =head1 NAME
43 Koha::Item - Koha Item object class
45 =head1 API
47 =head2 Class methods
49 =cut
51 =head3 effective_itemtype
53 Returns the itemtype for the item based on whether item level itemtypes are set or not.
55 =cut
57 sub effective_itemtype {
58 my ( $self ) = @_;
60 return $self->_result()->effective_itemtype();
63 =head3 home_branch
65 =cut
67 sub home_branch {
68 my ($self) = @_;
70 $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
72 return $self->{_home_branch};
75 =head3 holding_branch
77 =cut
79 sub holding_branch {
80 my ($self) = @_;
82 $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
84 return $self->{_holding_branch};
87 =head3 biblio
89 my $biblio = $item->biblio;
91 Return the bibliographic record of this item
93 =cut
95 sub biblio {
96 my ( $self ) = @_;
97 my $biblio_rs = $self->_result->biblio;
98 return Koha::Biblio->_new_from_dbic( $biblio_rs );
101 =head3 biblioitem
103 my $biblioitem = $item->biblioitem;
105 Return the biblioitem record of this item
107 =cut
109 sub biblioitem {
110 my ( $self ) = @_;
111 my $biblioitem_rs = $self->_result->biblioitem;
112 return Koha::Biblioitem->_new_from_dbic( $biblioitem_rs );
115 =head3 checkout
117 my $checkout = $item->checkout;
119 Return the checkout for this item
121 =cut
123 sub checkout {
124 my ( $self ) = @_;
125 my $checkout_rs = $self->_result->issue;
126 return unless $checkout_rs;
127 return Koha::Checkout->_new_from_dbic( $checkout_rs );
130 =head3 holds
132 my $holds = $item->holds();
133 my $holds = $item->holds($params);
134 my $holds = $item->holds({ found => 'W'});
136 Return holds attached to an item, optionally accept a hashref of params to pass to search
138 =cut
140 sub holds {
141 my ( $self,$params ) = @_;
142 my $holds_rs = $self->_result->reserves->search($params);
143 return unless $holds_rs->count;
144 return Koha::Holds->_new_from_dbic( $holds_rs );
147 =head3 get_transfer
149 my $transfer = $item->get_transfer;
151 Return the transfer if the item is in transit or undef
153 =cut
155 sub get_transfer {
156 my ( $self ) = @_;
157 my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
158 return unless $transfer_rs;
159 return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
162 =head3 last_returned_by
164 Gets and sets the last borrower to return an item.
166 Accepts and returns Koha::Patron objects
168 $item->last_returned_by( $borrowernumber );
170 $last_returned_by = $item->last_returned_by();
172 =cut
174 sub last_returned_by {
175 my ( $self, $borrower ) = @_;
177 my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
179 if ($borrower) {
180 return $items_last_returned_by_rs->update_or_create(
181 { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
183 else {
184 unless ( $self->{_last_returned_by} ) {
185 my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
186 if ($result) {
187 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
191 return $self->{_last_returned_by};
195 =head3 can_article_request
197 my $bool = $item->can_article_request( $borrower )
199 Returns true if item can be specifically requested
201 $borrower must be a Koha::Patron object
203 =cut
205 sub can_article_request {
206 my ( $self, $borrower ) = @_;
208 my $rule = $self->article_request_type($borrower);
210 return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
211 return q{};
214 =head3 hidden_in_opac
216 my $bool = $item->hidden_in_opac({ [ rules => $rules ] })
218 Returns true if item fields match the hidding criteria defined in $rules.
219 Returns false otherwise.
221 Takes HASHref that can have the following parameters:
222 OPTIONAL PARAMETERS:
223 $rules : { <field> => [ value_1, ... ], ... }
225 Note: $rules inherits its structure from the parsed YAML from reading
226 the I<OpacHiddenItems> system preference.
228 =cut
230 sub hidden_in_opac {
231 my ( $self, $params ) = @_;
233 my $rules = $params->{rules} // {};
235 return 1
236 if C4::Context->preference('hidelostitems') and
237 $self->itemlost > 0;
239 my $hidden_in_opac = 0;
241 foreach my $field ( keys %{$rules} ) {
243 if ( any { $self->$field eq $_ } @{ $rules->{$field} } ) {
244 $hidden_in_opac = 1;
245 last;
249 return $hidden_in_opac;
252 =head3 can_be_transferred
254 $item->can_be_transferred({ to => $to_library, from => $from_library })
255 Checks if an item can be transferred to given library.
257 This feature is controlled by two system preferences:
258 UseBranchTransferLimits to enable / disable the feature
259 BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
260 for setting the limitations
262 Takes HASHref that can have the following parameters:
263 MANDATORY PARAMETERS:
264 $to : Koha::Library
265 OPTIONAL PARAMETERS:
266 $from : Koha::Library # if not given, item holdingbranch
267 # will be used instead
269 Returns 1 if item can be transferred to $to_library, otherwise 0.
271 To find out whether at least one item of a Koha::Biblio can be transferred, please
272 see Koha::Biblio->can_be_transferred() instead of using this method for
273 multiple items of the same biblio.
275 =cut
277 sub can_be_transferred {
278 my ($self, $params) = @_;
280 my $to = $params->{to};
281 my $from = $params->{from};
283 $to = $to->branchcode;
284 $from = defined $from ? $from->branchcode : $self->holdingbranch;
286 return 1 if $from eq $to; # Transfer to current branch is allowed
287 return 1 unless C4::Context->preference('UseBranchTransferLimits');
289 my $limittype = C4::Context->preference('BranchTransferLimitsType');
290 return Koha::Item::Transfer::Limits->search({
291 toBranch => $to,
292 fromBranch => $from,
293 $limittype => $limittype eq 'itemtype'
294 ? $self->effective_itemtype : $self->ccode
295 })->count ? 0 : 1;
298 =head3 article_request_type
300 my $type = $item->article_request_type( $borrower )
302 returns 'yes', 'no', 'bib_only', or 'item_only'
304 $borrower must be a Koha::Patron object
306 =cut
308 sub article_request_type {
309 my ( $self, $borrower ) = @_;
311 my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
312 my $branchcode =
313 $branch_control eq 'homebranch' ? $self->homebranch
314 : $branch_control eq 'holdingbranch' ? $self->holdingbranch
315 : undef;
316 my $borrowertype = $borrower->categorycode;
317 my $itemtype = $self->effective_itemtype();
318 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
320 return q{} unless $issuing_rule;
321 return $issuing_rule->article_requests || q{}
324 =head3 current_holds
326 =cut
328 sub current_holds {
329 my ( $self ) = @_;
330 my $attributes = { order_by => 'priority' };
331 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
332 my $params = {
333 itemnumber => $self->itemnumber,
334 suspend => 0,
335 -or => [
336 reservedate => { '<=' => $dtf->format_date(dt_from_string) },
337 waitingdate => { '!=' => undef },
340 my $hold_rs = $self->_result->reserves->search( $params, $attributes );
341 return Koha::Holds->_new_from_dbic($hold_rs);
344 =head3 stockrotationitem
346 my $sritem = Koha::Item->stockrotationitem;
348 Returns the stock rotation item associated with the current item.
350 =cut
352 sub stockrotationitem {
353 my ( $self ) = @_;
354 my $rs = $self->_result->stockrotationitem;
355 return 0 if !$rs;
356 return Koha::StockRotationItem->_new_from_dbic( $rs );
359 =head3 add_to_rota
361 my $item = $item->add_to_rota($rota_id);
363 Add this item to the rota identified by $ROTA_ID, which means associating it
364 with the first stage of that rota. Should this item already be associated
365 with a rota, then we will move it to the new rota.
367 =cut
369 sub add_to_rota {
370 my ( $self, $rota_id ) = @_;
371 Koha::StockRotationRotas->find($rota_id)->add_item($self->itemnumber);
372 return $self;
375 =head3 has_pending_hold
377 my $is_pending_hold = $item->has_pending_hold();
379 This method checks the tmp_holdsqueue to see if this item has been selected for a hold, but not filled yet and returns true or false
381 =cut
383 sub has_pending_hold {
384 my ( $self ) = @_;
385 my $pending_hold = $self->_result->tmp_holdsqueues;
386 return !C4::Context->preference('AllowItemsOnHoldCheckout') && $pending_hold->count ? 1: 0;
389 =head3 as_marc_field
391 my $mss = C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 1 } );
392 my $field = $item->as_marc_field({ [ mss => $mss ] });
394 This method returns a MARC::Field object representing the Koha::Item object
395 with the current mappings configuration.
397 =cut
399 sub as_marc_field {
400 my ( $self, $params ) = @_;
402 my $mss = $params->{mss} // C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 1 } );
403 my $item_tag = $mss->{'items.itemnumber'}[0]->{tagfield};
405 my @subfields;
407 my @columns = $self->_result->result_source->columns;
409 foreach my $item_field ( @columns ) {
410 my $mapping = $mss->{ "items.$item_field"}[0];
411 my $tagfield = $mapping->{tagfield};
412 my $tagsubfield = $mapping->{tagsubfield};
413 next if !$tagfield; # TODO: Should we raise an exception instead?
414 # Feels like safe fallback is better
416 push @subfields, $tagsubfield => $self->$item_field;
419 my $unlinked_item_subfields = C4::Items::_parse_unlinked_item_subfields_from_xml($self->more_subfields_xml);
420 push( @subfields, @{$unlinked_item_subfields} )
421 if defined $unlinked_item_subfields and $#$unlinked_item_subfields > -1;
423 my $field;
425 $field = MARC::Field->new(
426 "$item_tag", ' ', ' ', @subfields
427 ) if @subfields;
429 return $field;
432 =head3 to_api_mapping
434 This method returns the mapping for representing a Koha::Item object
435 on the API.
437 =cut
439 sub to_api_mapping {
440 return {
441 itemnumber => 'item_id',
442 biblionumber => 'biblio_id',
443 biblioitemnumber => undef,
444 barcode => 'external_id',
445 dateaccessioned => 'acquisition_date',
446 booksellerid => 'acquisition_source',
447 homebranch => 'home_library_id',
448 price => 'purchase_price',
449 replacementprice => 'replacement_price',
450 replacementpricedate => 'replacement_price_date',
451 datelastborrowed => 'last_checkout_date',
452 datelastseen => 'last_seen_date',
453 stack => undef,
454 notforloan => 'not_for_loan_status',
455 damaged => 'damaged_status',
456 damaged_on => 'damaged_date',
457 itemlost => 'lost_status',
458 itemlost_on => 'lost_date',
459 withdrawn => 'withdrawn',
460 withdrawn_on => 'withdrawn_date',
461 itemcallnumber => 'callnumber',
462 coded_location_qualifier => 'coded_location_qualifier',
463 issues => 'checkouts_count',
464 renewals => 'renewals_count',
465 reserves => 'holds_count',
466 restricted => 'restricted_status',
467 itemnotes => 'public_notes',
468 itemnotes_nonpublic => 'internal_notes',
469 holdingbranch => 'holding_library_id',
470 paidfor => undef,
471 timestamp => 'timestamp',
472 location => 'location',
473 permanent_location => 'permanent_location',
474 onloan => 'checked_out_date',
475 cn_source => 'call_number_source',
476 cn_sort => 'call_number_sort',
477 ccode => 'collection_code',
478 materials => 'materials_notes',
479 uri => 'uri',
480 itype => 'item_type',
481 more_subfields_xml => 'extended_subfields',
482 enumchron => 'serial_issue_number',
483 copynumber => 'copy_number',
484 stocknumber => 'inventory_number',
485 new_status => 'new_status'
489 =head2 Internal methods
491 =head3 _type
493 =cut
495 sub _type {
496 return 'Item';
499 =head1 AUTHOR
501 Kyle M Hall <kyle@bywatersolutions.com>
503 =cut