1 package Koha::REST::V1::Checkout;
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);
123 openapi => _to_api( $checkout->TO_JSON )
129 Helper function that maps a hashref of Koha::Checkout attributes into REST api
135 my $checkout = shift;
137 foreach my $column ( keys %{ $Koha::REST::V1::Checkout::to_api_mapping } ) {
138 my $mapped_column = $Koha::REST::V1::Checkout::to_api_mapping->{$column};
139 if ( exists $checkout->{ $column } && defined $mapped_column )
141 $checkout->{ $mapped_column } = delete $checkout->{ $column };
143 elsif ( exists $checkout->{ $column } && !defined $mapped_column ) {
144 delete $checkout->{ $column };
152 Helper function that maps REST api objects into Koha::Checkouts
158 my $checkout = shift;
160 foreach my $attribute ( keys %{ $Koha::REST::V1::Checkout::to_model_mapping } ) {
161 my $mapped_attribute = $Koha::REST::V1::Checkout::to_model_mapping->{$attribute};
162 if ( exists $checkout->{ $attribute } && defined $mapped_attribute )
164 $checkout->{ $mapped_attribute } = delete $checkout->{ $attribute };
166 elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute )
168 delete $checkout->{ $attribute };
174 =head2 Global variables
176 =head3 $to_api_mapping
180 our $to_api_mapping = {
181 issue_id => 'checkout_id',
182 borrowernumber => 'patron_id',
183 itemnumber => 'item_id',
184 date_due => 'due_date',
185 branchcode => 'library_id',
186 returndate => 'checkin_date',
187 lastreneweddate => 'last_renewed_date',
188 issuedate => 'checked_out_date',
189 notedate => 'note_date',
192 =head3 $to_model_mapping
196 our $to_model_mapping = {
197 checkout_id => 'issue_id',
198 patron_id => 'borrowernumber',
199 item_id => 'itemnumber',
200 due_date => 'date_due',
201 library_id => 'branchcode',
202 checkin_date => 'returndate',
203 last_renewed_date => 'lastreneweddate',
204 checked_out_date => 'issuedate',
205 note_date => 'notedate',