Line 0
Link Here
|
|
|
1 |
package Koha::Recall; |
2 |
|
3 |
# Copyright 2020 Aleisha Amohia <aleisha@catalyst.net.nz> |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Koha::Database; |
23 |
use Koha::DateUtils; |
24 |
|
25 |
use base qw(Koha::Object); |
26 |
|
27 |
=head1 NAME |
28 |
|
29 |
Koha::Recall - Koha Recall Object class |
30 |
|
31 |
=head1 API |
32 |
|
33 |
=head2 Internal methods |
34 |
|
35 |
=cut |
36 |
|
37 |
=head3 biblio |
38 |
|
39 |
my $biblio = $recall->biblio; |
40 |
|
41 |
Returns the related Koha::Biblio object for this recall. |
42 |
|
43 |
=cut |
44 |
|
45 |
sub biblio { |
46 |
my ( $self ) = @_; |
47 |
my $biblio_rs = $self->_result->biblio; |
48 |
return Koha::Biblio->_new_from_dbic( $biblio_rs ); |
49 |
} |
50 |
|
51 |
=head3 item |
52 |
|
53 |
my $item = $recall->item; |
54 |
|
55 |
Returns the related Koha::Item object for this recall. |
56 |
|
57 |
=cut |
58 |
|
59 |
sub item { |
60 |
my ( $self ) = @_; |
61 |
my $item_rs = $self->_result->item; |
62 |
if ( $item_rs ){ |
63 |
return Koha::Item->_new_from_dbic( $item_rs ); |
64 |
} |
65 |
return; |
66 |
} |
67 |
|
68 |
=head3 patron |
69 |
|
70 |
my $patron = $recall->patron; |
71 |
|
72 |
Returns the related Koha::Patron object for this recall. |
73 |
|
74 |
=cut |
75 |
|
76 |
sub patron { |
77 |
my ( $self ) = @_; |
78 |
my $patron_rs = $self->_result->borrower; |
79 |
return Koha::Patron->_new_from_dbic( $patron_rs ); |
80 |
} |
81 |
|
82 |
=head3 library |
83 |
|
84 |
my $library = $recall->library; |
85 |
|
86 |
Returns the related Koha::Library object for this recall. |
87 |
|
88 |
=cut |
89 |
|
90 |
sub library { |
91 |
my ( $self ) = @_; |
92 |
my $library_rs = $self->_result->branch; |
93 |
return Koha::Library->_new_from_dbic( $library_rs ); |
94 |
} |
95 |
|
96 |
=head3 checkout |
97 |
|
98 |
my $checkout = $recall->checkout; |
99 |
|
100 |
Returns the related Koha::Checkout object for this recall. |
101 |
|
102 |
=cut |
103 |
|
104 |
sub checkout { |
105 |
my ( $self ) = @_; |
106 |
$self->{_checkout} ||= Koha::Checkouts->find({ itemnumber => $self->itemnumber }); |
107 |
|
108 |
unless ( $self->item_level_recall ) { |
109 |
# Only look at checkouts of items that are allowed to be recalled, and get the oldest one |
110 |
my @items = Koha::Items->search({ biblionumber => $self->biblionumber }); |
111 |
my @itemnumbers; |
112 |
foreach (@items) { |
113 |
my $recalls_allowed = Koha::CirculationRules->get_effective_rule({ |
114 |
branchcode => C4::Context->userenv->{'branch'}, |
115 |
categorycode => $self->patron->categorycode, |
116 |
itemtype => $_->effective_itemtype, |
117 |
rule_name => 'recalls_allowed', |
118 |
}); |
119 |
if ( defined $recalls_allowed and $recalls_allowed->rule_value > 0 ) { |
120 |
push ( @itemnumbers, $_->itemnumber ); |
121 |
} |
122 |
} |
123 |
my $checkouts = Koha::Checkouts->search({ itemnumber => [ @itemnumbers ] }, { order_by => { -asc => 'date_due' } }); |
124 |
$self->{_checkout} = $checkouts->next; |
125 |
} |
126 |
|
127 |
return $self->{_checkout}; |
128 |
} |
129 |
|
130 |
=head3 requested |
131 |
|
132 |
if ( $recall->requested ) |
133 |
|
134 |
[% IF recall.requested %] |
135 |
|
136 |
Return true if recall status is requested. |
137 |
|
138 |
=cut |
139 |
|
140 |
sub requested { |
141 |
my ( $self ) = @_; |
142 |
my $status = $self->status; |
143 |
return $status && $status eq 'R'; |
144 |
} |
145 |
|
146 |
=head3 waiting |
147 |
|
148 |
if ( $recall->waiting ) |
149 |
|
150 |
[% IF recall.waiting %] |
151 |
|
152 |
Return true if recall is awaiting pickup. |
153 |
|
154 |
=cut |
155 |
|
156 |
sub waiting { |
157 |
my ( $self ) = @_; |
158 |
my $status = $self->status; |
159 |
return $status && $status eq 'W'; |
160 |
} |
161 |
|
162 |
=head3 overdue |
163 |
|
164 |
if ( $recall->overdue ) |
165 |
|
166 |
[% IF recall.overdue %] |
167 |
|
168 |
Return true if recall is overdue to be returned. |
169 |
|
170 |
=cut |
171 |
|
172 |
sub overdue { |
173 |
my ( $self ) = @_; |
174 |
my $status = $self->status; |
175 |
return $status && $status eq 'O'; |
176 |
} |
177 |
|
178 |
=head3 in_transit |
179 |
|
180 |
if ( $recall->in_transit ) |
181 |
|
182 |
[% IF recall.in_transit %] |
183 |
|
184 |
Return true if recall is in transit. |
185 |
|
186 |
=cut |
187 |
|
188 |
sub in_transit { |
189 |
my ( $self ) = @_; |
190 |
my $status = $self->status; |
191 |
return $status && $status eq 'T'; |
192 |
} |
193 |
|
194 |
=head3 expired |
195 |
|
196 |
if ( $recall->expired ) |
197 |
|
198 |
[% IF recall.expired %] |
199 |
|
200 |
Return true if recall has expired. |
201 |
|
202 |
=cut |
203 |
|
204 |
sub expired { |
205 |
my ( $self ) = @_; |
206 |
my $status = $self->status; |
207 |
return $status && $status eq 'E'; |
208 |
} |
209 |
|
210 |
=head3 cancelled |
211 |
|
212 |
if ( $recall->cancelled ) |
213 |
|
214 |
[% IF recall.cancelled %] |
215 |
|
216 |
Return true if recall has been cancelled. |
217 |
|
218 |
=cut |
219 |
|
220 |
sub cancelled { |
221 |
my ( $self ) = @_; |
222 |
my $status = $self->status; |
223 |
return $status && $status eq 'C'; |
224 |
} |
225 |
|
226 |
=head3 finished |
227 |
|
228 |
if ( $recall->finished ) |
229 |
|
230 |
[% IF recall.finished %] |
231 |
|
232 |
Return true if recall is finished and has been fulfilled. |
233 |
|
234 |
=cut |
235 |
|
236 |
sub finished { |
237 |
my ( $self ) = @_; |
238 |
my $status = $self->status; |
239 |
return $status && $status eq 'F'; |
240 |
} |
241 |
|
242 |
=head3 calc_expirationdate |
243 |
|
244 |
my $expirationdate = $recall->calc_expirationdate; |
245 |
$recall->update({ expirationdate => $expirationdate }); |
246 |
|
247 |
Calculate the expirationdate to set based on circulation rules and system preferences. |
248 |
|
249 |
=cut |
250 |
|
251 |
sub calc_expirationdate { |
252 |
my ( $self ) = @_; |
253 |
|
254 |
my $branchcode = C4::Circulation::_GetCircControlBranch( $self->item->unblessed, $self->patron->unblessed ); |
255 |
my $rule = Koha::CirculationRules->get_effective_rule({ |
256 |
categorycode => $self->patron->categorycode, |
257 |
branchcode => $branchcode, |
258 |
itemtype => $self->item->effective_itemtype, |
259 |
rule_name => 'recall_shelf_time' |
260 |
}); |
261 |
|
262 |
my $shelf_time = defined $rule ? $rule->rule_value : C4::Context->preference('RecallsMaxPickUpDelay'); |
263 |
|
264 |
my $expirationdate = dt_from_string->add( days => $shelf_time ); |
265 |
return $expirationdate; |
266 |
} |
267 |
|
268 |
=head3 start_transfer |
269 |
|
270 |
$recall->start_transfer({ item => $item_object }); |
271 |
|
272 |
Set the recall as in transit. |
273 |
|
274 |
=cut |
275 |
|
276 |
sub start_transfer { |
277 |
my ( $self, $params ) = @_; |
278 |
|
279 |
if ( $self->item_level_recall ) { |
280 |
# already has an itemnumber |
281 |
$self->update({ status => 'T' }); |
282 |
} else { |
283 |
my $itemnumber = $params->{item}->itemnumber; |
284 |
$self->update({ status => 'T', itemnumber => $itemnumber }); |
285 |
} |
286 |
|
287 |
my $ignore_reserves = 1; |
288 |
my ( $dotransfer, $messages ) = C4::Circulation::transferbook( $self->branchcode, $self->item->barcode, $ignore_reserves, 'Recalled' ); |
289 |
|
290 |
return $self; |
291 |
} |
292 |
|
293 |
=head3 set_waiting |
294 |
|
295 |
$recall->set_waiting({ |
296 |
expirationdate => $expirationdate, |
297 |
item => $item_object |
298 |
}); |
299 |
|
300 |
Set the recall as waiting and update expiration date. |
301 |
Notify the recall requester. |
302 |
|
303 |
=cut |
304 |
|
305 |
sub set_waiting { |
306 |
my ( $self, $params ) = @_; |
307 |
|
308 |
my $itemnumber; |
309 |
if ( $self->item_level_recall ) { |
310 |
$itemnumber = $self->itemnumber; |
311 |
$self->update({ status => 'W', waitingdate => dt_from_string, expirationdate => $params->{expirationdate} }); |
312 |
} else { |
313 |
# biblio-level recall with no itemnumber. need to set itemnumber |
314 |
$itemnumber = $params->{item}->itemnumber; |
315 |
$self->update({ status => 'W', waitingdate => dt_from_string, expirationdate => $params->{expirationdate}, itemnumber => $itemnumber }); |
316 |
} |
317 |
|
318 |
# send notice to recaller to pick up item |
319 |
my $letter = C4::Letters::GetPreparedLetter( |
320 |
module => 'circulation', |
321 |
letter_code => 'PICKUP_RECALLED_ITEM', |
322 |
branchcode => $self->branchcode, |
323 |
want_librarian => 0, |
324 |
tables => { |
325 |
biblio => $self->biblionumber, |
326 |
borrowers => $self->borrowernumber, |
327 |
items => $itemnumber, |
328 |
recalls => $self->recall_id, |
329 |
}, |
330 |
); |
331 |
|
332 |
C4::Message->enqueue($letter, $self->patron->unblessed, 'email'); |
333 |
|
334 |
return $self; |
335 |
} |
336 |
|
337 |
=head3 revert_waiting |
338 |
|
339 |
$recall->revert_waiting; |
340 |
|
341 |
Revert recall waiting status. |
342 |
|
343 |
=cut |
344 |
|
345 |
sub revert_waiting { |
346 |
my ( $self ) = @_; |
347 |
if ( $self->item_level_recall ){ |
348 |
$self->update({ status => 'R', waitingdate => undef }); |
349 |
} else { |
350 |
$self->update({ status => 'R', waitingdate => undef, itemnumber => undef }); |
351 |
} |
352 |
return $self; |
353 |
} |
354 |
|
355 |
=head3 set_overdue |
356 |
|
357 |
$recall->set_overdue; |
358 |
|
359 |
Set a recall as overdue when the recall has been requested and the borrower who has checked out the recalled item is late to return it. This will only be used by a cron - a recall cannot be set as overdue manually. |
360 |
|
361 |
=cut |
362 |
|
363 |
sub set_overdue { |
364 |
my ( $self ) = @_; |
365 |
$self->update({ status => 'O' }); |
366 |
C4::Log::logaction( 'RECALLS', 'OVERDUE', $self->recall_id, "Recall status set to overdue", 'COMMANDLINE') if ( C4::Context->preference('RecallsLog') ); |
367 |
return $self; |
368 |
} |
369 |
|
370 |
=head3 set_expired |
371 |
|
372 |
$recall->set_expired({ location => 'INTRANET' }); |
373 |
|
374 |
Set a recall as expired. This may be done manually or by a cronjob, either when the borrower that placed the recall takes more than RecallsMaxPickUpDelay number of days to collect their item, or if the specified expirationdate passes. The location is either 'INTRANET' or 'COMMANDLINE' for logging purposes. |
375 |
|
376 |
=cut |
377 |
|
378 |
sub set_expired { |
379 |
my ( $self, $params ) = @_; |
380 |
my $location = $params->{location} || 'COMMANDLINE'; |
381 |
$self->update({ status => 'E', old => 1, expirationdate => dt_from_string }); |
382 |
C4::Log::logaction( 'RECALLS', 'EXPIRE', $self->recall_id, "Recall expired", $location) if ( C4::Context->preference('RecallsLog') ); |
383 |
return $self; |
384 |
} |
385 |
|
386 |
=head3 set_cancelled |
387 |
|
388 |
$recall->set_cancelled; |
389 |
|
390 |
Set a recall as cancelled. This may be done manually, either by the borrower that placed the recall, or by the library. |
391 |
|
392 |
=cut |
393 |
|
394 |
sub set_cancelled { |
395 |
my ( $self ) = @_; |
396 |
$self->update({ status => 'C', old => 1, cancellationdate => dt_from_string }); |
397 |
C4::Log::logaction( 'RECALLS', 'CANCEL', $self->recall_id, "Recall cancelled", 'INTRANET') if ( C4::Context->preference('RecallsLog') ); |
398 |
return $self; |
399 |
} |
400 |
|
401 |
=head3 set_finished |
402 |
|
403 |
$recall->set_finished; |
404 |
|
405 |
Set a recall as finished. This should only be called when the item allocated to a recall is checked out to the borrower who requested the recall. |
406 |
|
407 |
=cut |
408 |
|
409 |
sub set_finished { |
410 |
my ( $self ) = @_; |
411 |
$self->update({ status => 'F', old => 1 }); |
412 |
C4::Log::logaction( 'RECALLS', 'FULFILL', $self->recall_id, "Recall fulfilled", 'INTRANET') if ( C4::Context->preference('RecallsLog') ); |
413 |
return $self; |
414 |
} |
415 |
|
416 |
=head3 _type |
417 |
|
418 |
=cut |
419 |
|
420 |
sub _type { |
421 |
return 'Recall'; |
422 |
} |
423 |
|
424 |
1; |