Bug 19893: Support for joined subfields in mappings
[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     my $date = $dt ? $dt->clone()->truncate( to => 'day' )->datetime : 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     $self->suspend_until( $date );
95     $self->store();
96
97     logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper($self->unblessed) )
98         if C4::Context->preference('HoldsLog');
99
100     return $self;
101 }
102
103 =head3 resume
104
105 my $hold = $hold->resume();
106
107 =cut
108
109 sub resume {
110     my ( $self ) = @_;
111
112     $self->suspend(0);
113     $self->suspend_until( undef );
114
115     $self->store();
116
117     logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
118         if C4::Context->preference('HoldsLog');
119
120     return $self;
121 }
122
123 =head3 delete
124
125 $hold->delete();
126
127 =cut
128
129 sub delete {
130     my ( $self ) = @_;
131
132     my $deleted = $self->SUPER::delete($self);
133
134     logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
135         if C4::Context->preference('HoldsLog');
136
137     return $deleted;
138 }
139
140 =head3 set_waiting
141
142 =cut
143
144 sub set_waiting {
145     my ( $self, $transferToDo ) = @_;
146
147     $self->priority(0);
148
149     if ($transferToDo) {
150         $self->found('T')->store();
151         return $self;
152     }
153
154     my $today = dt_from_string();
155     my $values = {
156         found => 'W',
157         waitingdate => $today->ymd,
158     };
159
160     my $requested_expiration;
161     if ($self->expirationdate) {
162         $requested_expiration = dt_from_string($self->expirationdate);
163     }
164
165     my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
166     my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
167     my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
168
169     my $expirationdate = $today->clone;
170     $expirationdate->add(days => $max_pickup_delay);
171
172     if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
173         $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
174     }
175
176     # If patron's requested expiration date is prior to the
177     # calculated one, we keep the patron's one.
178     my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
179     $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
180
181     $self->set($values)->store();
182
183     return $self;
184 }
185
186 =head3 is_found
187
188 Returns true if hold is a waiting or in transit
189
190 =cut
191
192 sub is_found {
193     my ($self) = @_;
194
195     return 0 unless $self->found();
196     return 1 if $self->found() eq 'W';
197     return 1 if $self->found() eq 'T';
198 }
199
200 =head3 is_waiting
201
202 Returns true if hold is a waiting hold
203
204 =cut
205
206 sub is_waiting {
207     my ($self) = @_;
208
209     my $found = $self->found;
210     return $found && $found eq 'W';
211 }
212
213 =head3 is_in_transit
214
215 Returns true if hold is a in_transit hold
216
217 =cut
218
219 sub is_in_transit {
220     my ($self) = @_;
221
222     return 0 unless $self->found();
223     return $self->found() eq 'T';
224 }
225
226 =head3 is_cancelable_from_opac
227
228 Returns true if hold is a cancelable hold
229
230 Holds may be only canceled if they are not found.
231
232 This is used from the OPAC.
233
234 =cut
235
236 sub is_cancelable_from_opac {
237     my ($self) = @_;
238
239     return 1 unless $self->is_found();
240     return 0; # if ->is_in_transit or if ->is_waiting
241 }
242
243 =head3 is_at_destination
244
245 Returns true if hold is waiting
246 and the hold's pickup branch matches
247 the hold item's holding branch
248
249 =cut
250
251 sub is_at_destination {
252     my ($self) = @_;
253
254     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
255 }
256
257 =head3 biblio
258
259 Returns the related Koha::Biblio object for this hold
260
261 =cut
262
263 sub biblio {
264     my ($self) = @_;
265
266     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
267
268     return $self->{_biblio};
269 }
270
271 =head3 item
272
273 Returns the related Koha::Item object for this Hold
274
275 =cut
276
277 sub item {
278     my ($self) = @_;
279
280     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
281
282     return $self->{_item};
283 }
284
285 =head3 branch
286
287 Returns the related Koha::Library object for this Hold
288
289 =cut
290
291 sub branch {
292     my ($self) = @_;
293
294     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
295
296     return $self->{_branch};
297 }
298
299 =head3 borrower
300
301 Returns the related Koha::Patron object for this Hold
302
303 =cut
304
305 # FIXME Should be renamed with ->patron
306 sub borrower {
307     my ($self) = @_;
308
309     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
310
311     return $self->{_borrower};
312 }
313
314 =head3 is_suspended
315
316 my $bool = $hold->is_suspended();
317
318 =cut
319
320 sub is_suspended {
321     my ( $self ) = @_;
322
323     return $self->suspend();
324 }
325
326
327 =head3 cancel
328
329 my $cancel_hold = $hold->cancel();
330
331 Cancel a hold:
332 - The hold will be moved to the old_reserves table with a priority=0
333 - The priority of other holds will be updated
334 - The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
335 - a CANCEL HOLDS log will be done if the pref HoldsLog is on
336
337 =cut
338
339 sub cancel {
340     my ( $self, $params ) = @_;
341     $self->_result->result_source->schema->txn_do(
342         sub {
343             $self->cancellationdate(dt_from_string);
344             $self->priority(0);
345             $self->_move_to_old;
346             $self->SUPER::delete(); # Do not add a DELETE log
347
348             # now fix the priority on the others....
349             C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
350
351             # and, if desired, charge a cancel fee
352             my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
353             if ( $charge && $params->{'charge_cancel_fee'} ) {
354                 C4::Accounts::manualinvoice($self->borrowernumber, $self->itemnumber, '', 'HE', $charge);
355             }
356
357             C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
358                 if C4::Context->preference('HoldsLog');
359         }
360     );
361     return $self;
362 }
363
364 =head3 _move_to_old
365
366 my $is_moved = $hold->_move_to_old;
367
368 Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
369
370 =cut
371
372 sub _move_to_old {
373     my ($self) = @_;
374     my $hold_infos = $self->unblessed;
375     return Koha::Old::Hold->new( $hold_infos )->store;
376 }
377
378 =head3 type
379
380 =cut
381
382 sub _type {
383     return 'Reserve';
384 }
385
386 =head1 AUTHORS
387
388 Kyle M Hall <kyle@bywatersolutions.com>
389
390 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
391
392 =cut
393
394 1;