Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / REST / V1 / ReturnClaims.pm
1 package Koha::REST::V1::ReturnClaims;
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 Try::Tiny qw( catch try );
23
24 use Koha::Checkouts::ReturnClaims;
25 use Koha::Checkouts;
26
27 =head1 NAME
28
29 Koha::REST::V1::ReturnClaims
30
31 =head1 API
32
33 =head2 Methods
34
35 =head3 claim_returned
36
37 Claim that a checked out item was returned.
38
39 =cut
40
41 sub claim_returned {
42     my $c    = shift->openapi->valid_input or return;
43     my $body = $c->validation->param('body');
44
45     return try {
46         my $itemnumber      = $body->{item_id};
47         my $charge_lost_fee = $body->{charge_lost_fee} ? 1 : 0;
48         my $created_by      = $body->{created_by};
49         my $notes           = $body->{notes};
50
51         my $user = $c->stash('koha.user');
52         $created_by //= $user->borrowernumber;
53
54         my $checkout = Koha::Checkouts->find( { itemnumber => $itemnumber } );
55
56         return $c->render(
57             openapi => { error => "Checkout not found" },
58             status  => 404
59         ) unless $checkout;
60
61         my $claim = $checkout->claim_returned(
62             {
63                 charge_lost_fee => $charge_lost_fee,
64                 created_by      => $created_by,
65                 notes           => $notes,
66             }
67         );
68
69         $c->res->headers->location($c->req->url->to_string . '/' . $claim->id );
70         return $c->render(
71             status  => 201,
72             openapi => $claim->to_api
73         );
74     }
75     catch {
76         if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
77             return $c->render(
78                 status  => 409,
79                 openapi => { error => "$_" }
80             );
81         }
82         elsif ( $_->isa('Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy') ) {
83             return $c->render(
84                 status  => 400,
85                 openapi => { error => "Mandatory attribute created_by missing" }
86             );
87         }
88
89         $c->unhandled_exception($_);
90     };
91 }
92
93 =head3 update_notes
94
95 Update the notes of an existing claim
96
97 =cut
98
99 sub update_notes {
100     my $c = shift->openapi->valid_input or return;
101
102     my $claim_id = $c->validation->param('claim_id');
103     my $body     = $c->validation->param('body');
104
105     my $claim = Koha::Checkouts::ReturnClaims->find( $claim_id );
106
107     return $c->render(
108         status  => 404,
109         openapi => {
110             error => "Claim not found"
111         }
112     ) unless $claim;
113
114     return try {
115         my $updated_by = $body->{updated_by};
116         my $notes      = $body->{notes};
117
118         my $user = $c->stash('koha.user');
119         $updated_by //= $user->borrowernumber;
120
121         $claim->set(
122             {
123                 notes      => $notes,
124                 updated_by => $updated_by
125             }
126         )->store;
127         $claim->discard_changes;
128
129         return $c->render(
130             status  => 200,
131             openapi => $claim->to_api
132         );
133     }
134     catch {
135         $c->unhandled_exception($_);
136     };
137 }
138
139 =head3 resolve_claim
140
141 Marks a claim as resolved
142
143 =cut
144
145 sub resolve_claim {
146     my $c     = shift->openapi->valid_input or return;
147
148     my $claim_id = $c->validation->param('claim_id');
149     my $body     = $c->validation->param('body');
150
151     my $claim = Koha::Checkouts::ReturnClaims->find($claim_id);
152
153     return $c->render(
154         status  => 404,
155         openapi => { error => "Claim not found" }
156     ) unless $claim;
157
158     return try {
159
160         my $resolved_by     = $body->{resolved_by};
161         my $resolution      = $body->{resolution};
162         my $new_lost_status = $body->{new_lost_status};
163
164         my $user = $c->stash('koha.user');
165         $resolved_by //= $user->borrowernumber;
166
167         $claim->resolve(
168             {
169                 resolution      => $resolution,
170                 resolved_by     => $resolved_by,
171                 new_lost_status => $new_lost_status,
172             }
173         )->discard_changes;
174
175         return $c->render(
176             status  => 200,
177             openapi => $claim->to_api
178         );
179     }
180     catch {
181         $c->unhandled_exception($_);
182     };
183 }
184
185 =head3 delete_claim
186
187 Deletes the claim from the database
188
189 =cut
190
191 sub delete_claim {
192     my $c = shift->openapi->valid_input or return;
193
194     return try {
195
196         my $claim = Koha::Checkouts::ReturnClaims->find( $c->validation->param('claim_id') );
197
198         return $c->render(
199             status  => 404,
200             openapi => { error => "Claim not found" }
201         ) unless $claim;
202
203         $claim->delete();
204
205         return $c->render(
206             status  => 204,
207             openapi => {}
208         );
209     }
210     catch {
211         $c->unhandled_exception($_);
212     };
213 }
214
215 1;