Bug 24434: Reinstate updateWrongTransfer
[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 use Carp;
21 use Try::Tiny;
22
23 use C4::Items;
24
25 use Koha::Database;
26 use Koha::DateUtils;
27 use Koha::Exceptions::Item::Transfer;
28
29 use base qw(Koha::Object);
30
31 =head1 NAME
32
33 Koha::Item::Transfer - Koha Item Transfer Object class
34
35 =head1 API
36
37 =head2 Class Methods
38
39 =cut
40
41 =head3 item
42
43   my $item = $transfer->item;
44
45 Returns the associated item for this transfer.
46
47 =cut
48
49 sub item {
50     my ($self) = @_;
51     my $item_rs = $self->_result->itemnumber;
52     return Koha::Item->_new_from_dbic($item_rs);
53 }
54
55 =head3 from_library
56
57   my $from_library = $transfer->from_library;
58
59 Returns the associated from_library for this transfer.
60
61 =cut
62
63 sub from_library {
64     my ($self) = @_;
65     my $from_library_rs = $self->_result->frombranch;
66     return Koha::Library->_new_from_dbic($from_library_rs);
67 }
68
69 =head3 to_library
70
71   my $to_library = $transfer->to_library;
72
73 Returns the associated to_library for this transfer.
74
75 =cut
76
77 sub to_library {
78     my ($self) = @_;
79     my $to_library_rs = $self->_result->tobranch;
80     return Koha::Library->_new_from_dbic($to_library_rs);
81 }
82
83 =head3 transit
84
85 Set the transfer as in transit by updating the datesent time.
86
87 Also, update date last seen and ensure item holdingbranch is correctly set.
88
89 =cut
90
91 sub transit {
92     my ($self) = @_;
93
94     # Throw exception if item is still checked out
95     Koha::Exceptions::Item::Transfer::OnLoan->throw() if ( $self->item->checkout );
96
97     # Remove the 'shelving cart' location status if it is being used (Bug 3701)
98     CartToShelf( $self->item->itemnumber )
99       if $self->item->location
100       && $self->item->location eq 'CART'
101       && (!$self->item->permanent_location
102         || $self->item->permanent_location ne 'CART' );
103
104     # Update the transit state
105     $self->set(
106         {
107             frombranch => $self->item->holdingbranch,
108             datesent   => dt_from_string,
109         }
110     )->store;
111
112     ModDateLastSeen( $self->item->itemnumber );
113     return $self;
114
115 }
116
117 =head3 in_transit
118
119 Boolean returning whether the transfer is in transit or waiting
120
121 =cut
122
123 sub in_transit {
124     my ($self) = @_;
125
126     return ( defined( $self->datesent )
127           && !defined( $self->datearrived )
128           && !defined( $self->datecancelled ) );
129 }
130
131 =head3 receive
132
133 Receive the transfer by setting the datearrived time.
134
135 =cut
136
137 sub receive {
138     my ($self) = @_;
139
140     # Throw exception if item is checked out
141     Koha::Exceptions::Item::Transfer::OnLoan->throw() if ($self->item->checkout);
142
143     # Update the arrived date
144     $self->set({ datearrived => dt_from_string })->store;
145
146     ModDateLastSeen( $self->item->itemnumber );
147     return $self;
148 }
149
150 =head3 cancel
151
152   $transfer->cancel({ reason => $reason, [force => 1]});
153
154 Cancel the transfer by setting the datecancelled time and recording the reason.
155
156 =cut
157
158 sub cancel {
159     my ( $self, $params ) = @_;
160
161     Koha::Exceptions::MissingParameter->throw(
162         error => "The 'reason' parameter is mandatory" )
163       unless defined($params->{reason});
164
165     my $in_transit = $self->in_transit;
166
167     # Throw exception if item is in transit already
168     Koha::Exceptions::Item::Transfer::InTransit->throw() if ( !$params->{force} && $in_transit );
169
170     # Update the cancelled date
171     $self->set(
172         { datecancelled => dt_from_string, cancellation_reason => $params->{reason} } )
173       ->store;
174
175     # Set up return transfer if transfer was force cancelled whilst in transit
176     # and we were not notified that the transfer is being replaced.
177     # NOTE: We don't catch here, as we're happy to fail if there are already
178     # other transfers in the queue.
179     if ($in_transit && !$params->{replace}) {
180         try {
181             $self->item->request_transfer(
182                 { to => $self->from_library, reason => 'TransferCancellation' } );
183         };
184     }
185
186     return $self;
187 }
188
189 =head3 type
190
191 =cut
192
193 sub _type {
194     return 'Branchtransfer';
195 }
196
197 1;