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;
28 use Koha::Old::Checkouts;
34 Koha::REST::V1::Checkout
42 List Koha::Checkout objects
47 my $c = shift->openapi->valid_input or return;
48 my $checked_in = $c->validation->param('checked_in');
52 $checkouts_set = Koha::Old::Checkouts->new;
54 $checkouts_set = Koha::Checkouts->new;
56 my $checkouts = $c->objects->search( $checkouts_set, \&_to_model, \&_to_api );
57 return $c->render( status => 200, openapi => $checkouts );
59 if ( $_->isa('DBIx::Class::Exception') ) {
62 openapi => { error => $_->{msg} }
67 openapi => { error => "Something went wrong, check the logs." }
80 my $c = shift->openapi->valid_input or return;
82 my $checkout_id = $c->validation->param('checkout_id');
83 my $checkout = Koha::Checkouts->find( $checkout_id );
84 $checkout = Koha::Old::Checkouts->find( $checkout_id )
90 openapi => { error => "Checkout doesn't exist" }
96 openapi => _to_api($checkout->TO_JSON)
107 my $c = shift->openapi->valid_input or return;
109 my $checkout_id = $c->validation->param('checkout_id');
110 my $checkout = Koha::Checkouts->find( $checkout_id );
115 openapi => { error => "Checkout doesn't exist" }
119 my $borrowernumber = $checkout->borrowernumber;
120 my $itemnumber = $checkout->itemnumber;
122 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
123 $borrowernumber, $itemnumber);
128 openapi => { error => "Renewal not authorized ($error)" }
132 AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
133 $checkout = Koha::Checkouts->find($checkout_id);
135 $c->res->headers->location( $c->req->url->to_string );
138 openapi => _to_api( $checkout->TO_JSON )
142 =head3 allows_renewal
144 Checks if the checkout could be renewed and return the related information.
149 my $c = shift->openapi->valid_input or return;
151 my $checkout_id = $c->validation->param('checkout_id');
152 my $checkout = Koha::Checkouts->find( $checkout_id );
157 openapi => { error => "Checkout doesn't exist" }
161 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
162 $checkout->borrowernumber, $checkout->itemnumber);
164 my $renewable = Mojo::JSON->false;
165 $renewable = Mojo::JSON->true if $can_renew;
167 my $rule = Koha::IssuingRules->get_effective_issuing_rule(
169 categorycode => $checkout->patron->categorycode,
170 itemtype => $checkout->item->effective_itemtype,
171 branchcode => $checkout->branchcode,
177 allows_renewal => $renewable,
178 max_renewals => $rule->renewalsallowed,
179 current_renewals => $checkout->renewals,
187 Helper function that maps a hashref of Koha::Checkout attributes into REST api
193 my $checkout = shift;
195 foreach my $column ( keys %{ $Koha::REST::V1::Checkouts::to_api_mapping } ) {
196 my $mapped_column = $Koha::REST::V1::Checkouts::to_api_mapping->{$column};
197 if ( exists $checkout->{ $column } && defined $mapped_column )
199 $checkout->{ $mapped_column } = delete $checkout->{ $column };
201 elsif ( exists $checkout->{ $column } && !defined $mapped_column ) {
202 delete $checkout->{ $column };
210 Helper function that maps REST api objects into Koha::Checkouts
216 my $checkout = shift;
218 foreach my $attribute ( keys %{ $Koha::REST::V1::Checkouts::to_model_mapping } ) {
219 my $mapped_attribute = $Koha::REST::V1::Checkouts::to_model_mapping->{$attribute};
220 if ( exists $checkout->{ $attribute } && defined $mapped_attribute )
222 $checkout->{ $mapped_attribute } = delete $checkout->{ $attribute };
224 elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute )
226 delete $checkout->{ $attribute };
232 =head2 Global variables
234 =head3 $to_api_mapping
238 our $to_api_mapping = {
239 issue_id => 'checkout_id',
240 borrowernumber => 'patron_id',
241 itemnumber => 'item_id',
242 date_due => 'due_date',
243 branchcode => 'library_id',
244 returndate => 'checkin_date',
245 lastreneweddate => 'last_renewed_date',
246 issuedate => 'checkout_date',
247 notedate => 'note_date',
250 =head3 $to_model_mapping
254 our $to_model_mapping = {
255 checkout_id => 'issue_id',
256 patron_id => 'borrowernumber',
257 item_id => 'itemnumber',
258 due_date => 'date_due',
259 library_id => 'branchcode',
260 checkin_date => 'returndate',
261 last_renewed_date => 'lastreneweddate',
262 checkout_date => 'issuedate',
263 note_date => 'notedate',