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 );
32 Koha::REST::V1::Checkout
40 List Koha::Checkout objects
45 my $c = shift->openapi->valid_input or return;
47 my $checkouts_set = Koha::Checkouts->new;
48 my $checkouts = $c->objects->search( $checkouts_set, \&_to_model, \&_to_api );
49 return $c->render( status => 200, openapi => $checkouts );
51 if ( $_->isa('DBIx::Class::Exception') ) {
54 openapi => { error => $_->{msg} }
59 openapi => { error => "Something went wrong, check the logs." }
72 my $c = shift->openapi->valid_input or return;
74 my $checkout = Koha::Checkouts->find( $c->validation->param('checkout_id') );
79 openapi => { error => "Checkout doesn't exist" }
85 openapi => _to_api($checkout->TO_JSON)
96 my $c = shift->openapi->valid_input or return;
98 my $checkout_id = $c->validation->param('checkout_id');
99 my $checkout = Koha::Checkouts->find( $checkout_id );
104 openapi => { error => "Checkout doesn't exist" }
108 my $borrowernumber = $checkout->borrowernumber;
109 my $itemnumber = $checkout->itemnumber;
111 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
112 $borrowernumber, $itemnumber);
117 openapi => { error => "Renewal not authorized ($error)" }
121 AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
122 $checkout = Koha::Checkouts->find($checkout_id);
124 $c->res->headers->location( $c->req->url->to_string );
127 openapi => _to_api( $checkout->TO_JSON )
132 my ($c, $args, $cb) = @_;
134 my $user = $c->stash('koha.user');
136 my $OpacRenewalAllowed;
137 if ($user->borrowernumber == $borrowernumber) {
138 $OpacRenewalAllowed = C4::Context->preference('OpacRenewalAllowed');
141 unless ($user && ($OpacRenewalAllowed
142 || haspermission($user->userid, { circulate => "circulate_remaining_permissions" }))) {
143 return $c->$cb({error => "You don't have the required permission"}, 403);
146 my $checkout_id = $args->{checkout_id};
147 my $checkout = Koha::Issues->find($checkout_id);
151 error => "Checkout doesn't exist"
155 my $borrowernumber = $checkout->borrowernumber;
156 my $itemnumber = $checkout->itemnumber;
158 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
159 $borrowernumber, $itemnumber);
161 return $c->$cb({ renewable => Mojo::JSON->true, error => undef }, 200) if $can_renew;
162 return $c->$cb({ renewable => Mojo::JSON->false, error => $error }, 200);
167 Helper function that maps a hashref of Koha::Checkout attributes into REST api
173 my $checkout = shift;
175 foreach my $column ( keys %{ $Koha::REST::V1::Checkouts::to_api_mapping } ) {
176 my $mapped_column = $Koha::REST::V1::Checkouts::to_api_mapping->{$column};
177 if ( exists $checkout->{ $column } && defined $mapped_column )
179 $checkout->{ $mapped_column } = delete $checkout->{ $column };
181 elsif ( exists $checkout->{ $column } && !defined $mapped_column ) {
182 delete $checkout->{ $column };
190 Helper function that maps REST api objects into Koha::Checkouts
196 my $checkout = shift;
198 foreach my $attribute ( keys %{ $Koha::REST::V1::Checkouts::to_model_mapping } ) {
199 my $mapped_attribute = $Koha::REST::V1::Checkouts::to_model_mapping->{$attribute};
200 if ( exists $checkout->{ $attribute } && defined $mapped_attribute )
202 $checkout->{ $mapped_attribute } = delete $checkout->{ $attribute };
204 elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute )
206 delete $checkout->{ $attribute };
212 =head2 Global variables
214 =head3 $to_api_mapping
218 our $to_api_mapping = {
219 issue_id => 'checkout_id',
220 borrowernumber => 'patron_id',
221 itemnumber => 'item_id',
222 date_due => 'due_date',
223 branchcode => 'library_id',
224 returndate => 'checkin_date',
225 lastreneweddate => 'last_renewed_date',
226 issuedate => 'checkout_date',
227 notedate => 'note_date',
230 =head3 $to_model_mapping
234 our $to_model_mapping = {
235 checkout_id => 'issue_id',
236 patron_id => 'borrowernumber',
237 item_id => 'itemnumber',
238 due_date => 'date_due',
239 library_id => 'branchcode',
240 checkin_date => 'returndate',
241 last_renewed_date => 'lastreneweddate',
242 checkout_date => 'issuedate',
243 note_date => 'notedate',