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';
38 Mehtod that handles listing Koha::Hold objects
43 my $c = shift->openapi->valid_input or return;
46 my $holds_set = Koha::Holds->new;
47 my $holds = $c->objects->search( $holds_set );
48 return $c->render( status => 200, openapi => $holds );
51 $c->unhandled_exception($_);
57 Method that handles adding a new Koha::Hold object
62 my $c = shift->openapi->valid_input or return;
65 my $body = $c->validation->param('body');
69 my $biblio_id = $body->{biblio_id};
70 my $pickup_library_id = $body->{pickup_library_id};
71 my $item_id = $body->{item_id};
72 my $patron_id = $body->{patron_id};
73 my $item_type = $body->{item_type};
74 my $expiration_date = $body->{expiration_date};
75 my $notes = $body->{notes};
76 my $hold_date = $body->{hold_date};
77 my $non_priority = $body->{non_priority};
79 if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
82 openapi => { error => "Hold date in future not allowed" }
86 if ( $item_id and $biblio_id ) {
88 # check they are consistent
89 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
94 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
98 $biblio = Koha::Biblios->find($biblio_id);
102 my $item = Koha::Items->find($item_id);
107 openapi => { error => "item_id not found." }
111 $biblio = $item->biblio;
115 $biblio = Koha::Biblios->find($biblio_id);
120 openapi => { error => "At least one of biblio_id, item_id should be given" }
127 openapi => "Biblio not found."
131 my $patron = Koha::Patrons->find( $patron_id );
135 openapi => { error => 'patron_id not found' }
141 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
142 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
144 if ( $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
145 $can_place_hold->{status} = 'tooManyReserves';
148 my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
150 unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
154 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
158 my $priority = C4::Reserves::CalculatePriority($biblio_id);
160 # AddReserve expects date to be in syspref format
161 if ($expiration_date) {
162 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
165 my $hold_id = C4::Reserves::AddReserve(
167 branchcode => $pickup_library_id,
168 borrowernumber => $patron_id,
169 biblionumber => $biblio_id,
170 priority => $priority,
171 reservation_date => $hold_date,
172 expiration_date => $expiration_date,
174 title => $biblio->title,
175 itemnumber => $item_id,
176 found => undef, # TODO: Why not?
177 itemtype => $item_type,
178 non_priority => $non_priority,
185 openapi => 'Error placing the hold. See Koha logs for details.'
189 my $hold = Koha::Holds->find($hold_id);
193 openapi => $hold->to_api
197 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
198 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
199 my $broken_fk = $_->broken_fk;
201 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
204 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
210 $c->unhandled_exception($_);
216 Method that handles modifying a Koha::Hold object
221 my $c = shift->openapi->valid_input or return;
224 my $hold_id = $c->validation->param('hold_id');
225 my $hold = Koha::Holds->find( $hold_id );
228 return $c->render( status => 404,
229 openapi => {error => "Hold not found"} );
232 my $body = $c->req->json;
234 my $pickup_library_id = $body->{pickup_library_id};
237 !defined $pickup_library_id
238 or $hold->is_pickup_location_valid(
239 { library_id => $pickup_library_id }
246 error => 'The supplied pickup location is not valid'
251 $pickup_library_id = $hold->branchcode
252 unless defined $pickup_library_id;
253 my $priority = $body->{priority} // $hold->priority;
254 # suspended_until can also be set to undef
255 my $suspended_until = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
258 reserve_id => $hold_id,
259 branchcode => $pickup_library_id,
261 suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
262 itemnumber => $hold->itemnumber
265 C4::Reserves::ModReserve($params);
266 $hold->discard_changes; # refresh
270 openapi => $hold->to_api
274 $c->unhandled_exception($_);
280 Method that handles deleting a Koha::Hold object
285 my $c = shift->openapi->valid_input or return;
287 my $hold_id = $c->validation->param('hold_id');
288 my $hold = Koha::Holds->find($hold_id);
291 return $c->render( status => 404, openapi => { error => "Hold not found." } );
303 $c->unhandled_exception($_);
309 Method that handles suspending a hold
314 my $c = shift->openapi->valid_input or return;
316 my $hold_id = $c->validation->param('hold_id');
317 my $hold = Koha::Holds->find($hold_id);
318 my $body = $c->req->json;
319 my $end_date = ($body) ? $body->{end_date} : undef;
322 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
326 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
327 $hold->suspend_hold($date);
328 $hold->discard_changes;
329 $c->res->headers->location( $c->req->url->to_string );
330 my $suspend_end_date;
331 if ($hold->suspend_until) {
332 $suspend_end_date = output_pref({
333 dt => dt_from_string( $hold->suspend_until ),
334 dateformat => 'rfc3339',
342 end_date => $suspend_end_date
347 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
348 return $c->render( status => 400, openapi => { error => "$_" } );
351 $c->unhandled_exception($_);
357 Method that handles resuming a hold
362 my $c = shift->openapi->valid_input or return;
364 my $hold_id = $c->validation->param('hold_id');
365 my $hold = Koha::Holds->find($hold_id);
366 my $body = $c->req->json;
369 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
374 return $c->render( status => 204, openapi => {} );
377 $c->unhandled_exception($_);
381 =head3 update_priority
383 Method that handles modifying a Koha::Hold object
387 sub update_priority {
388 my $c = shift->openapi->valid_input or return;
390 my $hold_id = $c->validation->param('hold_id');
391 my $hold = Koha::Holds->find($hold_id);
396 openapi => { error => "Hold not found" }
401 my $priority = $c->req->json;
402 C4::Reserves::_FixPriority(
404 reserve_id => $hold_id,
409 return $c->render( status => 200, openapi => $priority );
412 $c->unhandled_exception($_);
416 =head3 pickup_locations
418 Method that returns the possible pickup_locations for a given hold
419 used for building the dropdown selector
423 sub pickup_locations {
424 my $c = shift->openapi->valid_input or return;
426 my $hold_id = $c->validation->param('hold_id');
427 my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
432 openapi => { error => "Hold not found" }
437 my @pickup_locations =
439 ? @{ $hold->item->pickup_locations( { patron => $hold->patron } )->as_list() }
440 : @{ $hold->biblio->pickup_locations( { patron => $hold->patron } )->as_list() };
442 @pickup_locations = map { $_->to_api } @pickup_locations;
446 openapi => \@pickup_locations
450 $c->unhandled_exception($_);