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 $seen = $c->validation->param('seen') || 1;
150 my $checkout = Koha
::Checkouts
->find( $checkout_id );
155 openapi
=> { error
=> "Checkout doesn't exist" }
160 my $borrowernumber = $checkout->borrowernumber;
161 my $itemnumber = $checkout->itemnumber;
163 my ($can_renew, $error) = C4
::Circulation
::CanBookBeRenewed
(
164 $borrowernumber, $itemnumber);
169 openapi
=> { error
=> "Renewal not authorized ($error)" }
176 $checkout->branchcode,
181 $checkout = Koha
::Checkouts
->find($checkout_id);
183 $c->res->headers->location( $c->req->url->to_string );
186 openapi
=> $checkout->to_api
190 $c->unhandled_exception($_);
194 =head3 allows_renewal
196 Checks if the checkout could be renewed and return the related information.
201 my $c = shift->openapi->valid_input or return;
203 my $checkout_id = $c->validation->param('checkout_id');
204 my $checkout = Koha
::Checkouts
->find( $checkout_id );
209 openapi
=> { error
=> "Checkout doesn't exist" }
214 my ($can_renew, $error) = C4
::Circulation
::CanBookBeRenewed
(
215 $checkout->borrowernumber, $checkout->itemnumber);
217 my $renewable = Mojo
::JSON
->false;
218 $renewable = Mojo
::JSON
->true if $can_renew;
220 my $rule = Koha
::CirculationRules
->get_effective_rule(
222 categorycode
=> $checkout->patron->categorycode,
223 itemtype
=> $checkout->item->effective_itemtype,
224 branchcode
=> $checkout->branchcode,
225 rule_name
=> 'renewalsallowed',
231 allows_renewal
=> $renewable,
232 max_renewals
=> $rule->rule_value,
233 current_renewals
=> $checkout->renewals,
234 unseen_renewals
=> $checkout->unseen_renewals,
240 $c->unhandled_exception($_);