Bug 30708: Add missing koha_object(s)_class definitions
[koha.git] / Koha / Item / Transfer.pm
1 package Koha::Item::Transfer;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20
21 use C4::Items qw( CartToShelf ModDateLastSeen );
22
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Exceptions::Item::Transfer;
26
27 use base qw(Koha::Object);
28
29 =head1 NAME
30
31 Koha::Item::Transfer - Koha Item Transfer Object class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =cut
38
39 =head3 item
40
41   my $item = $transfer->item;
42
43 Returns the associated item for this transfer.
44
45 =cut
46
47 sub item {
48     my ($self) = @_;
49     my $item_rs = $self->_result->itemnumber;
50     return Koha::Item->_new_from_dbic($item_rs);
51 }
52
53 =head3 from_library
54
55   my $from_library = $transfer->from_library;
56
57 Returns the associated from_library for this transfer.
58
59 =cut
60
61 sub from_library {
62     my ($self) = @_;
63     my $from_library_rs = $self->_result->frombranch;
64     return Koha::Library->_new_from_dbic($from_library_rs);
65 }
66
67 =head3 to_library
68
69   my $to_library = $transfer->to_library;
70
71 Returns the associated to_library for this transfer.
72
73 =cut
74
75 sub to_library {
76     my ($self) = @_;
77     my $to_library_rs = $self->_result->tobranch;
78     return Koha::Library->_new_from_dbic($to_library_rs);
79 }
80
81 =head3 transit
82
83     $transfer->transit({ [ skip_record_index => 0|1 ] });
84
85 Set the transfer as in transit by updating the I<datesent> time.
86
87 Also, update date last seen and ensure item holdingbranch is correctly set.
88
89 An optional I<skip_record_index> parameter can be passed to avoid triggering
90 reindex.
91
92 =cut
93
94 sub transit {
95     my ($self, $params) = @_;
96
97     # Throw exception if item is still checked out
98     Koha::Exceptions::Item::Transfer::OnLoan->throw() if ( $self->item->checkout );
99
100     # Remove the 'shelving cart' location status if it is being used (Bug 3701)
101     CartToShelf( $self->item->itemnumber )
102       if $self->item->location
103       && $self->item->location eq 'CART'
104       && (!$self->item->permanent_location
105         || $self->item->permanent_location ne 'CART' );
106
107     # Update the transit state
108     $self->set(
109         {
110             frombranch => $self->item->holdingbranch,
111             datesent   => dt_from_string,
112         }
113     )->store;
114
115     ModDateLastSeen( $self->item->itemnumber, undef, { skip_record_index => $params->{skip_record_index} } );
116     return $self;
117
118 }
119
120 =head3 in_transit
121
122 Boolean returning whether the transfer is in transit or waiting
123
124 =cut
125
126 sub in_transit {
127     my ($self) = @_;
128
129     return ( defined( $self->datesent )
130           && !defined( $self->datearrived )
131           && !defined( $self->datecancelled ) );
132 }
133
134 =head3 receive
135
136 Receive the transfer by setting the datearrived time.
137
138 =cut
139
140 sub receive {
141     my ($self) = @_;
142
143     # Throw exception if item is checked out
144     Koha::Exceptions::Item::Transfer::OnLoan->throw() if ($self->item->checkout);
145
146     # Update the arrived date
147     $self->set({ datearrived => dt_from_string })->store;
148
149     ModDateLastSeen( $self->item->itemnumber );
150     return $self;
151 }
152
153 =head3 cancel
154
155   $transfer->cancel({ reason => $reason, [force => 1]});
156
157 Cancel the transfer by setting the datecancelled time and recording the reason.
158
159 =cut
160
161 sub cancel {
162     my ( $self, $params ) = @_;
163
164     Koha::Exceptions::MissingParameter->throw(
165         error => "The 'reason' parameter is mandatory" )
166       unless defined($params->{reason});
167
168     # Throw exception if item is in transit already
169     Koha::Exceptions::Item::Transfer::InTransit->throw() if ( !$params->{force} && $self->in_transit );
170
171     # Update the cancelled date
172     $self->set(
173         { datecancelled => dt_from_string, cancellation_reason => $params->{reason} } )
174       ->store;
175
176     return $self;
177 }
178
179 =head3 type
180
181 =cut
182
183 sub _type {
184     return 'Branchtransfer';
185 }
186
187 1;