Bug 24545: Fix license statements
[koha-equinox.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         if ( $_->isa('DBIx::Class::Exception') ) {
104             return $c->render(
105                 status => 500,
106                 openapi => { error => $_->{msg} }
107             );
108         } else {
109             return $c->render(
110                 status => 500,
111                 openapi => { error => "Something went wrong, check the logs." }
112             );
113         }
114     };
115 }
116
117 =head3 get
118
119 get one checkout
120
121 =cut
122
123 sub get {
124     my $c = shift->openapi->valid_input or return;
125
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);
130
131     unless ($checkout) {
132         return $c->render(
133             status => 404,
134             openapi => { error => "Checkout doesn't exist" }
135         );
136     }
137
138     return $c->render(
139         status  => 200,
140         openapi => $checkout->to_api
141     );
142 }
143
144 =head3 renew
145
146 Renew a checkout
147
148 =cut
149
150 sub renew {
151     my $c = shift->openapi->valid_input or return;
152
153     my $checkout_id = $c->validation->param('checkout_id');
154     my $checkout = Koha::Checkouts->find( $checkout_id );
155
156     unless ($checkout) {
157         return $c->render(
158             status => 404,
159             openapi => { error => "Checkout doesn't exist" }
160         );
161     }
162
163     my $borrowernumber = $checkout->borrowernumber;
164     my $itemnumber = $checkout->itemnumber;
165
166     my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
167         $borrowernumber, $itemnumber);
168
169     if (!$can_renew) {
170         return $c->render(
171             status => 403,
172             openapi => { error => "Renewal not authorized ($error)" }
173         );
174     }
175
176     AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
177     $checkout = Koha::Checkouts->find($checkout_id);
178
179     $c->res->headers->location( $c->req->url->to_string );
180     return $c->render(
181         status  => 201,
182         openapi => $checkout->to_api
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     my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
206         $checkout->borrowernumber, $checkout->itemnumber);
207
208     my $renewable = Mojo::JSON->false;
209     $renewable = Mojo::JSON->true if $can_renew;
210
211     my $rule = Koha::CirculationRules->get_effective_rule(
212         {
213             categorycode => $checkout->patron->categorycode,
214             itemtype     => $checkout->item->effective_itemtype,
215             branchcode   => $checkout->branchcode,
216             rule_name    => 'renewalsallowed',
217         }
218     );
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
226         }
227     );
228 }
229
230 1;