mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 16:57:09 +00:00
Add some piggy bank events.
This commit is contained in:
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Factory;
|
namespace FireflyIII\Factory;
|
||||||
|
|
||||||
|
use FireflyIII\Events\Model\PiggyBank\ChangedAmount;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\PiggyBank;
|
use FireflyIII\Models\PiggyBank;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
@@ -147,8 +148,7 @@ class PiggyBankFactory
|
|||||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||||
->where('accounts.user_id', $this->user->id)
|
->where('accounts.user_id', $this->user->id)
|
||||||
->where('piggy_banks.id', $piggyBankId)
|
->where('piggy_banks.id', $piggyBankId)
|
||||||
->first(['piggy_banks.*'])
|
->first(['piggy_banks.*']);
|
||||||
;
|
|
||||||
if (null !== $piggyBank) {
|
if (null !== $piggyBank) {
|
||||||
return $piggyBank;
|
return $piggyBank;
|
||||||
}
|
}
|
||||||
@@ -172,8 +172,7 @@ class PiggyBankFactory
|
|||||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||||
->where('accounts.user_id', $this->user->id)
|
->where('accounts.user_id', $this->user->id)
|
||||||
->where('piggy_banks.name', $name)
|
->where('piggy_banks.name', $name)
|
||||||
->first(['piggy_banks.*'])
|
->first(['piggy_banks.*']);
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setOrder(PiggyBank $piggyBank, array $data): PiggyBank
|
private function setOrder(PiggyBank $piggyBank, array $data): PiggyBank
|
||||||
@@ -201,8 +200,7 @@ class PiggyBankFactory
|
|||||||
'objectGroups',
|
'objectGroups',
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
->orderBy('piggy_banks.order', 'ASC')->get(['piggy_banks.*'])
|
->orderBy('piggy_banks.order', 'ASC')->get(['piggy_banks.*']);
|
||||||
;
|
|
||||||
$current = 1;
|
$current = 1;
|
||||||
foreach ($set as $piggyBank) {
|
foreach ($set as $piggyBank) {
|
||||||
if ($piggyBank->order !== $current) {
|
if ($piggyBank->order !== $current) {
|
||||||
@@ -237,20 +235,45 @@ class PiggyBankFactory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @var array $info */
|
/** @var array $info */
|
||||||
foreach ($accounts as $info) {
|
foreach ($accounts as $info) {
|
||||||
$account = $this->accountRepository->find((int) ($info['account_id'] ?? 0));
|
$account = $this->accountRepository->find((int) ($info['account_id'] ?? 0));
|
||||||
if (null === $account) {
|
if (null === $account) {
|
||||||
|
Log::debug(sprintf('Account #%d not found, skipping.', (int) ($info['account_id'] ?? 0)));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (array_key_exists('current_amount', $info) && null !== $info['current_amount']) {
|
if (array_key_exists('current_amount', $info) && null !== $info['current_amount']) {
|
||||||
|
// an amount is set, first check out if there is a difference with the previous amount.
|
||||||
|
$previous = $toBeLinked[$account->id]['current_amount'] ?? '0';
|
||||||
|
$diff = bcsub($info['current_amount'], $previous);
|
||||||
|
|
||||||
|
// create event for difference.
|
||||||
|
if (0 !== bccomp($diff, '0')) {
|
||||||
|
Log::debug(sprintf('[a] Will save event for difference %s (previous value was %s)', $diff, $previous));
|
||||||
|
event(new ChangedAmount($piggyBank, $diff, null, null));
|
||||||
|
}
|
||||||
|
|
||||||
$toBeLinked[$account->id] = ['current_amount' => $info['current_amount']];
|
$toBeLinked[$account->id] = ['current_amount' => $info['current_amount']];
|
||||||
Log::debug(sprintf('[a] Will link account #%d with amount %s', $account->id, $info['current_amount']));
|
Log::debug(sprintf('[a] Will link account #%d with amount %s', $account->id, $info['current_amount']));
|
||||||
}
|
}
|
||||||
if (array_key_exists('current_amount', $info) && null === $info['current_amount']) {
|
if (array_key_exists('current_amount', $info) && null === $info['current_amount']) {
|
||||||
|
// an amount is set, first check out if there is a difference with the previous amount.
|
||||||
|
$previous = $toBeLinked[$account->id]['current_amount'] ?? '0';
|
||||||
|
$diff = bcsub('0', $previous);
|
||||||
|
|
||||||
|
// create event for difference.
|
||||||
|
if (0 !== bccomp($diff, '0')) {
|
||||||
|
Log::debug(sprintf('[b] Will save event for difference %s (previous value was %s)', $diff, $previous));
|
||||||
|
event(new ChangedAmount($piggyBank, $diff, null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
// no amount set, use previous amount or go to ZERO.
|
||||||
$toBeLinked[$account->id] = ['current_amount' => $toBeLinked[$account->id]['current_amount'] ?? '0'];
|
$toBeLinked[$account->id] = ['current_amount' => $toBeLinked[$account->id]['current_amount'] ?? '0'];
|
||||||
Log::debug(sprintf('[b] Will link account #%d with amount %s', $account->id, $toBeLinked[$account->id]['current_amount'] ?? '0'));
|
Log::debug(sprintf('[b] Will link account #%d with amount %s', $account->id, $toBeLinked[$account->id]['current_amount'] ?? '0'));
|
||||||
|
|
||||||
|
// create event:
|
||||||
|
Log::debug('linkToAccountIds: Trigger change for positive amount [b].');
|
||||||
|
event(new ChangedAmount($piggyBank, $toBeLinked[$account->id]['current_amount'], null, null));
|
||||||
}
|
}
|
||||||
if (!array_key_exists('current_amount', $info)) {
|
if (!array_key_exists('current_amount', $info)) {
|
||||||
$toBeLinked[$account->id] ??= [];
|
$toBeLinked[$account->id] ??= [];
|
||||||
@@ -258,6 +281,11 @@ class PiggyBankFactory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log::debug(sprintf('Link information: %s', json_encode($toBeLinked)));
|
Log::debug(sprintf('Link information: %s', json_encode($toBeLinked)));
|
||||||
|
if (0 !== count($toBeLinked)) {
|
||||||
$piggyBank->accounts()->sync($toBeLinked);
|
$piggyBank->accounts()->sync($toBeLinked);
|
||||||
}
|
}
|
||||||
|
if (0 === count($toBeLinked)) {
|
||||||
|
Log::warning('No accounts to link to piggy bank, will not change whatever is there now.');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user