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 );
27 use Koha::IssuingRules;
33 Koha::REST::V1::Checkout
41 List Koha::Checkout objects
46 my $c = shift->openapi->valid_input or return;
48 my $checkouts_set = Koha::Checkouts->new;
49 my $checkouts = $c->objects->search( $checkouts_set, \&_to_model, \&_to_api );
50 return $c->render( status => 200, openapi => $checkouts );
52 if ( $_->isa('DBIx::Class::Exception') ) {
55 openapi => { error => $_->{msg} }
60 openapi => { error => "Something went wrong, check the logs." }
73 my $c = shift->openapi->valid_input or return;
75 my $checkout = Koha::Checkouts->find( $c->validation->param('checkout_id') );
80 openapi => { error => "Checkout doesn't exist" }
86 openapi => _to_api($checkout->TO_JSON)
97 my $c = shift->openapi->valid_input or return;
99 my $checkout_id = $c->validation->param('checkout_id');
100 my $checkout = Koha::Checkouts->find( $checkout_id );
105 openapi => { error => "Checkout doesn't exist" }
109 my $borrowernumber = $checkout->borrowernumber;
110 my $itemnumber = $checkout->itemnumber;
112 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
113 $borrowernumber, $itemnumber);
118 openapi => { error => "Renewal not authorized ($error)" }
122 AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
123 $checkout = Koha::Checkouts->find($checkout_id);
125 $c->res->headers->location( $c->req->url->to_string );
128 openapi => _to_api( $checkout->TO_JSON )
132 =head3 allows_renewal
134 Checks if the checkout could be renewed and return the related information.
139 my $c = shift->openapi->valid_input or return;
141 my $checkout_id = $c->validation->param('checkout_id');
142 my $checkout = Koha::Checkouts->find( $checkout_id );
147 openapi => { error => "Checkout doesn't exist" }
151 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
152 $checkout->borrowernumber, $checkout->itemnumber);
154 my $renewable = Mojo::JSON->false;
155 $renewable = Mojo::JSON->true if $can_renew;
157 my $rule = Koha::IssuingRules->get_effective_issuing_rule(
159 categorycode => $checkout->patron->categorycode,
160 itemtype => $checkout->item->effective_itemtype,
161 branchcode => $checkout->branchcode,
167 allows_renewal => $renewable,
168 max_renewals => $rule->renewalsallowed,
169 current_renewals => $checkout->renewals,
177 Helper function that maps a hashref of Koha::Checkout attributes into REST api
183 my $checkout = shift;
185 foreach my $column ( keys %{ $Koha::REST::V1::Checkouts::to_api_mapping } ) {
186 my $mapped_column = $Koha::REST::V1::Checkouts::to_api_mapping->{$column};
187 if ( exists $checkout->{ $column } && defined $mapped_column )
189 $checkout->{ $mapped_column } = delete $checkout->{ $column };
191 elsif ( exists $checkout->{ $column } && !defined $mapped_column ) {
192 delete $checkout->{ $column };
200 Helper function that maps REST api objects into Koha::Checkouts
206 my $checkout = shift;
208 foreach my $attribute ( keys %{ $Koha::REST::V1::Checkouts::to_model_mapping } ) {
209 my $mapped_attribute = $Koha::REST::V1::Checkouts::to_model_mapping->{$attribute};
210 if ( exists $checkout->{ $attribute } && defined $mapped_attribute )
212 $checkout->{ $mapped_attribute } = delete $checkout->{ $attribute };
214 elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute )
216 delete $checkout->{ $attribute };
222 =head2 Global variables
224 =head3 $to_api_mapping
228 our $to_api_mapping = {
229 issue_id => 'checkout_id',
230 borrowernumber => 'patron_id',
231 itemnumber => 'item_id',
232 date_due => 'due_date',
233 branchcode => 'library_id',
234 returndate => 'checkin_date',
235 lastreneweddate => 'last_renewed_date',
236 issuedate => 'checkout_date',
237 notedate => 'note_date',
240 =head3 $to_model_mapping
244 our $to_model_mapping = {
245 checkout_id => 'issue_id',
246 patron_id => 'borrowernumber',
247 item_id => 'itemnumber',
248 due_date => 'date_due',
249 library_id => 'branchcode',
250 checkin_date => 'returndate',
251 last_renewed_date => 'lastreneweddate',
252 checkout_date => 'issuedate',
253 note_date => 'notedate',