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 if ( blessed $_ && $_->isa('Koha::Exceptions') ) {
54 openapi => { error => "$_" }
60 openapi => { error => "Something went wrong, check Koha logs for details." }
68 Method that handles adding a new Koha::Hold object
73 my $c = shift->openapi->valid_input or return;
76 my $body = $c->validation->param('body');
80 my $biblio_id = $body->{biblio_id};
81 my $pickup_library_id = $body->{pickup_library_id};
82 my $item_id = $body->{item_id};
83 my $patron_id = $body->{patron_id};
84 my $item_type = $body->{item_type};
85 my $expiration_date = $body->{expiration_date};
86 my $notes = $body->{notes};
87 my $hold_date = $body->{hold_date};
89 if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
92 openapi => { error => "Hold date in future not allowed" }
96 if ( $item_id and $biblio_id ) {
98 # check they are consistent
99 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
104 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
108 $biblio = Koha::Biblios->find($biblio_id);
112 my $item = Koha::Items->find($item_id);
117 openapi => { error => "item_id not found." }
121 $biblio = $item->biblio;
125 $biblio = Koha::Biblios->find($biblio_id);
130 openapi => { error => "At least one of biblio_id, item_id should be given" }
137 openapi => "Biblio not found."
143 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
144 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
146 my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
148 unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
152 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
156 my $priority = C4::Reserves::CalculatePriority($biblio_id);
158 # AddReserve expects date to be in syspref format
159 if ($expiration_date) {
160 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
163 my $hold_id = C4::Reserves::AddReserve(
165 branchcode => $pickup_library_id,
166 borrowernumber => $patron_id,
167 biblionumber => $biblio_id,
168 priority => $priority,
169 reservation_date => $hold_date,
170 expiration_date => $expiration_date,
172 title => $biblio->title,
173 itemnumber => $item_id,
174 found => undef, # TODO: Why not?
175 itemtype => $item_type,
182 openapi => 'Error placing the hold. See Koha logs for details.'
186 my $hold = Koha::Holds->find($hold_id);
190 openapi => $hold->to_api
194 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
195 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
196 my $broken_fk = $_->broken_fk;
198 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
201 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
207 openapi => { error => "Uncaught exception: $_" }
214 openapi => { error => "$_" }
221 openapi => { error => "Something went wrong. check the logs." }
229 Method that handles modifying a Koha::Hold object
234 my $c = shift->openapi->valid_input or return;
236 my $hold_id = $c->validation->param('hold_id');
237 my $hold = Koha::Holds->find( $hold_id );
240 return $c->render( status => 404,
241 openapi => {error => "Hold not found"} );
244 my $body = $c->req->json;
246 my $pickup_library_id = $body->{pickup_library_id} // $hold->branchcode;
247 my $priority = $body->{priority} // $hold->priority;
248 # suspended_until can also be set to undef
249 my $suspended_until = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
252 reserve_id => $hold_id,
253 branchcode => $pickup_library_id,
255 suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
256 itemnumber => $hold->itemnumber
259 C4::Reserves::ModReserve($params);
260 $hold->discard_changes; # refresh
264 openapi => $hold->to_api
270 Method that handles deleting a Koha::Hold object
275 my $c = shift->openapi->valid_input or return;
277 my $hold_id = $c->validation->param('hold_id');
278 my $hold = Koha::Holds->find($hold_id);
281 return $c->render( status => 404, openapi => { error => "Hold not found." } );
286 return $c->render( status => 200, openapi => {} );
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 => "$_" } );
335 openapi => { error => "Something went wrong. check the logs." }
343 Method that handles resuming a hold
348 my $c = shift->openapi->valid_input or return;
350 my $hold_id = $c->validation->param('hold_id');
351 my $hold = Koha::Holds->find($hold_id);
352 my $body = $c->req->json;
355 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
360 return $c->render( status => 204, openapi => {} );
365 openapi => { error => "Something went wrong. check the logs." }
370 =head3 update_priority
372 Method that handles modifying a Koha::Hold object
376 sub update_priority {
377 my $c = shift->openapi->valid_input or return;
379 my $hold_id = $c->validation->param('hold_id');
380 my $hold = Koha::Holds->find($hold_id);
385 openapi => { error => "Hold not found" }
390 my $priority = $c->req->json;
391 C4::Reserves::_FixPriority(
393 reserve_id => $hold_id,
398 return $c->render( status => 200, openapi => $priority );
403 openapi => { error => "Something went wrong. check the logs." }