c9fa83338795c49b9322df13a67615da17bf558e
[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;
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
90         $c->unhandled_exception($_);
91     };
92 }
93
94 =head3 update_notes
95
96 Update the notes of an existing claim
97
98 =cut
99
100 sub update_notes {
101     my $c = shift->openapi->valid_input or return;
102
103     my $claim_id = $c->validation->param('claim_id');
104     my $body     = $c->validation->param('body');
105
106     my $claim = Koha::Checkouts::ReturnClaims->find( $claim_id );
107
108     return $c->render(
109         status  => 404,
110         openapi => {
111             error => "Claim not found"
112         }
113     ) unless $claim;
114
115     return try {
116         my $updated_by = $body->{updated_by};
117         my $notes      = $body->{notes};
118
119         my $user = $c->stash('koha.user');
120         $updated_by //= $user->borrowernumber;
121
122         $claim->set(
123             {
124                 notes      => $notes,
125                 updated_by => $updated_by
126             }
127         )->store;
128         $claim->discard_changes;
129
130         return $c->render(
131             status  => 200,
132             openapi => $claim->to_api
133         );
134     }
135     catch {
136         $c->unhandled_exception($_);
137     };
138 }
139
140 =head3 resolve_claim
141
142 Marks a claim as resolved
143
144 =cut
145
146 sub resolve_claim {
147     my $c     = shift->openapi->valid_input or return;
148
149     my $claim_id = $c->validation->param('claim_id');
150     my $body     = $c->validation->param('body');
151
152     my $claim = Koha::Checkouts::ReturnClaims->find($claim_id);
153
154     return $c->render(
155         status  => 404,
156         openapi => { error => "Claim not found" }
157     ) unless $claim;
158
159     return try {
160
161         my $resolved_by     = $body->{resolved_by};
162         my $resolution      = $body->{resolution};
163         my $new_lost_status = $body->{new_lost_status};
164
165         my $user = $c->stash('koha.user');
166         $resolved_by //= $user->borrowernumber;
167
168         $claim->resolve(
169             {
170                 resolution      => $resolution,
171                 resolved_by     => $resolved_by,
172                 new_lost_status => $new_lost_status,
173             }
174         )->discard_changes;
175
176         return $c->render(
177             status  => 200,
178             openapi => $claim->to_api
179         );
180     }
181     catch {
182         $c->unhandled_exception($_);
183     };
184 }
185
186 =head3 delete_claim
187
188 Deletes the claim from the database
189
190 =cut
191
192 sub delete_claim {
193     my $c = shift->openapi->valid_input or return;
194
195     return try {
196
197         my $claim = Koha::Checkouts::ReturnClaims->find( $c->validation->param('claim_id') );
198
199         return $c->render(
200             status  => 404,
201             openapi => { error => "Claim not found" }
202         ) unless $claim;
203
204         $claim->delete();
205
206         return $c->render(
207             status  => 204,
208             openapi => {}
209         );
210     }
211     catch {
212         $c->unhandled_exception($_);
213     };
214 }
215
216 1;