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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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(
167 undef, # $bibitems param is unused
174 undef, # TODO: Why not?
181 openapi => 'Error placing the hold. See Koha logs for details.'
185 my $hold = Koha::Holds->find($hold_id);
189 openapi => $hold->to_api
193 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
194 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
195 my $broken_fk = $_->broken_fk;
197 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
200 openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
206 openapi => { error => "Uncaught exception: $_" }
213 openapi => { error => "$_" }
220 openapi => { error => "Something went wrong. check the logs." }
228 Method that handles modifying a Koha::Hold object
233 my $c = shift->openapi->valid_input or return;
235 my $hold_id = $c->validation->param('hold_id');
236 my $hold = Koha::Holds->find( $hold_id );
239 return $c->render( status => 404,
240 openapi => {error => "Hold not found"} );
243 my $body = $c->req->json;
245 my $pickup_library_id = $body->{pickup_library_id};
246 my $priority = $body->{priority};
247 my $suspended_until = $body->{suspended_until};
249 if ($suspended_until) {
250 $suspended_until = output_pref(dt_from_string($suspended_until, 'rfc3339'));
254 reserve_id => $hold_id,
255 branchcode => $pickup_library_id,
257 suspend_until => $suspended_until,
258 itemnumber => $hold->itemnumber
261 C4::Reserves::ModReserve($params);
262 $hold->discard_changes; # refresh
266 openapi => $hold->to_api
272 Method that handles deleting a Koha::Hold object
277 my $c = shift->openapi->valid_input or return;
279 my $hold_id = $c->validation->param('hold_id');
280 my $hold = Koha::Holds->find($hold_id);
283 return $c->render( status => 404, openapi => { error => "Hold not found." } );
288 return $c->render( status => 200, openapi => {} );
293 Method that handles suspending a hold
298 my $c = shift->openapi->valid_input or return;
300 my $hold_id = $c->validation->param('hold_id');
301 my $hold = Koha::Holds->find($hold_id);
302 my $body = $c->req->json;
303 my $end_date = ($body) ? $body->{end_date} : undef;
306 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
310 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
311 $hold->suspend_hold($date);
312 $hold->discard_changes;
313 $c->res->headers->location( $c->req->url->to_string );
317 end_date => output_pref(
318 { dt => dt_from_string( $hold->suspend_until ),
319 dateformat => 'rfc3339',
327 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
328 return $c->render( status => 400, openapi => { error => "$_" } );
333 openapi => { error => "Something went wrong. check the logs." }
341 Method that handles resuming a hold
346 my $c = shift->openapi->valid_input or return;
348 my $hold_id = $c->validation->param('hold_id');
349 my $hold = Koha::Holds->find($hold_id);
350 my $body = $c->req->json;
353 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
358 return $c->render( status => 204, openapi => {} );
363 openapi => { error => "Something went wrong. check the logs." }
368 =head3 update_priority
370 Method that handles modifying a Koha::Hold object
374 sub update_priority {
375 my $c = shift->openapi->valid_input or return;
377 my $hold_id = $c->validation->param('hold_id');
378 my $hold = Koha::Holds->find($hold_id);
383 openapi => { error => "Hold not found" }
388 my $priority = $c->req->json;
389 C4::Reserves::_FixPriority(
391 reserve_id => $hold_id,
396 return $c->render( status => 200, openapi => $priority );
401 openapi => { error => "Something went wrong. check the logs." }