Bug 24083: Add support for unseen_renewals
[koha.git] / Koha / REST / V1 / Checkouts.pm
blob93feeaeb24fff8f39aded360b5d2df06aa9a0fc9
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>.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
21 use Mojo::JSON;
23 use C4::Auth qw( haspermission );
24 use C4::Context;
25 use C4::Circulation;
26 use Koha::Checkouts;
27 use Koha::Old::Checkouts;
29 use Try::Tiny;
31 =head1 NAME
33 Koha::REST::V1::Checkout
35 =head1 API
37 =head2 Methods
39 =head3 list
41 List Koha::Checkout objects
43 =cut
45 sub list {
46 my $c = shift->openapi->valid_input or return;
48 my $checked_in = $c->validation->param('checked_in');
50 try {
51 my $checkouts_set;
53 if ( $checked_in ) {
54 $checkouts_set = Koha::Old::Checkouts->new;
55 } else {
56 $checkouts_set = Koha::Checkouts->new;
59 my $args = $c->validation->output;
60 my $attributes = {};
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 ) {
84 # remove checked_in
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 );
91 # Perform search
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,
97 params => $args,
98 });
101 return $c->render( status => 200, openapi => $checkouts->to_api );
102 } catch {
103 $c->unhandled_exception($_);
107 =head3 get
109 get one checkout
111 =cut
113 sub get {
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 )
119 unless ($checkout);
121 unless ($checkout) {
122 return $c->render(
123 status => 404,
124 openapi => { error => "Checkout doesn't exist" }
128 return try {
129 return $c->render(
130 status => 200,
131 openapi => $checkout->to_api
134 catch {
135 $c->unhandled_exception($_);
139 =head3 renew
141 Renew a checkout
143 =cut
145 sub renew {
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 );
152 unless ($checkout) {
153 return $c->render(
154 status => 404,
155 openapi => { error => "Checkout doesn't exist" }
159 return try {
160 my $borrowernumber = $checkout->borrowernumber;
161 my $itemnumber = $checkout->itemnumber;
163 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
164 $borrowernumber, $itemnumber);
166 if (!$can_renew) {
167 return $c->render(
168 status => 403,
169 openapi => { error => "Renewal not authorized ($error)" }
173 AddRenewal(
174 $borrowernumber,
175 $itemnumber,
176 $checkout->branchcode,
177 undef,
178 undef,
179 $seen
181 $checkout = Koha::Checkouts->find($checkout_id);
183 $c->res->headers->location( $c->req->url->to_string );
184 return $c->render(
185 status => 201,
186 openapi => $checkout->to_api
189 catch {
190 $c->unhandled_exception($_);
194 =head3 allows_renewal
196 Checks if the checkout could be renewed and return the related information.
198 =cut
200 sub allows_renewal {
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 );
206 unless ($checkout) {
207 return $c->render(
208 status => 404,
209 openapi => { error => "Checkout doesn't exist" }
213 return try {
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',
228 return $c->render(
229 status => 200,
230 openapi => {
231 allows_renewal => $renewable,
232 max_renewals => $rule->rule_value,
233 current_renewals => $checkout->renewals,
234 unseen_renewals => $checkout->unseen_renewals,
235 error => $error
239 catch {
240 $c->unhandled_exception($_);