mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Auto commit for release 'branch-v6.2' on 2024-12-27
This commit is contained in:
@@ -165,7 +165,7 @@ class CreditRecalculateService
|
||||
$direction = (string) $this->repository->getMetaValue($account, 'liability_direction');
|
||||
$openingBalance = $this->repository->getOpeningBalance($account);
|
||||
if (null !== $openingBalance) {
|
||||
// Log::debug(sprintf('Found opening balance transaction journal #%d', $openingBalance->id));
|
||||
// Log::debug(sprintf('Found opening balance transaction journal #%d', $openingBalance->id));
|
||||
// if account direction is "debit" ("I owe this amount") the opening balance must always be AWAY from the account:
|
||||
if ('debit' === $direction) {
|
||||
$this->validateOpeningBalance($account, $openingBalance);
|
||||
@@ -173,7 +173,7 @@ class CreditRecalculateService
|
||||
}
|
||||
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
|
||||
$leftOfDebt = app('steam')->positive($startOfDebt);
|
||||
// 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)));
|
||||
// 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);
|
||||
@@ -181,7 +181,7 @@ class CreditRecalculateService
|
||||
// amount is positive or negative, doesn't matter.
|
||||
$factory->crud($account, 'start_of_debt', $startOfDebt);
|
||||
|
||||
// Log::debug(sprintf('Debt direction is "%s"', $direction));
|
||||
// Log::debug(sprintf('Debt direction is "%s"', $direction));
|
||||
|
||||
// now loop all transactions (except opening balance and credit thing)
|
||||
$transactions = $account->transactions()
|
||||
@@ -190,11 +190,11 @@ class CreditRecalculateService
|
||||
->get(['transactions.*'])
|
||||
;
|
||||
$total = $transactions->count();
|
||||
// Log::debug(sprintf('Found %d transaction(s) to process.', $total));
|
||||
// Log::debug(sprintf('Found %d transaction(s) to process.', $total));
|
||||
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
// Log::debug(sprintf('[%d/%d] Processing transaction.', $index + 1, $total));
|
||||
// Log::debug(sprintf('[%d/%d] Processing transaction.', $index + 1, $total));
|
||||
$leftOfDebt = $this->processTransaction($account, $direction, $transaction, $leftOfDebt);
|
||||
}
|
||||
$factory->crud($account, 'current_debt', $leftOfDebt);
|
||||
@@ -255,10 +255,10 @@ class CreditRecalculateService
|
||||
$foreignCurrency = $transaction->foreignCurrency;
|
||||
$accountCurrency = $this->repository->getAccountCurrency($account);
|
||||
$type = $journal->transactionType->type;
|
||||
// Log::debug(sprintf('Left of debt is: %s', app('steam')->bcround($leftOfDebt, 2)));
|
||||
// Log::debug(sprintf('Left of debt is: %s', app('steam')->bcround($leftOfDebt, 2)));
|
||||
|
||||
if ('' === $direction) {
|
||||
// Log::warning('Direction is empty, so do nothing.');
|
||||
// Log::warning('Direction is empty, so do nothing.');
|
||||
|
||||
return $leftOfDebt;
|
||||
}
|
||||
@@ -267,7 +267,7 @@ class CreditRecalculateService
|
||||
|
||||
return $leftOfDebt;
|
||||
}
|
||||
// Log::debug(sprintf('Liability direction is "%s"', $direction));
|
||||
// Log::debug(sprintf('Liability direction is "%s"', $direction));
|
||||
|
||||
// amount to use depends on the currency:
|
||||
$usedAmount = $this->getAmountToUse($transaction, $accountCurrency, $foreignCurrency);
|
||||
@@ -277,88 +277,77 @@ class CreditRecalculateService
|
||||
|
||||
if ($isSameAccount && $isCredit && $this->isWithdrawalIn($usedAmount, $type)) { // case 1
|
||||
$usedAmount = app('steam')->positive($usedAmount);
|
||||
$result = bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 1 (withdrawal into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
|
||||
return $result;
|
||||
return bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 1 (withdrawal into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
|
||||
if ($isSameAccount && $isCredit && $this->isWithdrawalOut($usedAmount, $type)) { // case 2
|
||||
$usedAmount = app('steam')->positive($usedAmount);
|
||||
$result = bcsub($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 2 (withdrawal away from liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
|
||||
return $result;
|
||||
return bcsub($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 2 (withdrawal away from liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
|
||||
if ($isSameAccount && $isCredit && $this->isDepositOut($usedAmount, $type)) { // case 3
|
||||
$usedAmount = app('steam')->positive($usedAmount);
|
||||
$result = bcsub($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 3 (deposit away from liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
|
||||
return $result;
|
||||
return bcsub($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 3 (deposit away from liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
|
||||
if ($isSameAccount && $isCredit && $this->isDepositIn($usedAmount, $type)) { // case 4
|
||||
$usedAmount = app('steam')->positive($usedAmount);
|
||||
$result = bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 4 (deposit into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
|
||||
return $result;
|
||||
return bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 4 (deposit into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
if ($isSameAccount && $isCredit && $this->isTransferIn($usedAmount, $type)) { // case 5
|
||||
$usedAmount = app('steam')->positive($usedAmount);
|
||||
$result = bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 5 (transfer into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
|
||||
return $result;
|
||||
return bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 5 (transfer into credit liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
if ($isSameAccount && $isDebit && $this->isWithdrawalIn($usedAmount, $type)) { // case 6
|
||||
$usedAmount = app('steam')->positive($usedAmount);
|
||||
$result = bcsub($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 6 (withdrawal into debit liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
|
||||
return $result;
|
||||
return bcsub($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 6 (withdrawal into debit liability): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
if ($isSameAccount && $isDebit && $this->isDepositOut($usedAmount, $type)) { // case 7
|
||||
$usedAmount = app('steam')->positive($usedAmount);
|
||||
$result = bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 7 (deposit away from liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
|
||||
return $result;
|
||||
return bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 7 (deposit away from liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
if ($isSameAccount && $isDebit && $this->isWithdrawalOut($usedAmount, $type)) { // case 8
|
||||
$usedAmount = app('steam')->positive($usedAmount);
|
||||
$result = bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 8 (withdrawal away from liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
|
||||
return $result;
|
||||
return bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case 8 (withdrawal away from liability): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
|
||||
if ($isSameAccount && $isDebit && $this->isTransferIn($usedAmount, $type)) { // case 9
|
||||
$usedAmount = app('steam')->positive($usedAmount);
|
||||
$result = bcsub($leftOfDebt, $usedAmount);
|
||||
// 2024-10-05, #9225 this used to say you would owe more, but a transfer INTO a debit from wherever means you owe LESS.
|
||||
// Log::debug(sprintf('Case 9 (transfer into debit liability, means you owe LESS): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
|
||||
return $result;
|
||||
return bcsub($leftOfDebt, $usedAmount);
|
||||
// 2024-10-05, #9225 this used to say you would owe more, but a transfer INTO a debit from wherever means you owe LESS.
|
||||
// Log::debug(sprintf('Case 9 (transfer into debit liability, means you owe LESS): %s - %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
if ($isSameAccount && $isDebit && $this->isTransferOut($usedAmount, $type)) { // case 10
|
||||
$usedAmount = app('steam')->positive($usedAmount);
|
||||
$result = bcadd($leftOfDebt, $usedAmount);
|
||||
// 2024-10-05, #9225 this used to say you would owe less, but a transfer OUT OF a debit from wherever means you owe MORE.
|
||||
// Log::debug(sprintf('Case 10 (transfer out of 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;
|
||||
return bcadd($leftOfDebt, $usedAmount);
|
||||
// 2024-10-05, #9225 this used to say you would owe less, but a transfer OUT OF a debit from wherever means you owe MORE.
|
||||
// Log::debug(sprintf('Case 10 (transfer out of debit liability, means you owe MORE): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
|
||||
// in any other case, remove amount from left of debt.
|
||||
if (in_array($type, [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER], true)) {
|
||||
$usedAmount = app('steam')->negative($usedAmount);
|
||||
$result = bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case X (all other cases): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
|
||||
return $result;
|
||||
return bcadd($leftOfDebt, $usedAmount);
|
||||
// Log::debug(sprintf('Case X (all other cases): %s + %s = %s', app('steam')->bcround($leftOfDebt, 2), app('steam')->bcround($usedAmount, 2), app('steam')->bcround($result, 2)));
|
||||
}
|
||||
|
||||
Log::warning(sprintf('[-1] Catch-all, should not happen. Left of debt = %s', app('steam')->bcround($leftOfDebt, 2)));
|
||||
@@ -369,10 +358,10 @@ class CreditRecalculateService
|
||||
private function getAmountToUse(Transaction $transaction, TransactionCurrency $accountCurrency, ?TransactionCurrency $foreignCurrency): string
|
||||
{
|
||||
$usedAmount = $transaction->amount;
|
||||
// Log::debug(sprintf('Amount of transaction is %s', app('steam')->bcround($usedAmount, 2)));
|
||||
// Log::debug(sprintf('Amount of transaction is %s', app('steam')->bcround($usedAmount, 2)));
|
||||
if (null !== $foreignCurrency && $foreignCurrency->id === $accountCurrency->id) {
|
||||
$usedAmount = $transaction->foreign_amount;
|
||||
// Log::debug(sprintf('Overruled by foreign amount. Amount of transaction is now %s', app('steam')->bcround($usedAmount, 2)));
|
||||
// Log::debug(sprintf('Overruled by foreign amount. Amount of transaction is now %s', app('steam')->bcround($usedAmount, 2)));
|
||||
}
|
||||
|
||||
return $usedAmount;
|
||||
|
Reference in New Issue
Block a user