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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo::Base 'Mojolicious::Controller';
24 use Koha::Checkouts::ReturnClaims;
26 use Koha::DateUtils qw( dt_from_string output_pref );
30 Koha::REST::V1::ReturnClaims
36 Claim that a checked out item was returned.
41 my $c = shift->openapi->valid_input or return;
42 my $body = $c->validation->param('body');
45 my $itemnumber = $body->{item_id};
46 my $charge_lost_fee = $body->{charge_lost_fee} ? 1 : 0;
47 my $created_by = $body->{created_by};
48 my $notes = $body->{notes};
50 my $user = $c->stash('koha.user');
51 $created_by //= $user->borrowernumber;
53 my $checkout = Koha::Checkouts->find( { itemnumber => $itemnumber } );
56 openapi => { error => "Not found - Checkout not found" },
60 my $claim = $checkout->claim_returned(
62 charge_lost_fee => $charge_lost_fee,
63 created_by => $created_by,
68 $c->res->headers->location($c->req->url->to_string . '/' . $claim->id );
71 openapi => $claim->to_api
75 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
78 openapi => { error => "$_" }
81 elsif ( $_->isa('Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy') ) {
84 openapi => { error => "Mandatory attribute created_by missing" }
90 openapi => { error => "Something went wrong, check the logs." }
98 Update the notes of an existing claim
103 my $c = shift->openapi->valid_input or return;
104 my $input = $c->validation->output;
105 my $body = $c->validation->param('body');
108 my $id = $input->{claim_id};
109 my $updated_by = $body->{updated_by};
110 my $notes = $body->{notes};
113 C4::Context->userenv ? C4::Context->userenv->{number} : undef;
115 my $claim = Koha::Checkouts::ReturnClaims->find($id);
118 openapi => { error => "Not found - Claim not found" },
125 updated_by => $updated_by,
126 updated_on => dt_from_string(),
131 my $data = $claim->unblessed;
133 my $c_dt = dt_from_string( $data->{created_on} );
134 my $u_dt = dt_from_string( $data->{updated_on} );
136 $data->{created_on_formatted} = output_pref( { dt => $c_dt } );
137 $data->{updated_on_formatted} = output_pref( { dt => $u_dt } );
139 $data->{created_on} = $c_dt->iso8601;
140 $data->{updated_on} = $u_dt->iso8601;
142 return $c->render( openapi => $data, status => 200 );
145 if ( $_->isa('DBIx::Class::Exception') ) {
148 openapi => { error => $_->{msg} }
154 openapi => { error => "Something went wrong, check the logs." }
162 Marks a claim as resolved
167 my $c = shift->openapi->valid_input or return;
168 my $input = $c->validation->output;
169 my $body = $c->validation->param('body');
172 my $id = $input->{claim_id};
173 my $resolved_by = $body->{updated_by};
174 my $resolution = $body->{resolution};
177 C4::Context->userenv ? C4::Context->userenv->{number} : undef;
179 my $claim = Koha::Checkouts::ReturnClaims->find($id);
182 openapi => { error => "Not found - Claim not found" },
188 resolution => $resolution,
189 resolved_by => $resolved_by,
190 resolved_on => dt_from_string(),
195 return $c->render( openapi => $claim, status => 200 );
198 if ( $_->isa('DBIx::Class::Exception') ) {
201 openapi => { error => $_->{msg} }
207 openapi => { error => "Something went wrong, check the logs." }
215 Deletes the claim from the database
220 my $c = shift->openapi->valid_input or return;
221 my $input = $c->validation->output;
224 my $id = $input->{claim_id};
226 my $claim = Koha::Checkouts::ReturnClaims->find($id);
229 openapi => { error => "Not found - Claim not found" },
235 return $c->render( openapi => $claim, status => 200 );
238 if ( $_->isa('DBIx::Class::Exception') ) {
241 openapi => { error => $_->{msg} }
247 openapi => { error => "Something went wrong, check the logs." }