Bug 14642: Add logging for Holds
[koha.git] / Koha / Hold.pm
1 package Koha::Hold;
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 use Data::Dumper qw(Dumper);
24
25 use C4::Context qw(preference);
26 use C4::Log;
27
28 use Koha::DateUtils qw(dt_from_string);
29 use Koha::Patrons;
30 use Koha::Biblios;
31 use Koha::Items;
32 use Koha::Libraries;
33
34 use base qw(Koha::Object);
35
36 =head1 NAME
37
38 Koha::Hold - Koha Hold object class
39
40 =head1 API
41
42 =head2 Class Methods
43
44 =cut
45
46 =head3 suspend_hold
47
48 my $hold = $hold->suspend_hold( $suspend_until_dt );
49
50 =cut
51
52 sub suspend_hold {
53     my ( $self, $dt ) = @_;
54
55     $dt = $dt ? $dt->clone()->truncate( to => 'day' ) : undef;
56
57     if ( $self->is_waiting ) {    # We can't suspend waiting holds
58         carp "Unable to suspend waiting hold!";
59         return $self;
60     }
61
62     $self->suspend(1);
63     $self->suspend_until( $dt );
64
65     $self->store();
66
67     logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper($self->unblessed) )
68         if C4::Context->preference('HoldsLog');
69
70     return $self;
71 }
72
73 =head3 resume
74
75 my $hold = $hold->resume();
76
77 =cut
78
79 sub resume {
80     my ( $self ) = @_;
81
82     $self->suspend(0);
83     $self->suspend_until( undef );
84
85     $self->store();
86
87     logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
88         if C4::Context->preference('HoldsLog');
89
90     return $self;
91 }
92
93 =head3 delete
94
95 $hold->delete();
96
97 =cut
98
99 sub delete {
100     my ( $self ) = @_;
101
102     logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
103         if C4::Context->preference('HoldsLog');
104
105     return $self->SUPER::delete($self);
106 }
107
108 =head3 waiting_expires_on
109
110 Returns a DateTime for the date a waiting holds expires on.
111 Returns undef if the system peference ReservesMaxPickUpDelay is not set.
112 Returns undef if the hold is not waiting ( found = 'W' ).
113
114 =cut
115
116 sub waiting_expires_on {
117     my ($self) = @_;
118
119     my $found = $self->found;
120     return unless $found && $found eq 'W';
121
122     my $ReservesMaxPickUpDelay = C4::Context->preference('ReservesMaxPickUpDelay');
123     return unless $ReservesMaxPickUpDelay;
124
125     my $dt = dt_from_string( $self->waitingdate() );
126
127     $dt->add( days => $ReservesMaxPickUpDelay );
128
129     return $dt;
130 }
131
132 =head3 is_found
133
134 Returns true if hold is a waiting or in transit
135
136 =cut
137
138 sub is_found {
139     my ($self) = @_;
140
141     return 0 unless $self->found();
142     return 1 if $self->found() eq 'W';
143     return 1 if $self->found() eq 'T';
144 }
145
146 =head3 is_waiting
147
148 Returns true if hold is a waiting hold
149
150 =cut
151
152 sub is_waiting {
153     my ($self) = @_;
154
155     my $found = $self->found;
156     return $found && $found eq 'W';
157 }
158
159 =head3 is_in_transit
160
161 Returns true if hold is a in_transit hold
162
163 =cut
164
165 sub is_in_transit {
166     my ($self) = @_;
167
168     return 0 unless $self->found();
169     return $self->found() eq 'T';
170 }
171
172 =head3 is_cancelable
173
174 Returns true if hold is a cancelable hold
175
176 Holds may be canceled if they not found, or
177 are found and waiting. A hold found but in
178 transit cannot be canceled.
179
180 =cut
181
182 sub is_cancelable {
183     my ($self) = @_;
184
185     return 1 unless $self->is_found();
186     return 0 if $self->is_in_transit();
187     return 1 if $self->is_waiting();
188     return 0;
189 }
190
191 =head3 is_at_destination
192
193 Returns true if hold is waiting
194 and the hold's pickup branch matches
195 the hold item's holding branch
196
197 =cut
198
199 sub is_at_destination {
200     my ($self) = @_;
201
202     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
203 }
204
205 =head3 biblio
206
207 Returns the related Koha::Biblio object for this hold
208
209 =cut
210
211 sub biblio {
212     my ($self) = @_;
213
214     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
215
216     return $self->{_biblio};
217 }
218
219 =head3 item
220
221 Returns the related Koha::Item object for this Hold
222
223 =cut
224
225 sub item {
226     my ($self) = @_;
227
228     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
229
230     return $self->{_item};
231 }
232
233 =head3 branch
234
235 Returns the related Koha::Library object for this Hold
236
237 =cut
238
239 sub branch {
240     my ($self) = @_;
241
242     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
243
244     return $self->{_branch};
245 }
246
247 =head3 borrower
248
249 Returns the related Koha::Patron object for this Hold
250
251 =cut
252
253 sub borrower {
254     my ($self) = @_;
255
256     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
257
258     return $self->{_borrower};
259 }
260
261 =head3 is_suspended
262
263 my $bool = $hold->is_suspended();
264
265 =cut
266
267 sub is_suspended {
268     my ( $self ) = @_;
269
270     return $self->suspend();
271 }
272
273 =head3 type
274
275 =cut
276
277 sub _type {
278     return 'Reserve';
279 }
280
281 =head1 AUTHOR
282
283 Kyle M Hall <kyle@bywatersolutions.com>
284
285 =cut
286
287 1;