Bug 16699: Fix mixed-up indentation from 2-4 spaces to 2 spaces
[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     my $deleted = $self->SUPER::delete($self);
103
104     logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
105         if C4::Context->preference('HoldsLog');
106
107     return $deleted;
108 }
109
110 =head3 waiting_expires_on
111
112 Returns a DateTime for the date a waiting holds expires on.
113 Returns undef if the system peference ReservesMaxPickUpDelay is not set.
114 Returns undef if the hold is not waiting ( found = 'W' ).
115
116 =cut
117
118 sub waiting_expires_on {
119     my ($self) = @_;
120
121     my $found = $self->found;
122     return unless $found && $found eq 'W';
123
124     my $ReservesMaxPickUpDelay = C4::Context->preference('ReservesMaxPickUpDelay');
125     return unless $ReservesMaxPickUpDelay;
126
127     my $dt = dt_from_string( $self->waitingdate() );
128
129     $dt->add( days => $ReservesMaxPickUpDelay );
130
131     return $dt;
132 }
133
134 =head3 is_found
135
136 Returns true if hold is a waiting or in transit
137
138 =cut
139
140 sub is_found {
141     my ($self) = @_;
142
143     return 0 unless $self->found();
144     return 1 if $self->found() eq 'W';
145     return 1 if $self->found() eq 'T';
146 }
147
148 =head3 is_waiting
149
150 Returns true if hold is a waiting hold
151
152 =cut
153
154 sub is_waiting {
155     my ($self) = @_;
156
157     my $found = $self->found;
158     return $found && $found eq 'W';
159 }
160
161 =head3 is_in_transit
162
163 Returns true if hold is a in_transit hold
164
165 =cut
166
167 sub is_in_transit {
168     my ($self) = @_;
169
170     return 0 unless $self->found();
171     return $self->found() eq 'T';
172 }
173
174 =head3 is_cancelable
175
176 Returns true if hold is a cancelable hold
177
178 Holds may be canceled if they not found, or
179 are found and waiting. A hold found but in
180 transit cannot be canceled.
181
182 =cut
183
184 sub is_cancelable {
185     my ($self) = @_;
186
187     return 1 unless $self->is_found();
188     return 0 if $self->is_in_transit();
189     return 1 if $self->is_waiting();
190     return 0;
191 }
192
193 =head3 is_at_destination
194
195 Returns true if hold is waiting
196 and the hold's pickup branch matches
197 the hold item's holding branch
198
199 =cut
200
201 sub is_at_destination {
202     my ($self) = @_;
203
204     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
205 }
206
207 =head3 biblio
208
209 Returns the related Koha::Biblio object for this hold
210
211 =cut
212
213 sub biblio {
214     my ($self) = @_;
215
216     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
217
218     return $self->{_biblio};
219 }
220
221 =head3 item
222
223 Returns the related Koha::Item object for this Hold
224
225 =cut
226
227 sub item {
228     my ($self) = @_;
229
230     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
231
232     return $self->{_item};
233 }
234
235 =head3 branch
236
237 Returns the related Koha::Library object for this Hold
238
239 =cut
240
241 sub branch {
242     my ($self) = @_;
243
244     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
245
246     return $self->{_branch};
247 }
248
249 =head3 borrower
250
251 Returns the related Koha::Patron object for this Hold
252
253 =cut
254
255 sub borrower {
256     my ($self) = @_;
257
258     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
259
260     return $self->{_borrower};
261 }
262
263 =head3 is_suspended
264
265 my $bool = $hold->is_suspended();
266
267 =cut
268
269 sub is_suspended {
270     my ( $self ) = @_;
271
272     return $self->suspend();
273 }
274
275 =head3 type
276
277 =cut
278
279 sub _type {
280     return 'Reserve';
281 }
282
283 =head1 AUTHOR
284
285 Kyle M Hall <kyle@bywatersolutions.com>
286
287 =cut
288
289 1;