James Cole
2024-01-06 14:44:50 +01:00
parent c19b36a391
commit f615b9c252

View File

@@ -105,7 +105,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;
}
@@ -128,7 +128,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));
}
@@ -180,12 +180,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);
@@ -193,11 +193,12 @@ 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 */
@@ -218,7 +219,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)));
@@ -270,10 +271,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);
@@ -339,6 +340,7 @@ class CreditRecalculateService
$usedAmount = app('steam')->positive($usedAmount);
$result = bcadd($leftOfDebt, $usedAmount);
app('log')->debug(sprintf('Case 9 (transfer into debit liability, means you owe more): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
return $result;
}
if ($isSameAccount && $isDebit && $this->isTransferOut($usedAmount, $type)) { // case 10