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';
22 use Mojo::JSON qw(decode_json);
32 use List::MoreUtils qw(any);
41 Method that handles listing Koha::Hold objects
46 my $c = shift->openapi->valid_input or return;
49 my $holds_set = Koha::Holds->new;
50 my $holds = $c->objects->search( $holds_set );
51 return $c->render( status => 200, openapi => $holds );
54 $c->unhandled_exception($_);
60 Method that handles adding a new Koha::Hold object
65 my $c = shift->openapi->valid_input or return;
68 my $body = $c->validation->param('body');
73 my $biblio_id = $body->{biblio_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 if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
86 openapi => { error => "Hold date in future not allowed" }
90 if ( $item_id and $biblio_id ) {
92 # check they are consistent
93 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
98 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
102 $biblio = Koha::Biblios->find($biblio_id);
106 $item = Koha::Items->find($item_id);
111 openapi => { error => "item_id not found." }
115 $biblio = $item->biblio;
119 $biblio = Koha::Biblios->find($biblio_id);
124 openapi => { error => "At least one of biblio_id, item_id should be given" }
131 openapi => "Biblio not found."
135 my $patron = Koha::Patrons->find( $patron_id );
139 openapi => { error => 'patron_id not found' }
143 # Validate pickup location
144 my $valid_pickup_location;
145 if ($item) { # item-level hold
146 $valid_pickup_location =
147 any { $_->branchcode eq $pickup_library_id }
148 $item->pickup_locations(
149 { patron => $patron } );
152 $valid_pickup_location =
153 any { $_->branchcode eq $pickup_library_id }
154 $biblio->pickup_locations(
155 { patron => $patron } );
161 error => 'The supplied pickup location is not valid'
163 ) unless $valid_pickup_location;
167 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
168 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
170 if ( $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
171 $can_place_hold->{status} = 'tooManyReserves';
174 my $override_header = $c->req->headers->header('x-koha-override');
175 $override_header = decode_json($override_header)
178 my $can_override = $override_header->{AllowHoldPolicyOverride}
179 and C4::Context->preference('AllowHoldPolicyOverride');
181 unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
185 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
189 my $priority = C4::Reserves::CalculatePriority($biblio_id);
191 # AddReserve expects date to be in syspref format
192 if ($expiration_date) {
193 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
196 my $hold_id = C4::Reserves::AddReserve(
198 branchcode => $pickup_library_id,
199 borrowernumber => $patron_id,
200 biblionumber => $biblio_id,
201 priority => $priority,
202 reservation_date => $hold_date,
203 expiration_date => $expiration_date,
205 title => $biblio->title,
206 itemnumber => $item_id,
207 found => undef, # TODO: Why not?
208 itemtype => $item_type,
209 non_priority => $non_priority,
216 openapi => 'Error placing the hold. See Koha logs for details.'
220 my $hold = Koha::Holds->find($hold_id);
224 openapi => $hold->to_api
228 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
229 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
230 my $broken_fk = $_->broken_fk;
232 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
235 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
241 $c->unhandled_exception($_);
247 Method that handles modifying a Koha::Hold object
252 my $c = shift->openapi->valid_input or return;
255 my $hold_id = $c->validation->param('hold_id');
256 my $hold = Koha::Holds->find( $hold_id );
259 return $c->render( status => 404,
260 openapi => {error => "Hold not found"} );
263 my $body = $c->req->json;
265 my $pickup_library_id = $body->{pickup_library_id};
268 defined $pickup_library_id
269 and not $hold->is_pickup_location_valid({ library_id => $pickup_library_id })
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 );
478 openapi => $pickup_locations
482 $c->unhandled_exception($_);