From e89c5bfed6a75d7222c8328e2b4371066ee06ff3 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 29 Oct 2019 12:26:56 -0300 Subject: [PATCH] Bug 14697: Introduce Koha::Checkouts::ReturnClaim(s) This patch introduces the Koha::Object(s)-derived classes for handling return claims. Tests are added to cover the overloaded ->store method, and domain-specific exceptions are added as well. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Checkouts/ReturnClaim.t => SUCCESS: Tests pass! Signed-off-by: Tomas Cohen Arazi Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Lisette Scheer Signed-off-by: Martin Renvoize --- Koha/Checkouts/ReturnClaim.pm | 113 ++++++++++++++++++++ Koha/Checkouts/ReturnClaims.pm | 86 +++++++++++++++ Koha/Exceptions/Checkouts/ReturnClaims.pm | 50 +++++++++ t/db_dependent/Koha/Checkouts/ReturnClaim.t | 105 ++++++++++++++++++ 4 files changed, 354 insertions(+) create mode 100644 Koha/Checkouts/ReturnClaim.pm create mode 100644 Koha/Checkouts/ReturnClaims.pm create mode 100644 Koha/Exceptions/Checkouts/ReturnClaims.pm create mode 100644 t/db_dependent/Koha/Checkouts/ReturnClaim.t diff --git a/Koha/Checkouts/ReturnClaim.pm b/Koha/Checkouts/ReturnClaim.pm new file mode 100644 index 0000000000..9c49bbf8c4 --- /dev/null +++ b/Koha/Checkouts/ReturnClaim.pm @@ -0,0 +1,113 @@ +package Koha::Checkouts::ReturnClaim; + +# Copyright ByWater Solutions 2019 +# +# 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 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. + +use Modern::Perl; + +use base qw(Koha::Object); + +use Koha::Checkouts; +use Koha::Exceptions::Checkouts::ReturnClaims; +use Koha::Old::Checkouts; +use Koha::Patrons; + +=head1 NAME + +Koha::Checkouts::ReturnClaim - Koha ReturnClaim object class + +=head1 API + +=head2 Class methods + +=cut + +=head3 store + + my $return_claim = Koha::Checkout::ReturnClaim->new($args)->store; + +Overloaded I method that validates the attributes and raises relevant +exceptions as needed. + +=cut + +sub store { + my ( $self ) = @_; + + unless ( $self->created_by ) { + Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy->throw(); + } + + return $self->SUPER::store; +} + +=head3 checkout + +=cut + +sub checkout { + my ($self) = @_; + + my $checkout = Koha::Checkouts->find( $self->issue_id ) + || Koha::Old::Checkouts->find( $self->issue_id ); + + return $checkout; +} + +=head3 patron + +=cut + +sub patron { + my ( $self ) = @_; + + my $borrower = $self->_result->borrowernumber; + return Koha::Patron->_new_from_dbic( $borrower ) if $borrower; +} + +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::Checkouts::ReturnClaim object +on the API. + +=cut + +sub to_api_mapping { + return { + id => 'claim_id', + itemnumber => 'item_id', + borrowernumber => 'patron_id', + }; +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ReturnClaim'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/Checkouts/ReturnClaims.pm b/Koha/Checkouts/ReturnClaims.pm new file mode 100644 index 0000000000..414416ba68 --- /dev/null +++ b/Koha/Checkouts/ReturnClaims.pm @@ -0,0 +1,86 @@ +package Koha::Checkouts::ReturnClaims; + +# Copyright ByWater Solutions 2019 +# +# 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 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. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::Checkouts::ReturnClaim; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Checkouts::ReturnClaims - Koha ReturnClaim object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 unresolved + +=cut + +sub unresolved { + my ($self) = @_; + + my $results = $self->_resultset()->search_rs( { resolved_on => undef } ); + + return Koha::Checkouts::ReturnClaims->_new_from_dbic( $results ); +} + +=head3 resolved + +=cut + +sub resolved { + my ($self) = @_; + + my $results = $self->_resultset()->search_rs( { resolved_on => { '!=' => undef } } ); + + return Koha::Checkouts::ReturnClaims->_new_from_dbic( $results ); +} + +=head3 type + +=cut + +sub _type { + return 'ReturnClaim'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Checkouts::ReturnClaim'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/Exceptions/Checkouts/ReturnClaims.pm b/Koha/Exceptions/Checkouts/ReturnClaims.pm new file mode 100644 index 0000000000..27723af440 --- /dev/null +++ b/Koha/Exceptions/Checkouts/ReturnClaims.pm @@ -0,0 +1,50 @@ +package Koha::Exceptions::Checkouts::ReturnClaims; + +# 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 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. + +use Modern::Perl; + +use Koha::Exceptions::Exception; + +use Exception::Class ( + 'Koha::Exceptions::Checkouts::ReturnClaims' => { + isa => 'Koha::Exceptions::Exception', + description => 'Something went wrong!', + }, + 'Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy' => { + isa => 'Koha::Exceptions::Checkouts::ReturnClaims', + description => 'created_by is mandatory' + }, +); + +=head1 NAME + +Koha::Exceptions::Checkouts - Base class for Checkouts exceptions + +=head1 Exceptions + +=head2 Koha::Exceptions::Checkouts::ReturnClaims + +Generic return claim exception + +=head2 Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy + +Exception to be used when a return claim is requested to be store but +the 'created_by' param is not passed. + +=cut + +1; diff --git a/t/db_dependent/Koha/Checkouts/ReturnClaim.t b/t/db_dependent/Koha/Checkouts/ReturnClaim.t new file mode 100644 index 0000000000..ebb16b016f --- /dev/null +++ b/t/db_dependent/Koha/Checkouts/ReturnClaim.t @@ -0,0 +1,105 @@ +#!/usr/bin/perl + +# 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 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, see . + +use Modern::Perl; + +use Test::More tests => 1; +use Test::Exception; + +use Koha::Database; +use Koha::Checkouts::ReturnClaims; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest "store() tests" => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $librarian = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + my $item = $builder->build_sample_item; + + my $checkout = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { + borrowernumber => $patron->borrowernumber, + itemnumber => $item->itemnumber, + branchcode => $patron->branchcode + } + } + ); + + throws_ok + { Koha::Checkouts::ReturnClaim->new( + { + issue_id => $checkout->id, + itemnumber => $checkout->itemnumber, + borrowernumber => $checkout->borrowernumber, + notes => 'Some notes' + } + )->store } + 'Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy', + 'Exception thrown correctly'; + + is( Koha::Checkouts::ReturnClaims->search({ issue_id => $checkout->id })->count, 0, 'No claims stored' ); + + my $claim = Koha::Checkouts::ReturnClaim->new( + { + issue_id => $checkout->id, + itemnumber => $checkout->itemnumber, + borrowernumber => $checkout->borrowernumber, + notes => 'Some notes', + created_by => $librarian->borrowernumber + } + )->store; + + is( ref($claim), 'Koha::Checkouts::ReturnClaim', 'Object type is correct' ); + is( Koha::Checkouts::ReturnClaims->search( { issue_id => $checkout->id } )->count, 1, 'Claim stored on the DB'); + + { # hide useless warnings + local *STDERR; + open STDERR, '>', '/dev/null'; + throws_ok { + Koha::Checkouts::ReturnClaim->new( + { + issue_id => $checkout->id + 1000, + itemnumber => $checkout->itemnumber, + borrowernumber => $checkout->borrowernumber, + notes => 'Some notes', + created_by => $librarian->borrowernumber + } + )->store; + } + 'Koha::Exceptions::Object::FKConstraint', + 'An exception is thrown on invalid issue_id'; + close STDERR; + + is( + $@->broken_fk, + 'issue_id', + 'Exception field is correct' + ); + } + + $schema->storage->txn_rollback; +}; -- 2.39.5