Bug 17005: Fix bad rebase
[koha.git] / 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 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
8 # version.
9 #
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.
13 #
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.
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;
26 use Koha::Checkouts;
27 use Koha::IssuingRules;
28 use Koha::Old::Checkouts;
29
30 use Try::Tiny;
31
32 =head1 NAME
33
34 Koha::REST::V1::Checkout
35
36 =head1 API
37
38 =head2 Methods
39
40 =head3 list
41
42 List Koha::Checkout objects
43
44 =cut
45
46 sub list {
47     my $c = shift->openapi->valid_input or return;
48     my $checked_in = $c->validation->param('checked_in');
49     try {
50         my $checkouts_set;
51         if ( $checked_in ) {
52             $checkouts_set = Koha::Old::Checkouts->new;
53         } else {
54             $checkouts_set = Koha::Checkouts->new;
55         }
56         my $checkouts = $c->objects->search( $checkouts_set, \&_to_model, \&_to_api );
57         return $c->render( status => 200, openapi => $checkouts );
58     } catch {
59         if ( $_->isa('DBIx::Class::Exception') ) {
60             return $c->render(
61                 status => 500,
62                 openapi => { error => $_->{msg} }
63             );
64         } else {
65             return $c->render(
66                 status => 500,
67                 openapi => { error => "Something went wrong, check the logs." }
68             );
69         }
70     };
71 }
72
73 =head3 get
74
75 get one checkout
76
77 =cut
78
79 sub get {
80     my $c = shift->openapi->valid_input or return;
81
82     my $checkout_id = $c->validation->param('checkout_id');
83     my $checkout = Koha::Checkouts->find( $checkout_id );
84     $checkout = Koha::Old::Checkouts->find( $checkout_id )
85         unless ($checkout);
86
87     unless ($checkout) {
88         return $c->render(
89             status => 404,
90             openapi => { error => "Checkout doesn't exist" }
91         );
92     }
93
94     return $c->render(
95         status => 200,
96         openapi => _to_api($checkout->TO_JSON)
97     );
98 }
99
100 =head3 renew
101
102 Renew a checkout
103
104 =cut
105
106 sub renew {
107     my $c = shift->openapi->valid_input or return;
108
109     my $checkout_id = $c->validation->param('checkout_id');
110     my $checkout = Koha::Checkouts->find( $checkout_id );
111
112     unless ($checkout) {
113         return $c->render(
114             status => 404,
115             openapi => { error => "Checkout doesn't exist" }
116         );
117     }
118
119     my $borrowernumber = $checkout->borrowernumber;
120     my $itemnumber = $checkout->itemnumber;
121
122     my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
123         $borrowernumber, $itemnumber);
124
125     if (!$can_renew) {
126         return $c->render(
127             status => 403,
128             openapi => { error => "Renewal not authorized ($error)" }
129         );
130     }
131
132     AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
133     $checkout = Koha::Checkouts->find($checkout_id);
134
135     $c->res->headers->location( $c->req->url->to_string );
136     return $c->render(
137         status => 201,
138         openapi => _to_api( $checkout->TO_JSON )
139     );
140 }
141
142 =head3 allows_renewal
143
144 Checks if the checkout could be renewed and return the related information.
145
146 =cut
147
148 sub allows_renewal {
149     my $c = shift->openapi->valid_input or return;
150
151     my $checkout_id = $c->validation->param('checkout_id');
152     my $checkout = Koha::Checkouts->find( $checkout_id );
153
154     unless ($checkout) {
155         return $c->render(
156             status => 404,
157             openapi => { error => "Checkout doesn't exist" }
158         );
159     }
160
161     my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
162         $checkout->borrowernumber, $checkout->itemnumber);
163
164     my $renewable = Mojo::JSON->false;
165     $renewable = Mojo::JSON->true if $can_renew;
166
167     my $rule = Koha::IssuingRules->get_effective_issuing_rule(
168         {
169             categorycode => $checkout->patron->categorycode,
170             itemtype     => $checkout->item->effective_itemtype,
171             branchcode   => $checkout->branchcode,
172         }
173     );
174     return $c->render(
175         status => 200,
176         openapi => {
177             allows_renewal => $renewable,
178             max_renewals => $rule->renewalsallowed,
179             current_renewals => $checkout->renewals,
180             error => $error
181         }
182     );
183 }
184
185 =head3 _to_api
186
187 Helper function that maps a hashref of Koha::Checkout attributes into REST api
188 attribute names.
189
190 =cut
191
192 sub _to_api {
193     my $checkout = shift;
194
195     foreach my $column ( keys %{ $Koha::REST::V1::Checkouts::to_api_mapping } ) {
196         my $mapped_column = $Koha::REST::V1::Checkouts::to_api_mapping->{$column};
197         if ( exists $checkout->{ $column } && defined $mapped_column )
198         {
199             $checkout->{ $mapped_column } = delete $checkout->{ $column };
200         }
201         elsif ( exists $checkout->{ $column } && !defined $mapped_column ) {
202             delete $checkout->{ $column };
203         }
204     }
205     return $checkout;
206 }
207
208 =head3 _to_model
209
210 Helper function that maps REST api objects into Koha::Checkouts
211 attribute names.
212
213 =cut
214
215 sub _to_model {
216     my $checkout = shift;
217
218     foreach my $attribute ( keys %{ $Koha::REST::V1::Checkouts::to_model_mapping } ) {
219         my $mapped_attribute = $Koha::REST::V1::Checkouts::to_model_mapping->{$attribute};
220         if ( exists $checkout->{ $attribute } && defined $mapped_attribute )
221         {
222             $checkout->{ $mapped_attribute } = delete $checkout->{ $attribute };
223         }
224         elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute )
225         {
226             delete $checkout->{ $attribute };
227         }
228     }
229     return $checkout;
230 }
231
232 =head2 Global variables
233
234 =head3 $to_api_mapping
235
236 =cut
237
238 our $to_api_mapping = {
239     issue_id        => 'checkout_id',
240     borrowernumber  => 'patron_id',
241     itemnumber      => 'item_id',
242     date_due        => 'due_date',
243     branchcode      => 'library_id',
244     returndate      => 'checkin_date',
245     lastreneweddate => 'last_renewed_date',
246     issuedate       => 'checkout_date',
247     notedate        => 'note_date',
248 };
249
250 =head3 $to_model_mapping
251
252 =cut
253
254 our $to_model_mapping = {
255     checkout_id       => 'issue_id',
256     patron_id         => 'borrowernumber',
257     item_id           => 'itemnumber',
258     due_date          => 'date_due',
259     library_id        => 'branchcode',
260     checkin_date      => 'returndate',
261     last_renewed_date => 'lastreneweddate',
262     checkout_date     => 'issuedate',
263     note_date         => 'notedate',
264     checked_in        => undef,
265 };
266
267 1;