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';
29 use Koha::DateUtils qw( dt_from_string );
31 use List::MoreUtils qw( any );
32 use Try::Tiny qw( catch try );
40 Method that handles listing Koha::Hold objects
45 my $c = shift->openapi->valid_input or return;
48 my $holds_set = Koha::Holds->new;
49 my $holds = $c->objects->search( $holds_set );
50 return $c->render( status => 200, openapi => $holds );
53 $c->unhandled_exception($_);
59 Method that handles adding a new Koha::Hold object
64 my $c = shift->openapi->valid_input or return;
67 my $body = $c->validation->param('body');
72 my $biblio_id = $body->{biblio_id};
73 my $item_group_id = $body->{item_group_id};
74 my $pickup_library_id = $body->{pickup_library_id};
75 my $item_id = $body->{item_id};
76 my $patron_id = $body->{patron_id};
77 my $item_type = $body->{item_type};
78 my $expiration_date = $body->{expiration_date};
79 my $notes = $body->{notes};
80 my $hold_date = $body->{hold_date};
81 my $non_priority = $body->{non_priority};
83 my $overrides = $c->stash('koha.overrides');
84 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
86 if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
89 openapi => { error => "Hold date in future not allowed" }
93 if ( $item_id and $biblio_id ) {
95 # check they are consistent
96 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
101 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
105 $biblio = Koha::Biblios->find($biblio_id);
109 $item = Koha::Items->find($item_id);
114 openapi => { error => "item_id not found." }
118 $biblio = $item->biblio;
122 $biblio = Koha::Biblios->find($biblio_id);
127 openapi => { error => "At least one of biblio_id, item_id should be given" }
134 openapi => "Biblio not found."
138 my $patron = Koha::Patrons->find( $patron_id );
142 openapi => { error => 'patron_id not found' }
146 # Validate pickup location
147 my $valid_pickup_location;
148 if ($item) { # item-level hold
149 $valid_pickup_location =
150 any { $_->branchcode eq $pickup_library_id }
151 $item->pickup_locations(
152 { patron => $patron } )->as_list;
155 $valid_pickup_location =
156 any { $_->branchcode eq $pickup_library_id }
157 $biblio->pickup_locations(
158 { patron => $patron } )->as_list;
164 error => 'The supplied pickup location is not valid'
166 ) unless $valid_pickup_location || $can_override;
170 ? C4::Reserves::CanItemBeReserved( $patron, $item )
171 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
173 if ( C4::Context->preference('maxreserves') && $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
174 $can_place_hold->{status} = 'tooManyReserves';
177 unless ( $can_override || $can_place_hold->{status} eq 'OK' ) {
181 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
185 my $priority = C4::Reserves::CalculatePriority($biblio_id);
187 my $hold_id = C4::Reserves::AddReserve(
189 branchcode => $pickup_library_id,
190 borrowernumber => $patron_id,
191 biblionumber => $biblio->id,
192 priority => $priority,
193 reservation_date => $hold_date,
194 expiration_date => $expiration_date,
196 title => $biblio->title,
197 itemnumber => $item_id,
198 found => undef, # TODO: Why not?
199 itemtype => $item_type,
200 non_priority => $non_priority,
201 item_group_id => $item_group_id,
208 openapi => 'Error placing the hold. See Koha logs for details.'
212 my $hold = Koha::Holds->find($hold_id);
216 openapi => $hold->to_api
220 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
221 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
222 my $broken_fk = $_->broken_fk;
224 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
227 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
233 $c->unhandled_exception($_);
239 Method that handles modifying a Koha::Hold object
244 my $c = shift->openapi->valid_input or return;
247 my $hold_id = $c->validation->param('hold_id');
248 my $hold = Koha::Holds->find( $hold_id );
253 openapi => { error => "Hold not found" }
257 my $overrides = $c->stash('koha.overrides');
258 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
260 my $body = $c->validation->output->{body};
262 my $pickup_library_id = $body->{pickup_library_id};
265 defined $pickup_library_id
266 && ( !$hold->is_pickup_location_valid({ library_id => $pickup_library_id }) && !$can_override )
272 error => 'The supplied pickup location is not valid'
277 $pickup_library_id //= $hold->branchcode;
278 my $priority = $body->{priority} // $hold->priority;
279 # suspended_until can also be set to undef
280 my $suspended_until = $body->{suspended_until} || $hold->suspend_until;
283 reserve_id => $hold_id,
284 branchcode => $pickup_library_id,
286 suspend_until => $suspended_until,
287 itemnumber => $hold->itemnumber,
290 C4::Reserves::ModReserve($params);
291 $hold->discard_changes; # refresh
295 openapi => $hold->to_api
299 $c->unhandled_exception($_);
305 Method that handles deleting a Koha::Hold object
310 my $c = shift->openapi->valid_input or return;
312 my $hold_id = $c->validation->param('hold_id');
313 my $hold = Koha::Holds->find($hold_id);
316 return $c->render( status => 404, openapi => { error => "Hold not found." } );
328 $c->unhandled_exception($_);
334 Method that handles suspending a hold
339 my $c = shift->openapi->valid_input or return;
341 my $hold_id = $c->validation->param('hold_id');
342 my $hold = Koha::Holds->find($hold_id);
343 my $body = $c->req->json;
344 my $end_date = ($body) ? $body->{end_date} : undef;
347 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
351 $hold->suspend_hold($end_date);
352 $hold->discard_changes;
353 $c->res->headers->location( $c->req->url->to_string );
355 my $suspend_until = $end_date ? dt_from_string($hold->suspend_until)->ymd : undef;
359 end_date => $suspend_until,
364 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
365 return $c->render( status => 400, openapi => { error => "$_" } );
368 $c->unhandled_exception($_);
374 Method that handles resuming a hold
379 my $c = shift->openapi->valid_input or return;
381 my $hold_id = $c->validation->param('hold_id');
382 my $hold = Koha::Holds->find($hold_id);
383 my $body = $c->req->json;
386 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
391 return $c->render( status => 204, openapi => {} );
394 $c->unhandled_exception($_);
398 =head3 update_priority
400 Method that handles modifying a Koha::Hold object
404 sub update_priority {
405 my $c = shift->openapi->valid_input or return;
407 my $hold_id = $c->validation->param('hold_id');
408 my $hold = Koha::Holds->find($hold_id);
413 openapi => { error => "Hold not found" }
418 my $priority = $c->req->json;
419 C4::Reserves::_FixPriority(
421 reserve_id => $hold_id,
426 return $c->render( status => 200, openapi => $priority );
429 $c->unhandled_exception($_);
433 =head3 pickup_locations
435 Method that returns the possible pickup_locations for a given hold
436 used for building the dropdown selector
440 sub pickup_locations {
441 my $c = shift->openapi->valid_input or return;
443 my $hold_id = $c->validation->param('hold_id');
444 my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
449 openapi => { error => "Hold not found" }
456 if ( $hold->itemnumber ) {
457 $ps_set = $hold->item->pickup_locations( { patron => $hold->patron } );
460 $ps_set = $hold->biblio->pickup_locations( { patron => $hold->patron } );
463 my $pickup_locations = $c->objects->search( $ps_set );
466 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
468 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
469 my $libraries = $c->objects->search($libraries_rs);
473 $library->{needs_override} = (
474 any { $_->{library_id} eq $library->{library_id} }
484 openapi => \@response
488 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
492 openapi => \@response
496 $c->unhandled_exception($_);
500 =head3 update_pickup_location
502 Method that handles modifying the pickup location of a Koha::Hold object
506 sub update_pickup_location {
507 my $c = shift->openapi->valid_input or return;
509 my $hold_id = $c->validation->param('hold_id');
510 my $body = $c->validation->param('body');
511 my $pickup_library_id = $body->{pickup_library_id};
513 my $hold = Koha::Holds->find($hold_id);
518 openapi => { error => "Hold not found" }
524 my $overrides = $c->stash('koha.overrides');
525 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
527 $hold->set_pickup_location(
529 library_id => $pickup_library_id,
530 force => $can_override
537 pickup_library_id => $pickup_library_id
543 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
552 $c->unhandled_exception($_);