@@ -, +, @@ Koha::Account::Line::apply and unit test --- Koha/Account.pm | 1 + Koha/Account/Line.pm | 10 +++++ t/db_dependent/Koha/Account.t | 83 ++++++++++++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 1 deletion(-) --- a/Koha/Account.pm +++ a/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 ); --- a/Koha/Account/Line.pm +++ a/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 ); + } + } }); --- a/t/db_dependent/Koha/Account.t +++ a/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; +}; --