Bug 17032: Make sure Swagger object definition is up-to-date with Koha-object
[koha.git] / Koha / Item.pm
blob0bbc4933a052759294a20ddd51c2caa9cfe3595a
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;
26 use Koha::Item::Transfer;
27 use Koha::Patrons;
28 use Koha::Libraries;
30 use base qw(Koha::Object);
32 =head1 NAME
34 Koha::Item - Koha Item object class
36 =head1 API
38 =head2 Class Methods
40 =cut
42 =head3 effective_itemtype
44 Returns the itemtype for the item based on whether item level itemtypes are set or not.
46 =cut
48 sub effective_itemtype {
49 my ( $self ) = @_;
51 return $self->_result()->effective_itemtype();
54 =head3 home_branch
56 =cut
58 sub home_branch {
59 my ($self) = @_;
61 $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
63 return $self->{_home_branch};
66 =head3 holding_branch
68 =cut
70 sub holding_branch {
71 my ($self) = @_;
73 $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
75 return $self->{_holding_branch};
78 =head3 get_transfer
80 my $transfer = $item->get_transfer;
82 Return the transfer if the item is in transit or undef
84 =cut
86 sub get_transfer {
87 my ( $self ) = @_;
88 my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
89 return unless $transfer_rs;
90 return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
93 =head3 last_returned_by
95 Gets and sets the last borrower to return an item.
97 Accepts and returns Koha::Patron objects
99 $item->last_returned_by( $borrowernumber );
101 $last_returned_by = $item->last_returned_by();
103 =cut
105 sub last_returned_by {
106 my ( $self, $borrower ) = @_;
108 my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
110 if ($borrower) {
111 return $items_last_returned_by_rs->update_or_create(
112 { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
114 else {
115 unless ( $self->{_last_returned_by} ) {
116 my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
117 if ($result) {
118 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
122 return $self->{_last_returned_by};
126 =head3 type
128 =cut
130 sub _type {
131 return 'Item';
134 =head1 AUTHOR
136 Kyle M Hall <kyle@bywatersolutions.com>
138 =cut