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};
78 if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
81 openapi => { error => "Hold date in future not allowed" }
85 if ( $item_id and $biblio_id ) {
87 # check they are consistent
88 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
93 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
97 $biblio = Koha::Biblios->find($biblio_id);
101 my $item = Koha::Items->find($item_id);
106 openapi => { error => "item_id not found." }
110 $biblio = $item->biblio;
114 $biblio = Koha::Biblios->find($biblio_id);
119 openapi => { error => "At least one of biblio_id, item_id should be given" }
126 openapi => "Biblio not found."
130 my $patron = Koha::Patrons->find( $patron_id );
134 openapi => { error => 'patron_id not found' }
140 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
141 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
143 if ( $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
144 $can_place_hold->{status} = 'tooManyReserves';
147 my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
149 unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
153 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
157 my $priority = C4::Reserves::CalculatePriority($biblio_id);
159 # AddReserve expects date to be in syspref format
160 if ($expiration_date) {
161 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
164 my $hold_id = C4::Reserves::AddReserve(
166 branchcode => $pickup_library_id,
167 borrowernumber => $patron_id,
168 biblionumber => $biblio_id,
169 priority => $priority,
170 reservation_date => $hold_date,
171 expiration_date => $expiration_date,
173 title => $biblio->title,
174 itemnumber => $item_id,
175 found => undef, # TODO: Why not?
176 itemtype => $item_type,
183 openapi => 'Error placing the hold. See Koha logs for details.'
187 my $hold = Koha::Holds->find($hold_id);
191 openapi => $hold->to_api
195 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
196 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
197 my $broken_fk = $_->broken_fk;
199 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
202 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
208 $c->unhandled_exception($_);
214 Method that handles modifying a Koha::Hold object
219 my $c = shift->openapi->valid_input or return;
222 my $hold_id = $c->validation->param('hold_id');
223 my $hold = Koha::Holds->find( $hold_id );
226 return $c->render( status => 404,
227 openapi => {error => "Hold not found"} );
230 my $body = $c->req->json;
232 my $pickup_library_id = $body->{pickup_library_id} // $hold->branchcode;
233 my $priority = $body->{priority} // $hold->priority;
234 # suspended_until can also be set to undef
235 my $suspended_until = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
238 reserve_id => $hold_id,
239 branchcode => $pickup_library_id,
241 suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
242 itemnumber => $hold->itemnumber
245 C4::Reserves::ModReserve($params);
246 $hold->discard_changes; # refresh
250 openapi => $hold->to_api
254 $c->unhandled_exception($_);
260 Method that handles deleting a Koha::Hold object
265 my $c = shift->openapi->valid_input or return;
267 my $hold_id = $c->validation->param('hold_id');
268 my $hold = Koha::Holds->find($hold_id);
271 return $c->render( status => 404, openapi => { error => "Hold not found." } );
283 $c->unhandled_exception($_);
289 Method that handles suspending a hold
294 my $c = shift->openapi->valid_input or return;
296 my $hold_id = $c->validation->param('hold_id');
297 my $hold = Koha::Holds->find($hold_id);
298 my $body = $c->req->json;
299 my $end_date = ($body) ? $body->{end_date} : undef;
302 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
306 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
307 $hold->suspend_hold($date);
308 $hold->discard_changes;
309 $c->res->headers->location( $c->req->url->to_string );
310 my $suspend_end_date;
311 if ($hold->suspend_until) {
312 $suspend_end_date = output_pref({
313 dt => dt_from_string( $hold->suspend_until ),
314 dateformat => 'rfc3339',
322 end_date => $suspend_end_date
327 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
328 return $c->render( status => 400, openapi => { error => "$_" } );
331 $c->unhandled_exception($_);
337 Method that handles resuming 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;
349 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
354 return $c->render( status => 204, openapi => {} );
357 $c->unhandled_exception($_);
361 =head3 update_priority
363 Method that handles modifying a Koha::Hold object
367 sub update_priority {
368 my $c = shift->openapi->valid_input or return;
370 my $hold_id = $c->validation->param('hold_id');
371 my $hold = Koha::Holds->find($hold_id);
376 openapi => { error => "Hold not found" }
381 my $priority = $c->req->json;
382 C4::Reserves::_FixPriority(
384 reserve_id => $hold_id,
389 return $c->render( status => 200, openapi => $priority );
392 $c->unhandled_exception($_);