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 and not $hold->is_pickup_location_valid({ library_id => $pickup_library_id })
268 error => 'The supplied pickup location is not valid'
273 $pickup_library_id //= $hold->branchcode;
274 my $priority = $body->{priority} // $hold->priority;
275 # suspended_until can also be set to undef
276 my $suspended_until = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
279 reserve_id => $hold_id,
280 branchcode => $pickup_library_id,
282 suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
283 itemnumber => $hold->itemnumber
286 C4::Reserves::ModReserve($params);
287 $hold->discard_changes; # refresh
291 openapi => $hold->to_api
295 $c->unhandled_exception($_);
301 Method that handles deleting a Koha::Hold object
306 my $c = shift->openapi->valid_input or return;
308 my $hold_id = $c->validation->param('hold_id');
309 my $hold = Koha::Holds->find($hold_id);
312 return $c->render( status => 404, openapi => { error => "Hold not found." } );
324 $c->unhandled_exception($_);
330 Method that handles suspending a hold
335 my $c = shift->openapi->valid_input or return;
337 my $hold_id = $c->validation->param('hold_id');
338 my $hold = Koha::Holds->find($hold_id);
339 my $body = $c->req->json;
340 my $end_date = ($body) ? $body->{end_date} : undef;
343 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
347 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
348 $hold->suspend_hold($date);
349 $hold->discard_changes;
350 $c->res->headers->location( $c->req->url->to_string );
351 my $suspend_end_date;
352 if ($hold->suspend_until) {
353 $suspend_end_date = output_pref({
354 dt => dt_from_string( $hold->suspend_until ),
355 dateformat => 'rfc3339',
363 end_date => $suspend_end_date
368 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
369 return $c->render( status => 400, openapi => { error => "$_" } );
372 $c->unhandled_exception($_);
378 Method that handles resuming a hold
383 my $c = shift->openapi->valid_input or return;
385 my $hold_id = $c->validation->param('hold_id');
386 my $hold = Koha::Holds->find($hold_id);
387 my $body = $c->req->json;
390 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
395 return $c->render( status => 204, openapi => {} );
398 $c->unhandled_exception($_);
402 =head3 update_priority
404 Method that handles modifying a Koha::Hold object
408 sub update_priority {
409 my $c = shift->openapi->valid_input or return;
411 my $hold_id = $c->validation->param('hold_id');
412 my $hold = Koha::Holds->find($hold_id);
417 openapi => { error => "Hold not found" }
422 my $priority = $c->req->json;
423 C4::Reserves::_FixPriority(
425 reserve_id => $hold_id,
430 return $c->render( status => 200, openapi => $priority );
433 $c->unhandled_exception($_);
437 =head3 pickup_locations
439 Method that returns the possible pickup_locations for a given hold
440 used for building the dropdown selector
444 sub pickup_locations {
445 my $c = shift->openapi->valid_input or return;
447 my $hold_id = $c->validation->param('hold_id');
448 # FIXME: We should really skip the path params in $c->objects->search
449 delete $c->validation->output->{hold_id};
450 my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
455 openapi => { error => "Hold not found" }
462 if ( $hold->itemnumber ) {
463 $ps_set = $hold->item->pickup_locations( { patron => $hold->patron } );
466 $ps_set = $hold->biblio->pickup_locations( { patron => $hold->patron } );
469 my $pickup_locations = $c->objects->search( $ps_set );
473 openapi => $pickup_locations
477 $c->unhandled_exception($_);