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