Skip to content

Firefly III: a personal finances manager

Fix https://github.com/firefly-iii/firefly-iii/issues/12070

James Cole40246caaa891
diff --git a/app/Support/JsonApi/Enrichments/PiggyBankEnrichment.php b/app/Support/JsonApi/Enrichments/PiggyBankEnrichment.php
index 0d5f07f768..9eb950307f 100644
--- a/app/Support/JsonApi/Enrichments/PiggyBankEnrichment.php
+++ b/app/Support/JsonApi/Enrichments/PiggyBankEnrichment.php
@@ -42,19 +42,19 @@ use Illuminate\Support\Facades\Log;
class PiggyBankEnrichment implements EnrichmentInterface
{
- private array $accountIds = [];
- private array $accounts = [];
- private array $amounts = [];
+ private array $accountIds = [];
+ private array $accounts = [];
+ private array $amounts = [];
private Collection $collection;
- private array $currencies = [];
- private array $currencyIds = [];
- private array $ids = [];
+ private array $currencies = [];
+ private array $currencyIds = [];
+ private array $ids = [];
// private array $accountCurrencies = [];
- private array $mappedObjects = [];
- private array $notes = [];
- private array $objectGroups = [];
+ private array $mappedObjects = [];
+ private array $notes = [];
+ private array $objectGroups = [];
private readonly TransactionCurrency $primaryCurrency;
- private ?Carbon $date;
+ private ?Carbon $date;
public function __construct()
{
@@ -74,7 +74,7 @@ class PiggyBankEnrichment implements EnrichmentInterface
return $this->collection;
}
- public function enrichSingle(array|Model $model): array|Model
+ public function enrichSingle(array | Model $model): array | Model
{
Log::debug(__METHOD__);
$collection = new Collection()->push($model);
@@ -98,14 +98,14 @@ class PiggyBankEnrichment implements EnrichmentInterface
private function appendCollectedData(): void
{
$this->collection = $this->collection->map(function (PiggyBank $item): PiggyBank {
- $id = (int) $item->id;
- $currencyId = (int) $item->transaction_currency_id;
- $currency = $this->currencies[$currencyId] ?? $this->primaryCurrency;
- $targetAmount = null;
+ $id = (int)$item->id;
+ $currencyId = (int)$item->transaction_currency_id;
+ $currency = $this->currencies[$currencyId] ?? $this->primaryCurrency;
+ $targetAmount = null;
if (0 !== bccomp($item->target_amount, '0')) {
$targetAmount = $item->target_amount;
}
- $meta = [
+ $meta = [
'notes' => $this->notes[$id] ?? null,
'currency' => $this->currencies[$currencyId] ?? null,
// 'auto_budget' => $this->autoBudgets[$id] ?? null,
@@ -130,25 +130,25 @@ class PiggyBankEnrichment implements EnrichmentInterface
// add object group if available
if (array_key_exists($id, $this->mappedObjects)) {
$key = $this->mappedObjects[$id];
- $meta['object_group_id'] = (string) $this->objectGroups[$key]['id'];
+ $meta['object_group_id'] = (string)$this->objectGroups[$key]['id'];
$meta['object_group_title'] = $this->objectGroups[$key]['title'];
$meta['object_group_order'] = $this->objectGroups[$key]['order'];
}
// add current amount(s).
foreach ($this->amounts[$id] as $accountId => $row) {
- $meta['accounts'][] = [
- 'account_id' => (string) $accountId,
+ $meta['accounts'][] = [
+ 'account_id' => (string)$accountId,
'name' => $this->accounts[$accountId]['name'] ?? '',
'current_amount' => Steam::bcround($row['current_amount'], $currency->decimal_places),
'pc_current_amount' => Steam::bcround($row['pc_current_amount'], $this->primaryCurrency->decimal_places),
];
- $meta['current_amount'] = bcadd($meta['current_amount'], (string) $row['current_amount']);
+ $meta['current_amount'] = bcadd($meta['current_amount'], (string)$row['current_amount']);
// only add pc_current_amount when the pc_current_amount is set
$meta['pc_current_amount'] = null === $row['pc_current_amount']
? null
- : bcadd((string) $meta['pc_current_amount'], (string) $row['pc_current_amount']);
+ : bcadd((string)$meta['pc_current_amount'], (string)$row['pc_current_amount']);
}
- $meta['current_amount'] = Steam::bcround($meta['current_amount'], $currency->decimal_places);
+ $meta['current_amount'] = Steam::bcround($meta['current_amount'], $currency->decimal_places);
// only round this number when pc_current_amount is set.
$meta['pc_current_amount'] = null === $meta['pc_current_amount']
? null
@@ -156,14 +156,14 @@ class PiggyBankEnrichment implements EnrichmentInterface
// calculate left to save, only when there is a target amount.
if (null !== $targetAmount) {
- $meta['left_to_save'] = bcsub((string) $meta['target_amount'], (string) $meta['current_amount']);
+ $meta['left_to_save'] = bcsub((string)$meta['target_amount'], (string)$meta['current_amount']);
$meta['pc_left_to_save'] = null === $meta['pc_target_amount']
? null
- : bcsub((string) $meta['pc_target_amount'], (string) $meta['pc_current_amount']);
+ : bcsub((string)$meta['pc_target_amount'], (string)$meta['pc_current_amount']);
}
// get suggested per month.
- $meta['save_per_month'] = Steam::bcround(
+ $meta['save_per_month'] = Steam::bcround(
$this->getSuggestedMonthlyAmount($this->date, $item->target_date, $meta['target_amount'], $meta['current_amount']),
$currency->decimal_places
);
@@ -174,7 +174,7 @@ class PiggyBankEnrichment implements EnrichmentInterface
);
}
- $item->meta = $meta;
+ $item->meta = $meta;
return $item;
});
@@ -184,54 +184,60 @@ class PiggyBankEnrichment implements EnrichmentInterface
{
/** @var PiggyBank $piggy */
foreach ($this->collection as $piggy) {
- $id = (int) $piggy->id;
+ $id = (int)$piggy->id;
$this->ids[] = $id;
- $this->currencyIds[$id] = (int) $piggy->transaction_currency_id;
+ $this->currencyIds[$id] = (int)$piggy->transaction_currency_id;
}
- $this->ids = array_unique($this->ids);
+ $this->ids = array_unique($this->ids);
// collect currencies.
$currencies = TransactionCurrency::whereIn('id', $this->currencyIds)->get();
foreach ($currencies as $currency) {
- $this->currencies[(int) $currency->id] = $currency;
+ $this->currencies[(int)$currency->id] = $currency;
}
// collect accounts
- $set = DB::table('account_piggy_bank')->whereIn('piggy_bank_id', $this->ids)->get([
- 'piggy_bank_id',
- 'account_id',
- 'current_amount',
- 'native_current_amount',
- ]);
+ $set = DB::table('account_piggy_bank')->whereIn('piggy_bank_id', $this->ids)->get([
+ 'piggy_bank_id',
+ 'account_id',
+ 'current_amount',
+ 'native_current_amount',
+ ]);
foreach ($set as $item) {
- $id = (int) $item->piggy_bank_id;
- $accountId = (int) $item->account_id;
+ $id = (int)$item->piggy_bank_id;
+ $accountId = (int)$item->account_id;
$this->amounts[$id] ??= [];
if (!array_key_exists($id, $this->accountIds)) {
- $this->accountIds[$id] = (int) $item->account_id;
+ $this->accountIds[$id] = [];
}
+ $this->accountIds[$id][] = (int)$item->account_id;
if (!array_key_exists($accountId, $this->amounts[$id])) {
$this->amounts[$id][$accountId] = ['current_amount' => '0', 'pc_current_amount' => '0'];
}
$this->amounts[$id][$accountId]['current_amount'] = bcadd(
- (string) $this->amounts[$id][$accountId]['current_amount'],
- (string) $item->current_amount
+ (string)$this->amounts[$id][$accountId]['current_amount'],
+ (string)$item->current_amount
);
if (null !== $this->amounts[$id][$accountId]['pc_current_amount'] && null !== $item->native_current_amount) {
$this->amounts[$id][$accountId]['pc_current_amount'] = bcadd(
$this->amounts[$id][$accountId]['pc_current_amount'],
- (string) $item->native_current_amount
+ (string)$item->native_current_amount
);
}
}
+ $allAccountIds = [];
+ foreach ($this->accountIds as $accountIds) {
+ $allAccountIds = array_merge($allAccountIds, $accountIds);
+ }
+
// get account currency preference for ALL.
- $set = AccountMeta::whereIn('account_id', array_values($this->accountIds))->where('name', 'currency_id')->get();
+ $set = AccountMeta::whereIn('account_id', $allAccountIds)->where('name', 'currency_id')->get();
/** @var AccountMeta $item */
foreach ($set as $item) {
- $accountId = (int) $item->account_id;
- $currencyId = (int) $item->data;
+ // $accountId = (int) $item->account_id;
+ $currencyId = (int)$item->data;
if (!array_key_exists($currencyId, $this->currencies)) {
$this->currencies[$currencyId] = Amount::getTransactionCurrencyById($currencyId);
}
@@ -239,12 +245,11 @@ class PiggyBankEnrichment implements EnrichmentInterface
// $this->accountCurrencies[$accountId] = $this->currencies[$currencyId];
}
- // get account info.
- $set = Account::whereIn('id', array_values($this->accountIds))->get();
+ $set = Account::whereIn('id', $allAccountIds)->get();
/** @var Account $item */
foreach ($set as $item) {
- $id = (int) $item->id;
+ $id = (int)$item->id;
$this->accounts[$id] = ['id' => $id, 'name' => $item->name];
}
}
@@ -252,15 +257,14 @@ class PiggyBankEnrichment implements EnrichmentInterface
private function collectNotes(): void
{
$notes = Note::query()
- ->whereIn('noteable_id', $this->ids)
- ->whereNotNull('notes.text')
- ->where('notes.text', '!=', '')
- ->where('noteable_type', PiggyBank::class)
- ->get(['notes.noteable_id', 'notes.text'])
- ->toArray()
- ;
+ ->whereIn('noteable_id', $this->ids)
+ ->whereNotNull('notes.text')
+ ->where('notes.text', '!=', '')
+ ->where('noteable_type', PiggyBank::class)
+ ->get(['notes.noteable_id', 'notes.text'])
+ ->toArray();
foreach ($notes as $note) {
- $this->notes[(int) $note['noteable_id']] = (string) $note['text'];
+ $this->notes[(int)$note['noteable_id']] = (string)$note['text'];
}
// Log::debug(sprintf('Enrich with %d note(s)', count($this->notes)));
@@ -268,23 +272,22 @@ class PiggyBankEnrichment implements EnrichmentInterface
private function collectObjectGroups(): void
{
- $set = DB::table('object_groupables')
- ->whereIn('object_groupable_id', $this->ids)
- ->where('object_groupable_type', PiggyBank::class)
- ->get(['object_groupable_id', 'object_group_id'])
- ;
+ $set = DB::table('object_groupables')
+ ->whereIn('object_groupable_id', $this->ids)
+ ->where('object_groupable_type', PiggyBank::class)
+ ->get(['object_groupable_id', 'object_group_id']);
- $ids = array_unique($set->pluck('object_group_id')->toArray());
+ $ids = array_unique($set->pluck('object_group_id')->toArray());
foreach ($set as $entry) {
- $this->mappedObjects[(int) $entry->object_groupable_id] = (int) $entry->object_group_id;
+ $this->mappedObjects[(int)$entry->object_groupable_id] = (int)$entry->object_group_id;
}
$groups = ObjectGroup::whereIn('id', $ids)->get(['id', 'title', 'order'])->toArray();
foreach ($groups as $group) {
- $group['id'] = (int) $group['id'];
- $group['order'] = (int) $group['order'];
- $this->objectGroups[(int) $group['id']] = $group;
+ $group['id'] = (int)$group['id'];
+ $group['order'] = (int)$group['order'];
+ $this->objectGroups[(int)$group['id']] = $group;
}
}
@@ -299,12 +302,12 @@ class PiggyBankEnrichment implements EnrichmentInterface
$savePerMonth = '0';
if (1 === bccomp($targetAmount, $currentAmount)) {
$now = today(config('app.timezone'));
- $diffInMonths = (int) $startDate->diffInMonths($targetDate);
+ $diffInMonths = (int)$startDate->diffInMonths($targetDate);
$remainingAmount = bcsub($targetAmount, $currentAmount);
// more than 1 month to go and still need money to save:
if ($diffInMonths > 0 && 1 === bccomp($remainingAmount, '0')) {
- $savePerMonth = bcdiv($remainingAmount, (string) $diffInMonths);
+ $savePerMonth = bcdiv($remainingAmount, (string)$diffInMonths);
}
// less than 1 month to go but still need money to save: