Bug 28271: Add the ability to set a new lost status when a claim is resolved
[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
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 => {
157                 error => "Claim not found"
158             }
159     ) unless $claim;
160
161     return try {
162
163         my $resolved_by     = $body->{resolved_by};
164         my $resolution      = $body->{resolution};
165         my $new_lost_status = $body->{new_lost_status};
166
167         my $user = $c->stash('koha.user');
168         $resolved_by //= $user->borrowernumber;
169
170         $claim->set(
171             {
172                 resolution  => $resolution,
173                 resolved_by => $resolved_by,
174                 resolved_on => \'NOW()',
175                 updated_by  => $resolved_by,
176             }
177         )->store;
178
179         if ( defined $new_lost_status ) {
180             $claim->checkout->item->itemlost($new_lost_status)->store;
181         }
182         $claim->discard_changes;
183
184         return $c->render(
185             status  => 200,
186             openapi => $claim->to_api
187         );
188     }
189     catch {
190         $c->unhandled_exception($_);
191     };
192 }
193
194 =head3 delete_claim
195
196 Deletes the claim from the database
197
198 =cut
199
200 sub delete_claim {
201     my $c = shift->openapi->valid_input or return;
202
203     return try {
204
205         my $claim = Koha::Checkouts::ReturnClaims->find( $c->validation->param('claim_id') );
206
207         return $c->render(
208             status  => 404,
209             openapi => { error => "Claim not found" }
210         ) unless $claim;
211
212         $claim->delete();
213
214         return $c->render(
215             status  => 204,
216             openapi => {}
217         );
218     }
219     catch {
220         $c->unhandled_exception($_);
221     };
222 }
223
224 1;