Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / REST / V1 / Checkouts.pm
1 package Koha::REST::V1::Checkouts;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21 use Mojo::JSON;
22
23 use C4::Auth qw( haspermission );
24 use C4::Context;
25 use C4::Circulation qw( AddRenewal );
26 use Koha::Checkouts;
27 use Koha::Old::Checkouts;
28
29 use Try::Tiny qw( catch try );
30
31 =head1 NAME
32
33 Koha::REST::V1::Checkout
34
35 =head1 API
36
37 =head2 Methods
38
39 =head3 list
40
41 List Koha::Checkout objects
42
43 =cut
44
45 sub list {
46     my $c = shift->openapi->valid_input or return;
47
48     my $checked_in = delete $c->validation->output->{checked_in};
49
50     try {
51         my $checkouts_set;
52
53         if ( $checked_in ) {
54             $checkouts_set = Koha::Old::Checkouts->new;
55         } else {
56             $checkouts_set = Koha::Checkouts->new;
57         }
58
59         my $checkouts = $c->objects->search( $checkouts_set );
60
61         return $c->render(
62             status  => 200,
63             openapi => $checkouts
64         );
65     } catch {
66         $c->unhandled_exception($_);
67     };
68 }
69
70 =head3 get
71
72 get one checkout
73
74 =cut
75
76 sub get {
77     my $c = shift->openapi->valid_input or return;
78
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 )
82         unless ($checkout);
83
84     unless ($checkout) {
85         return $c->render(
86             status => 404,
87             openapi => { error => "Checkout doesn't exist" }
88         );
89     }
90
91     return try {
92         return $c->render(
93             status  => 200,
94             openapi => $checkout->to_api
95         );
96     }
97     catch {
98         $c->unhandled_exception($_);
99     };
100 }
101
102 =head3 renew
103
104 Renew a checkout
105
106 =cut
107
108 sub renew {
109     my $c = shift->openapi->valid_input or return;
110
111     my $checkout_id = $c->validation->param('checkout_id');
112     my $seen = $c->validation->param('seen') || 1;
113     my $checkout = Koha::Checkouts->find( $checkout_id );
114
115     unless ($checkout) {
116         return $c->render(
117             status => 404,
118             openapi => { error => "Checkout doesn't exist" }
119         );
120     }
121
122     return try {
123         my $borrowernumber = $checkout->borrowernumber;
124         my $itemnumber = $checkout->itemnumber;
125
126         my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
127             $borrowernumber, $itemnumber);
128
129         if (!$can_renew) {
130             return $c->render(
131                 status => 403,
132                 openapi => { error => "Renewal not authorized ($error)" }
133             );
134         }
135
136         AddRenewal(
137             $borrowernumber,
138             $itemnumber,
139             $checkout->branchcode,
140             undef,
141             undef,
142             $seen
143         );
144         $checkout = Koha::Checkouts->find($checkout_id);
145
146         $c->res->headers->location( $c->req->url->to_string );
147         return $c->render(
148             status  => 201,
149             openapi => $checkout->to_api
150         );
151     }
152     catch {
153         $c->unhandled_exception($_);
154     };
155 }
156
157 =head3 allows_renewal
158
159 Checks if the checkout could be renewed and return the related information.
160
161 =cut
162
163 sub allows_renewal {
164     my $c = shift->openapi->valid_input or return;
165
166     my $checkout_id = $c->validation->param('checkout_id');
167     my $checkout = Koha::Checkouts->find( $checkout_id );
168
169     unless ($checkout) {
170         return $c->render(
171             status => 404,
172             openapi => { error => "Checkout doesn't exist" }
173         );
174     }
175
176     return try {
177         my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
178             $checkout->borrowernumber, $checkout->itemnumber);
179
180         my $renewable = Mojo::JSON->false;
181         $renewable = Mojo::JSON->true if $can_renew;
182
183         my $rule = Koha::CirculationRules->get_effective_rule(
184             {
185                 categorycode => $checkout->patron->categorycode,
186                 itemtype     => $checkout->item->effective_itemtype,
187                 branchcode   => $checkout->branchcode,
188                 rule_name    => 'renewalsallowed',
189             }
190         );
191         return $c->render(
192             status => 200,
193             openapi => {
194                 allows_renewal => $renewable,
195                 max_renewals => $rule->rule_value,
196                 current_renewals => $checkout->renewals,
197                 unseen_renewals => $checkout->unseen_renewals,
198                 error => $error
199             }
200         );
201     }
202     catch {
203         $c->unhandled_exception($_);
204     };
205 }
206
207 1;