08bf1a1fe3df61d39216a9f576a72046a155f229
[srvgit] / Koha / REST / V1 / Holds.pm
1 package Koha::REST::V1::Holds;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Mojo::JSON qw(decode_json);
23
24 use C4::Biblio;
25 use C4::Reserves;
26
27 use Koha::Items;
28 use Koha::Patrons;
29 use Koha::Holds;
30 use Koha::DateUtils;
31
32 use List::MoreUtils qw(any);
33 use Try::Tiny;
34
35 =head1 API
36
37 =head2 Methods
38
39 =head3 list
40
41 Method that handles listing Koha::Hold objects
42
43 =cut
44
45 sub list {
46     my $c = shift->openapi->valid_input or return;
47
48     return try {
49         my $holds_set = Koha::Holds->new;
50         my $holds     = $c->objects->search( $holds_set );
51         return $c->render( status => 200, openapi => $holds );
52     }
53     catch {
54         $c->unhandled_exception($_);
55     };
56 }
57
58 =head3 add
59
60 Method that handles adding a new Koha::Hold object
61
62 =cut
63
64 sub add {
65     my $c = shift->openapi->valid_input or return;
66
67     return try {
68         my $body = $c->validation->param('body');
69
70         my $biblio;
71         my $item;
72
73         my $biblio_id         = $body->{biblio_id};
74         my $pickup_library_id = $body->{pickup_library_id};
75         my $item_id           = $body->{item_id};
76         my $patron_id         = $body->{patron_id};
77         my $item_type         = $body->{item_type};
78         my $expiration_date   = $body->{expiration_date};
79         my $notes             = $body->{notes};
80         my $hold_date         = $body->{hold_date};
81         my $non_priority      = $body->{non_priority};
82
83         my $overrides = $c->stash('koha.overrides');
84         my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
85
86         if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
87             return $c->render(
88                 status  => 400,
89                 openapi => { error => "Hold date in future not allowed" }
90             );
91         }
92
93         if ( $item_id and $biblio_id ) {
94
95             # check they are consistent
96             unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
97                 ->count > 0 )
98             {
99                 return $c->render(
100                     status  => 400,
101                     openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
102                 );
103             }
104             else {
105                 $biblio = Koha::Biblios->find($biblio_id);
106             }
107         }
108         elsif ($item_id) {
109             $item = Koha::Items->find($item_id);
110
111             unless ($item) {
112                 return $c->render(
113                     status  => 404,
114                     openapi => { error => "item_id not found." }
115                 );
116             }
117             else {
118                 $biblio = $item->biblio;
119             }
120         }
121         elsif ($biblio_id) {
122             $biblio = Koha::Biblios->find($biblio_id);
123         }
124         else {
125             return $c->render(
126                 status  => 400,
127                 openapi => { error => "At least one of biblio_id, item_id should be given" }
128             );
129         }
130
131         unless ($biblio) {
132             return $c->render(
133                 status  => 400,
134                 openapi => "Biblio not found."
135             );
136         }
137
138         my $patron = Koha::Patrons->find( $patron_id );
139         unless ($patron) {
140             return $c->render(
141                 status  => 400,
142                 openapi => { error => 'patron_id not found' }
143             );
144         }
145
146         # Validate pickup location
147         my $valid_pickup_location;
148         if ($item) {    # item-level hold
149             $valid_pickup_location =
150               any { $_->branchcode eq $pickup_library_id }
151             $item->pickup_locations(
152                 { patron => $patron } );
153         }
154         else {
155             $valid_pickup_location =
156               any { $_->branchcode eq $pickup_library_id }
157             $biblio->pickup_locations(
158                 { patron => $patron } );
159         }
160
161         return $c->render(
162             status  => 400,
163             openapi => {
164                 error => 'The supplied pickup location is not valid'
165             }
166         ) unless $valid_pickup_location || $can_override;
167
168         my $can_place_hold
169             = $item_id
170             ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
171             : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
172
173         if ( $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
174             $can_place_hold->{status} = 'tooManyReserves';
175         }
176
177         unless ( $can_override || $can_place_hold->{status} eq 'OK' ) {
178             return $c->render(
179                 status => 403,
180                 openapi =>
181                     { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
182             );
183         }
184
185         my $priority = C4::Reserves::CalculatePriority($biblio_id);
186
187         # AddReserve expects date to be in syspref format
188         if ($expiration_date) {
189             $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
190         }
191
192         my $hold_id = C4::Reserves::AddReserve(
193             {
194                 branchcode       => $pickup_library_id,
195                 borrowernumber   => $patron_id,
196                 biblionumber     => $biblio_id,
197                 priority         => $priority,
198                 reservation_date => $hold_date,
199                 expiration_date  => $expiration_date,
200                 notes            => $notes,
201                 title            => $biblio->title,
202                 itemnumber       => $item_id,
203                 found            => undef,                # TODO: Why not?
204                 itemtype         => $item_type,
205                 non_priority     => $non_priority,
206             }
207         );
208
209         unless ($hold_id) {
210             return $c->render(
211                 status  => 500,
212                 openapi => 'Error placing the hold. See Koha logs for details.'
213             );
214         }
215
216         my $hold = Koha::Holds->find($hold_id);
217
218         return $c->render(
219             status  => 201,
220             openapi => $hold->to_api
221         );
222     }
223     catch {
224         if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
225             if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
226                 my $broken_fk = $_->broken_fk;
227
228                 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
229                     $c->render(
230                         status  => 404,
231                         openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
232                     );
233                 }
234             }
235         }
236
237         $c->unhandled_exception($_);
238     };
239 }
240
241 =head3 edit
242
243 Method that handles modifying a Koha::Hold object
244
245 =cut
246
247 sub edit {
248     my $c = shift->openapi->valid_input or return;
249
250     return try {
251         my $hold_id = $c->validation->param('hold_id');
252         my $hold = Koha::Holds->find( $hold_id );
253
254         unless ($hold) {
255             return $c->render(
256                 status  => 404,
257                 openapi => { error => "Hold not found" }
258             );
259         }
260
261         my $overrides = $c->stash('koha.overrides');
262         my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
263
264         my $body = $c->validation->output->{body};
265
266         my $pickup_library_id = $body->{pickup_library_id};
267
268         if (
269             defined $pickup_library_id
270             && ( !$hold->is_pickup_location_valid({ library_id => $pickup_library_id }) && !$can_override )
271           )
272         {
273             return $c->render(
274                 status  => 400,
275                 openapi => {
276                     error => 'The supplied pickup location is not valid'
277                 }
278             );
279         }
280
281         $pickup_library_id //= $hold->branchcode;
282         my $priority         = $body->{priority} // $hold->priority;
283         # suspended_until can also be set to undef
284         my $suspended_until   = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
285
286         my $params = {
287             reserve_id    => $hold_id,
288             branchcode    => $pickup_library_id,
289             rank          => $priority,
290             suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
291             itemnumber    => $hold->itemnumber
292         };
293
294         C4::Reserves::ModReserve($params);
295         $hold->discard_changes; # refresh
296
297         return $c->render(
298             status  => 200,
299             openapi => $hold->to_api
300         );
301     }
302     catch {
303         $c->unhandled_exception($_);
304     };
305 }
306
307 =head3 delete
308
309 Method that handles deleting a Koha::Hold object
310
311 =cut
312
313 sub delete {
314     my $c = shift->openapi->valid_input or return;
315
316     my $hold_id = $c->validation->param('hold_id');
317     my $hold    = Koha::Holds->find($hold_id);
318
319     unless ($hold) {
320         return $c->render( status => 404, openapi => { error => "Hold not found." } );
321     }
322
323     return try {
324         $hold->cancel;
325
326         return $c->render(
327             status  => 204,
328             openapi => q{}
329         );
330     }
331     catch {
332         $c->unhandled_exception($_);
333     };
334 }
335
336 =head3 suspend
337
338 Method that handles suspending a hold
339
340 =cut
341
342 sub suspend {
343     my $c = shift->openapi->valid_input or return;
344
345     my $hold_id  = $c->validation->param('hold_id');
346     my $hold     = Koha::Holds->find($hold_id);
347     my $body     = $c->req->json;
348     my $end_date = ($body) ? $body->{end_date} : undef;
349
350     unless ($hold) {
351         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
352     }
353
354     return try {
355         my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
356         $hold->suspend_hold($date);
357         $hold->discard_changes;
358         $c->res->headers->location( $c->req->url->to_string );
359         my $suspend_end_date;
360         if ($hold->suspend_until) {
361             $suspend_end_date = output_pref({
362                 dt         => dt_from_string( $hold->suspend_until ),
363                 dateformat => 'rfc3339',
364                 dateonly   => 1
365                 }
366             );
367         }
368         return $c->render(
369             status  => 201,
370             openapi => {
371                 end_date => $suspend_end_date
372             }
373         );
374     }
375     catch {
376         if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
377             return $c->render( status => 400, openapi => { error => "$_" } );
378         }
379
380         $c->unhandled_exception($_);
381     };
382 }
383
384 =head3 resume
385
386 Method that handles resuming a hold
387
388 =cut
389
390 sub resume {
391     my $c = shift->openapi->valid_input or return;
392
393     my $hold_id = $c->validation->param('hold_id');
394     my $hold    = Koha::Holds->find($hold_id);
395     my $body    = $c->req->json;
396
397     unless ($hold) {
398         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
399     }
400
401     return try {
402         $hold->resume;
403         return $c->render( status => 204, openapi => {} );
404     }
405     catch {
406         $c->unhandled_exception($_);
407     };
408 }
409
410 =head3 update_priority
411
412 Method that handles modifying a Koha::Hold object
413
414 =cut
415
416 sub update_priority {
417     my $c = shift->openapi->valid_input or return;
418
419     my $hold_id = $c->validation->param('hold_id');
420     my $hold = Koha::Holds->find($hold_id);
421
422     unless ($hold) {
423         return $c->render(
424             status  => 404,
425             openapi => { error => "Hold not found" }
426         );
427     }
428
429     return try {
430         my $priority = $c->req->json;
431         C4::Reserves::_FixPriority(
432             {
433                 reserve_id => $hold_id,
434                 rank       => $priority
435             }
436         );
437
438         return $c->render( status => 200, openapi => $priority );
439     }
440     catch {
441         $c->unhandled_exception($_);
442     };
443 }
444
445 =head3 pickup_locations
446
447 Method that returns the possible pickup_locations for a given hold
448 used for building the dropdown selector
449
450 =cut
451
452 sub pickup_locations {
453     my $c = shift->openapi->valid_input or return;
454
455     my $hold_id = $c->validation->param('hold_id');
456     my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
457
458     unless ($hold) {
459         return $c->render(
460             status  => 404,
461             openapi => { error => "Hold not found" }
462         );
463     }
464
465     return try {
466         my $ps_set;
467
468         if ( $hold->itemnumber ) {
469             $ps_set = $hold->item->pickup_locations( { patron => $hold->patron } );
470         }
471         else {
472             $ps_set = $hold->biblio->pickup_locations( { patron => $hold->patron } );
473         }
474
475         my $pickup_locations = $c->objects->search( $ps_set );
476         my @response = ();
477
478         if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
479
480             my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
481             my $libraries    = $c->objects->search($libraries_rs);
482
483             @response = map {
484                 my $library = $_;
485                 $library->{needs_override} = (
486                     any { $_->{library_id} eq $library->{library_id} }
487                     @{$pickup_locations}
488                   )
489                   ? Mojo::JSON->false
490                   : Mojo::JSON->true;
491                 $library;
492             } @{$libraries};
493
494             return $c->render(
495                 status  => 200,
496                 openapi => \@response
497             );
498         }
499
500         @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
501
502         return $c->render(
503             status  => 200,
504             openapi => \@response
505         );
506     }
507     catch {
508         $c->unhandled_exception($_);
509     };
510 }
511
512 =head3 update_pickup_location
513
514 Method that handles modifying the pickup location of a Koha::Hold object
515
516 =cut
517
518 sub update_pickup_location {
519     my $c = shift->openapi->valid_input or return;
520
521     my $hold_id = $c->validation->param('hold_id');
522     my $body    = $c->validation->param('body');
523     my $pickup_library_id = $body->{pickup_library_id};
524
525     my $hold = Koha::Holds->find($hold_id);
526
527     unless ($hold) {
528         return $c->render(
529             status  => 404,
530             openapi => { error => "Hold not found" }
531         );
532     }
533
534     return try {
535
536         my $overrides    = $c->stash('koha.overrides');
537         my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
538
539         $hold->set_pickup_location(
540             {
541                 library_id => $pickup_library_id,
542                 force      => $can_override
543             }
544         );
545
546         return $c->render(
547             status  => 200,
548             openapi => {
549                 pickup_library_id => $pickup_library_id
550             }
551         );
552     }
553     catch {
554
555         if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
556             return $c->render(
557                 status  => 400,
558                 openapi => {
559                     error => "$_"
560                 }
561             );
562         }
563
564         $c->unhandled_exception($_);
565     };
566 }
567
568
569 1;