Bug 22982: (QA follow-up) Add behavior to Koha::Account::Line::apply and unit test

Signed-off-by: Nadine Pierre <nadine.pierre@inLibro.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
(cherry picked from commit f417c15e49)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Kyle M Hall 2019-05-28 07:54:58 -04:00 committed by Fridolin Somers
parent dfc86527ab
commit e09c4a2cb1
3 changed files with 93 additions and 1 deletions

View File

@ -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 );

View File

@ -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 );
}
}
});

View File

@ -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;
};