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."
132 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
133 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
135 my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
137 unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
141 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
145 my $priority = C4::Reserves::CalculatePriority($biblio_id);
147 # AddReserve expects date to be in syspref format
148 if ($expiration_date) {
149 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
152 my $hold_id = C4::Reserves::AddReserve(
154 branchcode => $pickup_library_id,
155 borrowernumber => $patron_id,
156 biblionumber => $biblio_id,
157 priority => $priority,
158 reservation_date => $hold_date,
159 expiration_date => $expiration_date,
161 title => $biblio->title,
162 itemnumber => $item_id,
163 found => undef, # TODO: Why not?
164 itemtype => $item_type,
171 openapi => 'Error placing the hold. See Koha logs for details.'
175 my $hold = Koha::Holds->find($hold_id);
179 openapi => $hold->to_api
183 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
184 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
185 my $broken_fk = $_->broken_fk;
187 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
190 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
196 $c->unhandled_exception($_);
202 Method that handles modifying a Koha::Hold object
207 my $c = shift->openapi->valid_input or return;
210 my $hold_id = $c->validation->param('hold_id');
211 my $hold = Koha::Holds->find( $hold_id );
214 return $c->render( status => 404,
215 openapi => {error => "Hold not found"} );
218 my $body = $c->req->json;
220 my $pickup_library_id = $body->{pickup_library_id} // $hold->branchcode;
221 my $priority = $body->{priority} // $hold->priority;
222 # suspended_until can also be set to undef
223 my $suspended_until = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
226 reserve_id => $hold_id,
227 branchcode => $pickup_library_id,
229 suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
230 itemnumber => $hold->itemnumber
233 C4::Reserves::ModReserve($params);
234 $hold->discard_changes; # refresh
238 openapi => $hold->to_api
242 $c->unhandled_exception($_);
248 Method that handles deleting a Koha::Hold object
253 my $c = shift->openapi->valid_input or return;
255 my $hold_id = $c->validation->param('hold_id');
256 my $hold = Koha::Holds->find($hold_id);
259 return $c->render( status => 404, openapi => { error => "Hold not found." } );
265 return $c->render( status => 200, openapi => {} );
268 $c->unhandled_exception($_);
274 Method that handles suspending a hold
279 my $c = shift->openapi->valid_input or return;
281 my $hold_id = $c->validation->param('hold_id');
282 my $hold = Koha::Holds->find($hold_id);
283 my $body = $c->req->json;
284 my $end_date = ($body) ? $body->{end_date} : undef;
287 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
291 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
292 $hold->suspend_hold($date);
293 $hold->discard_changes;
294 $c->res->headers->location( $c->req->url->to_string );
295 my $suspend_end_date;
296 if ($hold->suspend_until) {
297 $suspend_end_date = output_pref({
298 dt => dt_from_string( $hold->suspend_until ),
299 dateformat => 'rfc3339',
307 end_date => $suspend_end_date
312 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
313 return $c->render( status => 400, openapi => { error => "$_" } );
316 $c->unhandled_exception($_);
322 Method that handles resuming a hold
327 my $c = shift->openapi->valid_input or return;
329 my $hold_id = $c->validation->param('hold_id');
330 my $hold = Koha::Holds->find($hold_id);
331 my $body = $c->req->json;
334 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
339 return $c->render( status => 204, openapi => {} );
342 $c->unhandled_exception($_);
346 =head3 update_priority
348 Method that handles modifying a Koha::Hold object
352 sub update_priority {
353 my $c = shift->openapi->valid_input or return;
355 my $hold_id = $c->validation->param('hold_id');
356 my $hold = Koha::Holds->find($hold_id);
361 openapi => { error => "Hold not found" }
366 my $priority = $c->req->json;
367 C4::Reserves::_FixPriority(
369 reserve_id => $hold_id,
374 return $c->render( status => 200, openapi => $priority );
377 $c->unhandled_exception($_);