From 246af608d3fffd618c79e2f0bee17184f1d35528 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 4 Jan 2018 22:44:47 +0100 Subject: [PATCH] Update repository to fix #972 --- .../Account/AccountRepository.php | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 3a6d0155bb..ae2fa4015e 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -567,13 +567,15 @@ class AccountRepository implements AccountRepositoryInterface */ protected function updateOpeningBalanceJournal(Account $account, TransactionJournal $journal, array $data): bool { - $date = $data['openingBalanceDate']; - $amount = strval($data['openingBalance']); - $currencyId = intval($data['currency_id']); + $date = $data['openingBalanceDate']; + $amount = strval($data['openingBalance']); + $negativeAmount = bcmul($amount, '-1'); + $currencyId = intval($data['currency_id']); - Log::debug(sprintf('Submitted amount for opening balance to update is %s', $amount)); + Log::debug(sprintf('Submitted amount for opening balance to update is "%s"', $amount)); if (0 === bccomp($amount, '0')) { + Log::notice(sprintf('Amount "%s" is zero, delete opening balance.', $amount)); $journal->delete(); return true; @@ -583,18 +585,18 @@ class AccountRepository implements AccountRepositoryInterface $journal->date = $date; $journal->transaction_currency_id = $currencyId; $journal->save(); + // update transactions: /** @var Transaction $transaction */ foreach ($journal->transactions()->get() as $transaction) { - if ($account->id === $transaction->account_id) { - Log::debug(sprintf('Will change transaction #%d amount from %s to %s', $transaction->id, $transaction->amount, $amount)); + if (intval($account->id) === intval($transaction->account_id)) { + Log::debug(sprintf('Will (eq) change transaction #%d amount from "%s" to "%s"', $transaction->id, $transaction->amount, $amount)); $transaction->amount = $amount; $transaction->transaction_currency_id = $currencyId; $transaction->save(); } - if ($account->id !== $transaction->account_id) { - $negativeAmount = bcmul($amount, '-1'); - Log::debug(sprintf('Will change transaction #%d amount from %s to %s', $transaction->id, $transaction->amount, $negativeAmount)); + if (!(intval($account->id) === intval($transaction->account_id))) { + Log::debug(sprintf('Will (neq) change transaction #%d amount from "%s" to "%s"', $transaction->id, $transaction->amount, $negativeAmount)); $transaction->amount = $negativeAmount; $transaction->transaction_currency_id = $currencyId; $transaction->save();