Bug 25261: (QA follow-up) Restore previously returned list
[koha.git] / Koha / REST / V1 / ReturnClaims.pm
blobd33a326e55e48b3b03c6b310d3babaded6299af9
1 package Koha::REST::V1::ReturnClaims;
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use Try::Tiny;
24 use Koha::Checkouts::ReturnClaims;
25 use Koha::Checkouts;
26 use Koha::DateUtils qw( dt_from_string output_pref );
28 =head1 NAME
30 Koha::REST::V1::ReturnClaims
32 =head1 API
34 =head2 Methods
36 =head3 claim_returned
38 Claim that a checked out item was returned.
40 =cut
42 sub claim_returned {
43 my $c = shift->openapi->valid_input or return;
44 my $body = $c->validation->param('body');
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};
52 my $user = $c->stash('koha.user');
53 $created_by //= $user->borrowernumber;
55 my $checkout = Koha::Checkouts->find( { itemnumber => $itemnumber } );
57 return $c->render(
58 openapi => { error => "Checkout not found" },
59 status => 404
60 ) unless $checkout;
62 my $claim = $checkout->claim_returned(
64 charge_lost_fee => $charge_lost_fee,
65 created_by => $created_by,
66 notes => $notes,
70 $c->res->headers->location($c->req->url->to_string . '/' . $claim->id );
71 return $c->render(
72 status => 201,
73 openapi => $claim->to_api
76 catch {
77 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
78 return $c->render(
79 status => 409,
80 openapi => { error => "$_" }
83 elsif ( $_->isa('Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy') ) {
84 return $c->render(
85 status => 400,
86 openapi => { error => "Mandatory attribute created_by missing" }
90 $c->unhandled_exception($_);
94 =head3 update_notes
96 Update the notes of an existing claim
98 =cut
100 sub update_notes {
101 my $c = shift->openapi->valid_input or return;
103 my $claim_id = $c->validation->param('claim_id');
104 my $body = $c->validation->param('body');
106 my $claim = Koha::Checkouts::ReturnClaims->find( $claim_id );
108 return $c->render(
109 status => 404,
110 openapi => {
111 error => "Claim not found"
113 ) unless $claim;
115 return try {
116 my $updated_by = $body->{updated_by};
117 my $notes = $body->{notes};
119 my $user = $c->stash('koha.user');
120 $updated_by //= $user->borrowernumber;
122 $claim->set(
124 notes => $notes,
125 updated_by => $updated_by
127 )->store;
128 $claim->discard_changes;
130 return $c->render(
131 status => 200,
132 openapi => $claim->to_api
135 catch {
136 $c->unhandled_exception($_);
140 =head3 resolve_claim
142 Marks a claim as resolved
144 =cut
146 sub resolve_claim {
147 my $c = shift->openapi->valid_input or return;
149 my $claim_id = $c->validation->param('claim_id');
150 my $body = $c->validation->param('body');
152 my $claim = Koha::Checkouts::ReturnClaims->find($claim_id);
154 return $c->render(
155 status => 404,
156 openapi => {
157 error => "Claim not found"
159 ) unless $claim;
161 return try {
163 my $resolved_by = $body->{resolved_by};
164 my $resolution = $body->{resolution};
166 my $user = $c->stash('koha.user');
167 $resolved_by //= $user->borrowernumber;
169 $claim->set(
171 resolution => $resolution,
172 resolved_by => $resolved_by,
173 resolved_on => \'NOW()',
175 )->store;
176 $claim->discard_changes;
178 return $c->render(
179 status => 200,
180 openapi => $claim->to_api
183 catch {
184 $c->unhandled_exception($_);
188 =head3 delete_claim
190 Deletes the claim from the database
192 =cut
194 sub delete_claim {
195 my $c = shift->openapi->valid_input or return;
197 return try {
199 my $claim = Koha::Checkouts::ReturnClaims->find( $c->validation->param('claim_id') );
201 return $c->render(
202 status => 404,
203 openapi => { error => "Claim not found" }
204 ) unless $claim;
206 $claim->delete();
208 return $c->render(
209 status => 204,
210 openapi => {}
213 catch {
214 $c->unhandled_exception($_);