From f08de08858f570fcf836051f490cf661142e2388 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 29 Oct 2019 12:28:36 -0300 Subject: [PATCH] Bug 14697: Add Koha::Checkout->claim_returned Adds the ability to generate a return claim from a Checkout object. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Lisette Scheer Signed-off-by: Martin Renvoize --- Koha/Checkout.pm | 56 ++++++++++++- t/db_dependent/Circulation/ReturnClaims.t | 96 +++++++++++++++++++++++ 2 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 t/db_dependent/Circulation/ReturnClaims.t diff --git a/Koha/Checkout.pm b/Koha/Checkout.pm index 1cba41ea3f..f789de6d8d 100644 --- a/Koha/Checkout.pm +++ b/Koha/Checkout.pm @@ -21,9 +21,11 @@ package Koha::Checkout; use Modern::Perl; use Carp; +use DateTime; +use Try::Tiny; +use Koha::Checkouts::ReturnClaims; use Koha::Database; -use DateTime; use Koha::DateUtils; use Koha::Items; @@ -35,7 +37,7 @@ Koha::Checkout - Koha Checkout object class =head1 API -=head2 Class Methods +=head2 Class methods =cut @@ -109,6 +111,56 @@ sub to_api_mapping { }; } +=head3 claim_returned + +my $return_claim = $checkout->claim_returned(); + +=cut + +sub claim_returned { + my ( $self, $params ) = @_; + + my $charge_lost_fee = $params->{charge_lost_fee}; + + try { + $self->_result->result_source->schema->txn_do( + sub { + my $claim = Koha::Checkouts::ReturnClaim->new( + { + issue_id => $self->id, + itemnumber => $self->itemnumber, + borrowernumber => $self->borrowernumber, + notes => $params->{notes}, + created_by => $params->{created_by}, + created_on => dt_from_string, + } + )->store(); + + my $ClaimReturnedLostValue = C4::Context->preference('ClaimReturnedLostValue'); + C4::Items::ModItem( { itemlost => $ClaimReturnedLostValue }, undef, $self->itemnumber ); + + my $ClaimReturnedChargeFee = C4::Context->preference('ClaimReturnedChargeFee'); + $charge_lost_fee = + $ClaimReturnedChargeFee eq 'charge' ? 1 + : $ClaimReturnedChargeFee eq 'no_charge' ? 0 + : $charge_lost_fee; # $ClaimReturnedChargeFee eq 'ask' + C4::Circulation::LostItem( $self->itemnumber, 'claim_returned' ) if $charge_lost_fee; + + return $claim; + } + ); + } + catch { + if ( $_->isa('Koha::Exceptions::Exception') ) { + $_->rethrow(); + } + else { + # ? + Koha::Exceptions::Exception->throw( "Unhandled exception" ); + } + }; +} + =head2 Internal methods =head3 _type diff --git a/t/db_dependent/Circulation/ReturnClaims.t b/t/db_dependent/Circulation/ReturnClaims.t new file mode 100644 index 0000000000..5b094ef626 --- /dev/null +++ b/t/db_dependent/Circulation/ReturnClaims.t @@ -0,0 +1,96 @@ +#!/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::MockModule; +use Test::Warn; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +use C4::Circulation; + +# Mock userenv, used by AddIssue +my $branch; +my $manager_id; +my $context = Test::MockModule->new('C4::Context'); +$context->mock( + 'userenv', + sub { + return { + branch => $branch, + number => $manager_id, + firstname => "Adam", + surname => "Smaith" + }; + } +); + +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new(); +Koha::IssuingRules->search->delete; +my $rule = Koha::IssuingRule->new( + { + categorycode => '*', + itemtype => '*', + branchcode => '*', + issuelength => 1, + } +); +$rule->store(); + +$branch = $builder->build( { source => 'Branch' } )->{branchcode}; + +subtest 'Test Koha::Checkout::claim_returned' => sub { + plan tests => 6; + + t::lib::Mocks::mock_preference( 'ClaimReturnedLostValue', 1 ); + my $biblio = $builder->build_object( { class => 'Koha::Biblios' } ); + my $item = $builder->build_object( + { + class => 'Koha::Items', + value => { + biblionumber => $biblio->biblionumber, + notforloan => 0, + itemlost => 0, + withdrawn => 0, + } + } + ); + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $checkout = AddIssue( $patron->unblessed, $item->barcode ); + + my $claim = $checkout->claim_returned( + { + created_by => $patron->id, + notes => "Test note", + } + ); + + is( $claim->issue_id, $checkout->id, "Claim issue id matches" ); + is( $claim->itemnumber, $item->id, "Claim itemnumber matches" ); + is( $claim->borrowernumber, $patron->id, "Claim borrowernumber matches" ); + is( $claim->notes, "Test note", "Claim notes match" ); + is( $claim->created_by, $patron->id, "Claim created_by matches" ); + ok( $claim->created_on, "Claim created_on is set" ); +}; + +$schema->storage->txn_rollback; -- 2.39.5