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 );
27 use Koha::Old::Checkouts;
33 Koha::REST::V1::Checkout
41 List Koha::Checkout objects
46 my $c = shift->openapi->valid_input or return;
48 my $checked_in = $c->validation->param('checked_in');
54 $checkouts_set = Koha::Old::Checkouts->new;
56 $checkouts_set = Koha::Checkouts->new;
59 my $args = $c->validation->output;
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 ) {
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 );
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,
101 return $c->render( status => 200, openapi => $checkouts->to_api );
103 $c->unhandled_exception($_);
114 my $c = shift->openapi->valid_input or return;
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 )
124 openapi => { error => "Checkout doesn't exist" }
131 openapi => $checkout->to_api
135 $c->unhandled_exception($_);
146 my $c = shift->openapi->valid_input or return;
148 my $checkout_id = $c->validation->param('checkout_id');
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)" }
172 AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
173 $checkout = Koha::Checkouts->find($checkout_id);
175 $c->res->headers->location( $c->req->url->to_string );
178 openapi => $checkout->to_api
182 $c->unhandled_exception($_);
186 =head3 allows_renewal
188 Checks if the checkout could be renewed and return the related information.
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 );
201 openapi => { error => "Checkout doesn't exist" }
206 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
207 $checkout->borrowernumber, $checkout->itemnumber);
209 my $renewable = Mojo::JSON->false;
210 $renewable = Mojo::JSON->true if $can_renew;
212 my $rule = Koha::CirculationRules->get_effective_rule(
214 categorycode => $checkout->patron->categorycode,
215 itemtype => $checkout->item->effective_itemtype,
216 branchcode => $checkout->branchcode,
217 rule_name => 'renewalsallowed',
223 allows_renewal => $renewable,
224 max_renewals => $rule->rule_value,
225 current_renewals => $checkout->renewals,
231 $c->unhandled_exception($_);