Bug 25482: Explicitly set macros as not shard during tests
[koha.git] / Koha / REST / V1 / Checkouts.pm
1 package Koha::REST::V1::Checkouts;
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 use Mojo::JSON;
22
23 use C4::Auth qw( haspermission );
24 use C4::Context;
25 use C4::Circulation;
26 use Koha::Checkouts;
27 use Koha::Old::Checkouts;
28
29 use Try::Tiny;
30
31 =head1 NAME
32
33 Koha::REST::V1::Checkout
34
35 =head1 API
36
37 =head2 Methods
38
39 =head3 list
40
41 List Koha::Checkout objects
42
43 =cut
44
45 sub list {
46     my $c = shift->openapi->valid_input or return;
47
48     my $checked_in = $c->validation->param('checked_in');
49
50     try {
51         my $checkouts_set;
52
53         if ( $checked_in ) {
54             $checkouts_set = Koha::Old::Checkouts->new;
55         } else {
56             $checkouts_set = Koha::Checkouts->new;
57         }
58
59         my $args = $c->validation->output;
60         my $attributes = {};
61
62         # Extract reserved params
63         my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
64
65         # Merge sorting into query attributes
66         $c->dbic_merge_sorting(
67             {
68                 attributes => $attributes,
69                 params     => $reserved_params,
70                 result_set => $checkouts_set
71             }
72         );
73
74         # Merge pagination into query attributes
75         $c->dbic_merge_pagination(
76             {
77                 filter => $attributes,
78                 params => $reserved_params
79             }
80         );
81
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 );
89         }
90
91         # Perform search
92         my $checkouts = $checkouts_set->search( $filtered_params, $attributes );
93
94         if ($checkouts->is_paged) {
95             $c->add_pagination_headers({
96                 total => $checkouts->pager->total_entries,
97                 params => $args,
98             });
99         }
100
101         return $c->render( status => 200, openapi => $checkouts->to_api );
102     } catch {
103         $c->unhandled_exception($_);
104     };
105 }
106
107 =head3 get
108
109 get one checkout
110
111 =cut
112
113 sub get {
114     my $c = shift->openapi->valid_input or return;
115
116     my $checkout_id = $c->validation->param('checkout_id');
117     my $checkout = Koha::Checkouts->find( $checkout_id );
118     $checkout = Koha::Old::Checkouts->find( $checkout_id )
119         unless ($checkout);
120
121     unless ($checkout) {
122         return $c->render(
123             status => 404,
124             openapi => { error => "Checkout doesn't exist" }
125         );
126     }
127
128     return try {
129         return $c->render(
130             status  => 200,
131             openapi => $checkout->to_api
132         );
133     }
134     catch {
135         $c->unhandled_exception($_);
136     };
137 }
138
139 =head3 renew
140
141 Renew a checkout
142
143 =cut
144
145 sub renew {
146     my $c = shift->openapi->valid_input or return;
147
148     my $checkout_id = $c->validation->param('checkout_id');
149     my $checkout = Koha::Checkouts->find( $checkout_id );
150
151     unless ($checkout) {
152         return $c->render(
153             status => 404,
154             openapi => { error => "Checkout doesn't exist" }
155         );
156     }
157
158     return try {
159         my $borrowernumber = $checkout->borrowernumber;
160         my $itemnumber = $checkout->itemnumber;
161
162         my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
163             $borrowernumber, $itemnumber);
164
165         if (!$can_renew) {
166             return $c->render(
167                 status => 403,
168                 openapi => { error => "Renewal not authorized ($error)" }
169             );
170         }
171
172         AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
173         $checkout = Koha::Checkouts->find($checkout_id);
174
175         $c->res->headers->location( $c->req->url->to_string );
176         return $c->render(
177             status  => 201,
178             openapi => $checkout->to_api
179         );
180     }
181     catch {
182         $c->unhandled_exception($_);
183     };
184 }
185
186 =head3 allows_renewal
187
188 Checks if the checkout could be renewed and return the related information.
189
190 =cut
191
192 sub allows_renewal {
193     my $c = shift->openapi->valid_input or return;
194
195     my $checkout_id = $c->validation->param('checkout_id');
196     my $checkout = Koha::Checkouts->find( $checkout_id );
197
198     unless ($checkout) {
199         return $c->render(
200             status => 404,
201             openapi => { error => "Checkout doesn't exist" }
202         );
203     }
204
205     return try {
206         my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
207             $checkout->borrowernumber, $checkout->itemnumber);
208
209         my $renewable = Mojo::JSON->false;
210         $renewable = Mojo::JSON->true if $can_renew;
211
212         my $rule = Koha::CirculationRules->get_effective_rule(
213             {
214                 categorycode => $checkout->patron->categorycode,
215                 itemtype     => $checkout->item->effective_itemtype,
216                 branchcode   => $checkout->branchcode,
217                 rule_name    => 'renewalsallowed',
218             }
219         );
220         return $c->render(
221             status => 200,
222             openapi => {
223                 allows_renewal => $renewable,
224                 max_renewals => $rule->rule_value,
225                 current_renewals => $checkout->renewals,
226                 error => $error
227             }
228         );
229     }
230     catch {
231         $c->unhandled_exception($_);
232     };
233 }
234
235 1;