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