mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-18 10:16:49 +00:00
Fix #
This commit is contained in:
@@ -46,15 +46,15 @@ class PiggyBankFactory
|
|||||||
{
|
{
|
||||||
use CreatesObjectGroups;
|
use CreatesObjectGroups;
|
||||||
|
|
||||||
public User $user;
|
public User $user;
|
||||||
private AccountRepositoryInterface $accountRepository;
|
private AccountRepositoryInterface $accountRepository;
|
||||||
private CurrencyRepositoryInterface $currencyRepository;
|
private CurrencyRepositoryInterface $currencyRepository;
|
||||||
private PiggyBankRepositoryInterface $piggyBankRepository;
|
private PiggyBankRepositoryInterface $piggyBankRepository;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
|
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
|
||||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||||
$this->piggyBankRepository = app(PiggyBankRepositoryInterface::class);
|
$this->piggyBankRepository = app(PiggyBankRepositoryInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,21 +72,21 @@ class PiggyBankFactory
|
|||||||
public function store(array $data): PiggyBank
|
public function store(array $data): PiggyBank
|
||||||
{
|
{
|
||||||
|
|
||||||
$piggyBankData = $data;
|
$piggyBankData = $data;
|
||||||
|
|
||||||
// unset some fields
|
// unset some fields
|
||||||
unset($piggyBankData['object_group_title'], $piggyBankData['transaction_currency_code'], $piggyBankData['transaction_currency_id'], $piggyBankData['accounts'], $piggyBankData['object_group_id'], $piggyBankData['notes']);
|
unset($piggyBankData['object_group_title'], $piggyBankData['transaction_currency_code'], $piggyBankData['transaction_currency_id'], $piggyBankData['accounts'], $piggyBankData['object_group_id'], $piggyBankData['notes']);
|
||||||
|
|
||||||
// validate amount:
|
// validate amount:
|
||||||
if (array_key_exists('target_amount', $piggyBankData) && '' === (string) $piggyBankData['target_amount']) {
|
if (array_key_exists('target_amount', $piggyBankData) && '' === (string)$piggyBankData['target_amount']) {
|
||||||
$piggyBankData['target_amount'] = '0';
|
$piggyBankData['target_amount'] = '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
$piggyBankData['start_date_tz'] = $piggyBankData['start_date']?->format('e');
|
$piggyBankData['start_date_tz'] = $piggyBankData['start_date']?->format('e');
|
||||||
$piggyBankData['target_date_tz'] = $piggyBankData['target_date']?->format('e');
|
$piggyBankData['target_date_tz'] = $piggyBankData['target_date']?->format('e');
|
||||||
$piggyBankData['account_id'] = null;
|
$piggyBankData['account_id'] = null;
|
||||||
$piggyBankData['transaction_currency_id'] = $this->getCurrency($data)->id;
|
$piggyBankData['transaction_currency_id'] = $this->getCurrency($data)->id;
|
||||||
$piggyBankData['order'] = 131337;
|
$piggyBankData['order'] = 131337;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
/** @var PiggyBank $piggyBank */
|
/** @var PiggyBank $piggyBank */
|
||||||
@@ -96,11 +96,11 @@ class PiggyBankFactory
|
|||||||
|
|
||||||
throw new FireflyException('400005: Could not store new piggy bank.', 0, $e);
|
throw new FireflyException('400005: Could not store new piggy bank.', 0, $e);
|
||||||
}
|
}
|
||||||
$piggyBank = $this->setOrder($piggyBank, $data);
|
$piggyBank = $this->setOrder($piggyBank, $data);
|
||||||
$this->linkToAccountIds($piggyBank, $data['accounts']);
|
$this->linkToAccountIds($piggyBank, $data['accounts']);
|
||||||
$this->piggyBankRepository->updateNote($piggyBank, $data['notes']);
|
$this->piggyBankRepository->updateNote($piggyBank, $data['notes']);
|
||||||
|
|
||||||
$objectGroupTitle = $data['object_group_title'] ?? '';
|
$objectGroupTitle = $data['object_group_title'] ?? '';
|
||||||
if ('' !== $objectGroupTitle) {
|
if ('' !== $objectGroupTitle) {
|
||||||
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
||||||
if ($objectGroup instanceof ObjectGroup) {
|
if ($objectGroup instanceof ObjectGroup) {
|
||||||
@@ -108,7 +108,7 @@ class PiggyBankFactory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// try also with ID
|
// try also with ID
|
||||||
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
|
$objectGroupId = (int)($data['object_group_id'] ?? 0);
|
||||||
if (0 !== $objectGroupId) {
|
if (0 !== $objectGroupId) {
|
||||||
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
||||||
if ($objectGroup instanceof ObjectGroup) {
|
if ($objectGroup instanceof ObjectGroup) {
|
||||||
@@ -116,7 +116,7 @@ class PiggyBankFactory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log::debug('Touch piggy bank');
|
Log::debug('Touch piggy bank');
|
||||||
$piggyBank->encrypted = false;
|
$piggyBank->encrypted = false;
|
||||||
$piggyBank->save();
|
$piggyBank->save();
|
||||||
$piggyBank->touch();
|
$piggyBank->touch();
|
||||||
|
|
||||||
@@ -127,12 +127,12 @@ class PiggyBankFactory
|
|||||||
{
|
{
|
||||||
// currency:
|
// currency:
|
||||||
$defaultCurrency = app('amount')->getNativeCurrency();
|
$defaultCurrency = app('amount')->getNativeCurrency();
|
||||||
$currency = null;
|
$currency = null;
|
||||||
if (array_key_exists('transaction_currency_code', $data)) {
|
if (array_key_exists('transaction_currency_code', $data)) {
|
||||||
$currency = $this->currencyRepository->findByCode((string) ($data['transaction_currency_code'] ?? ''));
|
$currency = $this->currencyRepository->findByCode((string)($data['transaction_currency_code'] ?? ''));
|
||||||
}
|
}
|
||||||
if (array_key_exists('transaction_currency_id', $data)) {
|
if (array_key_exists('transaction_currency_id', $data)) {
|
||||||
$currency = $this->currencyRepository->find((int) ($data['transaction_currency_id'] ?? 0));
|
$currency = $this->currencyRepository->find((int)($data['transaction_currency_id'] ?? 0));
|
||||||
}
|
}
|
||||||
$currency ??= $defaultCurrency;
|
$currency ??= $defaultCurrency;
|
||||||
|
|
||||||
@@ -141,8 +141,8 @@ class PiggyBankFactory
|
|||||||
|
|
||||||
public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
|
public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
|
||||||
{
|
{
|
||||||
$piggyBankId = (int) $piggyBankId;
|
$piggyBankId = (int)$piggyBankId;
|
||||||
$piggyBankName = (string) $piggyBankName;
|
$piggyBankName = (string)$piggyBankName;
|
||||||
if ('' === $piggyBankName && 0 === $piggyBankId) {
|
if ('' === $piggyBankName && 0 === $piggyBankId) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -152,8 +152,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;
|
||||||
}
|
}
|
||||||
@@ -177,14 +176,13 @@ 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
|
||||||
{
|
{
|
||||||
$this->resetOrder();
|
$this->resetOrder();
|
||||||
$order = $this->getMaxOrder() + 1;
|
$order = $this->getMaxOrder() + 1;
|
||||||
if (array_key_exists('order', $data)) {
|
if (array_key_exists('order', $data)) {
|
||||||
$order = $data['order'];
|
$order = $data['order'];
|
||||||
}
|
}
|
||||||
@@ -198,7 +196,7 @@ class PiggyBankFactory
|
|||||||
public function resetOrder(): void
|
public function resetOrder(): void
|
||||||
{
|
{
|
||||||
// TODO duplicate code
|
// TODO duplicate code
|
||||||
$set = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
$set = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||||
->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)
|
||||||
->with(
|
->with(
|
||||||
@@ -206,8 +204,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) {
|
||||||
@@ -221,7 +218,7 @@ class PiggyBankFactory
|
|||||||
|
|
||||||
private function getMaxOrder(): int
|
private function getMaxOrder(): int
|
||||||
{
|
{
|
||||||
return (int) $this->piggyBankRepository->getPiggyBanks()->max('order');
|
return (int)$this->piggyBankRepository->getPiggyBanks()->max('order');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,11 +228,12 @@ class PiggyBankFactory
|
|||||||
// collect current current_amount so the sync does not remove them.
|
// collect current current_amount so the sync does not remove them.
|
||||||
// TODO this is a tedious check. Feels like a hack.
|
// TODO this is a tedious check. Feels like a hack.
|
||||||
$toBeLinked = [];
|
$toBeLinked = [];
|
||||||
|
$oldSavedAmount = $this->piggyBankRepository->getCurrentAmount($piggyBank);
|
||||||
foreach ($piggyBank->accounts as $account) {
|
foreach ($piggyBank->accounts as $account) {
|
||||||
Log::debug(sprintf('Checking account #%d', $account->id));
|
Log::debug(sprintf('Checking account #%d', $account->id));
|
||||||
foreach ($accounts as $info) {
|
foreach ($accounts as $info) {
|
||||||
Log::debug(sprintf(' Checking other account #%d', $info['account_id']));
|
Log::debug(sprintf(' Checking other account #%d', $info['account_id']));
|
||||||
if ((int) $account->id === (int) $info['account_id']) {
|
if ((int)$account->id === (int)$info['account_id']) {
|
||||||
$toBeLinked[$account->id] = ['current_amount' => $account->pivot->current_amount ?? '0'];
|
$toBeLinked[$account->id] = ['current_amount' => $account->pivot->current_amount ?? '0'];
|
||||||
Log::debug(sprintf('Prefilled for account #%d with amount %s', $account->id, $account->pivot->current_amount ?? '0'));
|
Log::debug(sprintf('Prefilled for account #%d with amount %s', $account->id, $account->pivot->current_amount ?? '0'));
|
||||||
}
|
}
|
||||||
@@ -244,16 +242,16 @@ 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 (!$account instanceof Account) {
|
if (!$account instanceof Account) {
|
||||||
Log::debug(sprintf('Account #%d not found, skipping.', (int) ($info['account_id'] ?? 0)));
|
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.
|
// an amount is set, first check out if there is a difference with the previous amount.
|
||||||
$previous = $toBeLinked[$account->id]['current_amount'] ?? '0';
|
$previous = $toBeLinked[$account->id]['current_amount'] ?? '0';
|
||||||
$diff = bcsub($info['current_amount'], $previous);
|
$diff = bcsub($info['current_amount'], $previous);
|
||||||
|
|
||||||
// create event for difference.
|
// create event for difference.
|
||||||
if (0 !== bccomp($diff, '0')) {
|
if (0 !== bccomp($diff, '0')) {
|
||||||
@@ -266,8 +264,8 @@ class PiggyBankFactory
|
|||||||
}
|
}
|
||||||
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.
|
// an amount is set, first check out if there is a difference with the previous amount.
|
||||||
$previous = $toBeLinked[$account->id]['current_amount'] ?? '0';
|
$previous = $toBeLinked[$account->id]['current_amount'] ?? '0';
|
||||||
$diff = bcsub('0', $previous);
|
$diff = bcsub('0', $previous);
|
||||||
|
|
||||||
// create event for difference.
|
// create event for difference.
|
||||||
if (0 !== bccomp($diff, '0')) {
|
if (0 !== bccomp($diff, '0')) {
|
||||||
@@ -290,7 +288,16 @@ class PiggyBankFactory
|
|||||||
}
|
}
|
||||||
Log::debug(sprintf('Link information: %s', json_encode($toBeLinked)));
|
Log::debug(sprintf('Link information: %s', json_encode($toBeLinked)));
|
||||||
if (0 !== count($toBeLinked)) {
|
if (0 !== count($toBeLinked)) {
|
||||||
|
Log::debug('Syncing accounts to piggy bank.');
|
||||||
$piggyBank->accounts()->sync($toBeLinked);
|
$piggyBank->accounts()->sync($toBeLinked);
|
||||||
|
$piggyBank->refresh();
|
||||||
|
$newSavedAmount = $this->piggyBankRepository->getCurrentAmount($piggyBank);
|
||||||
|
Log::debug(sprintf('Old saved amount: %s, new saved amount is %s', $oldSavedAmount, $newSavedAmount));
|
||||||
|
if (0 !== bccomp($oldSavedAmount, $newSavedAmount)) {
|
||||||
|
Log::debug('Amount changed, will create event for it.');
|
||||||
|
// create event for difference.
|
||||||
|
event(new ChangedAmount($piggyBank, bcsub($newSavedAmount, $oldSavedAmount), null, null));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (0 === count($toBeLinked)) {
|
if (0 === count($toBeLinked)) {
|
||||||
Log::warning('No accounts to link to piggy bank, will not change whatever is there now.');
|
Log::warning('No accounts to link to piggy bank, will not change whatever is there now.');
|
||||||
|
@@ -276,7 +276,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
|||||||
$amount = '' === $amount ? '0' : $amount;
|
$amount = '' === $amount ? '0' : $amount;
|
||||||
$sum = bcadd($sum, $amount);
|
$sum = bcadd($sum, $amount);
|
||||||
}
|
}
|
||||||
// Log::debug(sprintf('Current amount in piggy bank #%d ("%s") is %s', $piggyBank->id, $piggyBank->name, $sum));
|
Log::debug(sprintf('Current amount in piggy bank #%d ("%s") is %s', $piggyBank->id, $piggyBank->name, $sum));
|
||||||
|
|
||||||
return $sum;
|
return $sum;
|
||||||
}
|
}
|
||||||
|
@@ -73,8 +73,8 @@ class PiggyBankEventTransformer extends AbstractTransformer
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => (string) $event->id,
|
'id' => (string) $event->id,
|
||||||
'created_at' => $event->created_at->toAtomString(),
|
'created_at' => $event->created_at?->toAtomString(),
|
||||||
'updated_at' => $event->updated_at->toAtomString(),
|
'updated_at' => $event->updated_at?->toAtomString(),
|
||||||
'amount' => app('steam')->bcround($event->amount, $currency->decimal_places),
|
'amount' => app('steam')->bcround($event->amount, $currency->decimal_places),
|
||||||
'currency_id' => (string) $currency->id,
|
'currency_id' => (string) $currency->id,
|
||||||
'currency_code' => $currency->code,
|
'currency_code' => $currency->code,
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
<label for="{{ options.id }}" class="col-sm-4 control-label">{{ label }}</label>
|
<label for="{{ options.id }}" class="col-sm-4 control-label">{{ label }}</label>
|
||||||
|
|
||||||
<div class="col-sm-8">
|
<div class="col-sm-8">
|
||||||
{{ Html.multiselect(name~"[]", list, selected).id(options.id).class('form-control').attribute('autocomplete','off').attribute('spellcheck','false').attribute('placeholder', options.placeholder) }}
|
{{ Html.multiselect(name~"[]", list, selected).id(options.id).attribute('size',12).class('form-control').attribute('autocomplete','off').attribute('spellcheck','false').attribute('placeholder', options.placeholder) }}
|
||||||
{% include 'form.help' %}
|
{% include 'form.help' %}
|
||||||
{% include 'form.feedback' %}
|
{% include 'form.feedback' %}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user