James Cole
2024-01-06 14:40:06 +01:00
parent 935634e487
commit c19b36a391

View File

@@ -33,6 +33,7 @@ use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\Facades\Log;
/**
* Class CreditRecalculateService
@@ -195,8 +196,7 @@ class CreditRecalculateService
$transactions = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'ASC')
->get(['transactions.*'])
;
->get(['transactions.*']);
$total = $transactions->count();
app('log')->debug(sprintf('Found %d transaction(s) to process.', $total));
@@ -267,6 +267,7 @@ class CreditRecalculateService
return $leftOfDebt;
}
Log::debug(sprintf('Liability direction is "%s"', $direction));
// amount to use depends on the currency:
$usedAmount = $this->getAmountToUse($transaction, $accountCurrency, $foreignCurrency);
@@ -334,6 +335,20 @@ class CreditRecalculateService
return $result;
}
if ($isSameAccount && $isDebit && $this->isTransferIn($usedAmount, $type)) { // case 9
$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
$usedAmount = app('steam')->positive($usedAmount);
$result = bcsub($leftOfDebt, $usedAmount);
app('log')->debug(sprintf('Case 5 (transfer out of 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;
}
// 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);
@@ -400,7 +415,7 @@ class CreditRecalculateService
* because the person is paying us back.
*
* case 7
* if it's a credit ("I am owed") this increases the amount due.
* if it's a debit ("I owe") this increases the amount due.
* because we are borrowing more money.
*/
private function isDepositOut(string $amount, string $transactionType): bool
@@ -423,9 +438,25 @@ class CreditRecalculateService
* case 5: transfer into loan (from other loan).
* if it's a credit ("I am owed") this increases the amount due,
* because the person has to pay more back.
*
* case 8: transfer into loan (from other loan).
* if it's a debit ("I owe") this decreases the amount due.
* because the person has to pay more back.
*/
private function isTransferIn(string $amount, string $transactionType): bool
{
return TransactionType::TRANSFER === $transactionType && 1 === bccomp($amount, '0');
}
/**
* it's a transfer out of loan (from other loan)
*
* case 9
* if it's a debit ("I owe") this decreases the amount due.
* because we remove money from the amount left to owe
*/
private function isTransferOut(string $amount, string $transactionType): bool
{
return TransactionType::DEPOSIT === $transactionType && -1 === bccomp($amount, '0');
}
}