726f6f20deb772a637a51199d1279eefa5485470
[srvgit] / Koha / REST / V1 / Clubs / Holds.pm
1 package Koha::REST::V1::Clubs::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 C4::Biblio;
23 use C4::Reserves;
24
25 use Koha::Items;
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::Clubs;
29 use Koha::Club::Hold;
30 use Koha::DateUtils;
31
32 use Scalar::Util qw(blessed);
33 use Try::Tiny;
34 use List::Util 'shuffle';
35
36 =head1 API
37
38 =head2 Class methods
39
40 =head3 add
41
42 Method that handles adding a new Koha::Hold object
43
44 =cut
45
46 sub add {
47     my $c = shift->openapi->valid_input or return;
48
49     return try {
50         my $body    = $c->validation->param('body');
51         my $club_id = $c->validation->param('club_id');
52
53         my $biblio;
54
55         my $biblio_id         = $body->{biblio_id};
56         my $pickup_library_id = $body->{pickup_library_id};
57         my $item_id           = $body->{item_id};
58         my $item_type         = $body->{item_type};
59         my $expiration_date   = $body->{expiration_date};
60         my $notes             = $body->{notes};
61         my $default_patron_home = $body->{default_patron_home};
62
63         if ( $item_id and $biblio_id ) {
64
65             # check they are consistent
66             unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
67                 ->count > 0 )
68             {
69                 return $c->render(
70                     status  => 400,
71                     openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
72                 );
73             }
74             else {
75                 $biblio = Koha::Biblios->find($biblio_id);
76             }
77         }
78         elsif ($item_id) {
79             my $item = Koha::Items->find($item_id);
80
81             unless ($item) {
82                 return $c->render(
83                     status  => 404,
84                     openapi => { error => "Item not found" }
85                 );
86             }
87             else {
88                 $biblio = $item->biblio;
89             }
90         }
91         elsif ($biblio_id) {
92             $biblio = Koha::Biblios->find($biblio_id);
93         }
94         else {
95             return $c->render(
96                 status  => 400,
97                 openapi => { error => "At least one of biblio_id, item_id should be given" }
98             );
99         }
100
101         unless ($biblio) {
102             return $c->render(
103                 status  => 404,
104                 openapi => { error => "Biblio not found" }
105             );
106         }
107
108         # AddReserve expects date to be in syspref format
109         if ($expiration_date) {
110             $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
111         }
112
113         my $club_hold = Koha::Club::Hold::add(
114             {
115                 club_id             => $club_id,
116                 biblio_id           => $biblio->biblionumber,
117                 item_id             => $item_id,
118                 pickup_library_id   => $pickup_library_id,
119                 expiration_date     => $expiration_date,
120                 notes               => $notes,
121                 item_type           => $item_type,
122                 default_patron_home => $default_patron_home
123             }
124         );
125
126         return $c->render(
127             status  => 201,
128             openapi => $club_hold->to_api
129         );
130     }
131     catch {
132         if ( blessed $_ ) {
133             if ($_->isa('Koha::Exceptions::ClubHold::NoPatrons')) {
134                 return $c->render(
135                     status  => 409,
136                     openapi => { error => $_->description }
137                 );
138             }
139         }
140
141         $c->unhandled_exception($_);
142     };
143 }
144
145 1;