# This file is part of Koha.
#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use C4::Auth qw( haspermission );
use C4::Context;
-use C4::Circulation;
+use C4::Circulation qw( AddRenewal );
use Koha::Checkouts;
-use Koha::IssuingRules;
use Koha::Old::Checkouts;
-use Try::Tiny;
+use Try::Tiny qw( catch try );
=head1 NAME
sub list {
my $c = shift->openapi->valid_input or return;
- my $checked_in = $c->validation->param('checked_in');
+
+ my $checked_in = delete $c->validation->output->{checked_in};
+
try {
my $checkouts_set;
+
if ( $checked_in ) {
$checkouts_set = Koha::Old::Checkouts->new;
} else {
$checkouts_set = Koha::Checkouts->new;
}
- my $checkouts = $c->objects->search( $checkouts_set, \&_to_model, \&_to_api );
- return $c->render( status => 200, openapi => $checkouts );
+
+ my $checkouts = $c->objects->search( $checkouts_set );
+
+ return $c->render(
+ status => 200,
+ openapi => $checkouts
+ );
} catch {
- if ( $_->isa('DBIx::Class::Exception') ) {
- return $c->render(
- status => 500,
- openapi => { error => $_->{msg} }
- );
- } else {
- return $c->render(
- status => 500,
- openapi => { error => "Something went wrong, check the logs." }
- );
- }
+ $c->unhandled_exception($_);
};
}
);
}
- return $c->render(
- status => 200,
- openapi => _to_api($checkout->TO_JSON)
- );
+ return try {
+ return $c->render(
+ status => 200,
+ openapi => $checkout->to_api
+ );
+ }
+ catch {
+ $c->unhandled_exception($_);
+ };
+}
+
+=head3 get_renewals
+
+List Koha::Checkout::Renewals
+
+=cut
+
+sub get_renewals {
+ my $c = shift->openapi->valid_input or return;
+
+ try {
+ my $checkout_id = $c->validation->param('checkout_id');
+ my $checkout = Koha::Checkouts->find($checkout_id);
+ $checkout = Koha::Old::Checkouts->find($checkout_id)
+ unless ($checkout);
+
+ unless ($checkout) {
+ return $c->render(
+ status => 404,
+ openapi => { error => "Checkout doesn't exist" }
+ );
+ }
+
+ my $renewals_rs = $checkout->renewals;
+ my $renewals = $c->objects->search( $renewals_rs );
+
+ return $c->render(
+ status => 200,
+ openapi => $renewals
+ );
+ }
+ catch {
+ $c->unhandled_exception($_);
+ };
}
+
=head3 renew
Renew a checkout
my $c = shift->openapi->valid_input or return;
my $checkout_id = $c->validation->param('checkout_id');
+ my $seen = $c->validation->param('seen') || 1;
my $checkout = Koha::Checkouts->find( $checkout_id );
unless ($checkout) {
);
}
- my $borrowernumber = $checkout->borrowernumber;
- my $itemnumber = $checkout->itemnumber;
+ return try {
+ my $borrowernumber = $checkout->borrowernumber;
+ my $itemnumber = $checkout->itemnumber;
+
+ my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
+ $borrowernumber, $itemnumber);
+
+ if (!$can_renew) {
+ return $c->render(
+ status => 403,
+ openapi => { error => "Renewal not authorized ($error)" }
+ );
+ }
- my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
- $borrowernumber, $itemnumber);
+ AddRenewal(
+ $borrowernumber,
+ $itemnumber,
+ $checkout->branchcode,
+ undef,
+ undef,
+ $seen
+ );
+ $checkout = Koha::Checkouts->find($checkout_id);
- if (!$can_renew) {
+ $c->res->headers->location( $c->req->url->to_string );
return $c->render(
- status => 403,
- openapi => { error => "Renewal not authorized ($error)" }
+ status => 201,
+ openapi => $checkout->to_api
);
}
-
- AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
- $checkout = Koha::Checkouts->find($checkout_id);
-
- $c->res->headers->location( $c->req->url->to_string );
- return $c->render(
- status => 201,
- openapi => _to_api( $checkout->TO_JSON )
- );
+ catch {
+ $c->unhandled_exception($_);
+ };
}
=head3 allows_renewal
);
}
- my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
- $checkout->borrowernumber, $checkout->itemnumber);
-
- my $renewable = Mojo::JSON->false;
- $renewable = Mojo::JSON->true if $can_renew;
-
- my $rule = Koha::IssuingRules->get_effective_issuing_rule(
- {
- categorycode => $checkout->patron->categorycode,
- itemtype => $checkout->item->effective_itemtype,
- branchcode => $checkout->branchcode,
- }
- );
- return $c->render(
- status => 200,
- openapi => {
- allows_renewal => $renewable,
- max_renewals => $rule->renewalsallowed,
- current_renewals => $checkout->renewals,
- error => $error
- }
- );
-}
-
-=head3 _to_api
-
-Helper function that maps a hashref of Koha::Checkout attributes into REST api
-attribute names.
-
-=cut
-
-sub _to_api {
- my $checkout = shift;
-
- foreach my $column ( keys %{ $Koha::REST::V1::Checkouts::to_api_mapping } ) {
- my $mapped_column = $Koha::REST::V1::Checkouts::to_api_mapping->{$column};
- if ( exists $checkout->{ $column } && defined $mapped_column )
- {
- $checkout->{ $mapped_column } = delete $checkout->{ $column };
- }
- elsif ( exists $checkout->{ $column } && !defined $mapped_column ) {
- delete $checkout->{ $column };
- }
- }
- return $checkout;
-}
-
-=head3 _to_model
-
-Helper function that maps REST api objects into Koha::Checkouts
-attribute names.
-
-=cut
+ return try {
+ my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
+ $checkout->borrowernumber, $checkout->itemnumber);
-sub _to_model {
- my $checkout = shift;
+ my $renewable = Mojo::JSON->false;
+ $renewable = Mojo::JSON->true if $can_renew;
- foreach my $attribute ( keys %{ $Koha::REST::V1::Checkouts::to_model_mapping } ) {
- my $mapped_attribute = $Koha::REST::V1::Checkouts::to_model_mapping->{$attribute};
- if ( exists $checkout->{ $attribute } && defined $mapped_attribute )
- {
- $checkout->{ $mapped_attribute } = delete $checkout->{ $attribute };
- }
- elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute )
- {
- delete $checkout->{ $attribute };
- }
+ my $rule = Koha::CirculationRules->get_effective_rule(
+ {
+ categorycode => $checkout->patron->categorycode,
+ itemtype => $checkout->item->effective_itemtype,
+ branchcode => $checkout->branchcode,
+ rule_name => 'renewalsallowed',
+ }
+ );
+ return $c->render(
+ status => 200,
+ openapi => {
+ allows_renewal => $renewable,
+ max_renewals => $rule->rule_value,
+ current_renewals => $checkout->renewals_count,
+ unseen_renewals => $checkout->unseen_renewals,
+ error => $error
+ }
+ );
}
- return $checkout;
+ catch {
+ $c->unhandled_exception($_);
+ };
}
-=head2 Global variables
-
-=head3 $to_api_mapping
-
-=cut
-
-our $to_api_mapping = {
- issue_id => 'checkout_id',
- borrowernumber => 'patron_id',
- itemnumber => 'item_id',
- date_due => 'due_date',
- branchcode => 'library_id',
- returndate => 'checkin_date',
- lastreneweddate => 'last_renewed_date',
- issuedate => 'checkout_date',
- notedate => 'note_date',
-};
-
-=head3 $to_model_mapping
-
-=cut
-
-our $to_model_mapping = {
- checkout_id => 'issue_id',
- patron_id => 'borrowernumber',
- item_id => 'itemnumber',
- due_date => 'date_due',
- library_id => 'branchcode',
- checkin_date => 'returndate',
- last_renewed_date => 'lastreneweddate',
- checkout_date => 'issuedate',
- note_date => 'notedate',
- checked_in => undef,
-};
-
1;