Bug 18936: (follow-up) Fix checkouts endpoint and its tests
[koha.git] / Koha / REST / V1 / Checkouts.pm
blobc3ae141e672f3bf917b16c4f6b7441e59d3b3d9f
1 package Koha::REST::V1::Checkouts;
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
8 # version.
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.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
21 use Mojo::JSON;
23 use C4::Auth qw( haspermission );
24 use C4::Context;
25 use C4::Circulation;
26 use Koha::Checkouts;
27 use Koha::Old::Checkouts;
29 use Try::Tiny;
31 =head1 NAME
33 Koha::REST::V1::Checkout
35 =head1 API
37 =head2 Methods
39 =head3 list
41 List Koha::Checkout objects
43 =cut
45 sub list {
46 my $c = shift->openapi->valid_input or return;
48 my $checked_in = $c->validation->param('checked_in');
50 try {
51 my $checkouts_set;
53 if ( $checked_in ) {
54 $checkouts_set = Koha::Old::Checkouts->new;
55 } else {
56 $checkouts_set = Koha::Checkouts->new;
59 my $args = $c->validation->output;
60 my $attributes = {};
62 # Extract reserved params
63 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
65 # Merge sorting into query attributes
66 $c->dbic_merge_sorting(
68 attributes => $attributes,
69 params => $reserved_params,
70 result_set => $checkouts_set
74 # Merge pagination into query attributes
75 $c->dbic_merge_pagination(
77 filter => $attributes,
78 params => $reserved_params
82 # Call the to_model function by reference, if defined
83 if ( defined $filtered_params ) {
84 # remove checked_in
85 delete $filtered_params->{checked_in};
86 # Apply the mapping function to the passed params
87 $filtered_params = $checkouts_set->attributes_from_api($filtered_params);
88 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
91 # Perform search
92 my $checkouts = $checkouts_set->search( $filtered_params, $attributes );
94 if ($checkouts->is_paged) {
95 $c->add_pagination_headers({
96 total => $checkouts->pager->total_entries,
97 params => $args,
98 });
101 return $c->render( status => 200, openapi => $checkouts->to_api );
102 } catch {
103 if ( $_->isa('DBIx::Class::Exception') ) {
104 return $c->render(
105 status => 500,
106 openapi => { error => $_->{msg} }
108 } else {
109 return $c->render(
110 status => 500,
111 openapi => { error => "Something went wrong, check the logs." }
117 =head3 get
119 get one checkout
121 =cut
123 sub get {
124 my $c = shift->openapi->valid_input or return;
126 my $checkout_id = $c->validation->param('checkout_id');
127 my $checkout = Koha::Checkouts->find( $checkout_id );
128 $checkout = Koha::Old::Checkouts->find( $checkout_id )
129 unless ($checkout);
131 unless ($checkout) {
132 return $c->render(
133 status => 404,
134 openapi => { error => "Checkout doesn't exist" }
138 return $c->render(
139 status => 200,
140 openapi => $checkout->to_api
144 =head3 renew
146 Renew a checkout
148 =cut
150 sub renew {
151 my $c = shift->openapi->valid_input or return;
153 my $checkout_id = $c->validation->param('checkout_id');
154 my $checkout = Koha::Checkouts->find( $checkout_id );
156 unless ($checkout) {
157 return $c->render(
158 status => 404,
159 openapi => { error => "Checkout doesn't exist" }
163 my $borrowernumber = $checkout->borrowernumber;
164 my $itemnumber = $checkout->itemnumber;
166 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
167 $borrowernumber, $itemnumber);
169 if (!$can_renew) {
170 return $c->render(
171 status => 403,
172 openapi => { error => "Renewal not authorized ($error)" }
176 AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
177 $checkout = Koha::Checkouts->find($checkout_id);
179 $c->res->headers->location( $c->req->url->to_string );
180 return $c->render(
181 status => 201,
182 openapi => $checkout->to_api
186 =head3 allows_renewal
188 Checks if the checkout could be renewed and return the related information.
190 =cut
192 sub allows_renewal {
193 my $c = shift->openapi->valid_input or return;
195 my $checkout_id = $c->validation->param('checkout_id');
196 my $checkout = Koha::Checkouts->find( $checkout_id );
198 unless ($checkout) {
199 return $c->render(
200 status => 404,
201 openapi => { error => "Checkout doesn't exist" }
205 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
206 $checkout->borrowernumber, $checkout->itemnumber);
208 my $renewable = Mojo::JSON->false;
209 $renewable = Mojo::JSON->true if $can_renew;
211 my $rule = Koha::CirculationRules->get_effective_rule(
213 categorycode => $checkout->patron->categorycode,
214 itemtype => $checkout->item->effective_itemtype,
215 branchcode => $checkout->branchcode,
216 rule_name => 'renewalsallowed',
219 return $c->render(
220 status => 200,
221 openapi => {
222 allows_renewal => $renewable,
223 max_renewals => $rule->rule_value,
224 current_renewals => $checkout->renewals,
225 error => $error