Bug 26988: Add API route to fetch hold pickup locations and use it in the holds table
[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
166         my $user = $c->stash('koha.user');
167         $resolved_by //= $user->borrowernumber;
168
169         $claim->set(
170             {
171                 resolution  => $resolution,
172                 resolved_by => $resolved_by,
173                 resolved_on => \'NOW()',
174             }
175         )->store;
176         $claim->discard_changes;
177
178         return $c->render(
179             status  => 200,
180             openapi => $claim->to_api
181         );
182     }
183     catch {
184         $c->unhandled_exception($_);
185     };
186 }
187
188 =head3 delete_claim
189
190 Deletes the claim from the database
191
192 =cut
193
194 sub delete_claim {
195     my $c = shift->openapi->valid_input or return;
196
197     return try {
198
199         my $claim = Koha::Checkouts::ReturnClaims->find( $c->validation->param('claim_id') );
200
201         return $c->render(
202             status  => 404,
203             openapi => { error => "Claim not found" }
204         ) unless $claim;
205
206         $claim->delete();
207
208         return $c->render(
209             status  => 204,
210             openapi => {}
211         );
212     }
213     catch {
214         $c->unhandled_exception($_);
215     };
216 }
217
218 1;