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 } );
154 $valid_pickup_location =
155 any { $_->branchcode eq $pickup_library_id }
156 $biblio->pickup_locations(
157 { patron => $patron } );
163 error => 'The supplied pickup location is not valid'
165 ) unless $valid_pickup_location || $can_override;
169 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
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_from_string( $expiration_date, 'rfc3339' ) );
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 = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
286 reserve_id => $hold_id,
287 branchcode => $pickup_library_id,
289 suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
290 itemnumber => $hold->itemnumber
293 C4::Reserves::ModReserve($params);
294 $hold->discard_changes; # refresh
298 openapi => $hold->to_api
302 $c->unhandled_exception($_);
308 Method that handles deleting a Koha::Hold object
313 my $c = shift->openapi->valid_input or return;
315 my $hold_id = $c->validation->param('hold_id');
316 my $hold = Koha::Holds->find($hold_id);
319 return $c->render( status => 404, openapi => { error => "Hold not found." } );
331 $c->unhandled_exception($_);
337 Method that handles suspending a hold
342 my $c = shift->openapi->valid_input or return;
344 my $hold_id = $c->validation->param('hold_id');
345 my $hold = Koha::Holds->find($hold_id);
346 my $body = $c->req->json;
347 my $end_date = ($body) ? $body->{end_date} : undef;
350 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
354 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
355 $hold->suspend_hold($date);
356 $hold->discard_changes;
357 $c->res->headers->location( $c->req->url->to_string );
358 my $suspend_end_date;
359 if ($hold->suspend_until) {
360 $suspend_end_date = output_pref({
361 dt => dt_from_string( $hold->suspend_until ),
362 dateformat => 'rfc3339',
370 end_date => $suspend_end_date
375 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
376 return $c->render( status => 400, openapi => { error => "$_" } );
379 $c->unhandled_exception($_);
385 Method that handles resuming a hold
390 my $c = shift->openapi->valid_input or return;
392 my $hold_id = $c->validation->param('hold_id');
393 my $hold = Koha::Holds->find($hold_id);
394 my $body = $c->req->json;
397 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
402 return $c->render( status => 204, openapi => {} );
405 $c->unhandled_exception($_);
409 =head3 update_priority
411 Method that handles modifying a Koha::Hold object
415 sub update_priority {
416 my $c = shift->openapi->valid_input or return;
418 my $hold_id = $c->validation->param('hold_id');
419 my $hold = Koha::Holds->find($hold_id);
424 openapi => { error => "Hold not found" }
429 my $priority = $c->req->json;
430 C4::Reserves::_FixPriority(
432 reserve_id => $hold_id,
437 return $c->render( status => 200, openapi => $priority );
440 $c->unhandled_exception($_);
444 =head3 pickup_locations
446 Method that returns the possible pickup_locations for a given hold
447 used for building the dropdown selector
451 sub pickup_locations {
452 my $c = shift->openapi->valid_input or return;
454 my $hold_id = $c->validation->param('hold_id');
455 my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
460 openapi => { error => "Hold not found" }
467 if ( $hold->itemnumber ) {
468 $ps_set = $hold->item->pickup_locations( { patron => $hold->patron } );
471 $ps_set = $hold->biblio->pickup_locations( { patron => $hold->patron } );
474 my $pickup_locations = $c->objects->search( $ps_set );
477 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
479 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
480 my $libraries = $c->objects->search($libraries_rs);
484 $library->{needs_override} = (
485 any { $_->{library_id} eq $library->{library_id} }
495 openapi => \@response
499 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
503 openapi => \@response
507 $c->unhandled_exception($_);
511 =head3 update_pickup_location
513 Method that handles modifying the pickup location of a Koha::Hold object
517 sub update_pickup_location {
518 my $c = shift->openapi->valid_input or return;
520 my $hold_id = $c->validation->param('hold_id');
521 my $body = $c->validation->param('body');
522 my $pickup_library_id = $body->{pickup_library_id};
524 my $hold = Koha::Holds->find($hold_id);
529 openapi => { error => "Hold not found" }
535 my $overrides = $c->stash('koha.overrides');
536 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
538 $hold->set_pickup_location(
540 library_id => $pickup_library_id,
541 force => $can_override
548 pickup_library_id => $pickup_library_id
554 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
563 $c->unhandled_exception($_);