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';
30 use List::MoreUtils qw(any);
39 Mehtod that handles listing Koha::Hold objects
44 my $c = shift->openapi->valid_input or return;
47 my $holds_set = Koha::Holds->new;
48 my $holds = $c->objects->search( $holds_set );
49 return $c->render( status => 200, openapi => $holds );
52 $c->unhandled_exception($_);
58 Method that handles adding a new Koha::Hold object
63 my $c = shift->openapi->valid_input or return;
66 my $body = $c->validation->param('body');
71 my $biblio_id = $body->{biblio_id};
72 my $pickup_library_id = $body->{pickup_library_id};
73 my $item_id = $body->{item_id};
74 my $patron_id = $body->{patron_id};
75 my $item_type = $body->{item_type};
76 my $expiration_date = $body->{expiration_date};
77 my $notes = $body->{notes};
78 my $hold_date = $body->{hold_date};
79 my $non_priority = $body->{non_priority};
81 if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
84 openapi => { error => "Hold date in future not allowed" }
88 if ( $item_id and $biblio_id ) {
90 # check they are consistent
91 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
96 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
100 $biblio = Koha::Biblios->find($biblio_id);
104 $item = Koha::Items->find($item_id);
109 openapi => { error => "item_id not found." }
113 $biblio = $item->biblio;
117 $biblio = Koha::Biblios->find($biblio_id);
122 openapi => { error => "At least one of biblio_id, item_id should be given" }
129 openapi => "Biblio not found."
133 my $patron = Koha::Patrons->find( $patron_id );
137 openapi => { error => 'patron_id not found' }
141 # Validate pickup location
142 my $valid_pickup_location;
143 if ($item) { # item-level hold
144 $valid_pickup_location =
145 any { $_->branchcode eq $pickup_library_id }
146 $item->pickup_locations(
147 { patron => $patron } );
150 $valid_pickup_location =
151 any { $_->branchcode eq $pickup_library_id }
152 $biblio->pickup_locations(
153 { patron => $patron } );
159 error => 'The supplied pickup location is not valid'
161 ) unless $valid_pickup_location;
165 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
166 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
168 if ( $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
169 $can_place_hold->{status} = 'tooManyReserves';
172 my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
174 unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
178 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
182 my $priority = C4::Reserves::CalculatePriority($biblio_id);
184 # AddReserve expects date to be in syspref format
185 if ($expiration_date) {
186 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
189 my $hold_id = C4::Reserves::AddReserve(
191 branchcode => $pickup_library_id,
192 borrowernumber => $patron_id,
193 biblionumber => $biblio_id,
194 priority => $priority,
195 reservation_date => $hold_date,
196 expiration_date => $expiration_date,
198 title => $biblio->title,
199 itemnumber => $item_id,
200 found => undef, # TODO: Why not?
201 itemtype => $item_type,
202 non_priority => $non_priority,
209 openapi => 'Error placing the hold. See Koha logs for details.'
213 my $hold = Koha::Holds->find($hold_id);
217 openapi => $hold->to_api
221 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
222 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
223 my $broken_fk = $_->broken_fk;
225 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
228 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
234 $c->unhandled_exception($_);
240 Method that handles modifying a Koha::Hold object
245 my $c = shift->openapi->valid_input or return;
248 my $hold_id = $c->validation->param('hold_id');
249 my $hold = Koha::Holds->find( $hold_id );
252 return $c->render( status => 404,
253 openapi => {error => "Hold not found"} );
256 my $body = $c->req->json;
258 my $pickup_library_id = $body->{pickup_library_id};
261 !defined $pickup_library_id
262 or $hold->is_pickup_location_valid(
263 { library_id => $pickup_library_id }
270 error => 'The supplied pickup location is not valid'
275 $pickup_library_id = $hold->branchcode
276 unless defined $pickup_library_id;
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" }
461 my @pickup_locations =
463 ? @{ $hold->item->pickup_locations( { patron => $hold->patron } )->as_list() }
464 : @{ $hold->biblio->pickup_locations( { patron => $hold->patron } )->as_list() };
466 @pickup_locations = map { $_->to_api } @pickup_locations;
470 openapi => \@pickup_locations
474 $c->unhandled_exception($_);