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