mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-17 01:42:19 +00:00
Fix issue mentioned in https://github.com/firefly-iii/firefly-iii/pull/8020
This commit is contained in:
@@ -31,7 +31,6 @@ use Illuminate\Console\Command;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class CorrectionSkeleton
|
* Class CorrectionSkeleton
|
||||||
* TODO DONT FORGET TO ADD THIS TO THE DOCKER BUILD
|
|
||||||
*/
|
*/
|
||||||
class TriggerCreditCalculation extends Command
|
class TriggerCreditCalculation extends Command
|
||||||
{
|
{
|
||||||
|
@@ -185,6 +185,15 @@ class CreditRecalculateService
|
|||||||
app('log')->debug(sprintf('Now processing account #%d ("%s")', $account->id, $account->name));
|
app('log')->debug(sprintf('Now processing account #%d ("%s")', $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');
|
||||||
|
$openingBalance = $this->repository->getOpeningBalance($account);
|
||||||
|
if (null !== $openingBalance) {
|
||||||
|
app('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);
|
||||||
|
}
|
||||||
|
}
|
||||||
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
|
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
|
||||||
$leftOfDebt = app('steam')->positive($startOfDebt);
|
$leftOfDebt = app('steam')->positive($startOfDebt);
|
||||||
|
|
||||||
@@ -196,9 +205,6 @@ class CreditRecalculateService
|
|||||||
// 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);
|
||||||
|
|
||||||
// get direction of liability:
|
|
||||||
$direction = (string)$this->repository->getMetaValue($account, 'liability_direction');
|
|
||||||
|
|
||||||
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)
|
||||||
@@ -217,6 +223,42 @@ class CreditRecalculateService
|
|||||||
app('log')->debug(sprintf('Done processing account #%d ("%s")', $account->id, $account->name));
|
app('log')->debug(sprintf('Done processing account #%d ("%s")', $account->id, $account->name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If account direction is "debit" ("I owe this amount") the opening balance must always be AWAY from the account:
|
||||||
|
*
|
||||||
|
* @param Account $account
|
||||||
|
* @param TransactionJournal $openingBalance
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function validateOpeningBalance(Account $account, TransactionJournal $openingBalance)
|
||||||
|
{
|
||||||
|
/** @var Transaction $source */
|
||||||
|
$source = $openingBalance->transactions()->where('amount', '<', 0)->first();
|
||||||
|
/** @var Transaction $dest */
|
||||||
|
$dest = $openingBalance->transactions()->where('amount', '>', 0)->first();
|
||||||
|
if ((int)$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)));
|
||||||
|
app('log')->debug(sprintf('Destination amount "%s" is now "%s"', $dest->amount, app('steam')->negative($dest->amount)));
|
||||||
|
$source->amount = app('steam')->positive($source->amount);
|
||||||
|
$dest->amount = app('steam')->negative($source->amount);
|
||||||
|
var_dump($source->foreign_amount);
|
||||||
|
if (null !== $source->foreign_amount && '' !== $source->foreign_amount) {
|
||||||
|
$source->foreign_amount = app('steam')->positive($source->foreign_amount);
|
||||||
|
app('log')->debug(sprintf('Source foreign amount "%s" is now "%s"', $source->foreign_amount, app('steam')->positive($source->foreign_amount)));
|
||||||
|
}
|
||||||
|
if (null !== $dest->foreign_amount && '' !== $dest->foreign_amount) {
|
||||||
|
$dest->foreign_amount = app('steam')->negative($dest->foreign_amount);
|
||||||
|
app('log')->debug(sprintf('Destination amount "%s" is now "%s"', $dest->foreign_amount, app('steam')->negative($dest->foreign_amount)));
|
||||||
|
}
|
||||||
|
$source->save();
|
||||||
|
$dest->save();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
app('log')->debug('Opening balance is valid');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Account $account
|
* @param Account $account
|
||||||
* @param string $direction
|
* @param string $direction
|
||||||
|
Reference in New Issue
Block a user