From f417c15e499745a7cd35fd1c8c5ae79888857199 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 28 May 2019 07:54:58 -0400 Subject: [PATCH] Bug 22982: (QA follow-up) Add behavior to Koha::Account::Line::apply and unit test Signed-off-by: Nadine Pierre Signed-off-by: Martin Renvoize --- Koha/Account.pm | 1 + Koha/Account/Line.pm | 10 +++++ t/db_dependent/Koha/Account.t | 83 ++++++++++++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/Koha/Account.pm b/Koha/Account.pm index 5c8bcf30a7..4607fc2acb 100644 --- a/Koha/Account.pm +++ b/Koha/Account.pm @@ -108,6 +108,7 @@ sub pay { $fine->amountoutstanding($new_amountoutstanding)->store(); $balance_remaining = $balance_remaining - $amount_to_pay; + # Same logic exists in Koha::Account::Line::apply if ( $new_amountoutstanding == 0 && $fine->itemnumber && $fine->accounttype && ( $fine->accounttype eq 'L' ) ) { C4::Circulation::ReturnLostItem( $self->{patron_id}, $fine->itemnumber ); diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index 54ca0a3063..0df5a7e3c2 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -221,6 +221,16 @@ sub apply { $self->amountoutstanding( $available_credit * -1 )->store; $debit->amountoutstanding( $owed - $amount_to_cancel )->store; + + # Same logic exists in Koha::Account::pay + if ( $debit->amountoutstanding == 0 + && $debit->itemnumber + && $debit->accounttype + && $debit->accounttype eq 'L' ) + { + C4::Circulation::ReturnLostItem( $self->borrowernumber, $debit->itemnumber ); + } + } }); diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t index d376c58416..735a7c950a 100755 --- a/t/db_dependent/Koha/Account.t +++ b/t/db_dependent/Koha/Account.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 10; +use Test::More tests => 11; use Test::MockModule; use Test::Exception; @@ -730,3 +730,84 @@ subtest 'pay() handles lost items when paying by amount ( not specifying the los $schema->storage->txn_rollback; }; + +subtest 'Koha::Account::Line::apply() handles lost items' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $account = $patron->account; + + my $context = Test::MockModule->new('C4::Context'); + $context->mock( 'userenv', { branch => $library->id } ); + + my $biblio = $builder->build_sample_biblio(); + my $item = + $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); + + my $checkout = Koha::Checkout->new( + { + borrowernumber => $patron->id, + itemnumber => $item->id, + date_due => \'NOW()', + branchcode => $patron->branchcode, + issuedate => \'NOW()', + } + )->store(); + + $item->itemlost('1')->store(); + + my $debit = Koha::Account::Line->new( + { + issue_id => $checkout->id, + borrowernumber => $patron->id, + itemnumber => $item->id, + date => \'NOW()', + accounttype => 'L', + interface => 'cli', + amount => '1', + amountoutstanding => '1', + } + )->store(); + + my $credit = Koha::Account::Line->new( + { + borrowernumber => $patron->id, + date => '1900-01-01', + amount => "-0.500000", + amountoutstanding => "-0.500000", + interface => 'commandline' + } + )->store(); + my $debits = $account->outstanding_debits; + $credit->apply({ debits => $debits }); + + $debit = Koha::Account::Lines->find( $debit->id ); + is( $debit->amountoutstanding, '0.500000', 'Account line was paid down by half' ); + + $checkout = Koha::Checkouts->find( $checkout->id ); + ok( $checkout, 'Item still checked out to patron' ); + + $credit = Koha::Account::Line->new( + { + borrowernumber => $patron->id, + date => '1900-01-01', + amount => "-0.500000", + amountoutstanding => "-0.500000", + interface => 'commandline' + } + )->store(); + $debits = $account->outstanding_debits; + $credit->apply({ debits => $debits }); + + $debit = Koha::Account::Lines->find( $debit->id ); + is( $debit->amountoutstanding, '0.000000', 'Account line was paid down by half' ); + + $checkout = Koha::Checkouts->find( $checkout->id ); + ok( !$checkout, 'Item was removed from patron account' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5