From 9c1705b2b2cce73a84dc681fdd5c5dc20a2b9877 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 9 Jan 2020 11:59:43 +0000 Subject: [PATCH] Bug 23442: Prevent payouts from being reduced Signed-off-by: Martin Renvoize Signed-off-by: Josef Moravec Signed-off-by: Martin Renvoize --- Koha/Account/Line.pm | 8 +++- .../prog/en/modules/members/boraccount.tt | 2 +- t/db_dependent/Koha/Account/Lines.t | 47 ++++++++++++++----- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index cecf0c26f0..bd86317bb4 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -304,6 +304,10 @@ sub reduce { Koha::Exceptions::Account::IsNotDebit->throw( error => 'Account line ' . $self->id . 'is not a debit' ); } + if ( $self->debit_type_code eq 'PAYOUT' ) { + Koha::Exceptions::Account::IsNotDebit->throw( + error => 'Account line ' . $self->id . 'is a payout' ); + } # Check for mandatory parameters my @mandatory = ( 'interface', 'reduction_type', 'amount' ); @@ -401,7 +405,8 @@ sub reduce { } ); - return $reduction->discard_changes; + $reduction->discard_changes; + return $reduction; } =head3 apply @@ -583,6 +588,7 @@ sub payout { } ); + $payout->discard_changes; return $payout; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/boraccount.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/boraccount.tt index 195db37642..7427d41e38 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/boraccount.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/boraccount.tt @@ -87,7 +87,7 @@ [% IF CAN_user_updatecharges_payout && account.is_credit && ( account.amountoutstanding < 0 ) %] [% END %] - [% IF CAN_user_updatecharges_refund && account.is_debit && ( account.amountoutstanding != account.amount ) && !(account.status == 'REFUNDED' ) %] + [% IF CAN_user_updatecharges_refund && account.is_debit && ( account.amountoutstanding != account.amount ) && !(account.status == 'REFUNDED') && !(account.debit_type_code == 'PAYOUT') %] [% END %] diff --git a/t/db_dependent/Koha/Account/Lines.t b/t/db_dependent/Koha/Account/Lines.t index 873cb38739..a2a12c16f0 100755 --- a/t/db_dependent/Koha/Account/Lines.t +++ b/t/db_dependent/Koha/Account/Lines.t @@ -895,7 +895,7 @@ subtest "void() tests" => sub { subtest "payout() tests" => sub { - plan tests => 17; + plan tests => 18; $schema->storage->txn_begin; @@ -948,9 +948,9 @@ subtest "payout() tests" => sub { )->store(); is( $account->balance(), -10, "Account balance is -10" ); - is( $debit1->amountoutstanding, + is( $debit1->amountoutstanding + 0, 10, 'Overdue fee has an amount outstanding of 10' ); - is( $credit1->amountoutstanding, + is( $credit1->amountoutstanding + 0, -20, 'Credit has an amount outstanding of -20' ); my $pay_params = { @@ -1017,12 +1017,14 @@ subtest "payout() tests" => sub { } ); - is( $payout->amount(), 10, "Payout amount is 10" ); - is( $payout->amountoutstanding(), 0, "Payout amountoutstanding is 0" ); - is( $account->balance(), 0, "Account balance is 0" ); - is( $debit1->amountoutstanding, + is( ref($payout), 'Koha::Account::Line', + '->payout() returns a Koha::Account::Line' ); + is( $payout->amount() + 0, 10, "Payout amount is 10" ); + is( $payout->amountoutstanding() + 0, 0, "Payout amountoutstanding is 0" ); + is( $account->balance() + 0, 0, "Account balance is 0" ); + is( $debit1->amountoutstanding + 0, 10, 'Overdue fee still has an amount outstanding of 10' ); - is( $credit1->amountoutstanding, + is( $credit1->amountoutstanding + 0, -10, 'Credit has an new amount outstanding of -10' ); is( $credit1->status(), 'PAID', "Credit has a new status of PAID" ); @@ -1031,7 +1033,7 @@ subtest "payout() tests" => sub { subtest "reduce() tests" => sub { - plan tests => 25; + plan tests => 27; $schema->storage->txn_begin; @@ -1142,12 +1144,14 @@ subtest "reduce() tests" => sub { # (Refund 5 on debt of 20) my $reduction = $debit1->reduce($reduce_params); + is( ref($reduction), 'Koha::Account::Line', + '->reduce() returns a Koha::Account::Line' ); is( $reduction->amount() * 1, -5, "Reduce amount is -5" ); is( $reduction->amountoutstanding() * 1, 0, "Reduce amountoutstanding is 0" ); is( $debit1->amountoutstanding() * 1, 15, "Debit amountoutstanding reduced by 5 to 15" ); - is( $account->balance() * 1, -5, "Account balance is -5" ); + is( $account->balance() * 1, -5, "Account balance is -5" ); is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" ); my $offsets = Koha::Account::Offsets->search( @@ -1160,8 +1164,9 @@ subtest "reduce() tests" => sub { # Zero offset created when zero outstanding # (Refund another 5 on paid debt of 20) - $credit1->apply( { debits => [ $debit1 ] } ); - is($debit1->amountoutstanding + 0, 0, 'Debit1 amountoutstanding reduced to 0'); + $credit1->apply( { debits => [$debit1] } ); + is( $debit1->amountoutstanding + 0, + 0, 'Debit1 amountoutstanding reduced to 0' ); $reduction = $debit1->reduce($reduce_params); is( $reduction->amount() * 1, -5, "Reduce amount is -5" ); is( $reduction->amountoutstanding() * 1, @@ -1182,7 +1187,23 @@ subtest "reduce() tests" => sub { $debit1->reduce($reduce_params); } 'Koha::Exceptions::ParameterTooHigh', -'->reduce cannot reduce mor than the original amount (combined reductions test)'; +'->reduce cannot reduce more than the original amount (combined reductions test)'; + + # Throw exception if attempting to reduce a payout + my $payout = $reduction->payout( + { + interface => 'intranet', + staff_id => $staff->borrowernumber, + branch => $branchcode, + payout_type => 'CASH', + amount => 5 + } + ); + throws_ok { + $payout->reduce($reduce_params); + } + 'Koha::Exceptions::Account::IsNotDebit', + '->reduce() cannot be used on a payout debit'; $schema->storage->txn_rollback; }; -- 2.39.5