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