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 if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
86 openapi => { error => "Hold date in future not allowed" }
90 if ( $item_id and $biblio_id ) {
92 # check they are consistent
93 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
98 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
102 $biblio = Koha::Biblios->find($biblio_id);
106 $item = Koha::Items->find($item_id);
111 openapi => { error => "item_id not found." }
115 $biblio = $item->biblio;
119 $biblio = Koha::Biblios->find($biblio_id);
124 openapi => { error => "At least one of biblio_id, item_id should be given" }
131 openapi => "Biblio not found."
135 my $patron = Koha::Patrons->find( $patron_id );
139 openapi => { error => 'patron_id not found' }
143 # Validate pickup location
144 my $valid_pickup_location;
145 if ($item) { # item-level hold
146 $valid_pickup_location =
147 any { $_->branchcode eq $pickup_library_id }
148 $item->pickup_locations(
149 { patron => $patron } );
152 $valid_pickup_location =
153 any { $_->branchcode eq $pickup_library_id }
154 $biblio->pickup_locations(
155 { patron => $patron } );
161 error => 'The supplied pickup location is not valid'
163 ) unless $valid_pickup_location;
167 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
168 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
170 if ( $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
171 $can_place_hold->{status} = 'tooManyReserves';
174 my $overrides = $c->stash('koha.overrides');
175 my $can_override = $overrides->{any} and C4::Context->preference('AllowHoldPolicyOverride');
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 );
255 return $c->render( status => 404,
256 openapi => {error => "Hold not found"} );
259 my $body = $c->req->json;
261 my $pickup_library_id = $body->{pickup_library_id};
264 defined $pickup_library_id
265 and not $hold->is_pickup_location_valid({ library_id => $pickup_library_id })
271 error => 'The supplied pickup location is not valid'
276 $pickup_library_id //= $hold->branchcode;
277 my $priority = $body->{priority} // $hold->priority;
278 # suspended_until can also be set to undef
279 my $suspended_until = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
282 reserve_id => $hold_id,
283 branchcode => $pickup_library_id,
285 suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
286 itemnumber => $hold->itemnumber
289 C4::Reserves::ModReserve($params);
290 $hold->discard_changes; # refresh
294 openapi => $hold->to_api
298 $c->unhandled_exception($_);
304 Method that handles deleting a Koha::Hold object
309 my $c = shift->openapi->valid_input or return;
311 my $hold_id = $c->validation->param('hold_id');
312 my $hold = Koha::Holds->find($hold_id);
315 return $c->render( status => 404, openapi => { error => "Hold not found." } );
327 $c->unhandled_exception($_);
333 Method that handles suspending a hold
338 my $c = shift->openapi->valid_input or return;
340 my $hold_id = $c->validation->param('hold_id');
341 my $hold = Koha::Holds->find($hold_id);
342 my $body = $c->req->json;
343 my $end_date = ($body) ? $body->{end_date} : undef;
346 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
350 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
351 $hold->suspend_hold($date);
352 $hold->discard_changes;
353 $c->res->headers->location( $c->req->url->to_string );
354 my $suspend_end_date;
355 if ($hold->suspend_until) {
356 $suspend_end_date = output_pref({
357 dt => dt_from_string( $hold->suspend_until ),
358 dateformat => 'rfc3339',
366 end_date => $suspend_end_date
371 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
372 return $c->render( status => 400, openapi => { error => "$_" } );
375 $c->unhandled_exception($_);
381 Method that handles resuming a hold
386 my $c = shift->openapi->valid_input or return;
388 my $hold_id = $c->validation->param('hold_id');
389 my $hold = Koha::Holds->find($hold_id);
390 my $body = $c->req->json;
393 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
398 return $c->render( status => 204, openapi => {} );
401 $c->unhandled_exception($_);
405 =head3 update_priority
407 Method that handles modifying a Koha::Hold object
411 sub update_priority {
412 my $c = shift->openapi->valid_input or return;
414 my $hold_id = $c->validation->param('hold_id');
415 my $hold = Koha::Holds->find($hold_id);
420 openapi => { error => "Hold not found" }
425 my $priority = $c->req->json;
426 C4::Reserves::_FixPriority(
428 reserve_id => $hold_id,
433 return $c->render( status => 200, openapi => $priority );
436 $c->unhandled_exception($_);
440 =head3 pickup_locations
442 Method that returns the possible pickup_locations for a given hold
443 used for building the dropdown selector
447 sub pickup_locations {
448 my $c = shift->openapi->valid_input or return;
450 my $hold_id = $c->validation->param('hold_id');
451 my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
456 openapi => { error => "Hold not found" }
463 if ( $hold->itemnumber ) {
464 $ps_set = $hold->item->pickup_locations( { patron => $hold->patron } );
467 $ps_set = $hold->biblio->pickup_locations( { patron => $hold->patron } );
470 my $pickup_locations = $c->objects->search( $ps_set );
473 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
475 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
476 my $libraries = $c->objects->search($libraries_rs);
480 $library->{needs_override} = (
481 any { $_->{library_id} eq $library->{library_id} }
491 openapi => \@response
495 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
499 openapi => \@response
503 $c->unhandled_exception($_);
507 =head3 update_pickup_location
509 Method that handles modifying the pickup location of a Koha::Hold object
513 sub update_pickup_location {
514 my $c = shift->openapi->valid_input or return;
516 my $hold_id = $c->validation->param('hold_id');
517 my $body = $c->validation->param('body');
518 my $pickup_library_id = $body->{pickup_library_id};
520 my $hold = Koha::Holds->find($hold_id);
525 openapi => { error => "Hold not found" }
531 $hold->set_pickup_location({ library_id => $pickup_library_id });
536 pickup_library_id => $pickup_library_id
542 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
551 $c->unhandled_exception($_);