Remove null exceptions

This commit is contained in:
James Cole
2022-12-24 05:48:04 +01:00
parent 22a237d316
commit 5f6772260d
5 changed files with 45 additions and 24 deletions

View File

@@ -99,7 +99,7 @@ class CreditRecalculateService
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @throws FireflyException
*/
@@ -121,7 +121,7 @@ class CreditRecalculateService
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Account
* @throws FireflyException
@@ -132,8 +132,8 @@ class CreditRecalculateService
}
/**
* @param TransactionJournal $journal
* @param string $direction
* @param TransactionJournal $journal
* @param string $direction
*
* @return Account
* @throws FireflyException
@@ -154,7 +154,7 @@ class CreditRecalculateService
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Account
* @throws FireflyException
@@ -189,7 +189,7 @@ class CreditRecalculateService
}
/**
* @param Account $account
* @param Account $account
*/
private function processWorkAccount(Account $account): void
{
@@ -205,7 +205,7 @@ class CreditRecalculateService
$factory->crud($account, 'start_of_debt', $startOfDebt);
// get direction of liability:
$direction = (string) $this->repository->getMetaValue($account, 'liability_direction');
$direction = (string)$this->repository->getMetaValue($account, 'liability_direction');
// now loop all transactions (except opening balance and credit thing)
$transactions = $account->transactions()->get();
@@ -220,24 +220,36 @@ class CreditRecalculateService
}
/**
* @param Account $account
* @param string $direction
* @param Transaction $transaction
* @param string $amount
* @param Account $account
* @param string $direction
* @param Transaction $transaction
* @param string $amount
*
* @return string
*/
private function processTransaction(Account $account, string $direction, Transaction $transaction, string $amount): string
{
Log::debug(sprintf('Now in %s(#%d, %s)', __METHOD__, $transaction->id, $amount));
$journal = $transaction->transactionJournal;
$groupId = $journal->transaction_group_id;
$type = $journal->transactionType->type;
$journal = $transaction->transactionJournal;
$foreignCurrency = $transaction->foreignCurrency;
$accountCurrency = $this->repository->getAccountCurrency($account);
$groupId = $journal->transaction_group_id;
$type = $journal->transactionType->type;
Log::debug(sprintf('Account currency is #%d (%s)', $accountCurrency->id, $accountCurrency->code));
if ('' === $direction) {
Log::debug('Since direction is "", do nothing.');
return $amount;
}
// amount to use depends on the currency:
$usedAmount = $transaction->amount;
if (null !== $foreignCurrency && $foreignCurrency->id === $accountCurrency->id) {
$usedAmount = $transaction->foreign_amount;
Log::debug('Will now use foreign amount!');
}
Log::debug(sprintf('Processing group #%d, journal #%d of type "%s"', $journal->id, $groupId, $type));
@@ -246,8 +258,8 @@ class CreditRecalculateService
// to a credit-liability doesn't increase the amount (yet)
if (
$type === TransactionType::WITHDRAWAL
&& (int) $account->id === (int) $transaction->account_id
&& 1 === bccomp($transaction->amount, '0')
&& (int)$account->id === (int)$transaction->account_id
&& 1 === bccomp($usedAmount, '0')
&& 'credit' === $direction
) {
Log::debug(sprintf('Is withdrawal into credit liability #%d, does not influence the amount due.', $transaction->account_id));
@@ -258,8 +270,8 @@ class CreditRecalculateService
// likewise deposit into a credit debt does not change the amount
if (
$type === TransactionType::DEPOSIT
&& (int) $account->id === (int) $transaction->account_id
&& -1 === bccomp($transaction->amount, '0')
&& (int)$account->id === (int)$transaction->account_id
&& -1 === bccomp($usedAmount, '0')
&& 'credit' === $direction
) {
Log::debug(sprintf('Is deposit from liability #%d,does not influence the amount left.', $transaction->account_id));
@@ -268,7 +280,7 @@ class CreditRecalculateService
}
if (in_array($type, [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER], true)) {
$amount = bcadd($amount, bcmul($transaction->amount, '-1'));
$amount = bcadd($amount, bcmul($usedAmount, '-1'));
}
Log::debug(sprintf('Amount is now %s', $amount));
@@ -276,7 +288,7 @@ class CreditRecalculateService
}
/**
* @param Account|null $account
* @param Account|null $account
*/
public function setAccount(?Account $account): void
{
@@ -284,7 +296,7 @@ class CreditRecalculateService
}
/**
* @param TransactionGroup $group
* @param TransactionGroup $group
*/
public function setGroup(TransactionGroup $group): void
{