diff --git a/app/Services/Internal/Support/CreditRecalculateService.php b/app/Services/Internal/Support/CreditRecalculateService.php index cea77d8518..034052e543 100644 --- a/app/Services/Internal/Support/CreditRecalculateService.php +++ b/app/Services/Internal/Support/CreditRecalculateService.php @@ -95,7 +95,7 @@ class CreditRecalculateService $destination = $this->getDestinationAccount($journal); // destination or source must be liability. - $valid = config('firefly.valid_liabilities'); + $valid = config('firefly.valid_liabilities'); if (in_array($destination->accountType->type, $valid, true)) { $this->work[] = $destination; } @@ -118,7 +118,7 @@ class CreditRecalculateService private function getAccountByDirection(TransactionJournal $journal, string $direction): Account { /** @var null|Transaction $transaction */ - $transaction = $journal->transactions()->where('amount', $direction, '0')->first(); + $transaction = $journal->transactions()->where('amount', $direction, '0')->first(); if (null === $transaction) { throw new FireflyException(sprintf('Cannot find "%s"-transaction of journal #%d', $direction, $journal->id)); } @@ -161,7 +161,7 @@ class CreditRecalculateService app('log')->debug(sprintf('Now processing account #%d ("%s"). All amounts with 2 decimals!', $account->id, $account->name)); // get opening balance (if present) $this->repository->setUser($account->user); - $direction = (string)$this->repository->getMetaValue($account, 'liability_direction'); + $direction = (string) $this->repository->getMetaValue($account, 'liability_direction'); $openingBalance = $this->repository->getOpeningBalance($account); if (null !== $openingBalance) { app('log')->debug(sprintf('Found opening balance transaction journal #%d', $openingBalance->id)); @@ -170,12 +170,12 @@ class CreditRecalculateService $this->validateOpeningBalance($account, $openingBalance); } } - $startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0'; - $leftOfDebt = app('steam')->positive($startOfDebt); + $startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0'; + $leftOfDebt = app('steam')->positive($startOfDebt); app('log')->debug(sprintf('Start of debt is "%s", so initial left of debt is "%s"', app('steam')->bcround($startOfDebt, 2), app('steam')->bcround($leftOfDebt, 2))); /** @var AccountMetaFactory $factory */ - $factory = app(AccountMetaFactory::class); + $factory = app(AccountMetaFactory::class); // amount is positive or negative, doesn't matter. $factory->crud($account, 'start_of_debt', $startOfDebt); @@ -183,12 +183,11 @@ class CreditRecalculateService app('log')->debug(sprintf('Debt direction is "%s"', $direction)); // now loop all transactions (except opening balance and credit thing) - $transactions = $account->transactions() - ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') - ->orderBy('transaction_journals.date', 'ASC') - ->get(['transactions.*']) - ; - $total = $transactions->count(); + $transactions = $account->transactions() + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->orderBy('transaction_journals.date', 'ASC') + ->get(['transactions.*']); + $total = $transactions->count(); app('log')->debug(sprintf('Found %d transaction(s) to process.', $total)); /** @var Transaction $transaction */ @@ -209,7 +208,7 @@ class CreditRecalculateService $source = $openingBalance->transactions()->where('amount', '<', 0)->first(); /** @var Transaction $dest */ - $dest = $openingBalance->transactions()->where('amount', '>', 0)->first(); + $dest = $openingBalance->transactions()->where('amount', '>', 0)->first(); if ($source->account_id !== $account->id) { app('log')->info(sprintf('Liability #%d has a reversed opening balance. Will fix this now.', $account->id)); app('log')->debug(sprintf('Source amount "%s" is now "%s"', $source->amount, app('steam')->positive($source->amount))); @@ -241,7 +240,15 @@ class CreditRecalculateService */ private function processTransaction(Account $account, string $direction, Transaction $transaction, string $leftOfDebt): string { - $journal = $transaction->transactionJournal; + $journal = $transaction->transactionJournal; + + // here be null pointers. + if (null === $journal) { + app('log')->warning(sprintf('Transaction #%d has no journal.', $transaction->id)); + return $leftOfDebt; + } + + $foreignCurrency = $transaction->foreignCurrency; $accountCurrency = $this->repository->getAccountCurrency($account); $type = $journal->transactionType->type; @@ -260,10 +267,10 @@ class CreditRecalculateService Log::debug(sprintf('Liability direction is "%s"', $direction)); // amount to use depends on the currency: - $usedAmount = $this->getAmountToUse($transaction, $accountCurrency, $foreignCurrency); - $isSameAccount = $account->id === $transaction->account_id; - $isDebit = 'debit' === $direction; - $isCredit = 'credit' === $direction; + $usedAmount = $this->getAmountToUse($transaction, $accountCurrency, $foreignCurrency); + $isSameAccount = $account->id === $transaction->account_id; + $isDebit = 'debit' === $direction; + $isCredit = 'credit' === $direction; if ($isSameAccount && $isCredit && $this->isWithdrawalIn($usedAmount, $type)) { // case 1 $usedAmount = app('steam')->positive($usedAmount);