1 package Koha::REST::V1::Holds;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Controller';
30 use Koha::DateUtils qw( dt_from_string );
32 use List::MoreUtils qw( any );
33 use Try::Tiny qw( catch try );
41 Method that handles listing Koha::Hold objects
46 my $c = shift->openapi->valid_input or return;
48 my $old = $c->param('old');
49 $c->req->params->remove('old');
54 ? Koha::Old::Holds->new
57 my $holds = $c->objects->search($holds_set);
58 return $c->render( status => 200, openapi => $holds );
60 $c->unhandled_exception($_);
66 Method that handles adding a new Koha::Hold object
71 my $c = shift->openapi->valid_input or return;
74 my $body = $c->req->json;
79 my $biblio_id = $body->{biblio_id};
80 my $item_group_id = $body->{item_group_id};
81 my $pickup_library_id = $body->{pickup_library_id};
82 my $item_id = $body->{item_id};
83 my $patron_id = $body->{patron_id};
84 my $item_type = $body->{item_type};
85 my $expiration_date = $body->{expiration_date};
86 my $notes = $body->{notes};
87 my $hold_date = $body->{hold_date};
88 my $non_priority = $body->{non_priority};
90 my $overrides = $c->stash('koha.overrides');
91 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
93 if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
96 openapi => { error => "Hold date in future not allowed" }
100 if ( $item_id and $biblio_id ) {
102 $biblio = Koha::Biblios->find($biblio_id);
103 $item = $biblio->items->find($item_id);
105 # check they are consistent
109 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
114 $item = Koha::Items->find($item_id);
119 openapi => { error => "item_id not found." }
123 $biblio = $item->biblio;
127 $biblio = Koha::Biblios->find($biblio_id);
132 openapi => { error => "At least one of biblio_id, item_id should be given" }
139 openapi => "Biblio not found."
143 my $patron = Koha::Patrons->find( $patron_id );
147 openapi => { error => 'patron_id not found' }
151 # If the hold is being forced, no need to validate
152 unless( $can_override ){
153 # Validate pickup location
154 my $valid_pickup_location;
155 if ($item) { # item-level hold
156 $valid_pickup_location =
157 any { $_->branchcode eq $pickup_library_id }
158 $item->pickup_locations(
159 { patron => $patron } )->as_list;
162 $valid_pickup_location =
163 any { $_->branchcode eq $pickup_library_id }
164 $biblio->pickup_locations(
165 { patron => $patron } )->as_list;
171 error => 'The supplied pickup location is not valid'
173 ) unless $valid_pickup_location;
177 ? C4::Reserves::CanItemBeReserved( $patron, $item )
178 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
180 if ( C4::Context->preference('maxreserves') && $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
181 $can_place_hold->{status} = 'tooManyReserves';
184 unless ( $can_place_hold->{status} eq 'OK' ) {
188 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
193 my $priority = C4::Reserves::CalculatePriority($biblio_id);
195 my $hold_id = C4::Reserves::AddReserve(
197 branchcode => $pickup_library_id,
198 borrowernumber => $patron_id,
199 biblionumber => $biblio->id,
200 priority => $priority,
201 reservation_date => $hold_date,
202 expiration_date => $expiration_date,
204 title => $biblio->title,
205 itemnumber => $item_id,
206 found => undef, # TODO: Why not?
207 itemtype => $item_type,
208 non_priority => $non_priority,
209 item_group_id => $item_group_id,
216 openapi => 'Error placing the hold. See Koha logs for details.'
220 my $hold = Koha::Holds->find($hold_id);
224 openapi => $c->objects->to_api($hold),
228 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
229 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
230 my $broken_fk = $_->broken_fk;
232 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
235 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
241 $c->unhandled_exception($_);
247 Method that handles modifying a Koha::Hold object
252 my $c = shift->openapi->valid_input or return;
255 my $hold = Koha::Holds->find( $c->param('hold_id') );
260 openapi => { error => "Hold not found" }
264 my $overrides = $c->stash('koha.overrides');
265 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
267 my $body = $c->req->json;
269 my $pickup_library_id = $body->{pickup_library_id};
272 defined $pickup_library_id
273 && ( !$hold->is_pickup_location_valid({ library_id => $pickup_library_id }) && !$can_override )
279 error => 'The supplied pickup location is not valid'
284 $pickup_library_id //= $hold->branchcode;
285 my $priority = $body->{priority} // $hold->priority;
286 # suspended_until can also be set to undef
287 my $suspended_until = $body->{suspended_until} || $hold->suspend_until;
290 reserve_id => $hold->id,
291 branchcode => $pickup_library_id,
293 suspend_until => $suspended_until,
294 itemnumber => $hold->itemnumber,
297 C4::Reserves::ModReserve($params);
298 $hold->discard_changes; # refresh
302 openapi => $c->objects->to_api($hold),
306 $c->unhandled_exception($_);
312 Method that handles deleting a Koha::Hold object
317 my $c = shift->openapi->valid_input or return;
319 my $hold = Koha::Holds->find($c->param('hold_id'));
322 return $c->render( status => 404, openapi => { error => "Hold not found." } );
327 my $overrides = $c->stash('koha.overrides');
329 if ( $overrides->{'cancellation-request-flow'} && $hold->is_waiting ) {
331 $hold->add_cancellation_request;
347 $c->unhandled_exception($_);
353 Method that handles suspending a hold
358 my $c = shift->openapi->valid_input or return;
360 my $hold = Koha::Holds->find( $c->param('hold_id') );
361 my $body = $c->req->json;
363 my $end_date = ($body) ? $body->{end_date} : undef;
366 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
370 $hold->suspend_hold($end_date);
371 $hold->discard_changes;
372 $c->res->headers->location( $c->req->url->to_string );
374 my $suspend_until = $end_date ? dt_from_string($hold->suspend_until)->ymd : undef;
378 end_date => $suspend_until,
383 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
384 return $c->render( status => 400, openapi => { error => "$_" } );
387 $c->unhandled_exception($_);
393 Method that handles resuming a hold
398 my $c = shift->openapi->valid_input or return;
400 my $hold = Koha::Holds->find($c->param('hold_id'));
401 my $body = $c->req->json;
404 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
409 return $c->render( status => 204, openapi => {} );
412 $c->unhandled_exception($_);
416 =head3 update_priority
418 Method that handles modifying a Koha::Hold object
422 sub update_priority {
423 my $c = shift->openapi->valid_input or return;
425 my $hold = Koha::Holds->find($c->param('hold_id'));
430 openapi => { error => "Hold not found" }
435 my $priority = $c->req->json;
436 C4::Reserves::_FixPriority(
438 reserve_id => $hold->id,
443 return $c->render( status => 200, openapi => $priority );
446 $c->unhandled_exception($_);
450 =head3 pickup_locations
452 Method that returns the possible pickup_locations for a given hold
453 used for building the dropdown selector
457 sub pickup_locations {
458 my $c = shift->openapi->valid_input or return;
460 my $hold = Koha::Holds->find( $c->param('hold_id'), { prefetch => [ 'patron' ] } );
465 openapi => { error => "Hold not found" }
472 if ( $hold->itemnumber ) {
473 $ps_set = $hold->item->pickup_locations( { patron => $hold->patron } );
476 $ps_set = $hold->biblio->pickup_locations( { patron => $hold->patron } );
479 my $pickup_locations = $c->objects->search( $ps_set );
482 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
484 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
485 my $libraries = $c->objects->search($libraries_rs);
489 $library->{needs_override} = (
490 any { $_->{library_id} eq $library->{library_id} }
500 openapi => \@response
504 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
508 openapi => \@response
512 $c->unhandled_exception($_);
516 =head3 update_pickup_location
518 Method that handles modifying the pickup location of a Koha::Hold object
522 sub update_pickup_location {
523 my $c = shift->openapi->valid_input or return;
525 my $hold = Koha::Holds->find($c->param('hold_id'));
530 openapi => { error => "Hold not found" }
537 my $body = $c->req->json;
539 my $pickup_library_id = $body->{pickup_library_id};
541 my $overrides = $c->stash('koha.overrides');
542 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
545 $hold->is_waiting ? 'hold_waiting'
546 : $hold->is_in_processing ? 'hold_in_processing'
551 openapi => { error => 'Cannot change pickup location', error_code => $error_code }
554 $hold->set_pickup_location(
556 library_id => $pickup_library_id,
557 force => $can_override
564 pickup_library_id => $pickup_library_id
570 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
579 $c->unhandled_exception($_);