Bug 18382: (QA follow-up) Replace ->ymd by ->datetime
[koha.git] / Koha / Hold.pm
1 package Koha::Hold;
2
3 # Copyright ByWater Solutions 2014
4 # Copyright 2017 Koha Development team
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use Modern::Perl;
22
23 use Carp;
24 use Data::Dumper qw(Dumper);
25
26 use C4::Context qw(preference);
27 use C4::Log;
28
29 use Koha::DateUtils qw(dt_from_string output_pref);
30 use Koha::Patrons;
31 use Koha::Biblios;
32 use Koha::Items;
33 use Koha::Libraries;
34 use Koha::Old::Holds;
35 use Koha::Calendar;
36
37 use base qw(Koha::Object);
38
39 =head1 NAME
40
41 Koha::Hold - Koha Hold object class
42
43 =head1 API
44
45 =head2 Class Methods
46
47 =cut
48
49 =head3 age
50
51 returns the number of days since a hold was placed, optionally
52 using the calendar
53
54 my $age = $hold->age( $use_calendar );
55
56 =cut
57
58 sub age {
59     my ( $self, $use_calendar ) = @_;
60
61     my $today = dt_from_string;
62     my $age;
63
64     if ( $use_calendar ) {
65         my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
66         $age = $calendar->days_between( dt_from_string( $self->reservedate ), $today );
67     }
68     else {
69         $age = $today->delta_days( dt_from_string( $self->reservedate ) );
70     }
71
72     $age = $age->in_units( 'days' );
73
74     return $age;
75 }
76
77 =head3 suspend_hold
78
79 my $hold = $hold->suspend_hold( $suspend_until_dt );
80
81 =cut
82
83 sub suspend_hold {
84     my ( $self, $dt ) = @_;
85
86     $dt = $dt ? $dt->clone()->truncate( to => 'day' ) : undef;
87
88     if ( $self->is_waiting ) {    # We can't suspend waiting holds
89         carp "Unable to suspend waiting hold!";
90         return $self;
91     }
92
93     $self->suspend(1);
94     if ( defined $dt ){
95         $self->suspend_until( $dt->datetime );
96     } else {
97         $self->suspend_until( $dt );
98     }
99     $self->store();
100
101     logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper($self->unblessed) )
102         if C4::Context->preference('HoldsLog');
103
104     return $self;
105 }
106
107 =head3 resume
108
109 my $hold = $hold->resume();
110
111 =cut
112
113 sub resume {
114     my ( $self ) = @_;
115
116     $self->suspend(0);
117     $self->suspend_until( undef );
118
119     $self->store();
120
121     logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
122         if C4::Context->preference('HoldsLog');
123
124     return $self;
125 }
126
127 =head3 delete
128
129 $hold->delete();
130
131 =cut
132
133 sub delete {
134     my ( $self ) = @_;
135
136     my $deleted = $self->SUPER::delete($self);
137
138     logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
139         if C4::Context->preference('HoldsLog');
140
141     return $deleted;
142 }
143
144 =head3 set_waiting
145
146 =cut
147
148 sub set_waiting {
149     my ( $self, $transferToDo ) = @_;
150
151     $self->priority(0);
152
153     if ($transferToDo) {
154         $self->found('T')->store();
155         return $self;
156     }
157
158     my $today = dt_from_string();
159     my $values = {
160         found => 'W',
161         waitingdate => $today->ymd,
162     };
163
164     my $requested_expiration;
165     if ($self->expirationdate) {
166         $requested_expiration = dt_from_string($self->expirationdate);
167     }
168
169     my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
170     my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
171     my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
172
173     my $expirationdate = $today->clone;
174     $expirationdate->add(days => $max_pickup_delay);
175
176     if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
177         $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
178     }
179
180     # If patron's requested expiration date is prior to the
181     # calculated one, we keep the patron's one.
182     my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
183     $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
184
185     $self->set($values)->store();
186
187     return $self;
188 }
189
190 =head3 is_found
191
192 Returns true if hold is a waiting or in transit
193
194 =cut
195
196 sub is_found {
197     my ($self) = @_;
198
199     return 0 unless $self->found();
200     return 1 if $self->found() eq 'W';
201     return 1 if $self->found() eq 'T';
202 }
203
204 =head3 is_waiting
205
206 Returns true if hold is a waiting hold
207
208 =cut
209
210 sub is_waiting {
211     my ($self) = @_;
212
213     my $found = $self->found;
214     return $found && $found eq 'W';
215 }
216
217 =head3 is_in_transit
218
219 Returns true if hold is a in_transit hold
220
221 =cut
222
223 sub is_in_transit {
224     my ($self) = @_;
225
226     return 0 unless $self->found();
227     return $self->found() eq 'T';
228 }
229
230 =head3 is_cancelable
231
232 Returns true if hold is a cancelable hold
233
234 Holds may be canceled if they not found, or
235 are found and waiting. A hold found but in
236 transit cannot be canceled.
237
238 =cut
239
240 sub is_cancelable {
241     my ($self) = @_;
242
243     return 1 unless $self->is_found();
244     return 0 if $self->is_in_transit();
245     return 1 if $self->is_waiting();
246     return 0;
247 }
248
249 =head3 is_at_destination
250
251 Returns true if hold is waiting
252 and the hold's pickup branch matches
253 the hold item's holding branch
254
255 =cut
256
257 sub is_at_destination {
258     my ($self) = @_;
259
260     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
261 }
262
263 =head3 biblio
264
265 Returns the related Koha::Biblio object for this hold
266
267 =cut
268
269 sub biblio {
270     my ($self) = @_;
271
272     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
273
274     return $self->{_biblio};
275 }
276
277 =head3 item
278
279 Returns the related Koha::Item object for this Hold
280
281 =cut
282
283 sub item {
284     my ($self) = @_;
285
286     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
287
288     return $self->{_item};
289 }
290
291 =head3 branch
292
293 Returns the related Koha::Library object for this Hold
294
295 =cut
296
297 sub branch {
298     my ($self) = @_;
299
300     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
301
302     return $self->{_branch};
303 }
304
305 =head3 borrower
306
307 Returns the related Koha::Patron object for this Hold
308
309 =cut
310
311 # FIXME Should be renamed with ->patron
312 sub borrower {
313     my ($self) = @_;
314
315     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
316
317     return $self->{_borrower};
318 }
319
320 =head3 is_suspended
321
322 my $bool = $hold->is_suspended();
323
324 =cut
325
326 sub is_suspended {
327     my ( $self ) = @_;
328
329     return $self->suspend();
330 }
331
332
333 =head3 cancel
334
335 my $cancel_hold = $hold->cancel();
336
337 Cancel a hold:
338 - The hold will be moved to the old_reserves table with a priority=0
339 - The priority of other holds will be updated
340 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
341 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
342
343 =cut
344
345 sub cancel {
346     my ( $self, $params ) = @_;
347     $self->_result->result_source->schema->txn_do(
348         sub {
349             $self->cancellationdate(dt_from_string);
350             $self->priority(0);
351             $self->_move_to_old;
352             $self->SUPER::delete(); # Do not add a DELETE log
353
354             # now fix the priority on the others....
355             C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
356
357             # and, if desired, charge a cancel fee
358             my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
359             if ( $charge && $params->{'charge_cancel_fee'} ) {
360                 C4::Accounts::manualinvoice($self->borrowernumber, $self->itemnumber, '', 'HE', $charge);
361             }
362
363             C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
364                 if C4::Context->preference('HoldsLog');
365         }
366     );
367     return $self;
368 }
369
370 =head3 _move_to_old
371
372 my $is_moved = $hold->_move_to_old;
373
374 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
375
376 =cut
377
378 sub _move_to_old {
379     my ($self) = @_;
380     my $hold_infos = $self->unblessed;
381     return Koha::Old::Hold->new( $hold_infos )->store;
382 }
383
384 =head3 type
385
386 =cut
387
388 sub _type {
389     return 'Reserve';
390 }
391
392 =head1 AUTHORS
393
394 Kyle M Hall <kyle@bywatersolutions.com>
395
396 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
397
398 =cut
399
400 1;