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} // $hold->branchcode;
235 my $priority = $body->{priority} // $hold->priority;
236 # suspended_until can also be set to undef
237 my $suspended_until = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
240 reserve_id => $hold_id,
241 branchcode => $pickup_library_id,
243 suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
244 itemnumber => $hold->itemnumber
247 C4::Reserves::ModReserve($params);
248 $hold->discard_changes; # refresh
252 openapi => $hold->to_api
256 $c->unhandled_exception($_);
262 Method that handles deleting a Koha::Hold object
267 my $c = shift->openapi->valid_input or return;
269 my $hold_id = $c->validation->param('hold_id');
270 my $hold = Koha::Holds->find($hold_id);
273 return $c->render( status => 404, openapi => { error => "Hold not found." } );
285 $c->unhandled_exception($_);
291 Method that handles suspending a hold
296 my $c = shift->openapi->valid_input or return;
298 my $hold_id = $c->validation->param('hold_id');
299 my $hold = Koha::Holds->find($hold_id);
300 my $body = $c->req->json;
301 my $end_date = ($body) ? $body->{end_date} : undef;
304 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
308 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
309 $hold->suspend_hold($date);
310 $hold->discard_changes;
311 $c->res->headers->location( $c->req->url->to_string );
312 my $suspend_end_date;
313 if ($hold->suspend_until) {
314 $suspend_end_date = output_pref({
315 dt => dt_from_string( $hold->suspend_until ),
316 dateformat => 'rfc3339',
324 end_date => $suspend_end_date
329 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
330 return $c->render( status => 400, openapi => { error => "$_" } );
333 $c->unhandled_exception($_);
339 Method that handles resuming a hold
344 my $c = shift->openapi->valid_input or return;
346 my $hold_id = $c->validation->param('hold_id');
347 my $hold = Koha::Holds->find($hold_id);
348 my $body = $c->req->json;
351 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
356 return $c->render( status => 204, openapi => {} );
359 $c->unhandled_exception($_);
363 =head3 update_priority
365 Method that handles modifying a Koha::Hold object
369 sub update_priority {
370 my $c = shift->openapi->valid_input or return;
372 my $hold_id = $c->validation->param('hold_id');
373 my $hold = Koha::Holds->find($hold_id);
378 openapi => { error => "Hold not found" }
383 my $priority = $c->req->json;
384 C4::Reserves::_FixPriority(
386 reserve_id => $hold_id,
391 return $c->render( status => 200, openapi => $priority );
394 $c->unhandled_exception($_);
398 =head3 pickup_locations
400 Method that returns the possible pickup_locations for a given hold
401 used for building the dropdown selector
405 sub pickup_locations {
406 my $c = shift->openapi->valid_input or return;
408 my $hold_id = $c->validation->param('hold_id');
409 my $hold = Koha::Holds->find($hold_id);
414 openapi => { error => "Hold not found" }
419 my $pickup_locations = $hold->itemnumber ?
420 $hold->item->pickup_locations({ patron => $hold->patron }) : $hold->biblio->pickup_locations({ patron => $hold->patron });
421 warn Data::Dumper::Dumper( $pickup_locations );
423 return $c->render( status => 200, openapi => $pickup_locations );
426 $c->unhandled_exception($_);