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.
18 use Mojo::Base 'Mojolicious::Controller';
20 use C4::Auth qw( haspermission );
29 Koha::REST::V1::Checkout
37 List Koha::Checkout objects
42 my $c = shift->openapi->valid_input or return;
44 my $checkouts_set = Koha::Checkouts->new;
45 my $checkouts = $c->objects->search( $checkouts_set, \&_to_model, \&_to_api );
46 return $c->render( status => 200, openapi => $checkouts );
48 if ( $_->isa('DBIx::Class::Exception') ) {
51 openapi => { error => $_->{msg} }
56 openapi => { error => "Something went wrong, check the logs." }
69 my $c = shift->openapi->valid_input or return;
71 my $checkout = Koha::Checkouts->find( $c->validation->param('checkout_id') );
76 openapi => { error => "Checkout doesn't exist" }
82 openapi => _to_api($checkout->TO_JSON)
93 my $c = shift->openapi->valid_input or return;
95 my $checkout_id = $c->validation->param('checkout_id');
96 my $checkout = Koha::Checkouts->find( $checkout_id );
101 openapi => { error => "Checkout doesn't exist" }
105 my $borrowernumber = $checkout->borrowernumber;
106 my $itemnumber = $checkout->itemnumber;
108 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
109 $borrowernumber, $itemnumber);
114 openapi => { error => "Renewal not authorized ($error)" }
118 AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
119 $checkout = Koha::Checkouts->find($checkout_id);
121 $c->res->headers->location( $c->req->url->to_string );
124 openapi => _to_api( $checkout->TO_JSON )
130 Helper function that maps a hashref of Koha::Checkout attributes into REST api
136 my $checkout = shift;
138 foreach my $column ( keys %{ $Koha::REST::V1::Checkouts::to_api_mapping } ) {
139 my $mapped_column = $Koha::REST::V1::Checkouts::to_api_mapping->{$column};
140 if ( exists $checkout->{ $column } && defined $mapped_column )
142 $checkout->{ $mapped_column } = delete $checkout->{ $column };
144 elsif ( exists $checkout->{ $column } && !defined $mapped_column ) {
145 delete $checkout->{ $column };
153 Helper function that maps REST api objects into Koha::Checkouts
159 my $checkout = shift;
161 foreach my $attribute ( keys %{ $Koha::REST::V1::Checkouts::to_model_mapping } ) {
162 my $mapped_attribute = $Koha::REST::V1::Checkouts::to_model_mapping->{$attribute};
163 if ( exists $checkout->{ $attribute } && defined $mapped_attribute )
165 $checkout->{ $mapped_attribute } = delete $checkout->{ $attribute };
167 elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute )
169 delete $checkout->{ $attribute };
175 =head2 Global variables
177 =head3 $to_api_mapping
181 our $to_api_mapping = {
182 issue_id => 'checkout_id',
183 borrowernumber => 'patron_id',
184 itemnumber => 'item_id',
185 date_due => 'due_date',
186 branchcode => 'library_id',
187 returndate => 'checkin_date',
188 lastreneweddate => 'last_renewed_date',
189 issuedate => 'checkout_date',
190 notedate => 'note_date',
193 =head3 $to_model_mapping
197 our $to_model_mapping = {
198 checkout_id => 'issue_id',
199 patron_id => 'borrowernumber',
200 item_id => 'itemnumber',
201 due_date => 'date_due',
202 library_id => 'branchcode',
203 checkin_date => 'returndate',
204 last_renewed_date => 'lastreneweddate',
205 checkout_date => 'issuedate',
206 note_date => 'notedate',