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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 if ( $_->isa('DBIx::Class::Exception') ) {
106 openapi => { error => $_->{msg} }
111 openapi => { error => "Something went wrong, check the logs." }
124 my $c = shift->openapi->valid_input or return;
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 )
134 openapi => { error => "Checkout doesn't exist" }
140 openapi => $checkout->to_api
151 my $c = shift->openapi->valid_input or return;
153 my $checkout_id = $c->validation->param('checkout_id');
154 my $checkout = Koha::Checkouts->find( $checkout_id );
159 openapi => { error => "Checkout doesn't exist" }
163 my $borrowernumber = $checkout->borrowernumber;
164 my $itemnumber = $checkout->itemnumber;
166 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
167 $borrowernumber, $itemnumber);
172 openapi => { error => "Renewal not authorized ($error)" }
176 AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
177 $checkout = Koha::Checkouts->find($checkout_id);
179 $c->res->headers->location( $c->req->url->to_string );
182 openapi => $checkout->to_api
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" }
205 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
206 $checkout->borrowernumber, $checkout->itemnumber);
208 my $renewable = Mojo::JSON->false;
209 $renewable = Mojo::JSON->true if $can_renew;
211 my $rule = Koha::CirculationRules->get_effective_rule(
213 categorycode => $checkout->patron->categorycode,
214 itemtype => $checkout->item->effective_itemtype,
215 branchcode => $checkout->branchcode,
221 allows_renewal => $renewable,
222 max_renewals => $rule->renewalsallowed,
223 current_renewals => $checkout->renewals,