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 # If the hold is being forced, no need to validate
147 unless( $can_override ){
148 # Validate pickup location
149 my $valid_pickup_location;
150 if ($item) { # item-level hold
151 $valid_pickup_location =
152 any { $_->branchcode eq $pickup_library_id }
153 $item->pickup_locations(
154 { patron => $patron } )->as_list;
157 $valid_pickup_location =
158 any { $_->branchcode eq $pickup_library_id }
159 $biblio->pickup_locations(
160 { patron => $patron } )->as_list;
166 error => 'The supplied pickup location is not valid'
168 ) unless $valid_pickup_location;
172 ? C4::Reserves::CanItemBeReserved( $patron, $item )
173 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
175 if ( C4::Context->preference('maxreserves') && $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
176 $can_place_hold->{status} = 'tooManyReserves';
179 unless ( $can_place_hold->{status} eq 'OK' ) {
183 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
188 my $priority = C4::Reserves::CalculatePriority($biblio_id);
190 my $hold_id = C4::Reserves::AddReserve(
192 branchcode => $pickup_library_id,
193 borrowernumber => $patron_id,
194 biblionumber => $biblio->id,
195 priority => $priority,
196 reservation_date => $hold_date,
197 expiration_date => $expiration_date,
199 title => $biblio->title,
200 itemnumber => $item_id,
201 found => undef, # TODO: Why not?
202 itemtype => $item_type,
203 non_priority => $non_priority,
204 item_group_id => $item_group_id,
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 = $body->{suspended_until} || $hold->suspend_until;
286 reserve_id => $hold_id,
287 branchcode => $pickup_library_id,
289 suspend_until => $suspended_until,
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 $hold->suspend_hold($end_date);
355 $hold->discard_changes;
356 $c->res->headers->location( $c->req->url->to_string );
358 my $suspend_until = $end_date ? dt_from_string($hold->suspend_until)->ymd : undef;
362 end_date => $suspend_until,
367 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
368 return $c->render( status => 400, openapi => { error => "$_" } );
371 $c->unhandled_exception($_);
377 Method that handles resuming a hold
382 my $c = shift->openapi->valid_input or return;
384 my $hold_id = $c->validation->param('hold_id');
385 my $hold = Koha::Holds->find($hold_id);
386 my $body = $c->req->json;
389 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
394 return $c->render( status => 204, openapi => {} );
397 $c->unhandled_exception($_);
401 =head3 update_priority
403 Method that handles modifying a Koha::Hold object
407 sub update_priority {
408 my $c = shift->openapi->valid_input or return;
410 my $hold_id = $c->validation->param('hold_id');
411 my $hold = Koha::Holds->find($hold_id);
416 openapi => { error => "Hold not found" }
421 my $priority = $c->req->json;
422 C4::Reserves::_FixPriority(
424 reserve_id => $hold_id,
429 return $c->render( status => 200, openapi => $priority );
432 $c->unhandled_exception($_);
436 =head3 pickup_locations
438 Method that returns the possible pickup_locations for a given hold
439 used for building the dropdown selector
443 sub pickup_locations {
444 my $c = shift->openapi->valid_input or return;
446 my $hold_id = $c->validation->param('hold_id');
447 my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
452 openapi => { error => "Hold not found" }
459 if ( $hold->itemnumber ) {
460 $ps_set = $hold->item->pickup_locations( { patron => $hold->patron } );
463 $ps_set = $hold->biblio->pickup_locations( { patron => $hold->patron } );
466 my $pickup_locations = $c->objects->search( $ps_set );
469 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
471 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
472 my $libraries = $c->objects->search($libraries_rs);
476 $library->{needs_override} = (
477 any { $_->{library_id} eq $library->{library_id} }
487 openapi => \@response
491 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
495 openapi => \@response
499 $c->unhandled_exception($_);
503 =head3 update_pickup_location
505 Method that handles modifying the pickup location of a Koha::Hold object
509 sub update_pickup_location {
510 my $c = shift->openapi->valid_input or return;
512 my $hold_id = $c->validation->param('hold_id');
513 my $body = $c->validation->param('body');
514 my $pickup_library_id = $body->{pickup_library_id};
516 my $hold = Koha::Holds->find($hold_id);
521 openapi => { error => "Hold not found" }
527 my $overrides = $c->stash('koha.overrides');
528 my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
530 $hold->set_pickup_location(
532 library_id => $pickup_library_id,
533 force => $can_override
540 pickup_library_id => $pickup_library_id
546 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
555 $c->unhandled_exception($_);