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
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Controller';
23 use C4::Auth qw( haspermission );
25 use C4::Circulation qw( AddRenewal );
27 use Koha::Old::Checkouts;
29 use Try::Tiny qw( catch try );
33 Koha::REST::V1::Checkout
41 List Koha::Checkout objects
46 my $c = shift->openapi->valid_input or return;
48 my $checked_in = delete $c->validation->output->{checked_in};
54 $checkouts_set = Koha::Old::Checkouts->new;
56 $checkouts_set = Koha::Checkouts->new;
59 my $checkouts = $c->objects->search( $checkouts_set );
66 $c->unhandled_exception($_);
77 my $c = shift->openapi->valid_input or return;
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 )
87 openapi => { error => "Checkout doesn't exist" }
94 openapi => $checkout->to_api
98 $c->unhandled_exception($_);
104 List Koha::Checkout::Renewals
109 my $c = shift->openapi->valid_input or return;
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)
120 openapi => { error => "Checkout doesn't exist" }
124 my $renewals_rs = $checkout->renewals;
125 my $renewals = $c->objects->search( $renewals_rs );
133 $c->unhandled_exception($_);
145 my $c = shift->openapi->valid_input or return;
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 );
154 openapi => { error => "Checkout doesn't exist" }
159 my $borrowernumber = $checkout->borrowernumber;
160 my $itemnumber = $checkout->itemnumber;
162 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
163 $borrowernumber, $itemnumber);
168 openapi => { error => "Renewal not authorized ($error)" }
175 $checkout->branchcode,
180 $checkout = Koha::Checkouts->find($checkout_id);
182 $c->res->headers->location( $c->req->url->to_string );
185 openapi => $checkout->to_api
189 $c->unhandled_exception($_);
193 =head3 allows_renewal
195 Checks if the checkout could be renewed and return the related information.
200 my $c = shift->openapi->valid_input or return;
202 my $checkout_id = $c->validation->param('checkout_id');
203 my $checkout = Koha::Checkouts->find( $checkout_id );
208 openapi => { error => "Checkout doesn't exist" }
213 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
214 $checkout->borrowernumber, $checkout->itemnumber);
216 my $renewable = Mojo::JSON->false;
217 $renewable = Mojo::JSON->true if $can_renew;
219 my $rule = Koha::CirculationRules->get_effective_rule(
221 categorycode => $checkout->patron->categorycode,
222 itemtype => $checkout->item->effective_itemtype,
223 branchcode => $checkout->branchcode,
224 rule_name => 'renewalsallowed',
230 allows_renewal => $renewable,
231 max_renewals => $rule->rule_value,
232 current_renewals => $checkout->renewals_count,
233 unseen_renewals => $checkout->unseen_renewals,
239 $c->unhandled_exception($_);