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