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';
22 use Mojo::JSON qw(decode_json);
32 use List::MoreUtils qw(any);
41 Method that handles listing Koha::Hold objects
46 my $c = shift->openapi->valid_input or return;
49 my $holds_set = Koha::Holds->new;
50 my $holds = $c->objects->search( $holds_set );
51 return $c->render( status => 200, openapi => $holds );
54 $c->unhandled_exception($_);
60 Method that handles adding a new Koha::Hold object
65 my $c = shift->openapi->valid_input or return;
68 my $body = $c->validation->param('body');
73 my $biblio_id = $body->{biblio_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 } );
155 $valid_pickup_location =
156 any { $_->branchcode eq $pickup_library_id }
157 $biblio->pickup_locations(
158 { patron => $patron } );
164 error => 'The supplied pickup location is not valid'
166 ) unless $valid_pickup_location || $can_override;
170 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
171 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
173 if ( $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 # AddReserve expects date to be in syspref format
188 if ($expiration_date) {
189 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
192 my $hold_id = C4::Reserves::AddReserve(
194 branchcode => $pickup_library_id,
195 borrowernumber => $patron_id,
196 biblionumber => $biblio_id,
197 priority => $priority,
198 reservation_date => $hold_date,
199 expiration_date => $expiration_date,
201 title => $biblio->title,
202 itemnumber => $item_id,
203 found => undef, # TODO: Why not?
204 itemtype => $item_type,
205 non_priority => $non_priority,
212 openapi => 'Error placing the hold. See Koha logs for details.'
216 my $hold = Koha::Holds->find($hold_id);
220 openapi => $hold->to_api
224 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
225 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
226 my $broken_fk = $_->broken_fk;
228 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
231 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
237 $c->unhandled_exception($_);
243 Method that handles modifying a Koha::Hold object
248 my $c = shift->openapi->valid_input or return;
251 my $hold_id = $c->validation->param('hold_id');
252 my $hold = Koha::Holds->find( $hold_id );
257 openapi => { error => "Hold not found" }
261 my $overrides = $c->stash('koha.overrides');
262 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
264 my $body = $c->validation->output->{body};
266 my $pickup_library_id = $body->{pickup_library_id};
269 defined $pickup_library_id
270 && ( !$hold->is_pickup_location_valid({ library_id => $pickup_library_id }) && !$can_override )
276 error => 'The supplied pickup location is not valid'
281 $pickup_library_id //= $hold->branchcode;
282 my $priority = $body->{priority} // $hold->priority;
283 # suspended_until can also be set to undef
284 my $suspended_until = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
287 reserve_id => $hold_id,
288 branchcode => $pickup_library_id,
290 suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
291 itemnumber => $hold->itemnumber
294 C4::Reserves::ModReserve($params);
295 $hold->discard_changes; # refresh
299 openapi => $hold->to_api
303 $c->unhandled_exception($_);
309 Method that handles deleting a Koha::Hold object
314 my $c = shift->openapi->valid_input or return;
316 my $hold_id = $c->validation->param('hold_id');
317 my $hold = Koha::Holds->find($hold_id);
320 return $c->render( status => 404, openapi => { error => "Hold not found." } );
332 $c->unhandled_exception($_);
338 Method that handles suspending a hold
343 my $c = shift->openapi->valid_input or return;
345 my $hold_id = $c->validation->param('hold_id');
346 my $hold = Koha::Holds->find($hold_id);
347 my $body = $c->req->json;
348 my $end_date = ($body) ? $body->{end_date} : undef;
351 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
355 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
356 $hold->suspend_hold($date);
357 $hold->discard_changes;
358 $c->res->headers->location( $c->req->url->to_string );
359 my $suspend_end_date;
360 if ($hold->suspend_until) {
361 $suspend_end_date = output_pref({
362 dt => dt_from_string( $hold->suspend_until ),
363 dateformat => 'rfc3339',
371 end_date => $suspend_end_date
376 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
377 return $c->render( status => 400, openapi => { error => "$_" } );
380 $c->unhandled_exception($_);
386 Method that handles resuming a hold
391 my $c = shift->openapi->valid_input or return;
393 my $hold_id = $c->validation->param('hold_id');
394 my $hold = Koha::Holds->find($hold_id);
395 my $body = $c->req->json;
398 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
403 return $c->render( status => 204, openapi => {} );
406 $c->unhandled_exception($_);
410 =head3 update_priority
412 Method that handles modifying a Koha::Hold object
416 sub update_priority {
417 my $c = shift->openapi->valid_input or return;
419 my $hold_id = $c->validation->param('hold_id');
420 my $hold = Koha::Holds->find($hold_id);
425 openapi => { error => "Hold not found" }
430 my $priority = $c->req->json;
431 C4::Reserves::_FixPriority(
433 reserve_id => $hold_id,
438 return $c->render( status => 200, openapi => $priority );
441 $c->unhandled_exception($_);
445 =head3 pickup_locations
447 Method that returns the possible pickup_locations for a given hold
448 used for building the dropdown selector
452 sub pickup_locations {
453 my $c = shift->openapi->valid_input or return;
455 my $hold_id = $c->validation->param('hold_id');
456 my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
461 openapi => { error => "Hold not found" }
468 if ( $hold->itemnumber ) {
469 $ps_set = $hold->item->pickup_locations( { patron => $hold->patron } );
472 $ps_set = $hold->biblio->pickup_locations( { patron => $hold->patron } );
475 my $pickup_locations = $c->objects->search( $ps_set );
478 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
480 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
481 my $libraries = $c->objects->search($libraries_rs);
485 $library->{needs_override} = (
486 any { $_->{library_id} eq $library->{library_id} }
496 openapi => \@response
500 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
504 openapi => \@response
508 $c->unhandled_exception($_);
512 =head3 update_pickup_location
514 Method that handles modifying the pickup location of a Koha::Hold object
518 sub update_pickup_location {
519 my $c = shift->openapi->valid_input or return;
521 my $hold_id = $c->validation->param('hold_id');
522 my $body = $c->validation->param('body');
523 my $pickup_library_id = $body->{pickup_library_id};
525 my $hold = Koha::Holds->find($hold_id);
530 openapi => { error => "Hold not found" }
536 $hold->set_pickup_location({ library_id => $pickup_library_id });
541 pickup_library_id => $pickup_library_id
547 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
556 $c->unhandled_exception($_);