From 854c7f090b35acf90ae54935691c9a55127e129d Mon Sep 17 00:00:00 2001 From: tieu1991 Date: Thu, 5 Oct 2023 12:36:37 -0400 Subject: [PATCH] Fix edge case amount due in liability New Case 8 for interest paid to expense account Signed-off-by: tieu1991 --- .../Support/CreditRecalculateService.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/Services/Internal/Support/CreditRecalculateService.php b/app/Services/Internal/Support/CreditRecalculateService.php index 2ca91024ab..bbe34fe403 100644 --- a/app/Services/Internal/Support/CreditRecalculateService.php +++ b/app/Services/Internal/Support/CreditRecalculateService.php @@ -404,6 +404,22 @@ class CreditRecalculateService app('log')->debug(sprintf('Case 7 (deposit away from liability): %s - %s = %s', $leftOfDebt, $usedAmount, $result)); return $result; } + // case 8 + // it's a withdrawal from this liability (to expense account). + // if it's a debit ("I owe this amount") this increase the amount due. + // because we are paying interest. + if ( + $type === TransactionType::WITHDRAWAL + && (int)$account->id === (int)$transaction->account_id + && -1 === bccomp($usedAmount, '0') + && 'debit' === $direction + ) { + $usedAmount = app('steam')->positive($usedAmount); + $result = bcadd($leftOfDebt, $usedAmount); + app('log')->debug(sprintf('Case 8 (withdrawal away from liability): %s + %s = %s', $leftOfDebt, $usedAmount, $result)); + return $result; + } + // in any other case, remove amount from left of debt. if (in_array($type, [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER], true)) {