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 output_pref );
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 $pickup_library_id = $body->{pickup_library_id};
74 my $item_id = $body->{item_id};
75 my $patron_id = $body->{patron_id};
76 my $item_type = $body->{item_type};
77 my $expiration_date = $body->{expiration_date};
78 my $notes = $body->{notes};
79 my $hold_date = $body->{hold_date};
80 my $non_priority = $body->{non_priority};
82 my $overrides = $c->stash('koha.overrides');
83 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
85 if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
88 openapi => { error => "Hold date in future not allowed" }
92 if ( $item_id and $biblio_id ) {
94 # check they are consistent
95 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
100 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
104 $biblio = Koha::Biblios->find($biblio_id);
108 $item = Koha::Items->find($item_id);
113 openapi => { error => "item_id not found." }
117 $biblio = $item->biblio;
121 $biblio = Koha::Biblios->find($biblio_id);
126 openapi => { error => "At least one of biblio_id, item_id should be given" }
133 openapi => "Biblio not found."
137 my $patron = Koha::Patrons->find( $patron_id );
141 openapi => { error => 'patron_id not found' }
145 # Validate pickup location
146 my $valid_pickup_location;
147 if ($item) { # item-level hold
148 $valid_pickup_location =
149 any { $_->branchcode eq $pickup_library_id }
150 $item->pickup_locations(
151 { patron => $patron } )->as_list;
154 $valid_pickup_location =
155 any { $_->branchcode eq $pickup_library_id }
156 $biblio->pickup_locations(
157 { patron => $patron } )->as_list;
163 error => 'The supplied pickup location is not valid'
165 ) unless $valid_pickup_location || $can_override;
169 ? C4::Reserves::CanItemBeReserved( $patron, $item )
170 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
172 if ( $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
173 $can_place_hold->{status} = 'tooManyReserves';
176 unless ( $can_override || $can_place_hold->{status} eq 'OK' ) {
180 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
184 my $priority = C4::Reserves::CalculatePriority($biblio_id);
186 # AddReserve expects date to be in syspref format
187 if ($expiration_date) {
188 $expiration_date = output_pref( { dt => dt_from_string($expiration_date, 'iso'), dateformat => 'iso', dateonly => 1 } );
191 my $hold_id = C4::Reserves::AddReserve(
193 branchcode => $pickup_library_id,
194 borrowernumber => $patron_id,
195 biblionumber => $biblio->id,
196 priority => $priority,
197 reservation_date => $hold_date,
198 expiration_date => $expiration_date,
200 title => $biblio->title,
201 itemnumber => $item_id,
202 found => undef, # TODO: Why not?
203 itemtype => $item_type,
204 non_priority => $non_priority,
211 openapi => 'Error placing the hold. See Koha logs for details.'
215 my $hold = Koha::Holds->find($hold_id);
219 openapi => $hold->to_api
223 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
224 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
225 my $broken_fk = $_->broken_fk;
227 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
230 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
236 $c->unhandled_exception($_);
242 Method that handles modifying a Koha::Hold object
247 my $c = shift->openapi->valid_input or return;
250 my $hold_id = $c->validation->param('hold_id');
251 my $hold = Koha::Holds->find( $hold_id );
256 openapi => { error => "Hold not found" }
260 my $overrides = $c->stash('koha.overrides');
261 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
263 my $body = $c->validation->output->{body};
265 my $pickup_library_id = $body->{pickup_library_id};
268 defined $pickup_library_id
269 && ( !$hold->is_pickup_location_valid({ library_id => $pickup_library_id }) && !$can_override )
275 error => 'The supplied pickup location is not valid'
280 $pickup_library_id //= $hold->branchcode;
281 my $priority = $body->{priority} // $hold->priority;
282 # suspended_until can also be set to undef
283 my $suspended_until =
284 exists $body->{suspended_until}
285 ? dt_from_string( $body->{suspended_until}, 'rfc3339' )
286 : dt_from_string( $hold->suspend_until, 'iso' );
289 reserve_id => $hold_id,
290 branchcode => $pickup_library_id,
292 suspend_until => $suspended_until
293 ? output_pref({ dt => $suspended_until, dateformat => 'iso', dateonly => 1 })
295 itemnumber => $hold->itemnumber
298 C4::Reserves::ModReserve($params);
299 $hold->discard_changes; # refresh
303 openapi => $hold->to_api
307 $c->unhandled_exception($_);
313 Method that handles deleting a Koha::Hold object
318 my $c = shift->openapi->valid_input or return;
320 my $hold_id = $c->validation->param('hold_id');
321 my $hold = Koha::Holds->find($hold_id);
324 return $c->render( status => 404, openapi => { error => "Hold not found." } );
336 $c->unhandled_exception($_);
342 Method that handles suspending a hold
347 my $c = shift->openapi->valid_input or return;
349 my $hold_id = $c->validation->param('hold_id');
350 my $hold = Koha::Holds->find($hold_id);
351 my $body = $c->req->json;
352 my $end_date = ($body) ? $body->{end_date} : undef;
355 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
359 my $date = ($end_date) ? dt_from_string( $end_date, 'iso' ) : undef;
360 $hold->suspend_hold($date);
361 $hold->discard_changes;
362 $c->res->headers->location( $c->req->url->to_string );
363 my $suspend_end_date;
364 if ($hold->suspend_until) {
365 $suspend_end_date = output_pref({
366 dt => dt_from_string( $hold->suspend_until, 'iso' ),
375 end_date => $suspend_end_date
380 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
381 return $c->render( status => 400, openapi => { error => "$_" } );
384 $c->unhandled_exception($_);
390 Method that handles resuming a hold
395 my $c = shift->openapi->valid_input or return;
397 my $hold_id = $c->validation->param('hold_id');
398 my $hold = Koha::Holds->find($hold_id);
399 my $body = $c->req->json;
402 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
407 return $c->render( status => 204, openapi => {} );
410 $c->unhandled_exception($_);
414 =head3 update_priority
416 Method that handles modifying a Koha::Hold object
420 sub update_priority {
421 my $c = shift->openapi->valid_input or return;
423 my $hold_id = $c->validation->param('hold_id');
424 my $hold = Koha::Holds->find($hold_id);
429 openapi => { error => "Hold not found" }
434 my $priority = $c->req->json;
435 C4::Reserves::_FixPriority(
437 reserve_id => $hold_id,
442 return $c->render( status => 200, openapi => $priority );
445 $c->unhandled_exception($_);
449 =head3 pickup_locations
451 Method that returns the possible pickup_locations for a given hold
452 used for building the dropdown selector
456 sub pickup_locations {
457 my $c = shift->openapi->valid_input or return;
459 my $hold_id = $c->validation->param('hold_id');
460 my $hold = Koha::Holds->find( $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_id = $c->validation->param('hold_id');
526 my $body = $c->validation->param('body');
527 my $pickup_library_id = $body->{pickup_library_id};
529 my $hold = Koha::Holds->find($hold_id);
534 openapi => { error => "Hold not found" }
540 my $overrides = $c->stash('koha.overrides');
541 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
543 $hold->set_pickup_location(
545 library_id => $pickup_library_id,
546 force => $can_override
553 pickup_library_id => $pickup_library_id
559 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
568 $c->unhandled_exception($_);