Fix nullpointer

This commit is contained in:
James Cole
2024-10-27 10:49:55 +01:00
parent d89d46aaec
commit bb4f90d730

View File

@@ -95,7 +95,7 @@ class CreditRecalculateService
$destination = $this->getDestinationAccount($journal); $destination = $this->getDestinationAccount($journal);
// destination or source must be liability. // destination or source must be liability.
$valid = config('firefly.valid_liabilities'); $valid = config('firefly.valid_liabilities');
if (in_array($destination->accountType->type, $valid, true)) { if (in_array($destination->accountType->type, $valid, true)) {
$this->work[] = $destination; $this->work[] = $destination;
} }
@@ -118,7 +118,7 @@ class CreditRecalculateService
private function getAccountByDirection(TransactionJournal $journal, string $direction): Account private function getAccountByDirection(TransactionJournal $journal, string $direction): Account
{ {
/** @var null|Transaction $transaction */ /** @var null|Transaction $transaction */
$transaction = $journal->transactions()->where('amount', $direction, '0')->first(); $transaction = $journal->transactions()->where('amount', $direction, '0')->first();
if (null === $transaction) { if (null === $transaction) {
throw new FireflyException(sprintf('Cannot find "%s"-transaction of journal #%d', $direction, $journal->id)); 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)); app('log')->debug(sprintf('Now processing account #%d ("%s"). All amounts with 2 decimals!', $account->id, $account->name));
// get opening balance (if present) // get opening balance (if present)
$this->repository->setUser($account->user); $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); $openingBalance = $this->repository->getOpeningBalance($account);
if (null !== $openingBalance) { if (null !== $openingBalance) {
app('log')->debug(sprintf('Found opening balance transaction journal #%d', $openingBalance->id)); app('log')->debug(sprintf('Found opening balance transaction journal #%d', $openingBalance->id));
@@ -170,12 +170,12 @@ class CreditRecalculateService
$this->validateOpeningBalance($account, $openingBalance); $this->validateOpeningBalance($account, $openingBalance);
} }
} }
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0'; $startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
$leftOfDebt = app('steam')->positive($startOfDebt); $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))); 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 */ /** @var AccountMetaFactory $factory */
$factory = app(AccountMetaFactory::class); $factory = app(AccountMetaFactory::class);
// amount is positive or negative, doesn't matter. // amount is positive or negative, doesn't matter.
$factory->crud($account, 'start_of_debt', $startOfDebt); $factory->crud($account, 'start_of_debt', $startOfDebt);
@@ -183,12 +183,11 @@ class CreditRecalculateService
app('log')->debug(sprintf('Debt direction is "%s"', $direction)); app('log')->debug(sprintf('Debt direction is "%s"', $direction));
// now loop all transactions (except opening balance and credit thing) // now loop all transactions (except opening balance and credit thing)
$transactions = $account->transactions() $transactions = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'ASC') ->orderBy('transaction_journals.date', 'ASC')
->get(['transactions.*']) ->get(['transactions.*']);
; $total = $transactions->count();
$total = $transactions->count();
app('log')->debug(sprintf('Found %d transaction(s) to process.', $total)); app('log')->debug(sprintf('Found %d transaction(s) to process.', $total));
/** @var Transaction $transaction */ /** @var Transaction $transaction */
@@ -209,7 +208,7 @@ class CreditRecalculateService
$source = $openingBalance->transactions()->where('amount', '<', 0)->first(); $source = $openingBalance->transactions()->where('amount', '<', 0)->first();
/** @var Transaction $dest */ /** @var Transaction $dest */
$dest = $openingBalance->transactions()->where('amount', '>', 0)->first(); $dest = $openingBalance->transactions()->where('amount', '>', 0)->first();
if ($source->account_id !== $account->id) { 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')->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))); 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 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; $foreignCurrency = $transaction->foreignCurrency;
$accountCurrency = $this->repository->getAccountCurrency($account); $accountCurrency = $this->repository->getAccountCurrency($account);
$type = $journal->transactionType->type; $type = $journal->transactionType->type;
@@ -260,10 +267,10 @@ class CreditRecalculateService
Log::debug(sprintf('Liability direction is "%s"', $direction)); Log::debug(sprintf('Liability direction is "%s"', $direction));
// amount to use depends on the currency: // amount to use depends on the currency:
$usedAmount = $this->getAmountToUse($transaction, $accountCurrency, $foreignCurrency); $usedAmount = $this->getAmountToUse($transaction, $accountCurrency, $foreignCurrency);
$isSameAccount = $account->id === $transaction->account_id; $isSameAccount = $account->id === $transaction->account_id;
$isDebit = 'debit' === $direction; $isDebit = 'debit' === $direction;
$isCredit = 'credit' === $direction; $isCredit = 'credit' === $direction;
if ($isSameAccount && $isCredit && $this->isWithdrawalIn($usedAmount, $type)) { // case 1 if ($isSameAccount && $isCredit && $this->isWithdrawalIn($usedAmount, $type)) { // case 1
$usedAmount = app('steam')->positive($usedAmount); $usedAmount = app('steam')->positive($usedAmount);