Bug 33328: Rename x-marc-schema => x-record-schema
[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 qw( AddRenewal );
26 use Koha::Checkouts;
27 use Koha::Old::Checkouts;
28
29 use Try::Tiny qw( catch try );
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 = delete $c->validation->output->{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 $checkouts = $c->objects->search( $checkouts_set );
60
61         return $c->render(
62             status  => 200,
63             openapi => $checkouts
64         );
65     } catch {
66         $c->unhandled_exception($_);
67     };
68 }
69
70 =head3 get
71
72 get one checkout
73
74 =cut
75
76 sub get {
77     my $c = shift->openapi->valid_input or return;
78
79     my $checkout_id = $c->validation->param('checkout_id');
80     my $checkout = Koha::Checkouts->find( $checkout_id );
81     $checkout = Koha::Old::Checkouts->find( $checkout_id )
82         unless ($checkout);
83
84     unless ($checkout) {
85         return $c->render(
86             status => 404,
87             openapi => { error => "Checkout doesn't exist" }
88         );
89     }
90
91     return try {
92         return $c->render(
93             status  => 200,
94             openapi => $checkout->to_api
95         );
96     }
97     catch {
98         $c->unhandled_exception($_);
99     };
100 }
101
102 =head3 get_renewals
103
104 List Koha::Checkout::Renewals
105
106 =cut
107
108 sub get_renewals {
109     my $c = shift->openapi->valid_input or return;
110
111     try {
112         my $checkout_id = $c->validation->param('checkout_id');
113         my $checkout    = Koha::Checkouts->find($checkout_id);
114         $checkout = Koha::Old::Checkouts->find($checkout_id)
115           unless ($checkout);
116
117         unless ($checkout) {
118             return $c->render(
119                 status  => 404,
120                 openapi => { error => "Checkout doesn't exist" }
121             );
122         }
123
124         my $renewals_rs = $checkout->renewals;
125         my $renewals = $c->objects->search( $renewals_rs );
126
127         return $c->render(
128             status  => 200,
129             openapi => $renewals
130         );
131     }
132     catch {
133         $c->unhandled_exception($_);
134     };
135 }
136
137
138 =head3 renew
139
140 Renew a checkout
141
142 =cut
143
144 sub renew {
145     my $c = shift->openapi->valid_input or return;
146
147     my $checkout_id = $c->validation->param('checkout_id');
148     my $seen = $c->validation->param('seen') || 1;
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(
173             $borrowernumber,
174             $itemnumber,
175             $checkout->branchcode,
176             undef,
177             undef,
178             $seen
179         );
180         $checkout = Koha::Checkouts->find($checkout_id);
181
182         $c->res->headers->location( $c->req->url->to_string );
183         return $c->render(
184             status  => 201,
185             openapi => $checkout->to_api
186         );
187     }
188     catch {
189         $c->unhandled_exception($_);
190     };
191 }
192
193 =head3 allows_renewal
194
195 Checks if the checkout could be renewed and return the related information.
196
197 =cut
198
199 sub allows_renewal {
200     my $c = shift->openapi->valid_input or return;
201
202     my $checkout_id = $c->validation->param('checkout_id');
203     my $checkout = Koha::Checkouts->find( $checkout_id );
204
205     unless ($checkout) {
206         return $c->render(
207             status => 404,
208             openapi => { error => "Checkout doesn't exist" }
209         );
210     }
211
212     return try {
213         my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
214             $checkout->borrowernumber, $checkout->itemnumber);
215
216         my $renewable = Mojo::JSON->false;
217         $renewable = Mojo::JSON->true if $can_renew;
218
219         my $rule = Koha::CirculationRules->get_effective_rule(
220             {
221                 categorycode => $checkout->patron->categorycode,
222                 itemtype     => $checkout->item->effective_itemtype,
223                 branchcode   => $checkout->branchcode,
224                 rule_name    => 'renewalsallowed',
225             }
226         );
227         return $c->render(
228             status => 200,
229             openapi => {
230                 allows_renewal => $renewable,
231                 max_renewals => $rule->rule_value,
232                 current_renewals => $checkout->renewals_count,
233                 unseen_renewals => $checkout->unseen_renewals,
234                 error => $error
235             }
236         );
237     }
238     catch {
239         $c->unhandled_exception($_);
240     };
241 }
242
243 1;