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';
22 use C4::Auth qw( haspermission );
31 Koha::REST::V1::Checkout
39 List Koha::Checkout objects
44 my $c = shift->openapi->valid_input or return;
46 my $checkouts_set = Koha::Checkouts->new;
47 my $checkouts = $c->objects->search( $checkouts_set, \&_to_model, \&_to_api );
48 return $c->render( status => 200, openapi => $checkouts );
50 if ( $_->isa('DBIx::Class::Exception') ) {
53 openapi => { error => $_->{msg} }
58 openapi => { error => "Something went wrong, check the logs." }
71 my $c = shift->openapi->valid_input or return;
73 my $checkout = Koha::Checkouts->find( $c->validation->param('checkout_id') );
78 openapi => { error => "Checkout doesn't exist" }
84 openapi => _to_api($checkout->TO_JSON)
95 my $c = shift->openapi->valid_input or return;
97 my $checkout_id = $c->validation->param('checkout_id');
98 my $checkout = Koha::Checkouts->find( $checkout_id );
103 openapi => { error => "Checkout doesn't exist" }
107 my $borrowernumber = $checkout->borrowernumber;
108 my $itemnumber = $checkout->itemnumber;
110 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
111 $borrowernumber, $itemnumber);
116 openapi => { error => "Renewal not authorized ($error)" }
120 AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
121 $checkout = Koha::Checkouts->find($checkout_id);
123 $c->res->headers->location( $c->req->url->to_string );
126 openapi => _to_api( $checkout->TO_JSON )
132 Helper function that maps a hashref of Koha::Checkout attributes into REST api
138 my $checkout = shift;
140 foreach my $column ( keys %{ $Koha::REST::V1::Checkouts::to_api_mapping } ) {
141 my $mapped_column = $Koha::REST::V1::Checkouts::to_api_mapping->{$column};
142 if ( exists $checkout->{ $column } && defined $mapped_column )
144 $checkout->{ $mapped_column } = delete $checkout->{ $column };
146 elsif ( exists $checkout->{ $column } && !defined $mapped_column ) {
147 delete $checkout->{ $column };
155 Helper function that maps REST api objects into Koha::Checkouts
161 my $checkout = shift;
163 foreach my $attribute ( keys %{ $Koha::REST::V1::Checkouts::to_model_mapping } ) {
164 my $mapped_attribute = $Koha::REST::V1::Checkouts::to_model_mapping->{$attribute};
165 if ( exists $checkout->{ $attribute } && defined $mapped_attribute )
167 $checkout->{ $mapped_attribute } = delete $checkout->{ $attribute };
169 elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute )
171 delete $checkout->{ $attribute };
177 =head2 Global variables
179 =head3 $to_api_mapping
183 our $to_api_mapping = {
184 issue_id => 'checkout_id',
185 borrowernumber => 'patron_id',
186 itemnumber => 'item_id',
187 date_due => 'due_date',
188 branchcode => 'library_id',
189 returndate => 'checkin_date',
190 lastreneweddate => 'last_renewed_date',
191 issuedate => 'checkout_date',
192 notedate => 'note_date',
195 =head3 $to_model_mapping
199 our $to_model_mapping = {
200 checkout_id => 'issue_id',
201 patron_id => 'borrowernumber',
202 item_id => 'itemnumber',
203 due_date => 'date_due',
204 library_id => 'branchcode',
205 checkin_date => 'returndate',
206 last_renewed_date => 'lastreneweddate',
207 checkout_date => 'issuedate',
208 note_date => 'notedate',