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};
247 my $priority = $body->{priority};
248 my $suspended_until = $body->{suspended_until};
250 if ($suspended_until) {
251 $suspended_until = output_pref(dt_from_string($suspended_until, 'rfc3339'));
255 reserve_id => $hold_id,
256 branchcode => $pickup_library_id,
258 suspend_until => $suspended_until,
259 itemnumber => $hold->itemnumber
262 C4::Reserves::ModReserve($params);
263 $hold->discard_changes; # refresh
267 openapi => $hold->to_api
273 Method that handles deleting a Koha::Hold object
278 my $c = shift->openapi->valid_input or return;
280 my $hold_id = $c->validation->param('hold_id');
281 my $hold = Koha::Holds->find($hold_id);
284 return $c->render( status => 404, openapi => { error => "Hold not found." } );
289 return $c->render( status => 200, openapi => {} );
294 Method that handles suspending a hold
299 my $c = shift->openapi->valid_input or return;
301 my $hold_id = $c->validation->param('hold_id');
302 my $hold = Koha::Holds->find($hold_id);
303 my $body = $c->req->json;
304 my $end_date = ($body) ? $body->{end_date} : undef;
307 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
311 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
312 $hold->suspend_hold($date);
313 $hold->discard_changes;
314 $c->res->headers->location( $c->req->url->to_string );
318 end_date => output_pref(
319 { dt => dt_from_string( $hold->suspend_until ),
320 dateformat => 'rfc3339',
328 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
329 return $c->render( status => 400, openapi => { error => "$_" } );
334 openapi => { error => "Something went wrong. check the logs." }
342 Method that handles resuming a hold
347 my $c = shift->openapi->valid_input or return;
349 my $hold_id = $c->validation->param('hold_id');
350 my $hold = Koha::Holds->find($hold_id);
351 my $body = $c->req->json;
354 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
359 return $c->render( status => 204, openapi => {} );
364 openapi => { error => "Something went wrong. check the logs." }
369 =head3 update_priority
371 Method that handles modifying a Koha::Hold object
375 sub update_priority {
376 my $c = shift->openapi->valid_input or return;
378 my $hold_id = $c->validation->param('hold_id');
379 my $hold = Koha::Holds->find($hold_id);
384 openapi => { error => "Hold not found" }
389 my $priority = $c->req->json;
390 C4::Reserves::_FixPriority(
392 reserve_id => $hold_id,
397 return $c->render( status => 200, openapi => $priority );
402 openapi => { error => "Something went wrong. check the logs." }