Bug 17974 (QA Followup)
[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
26 use Koha::Item::Transfer;
27 use Koha::Patrons;
28 use Koha::Libraries;
29
30 use base qw(Koha::Object);
31
32 =head1 NAME
33
34 Koha::Item - Koha Item object class
35
36 =head1 API
37
38 =head2 Class Methods
39
40 =cut
41
42 =head3 effective_itemtype
43
44 Returns the itemtype for the item based on whether item level itemtypes are set or not.
45
46 =cut
47
48 sub effective_itemtype {
49     my ( $self ) = @_;
50
51     return $self->_result()->effective_itemtype();
52 }
53
54 =head3 home_branch
55
56 =cut
57
58 sub home_branch {
59     my ($self) = @_;
60
61     $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
62
63     return $self->{_home_branch};
64 }
65
66 =head3 holding_branch
67
68 =cut
69
70 sub holding_branch {
71     my ($self) = @_;
72
73     $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
74
75     return $self->{_holding_branch};
76 }
77
78 =head3 biblio
79
80 my $biblio = $item->biblio;
81
82 Return the bibliographic record of this item
83
84 =cut
85
86 sub biblio {
87     my ( $self ) = @_;
88     my $biblio_rs = $self->_result->biblio;
89     return Koha::Biblio->_new_from_dbic( $biblio_rs );
90 }
91
92 =head3 get_transfer
93
94 my $transfer = $item->get_transfer;
95
96 Return the transfer if the item is in transit or undef
97
98 =cut
99
100 sub get_transfer {
101     my ( $self ) = @_;
102     my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
103     return unless $transfer_rs;
104     return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
105 }
106
107 =head3 last_returned_by
108
109 Gets and sets the last borrower to return an item.
110
111 Accepts and returns Koha::Patron objects
112
113 $item->last_returned_by( $borrowernumber );
114
115 $last_returned_by = $item->last_returned_by();
116
117 =cut
118
119 sub last_returned_by {
120     my ( $self, $borrower ) = @_;
121
122     my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
123
124     if ($borrower) {
125         return $items_last_returned_by_rs->update_or_create(
126             { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
127     }
128     else {
129         unless ( $self->{_last_returned_by} ) {
130             my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
131             if ($result) {
132                 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
133             }
134         }
135
136         return $self->{_last_returned_by};
137     }
138 }
139
140 =head3 type
141
142 =cut
143
144 sub _type {
145     return 'Item';
146 }
147
148 =head1 AUTHOR
149
150 Kyle M Hall <kyle@bywatersolutions.com>
151
152 =cut
153
154 1;