From 74664afa682b79099f4e02eb569207f05c7f259a Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 2 Jun 2017 13:00:24 +0200 Subject: [PATCH] Was not able to remove opening balance. --- .../Account/AccountRepository.php | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 423efcd23e..122e8f7d5c 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -466,7 +466,12 @@ class AccountRepository implements AccountRepositoryInterface */ protected function storeInitialBalance(Account $account, array $data): TransactionJournal { - $amount = $data['openingBalance']; + $amount = strval($data['openingBalance']); + + if (bccomp($amount, '0') === 0) { + return new TransactionJournal; + } + $name = $data['name']; $currencyId = $data['currency_id']; $opposing = $this->storeOpposingAccount($name); @@ -487,12 +492,12 @@ class AccountRepository implements AccountRepositoryInterface $firstAccount = $account; $secondAccount = $opposing; $firstAmount = $amount; - $secondAmount = $amount * -1; + $secondAmount = bcmul($amount, '-1'); if ($data['openingBalance'] < 0) { $firstAccount = $opposing; $secondAccount = $account; - $firstAmount = $amount * -1; + $firstAmount = bcmul($amount, '-1'); $secondAmount = $amount; } @@ -606,9 +611,15 @@ class AccountRepository implements AccountRepositoryInterface protected function updateOpeningBalanceJournal(Account $account, TransactionJournal $journal, array $data): bool { $date = $data['openingBalanceDate']; - $amount = $data['openingBalance']; + $amount = strval($data['openingBalance']); $currencyId = intval($data['currency_id']); + if (bccomp($amount, '0') === 0) { + $journal->delete(); + + return true; + } + // update date: $journal->date = $date; $journal->transaction_currency_id = $currencyId; @@ -621,7 +632,7 @@ class AccountRepository implements AccountRepositoryInterface $transaction->save(); } if ($account->id != $transaction->account_id) { - $transaction->amount = $amount * -1; + $transaction->amount = bcmul($amount, '-1'); $transaction->save(); } } @@ -631,6 +642,7 @@ class AccountRepository implements AccountRepositoryInterface } + /** * @param array $data * @@ -638,9 +650,7 @@ class AccountRepository implements AccountRepositoryInterface */ protected function validOpeningBalanceData(array $data): bool { - if (isset($data['openingBalance']) && isset($data['openingBalanceDate']) - && bccomp(strval($data['openingBalance']), '0') !== 0 - ) { + if (isset($data['openingBalance']) && isset($data['openingBalanceDate'])) { Log::debug('Array has valid opening balance data.'); return true;