Catch null

This commit is contained in:
James Cole
2025-08-06 20:53:30 +02:00
parent b506281bd6
commit 7f5a1bda8d

View File

@@ -82,7 +82,7 @@ class PiggyBankEnrichment implements EnrichmentInterface
$this->ids[] = $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. // collect currencies.
$currencies = TransactionCurrency::whereIn('id', $this->currencyIds)->get(); $currencies = TransactionCurrency::whereIn('id', $this->currencyIds)->get();
@@ -91,10 +91,10 @@ class PiggyBankEnrichment implements EnrichmentInterface
} }
// collect accounts // 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) { foreach ($set as $item) {
$id = (int)$item->piggy_bank_id; $id = (int)$item->piggy_bank_id;
$accountId = (int)$item->account_id; $accountId = (int)$item->account_id;
$this->amounts[$id] ??= []; $this->amounts[$id] ??= [];
if (!array_key_exists($id, $this->accountIds)) { if (!array_key_exists($id, $this->accountIds)) {
$this->accountIds[$id] = (int)$item->account_id; $this->accountIds[$id] = (int)$item->account_id;
@@ -105,17 +105,19 @@ class PiggyBankEnrichment implements EnrichmentInterface
'pc_current_amount' => '0', 'pc_current_amount' => '0',
]; ];
} }
$this->amounts[$id][$accountId]['current_amount'] = bcadd($this->amounts[$id][$accountId]['current_amount'], $item->current_amount); $this->amounts[$id][$accountId]['current_amount'] = bcadd($this->amounts[$id][$accountId]['current_amount'], $item->current_amount);
$this->amounts[$id][$accountId]['pc_current_amount'] = bcadd($this->amounts[$id][$accountId]['pc_current_amount'], $item->native_current_amount); if (null !== $this->amounts[$id][$accountId]['pc_current_amount']) {
$this->amounts[$id][$accountId]['pc_current_amount'] = bcadd($this->amounts[$id][$accountId]['pc_current_amount'], $item->native_current_amount);
}
} }
// get account currency preference for ALL. // 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', array_values($this->accountIds))->where('name', 'currency_id')->get();
/** @var AccountMeta $item */ /** @var AccountMeta $item */
foreach ($set as $item) { foreach ($set as $item) {
$accountId = (int)$item->account_id; $accountId = (int)$item->account_id;
$currencyId = (int)$item->data; $currencyId = (int)$item->data;
if (!array_key_exists($currencyId, $this->currencies)) { if (!array_key_exists($currencyId, $this->currencies)) {
$this->currencies[$currencyId] = TransactionCurrency::find($currencyId); $this->currencies[$currencyId] = TransactionCurrency::find($currencyId);
} }
@@ -123,7 +125,7 @@ class PiggyBankEnrichment implements EnrichmentInterface
} }
// get account info. // get account info.
$set = Account::whereIn('id', array_values($this->accountIds))->get(); $set = Account::whereIn('id', array_values($this->accountIds))->get();
/** @var Account $item */ /** @var Account $item */
foreach ($set as $item) { foreach ($set as $item) {
@@ -138,14 +140,14 @@ class PiggyBankEnrichment implements EnrichmentInterface
private function appendCollectedData(): void private function appendCollectedData(): void
{ {
$this->collection = $this->collection->map(function (PiggyBank $item) { $this->collection = $this->collection->map(function (PiggyBank $item) {
$id = (int)$item->id; $id = (int)$item->id;
$currencyId = (int)$item->transaction_currency_id; $currencyId = (int)$item->transaction_currency_id;
$currency = $this->currencies[$currencyId] ?? $this->primaryCurrency; $currency = $this->currencies[$currencyId] ?? $this->primaryCurrency;
$targetAmount = null; $targetAmount = null;
if (0 !== bccomp($item->target_amount, '0')) { if (0 !== bccomp($item->target_amount, '0')) {
$targetAmount = $item->target_amount; $targetAmount = $item->target_amount;
} }
$meta = [ $meta = [
'notes' => $this->notes[$id] ?? null, 'notes' => $this->notes[$id] ?? null,
'currency' => $this->currencies[$currencyId] ?? null, 'currency' => $this->currencies[$currencyId] ?? null,
// 'auto_budget' => $this->autoBudgets[$id] ?? null, // 'auto_budget' => $this->autoBudgets[$id] ?? null,
@@ -174,17 +176,17 @@ class PiggyBankEnrichment implements EnrichmentInterface
} }
// add current amount(s). // add current amount(s).
foreach ($this->amounts[$id] as $accountId => $row) { foreach ($this->amounts[$id] as $accountId => $row) {
$meta['accounts'][] = [ $meta['accounts'][] = [
'account_id' => (string)$accountId, 'account_id' => (string)$accountId,
'name' => $this->accounts[$accountId]['name'] ?? '', 'name' => $this->accounts[$accountId]['name'] ?? '',
'current_amount' => Steam::bcround($row['current_amount'], $currency->decimal_places), 'current_amount' => Steam::bcround($row['current_amount'], $currency->decimal_places),
'pc_current_amount' => Steam::bcround($row['pc_current_amount'], $this->primaryCurrency->decimal_places), 'pc_current_amount' => Steam::bcround($row['pc_current_amount'], $this->primaryCurrency->decimal_places),
]; ];
$meta['current_amount'] = bcadd($meta['current_amount'], $row['current_amount']); $meta['current_amount'] = bcadd($meta['current_amount'], $row['current_amount']);
// only add pc_current_amount when the pc_current_amount is set // only add pc_current_amount when the pc_current_amount is set
$meta['pc_current_amount'] = null === $row['pc_current_amount'] ? null : bcadd($meta['pc_current_amount'], $row['pc_current_amount']); $meta['pc_current_amount'] = null === $row['pc_current_amount'] ? null : bcadd($meta['pc_current_amount'], $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. // only round this number when pc_current_amount is set.
$meta['pc_current_amount'] = null === $meta['pc_current_amount'] ? null : Steam::bcround($meta['pc_current_amount'], $this->primaryCurrency->decimal_places); $meta['pc_current_amount'] = null === $meta['pc_current_amount'] ? null : Steam::bcround($meta['pc_current_amount'], $this->primaryCurrency->decimal_places);
@@ -198,7 +200,7 @@ class PiggyBankEnrichment implements EnrichmentInterface
$meta['save_per_month'] = Steam::bcround($this->getSuggestedMonthlyAmount($item->start_date, $item->target_date, $meta['target_amount'], $meta['current_amount']), $currency->decimal_places); $meta['save_per_month'] = Steam::bcround($this->getSuggestedMonthlyAmount($item->start_date, $item->target_date, $meta['target_amount'], $meta['current_amount']), $currency->decimal_places);
$meta['pc_save_per_month'] = Steam::bcround($this->getSuggestedMonthlyAmount($item->start_date, $item->target_date, $meta['pc_target_amount'], $meta['pc_current_amount']), $currency->decimal_places); $meta['pc_save_per_month'] = Steam::bcround($this->getSuggestedMonthlyAmount($item->start_date, $item->target_date, $meta['pc_target_amount'], $meta['pc_current_amount']), $currency->decimal_places);
$item->meta = $meta; $item->meta = $meta;
return $item; return $item;
}); });
@@ -207,10 +209,9 @@ class PiggyBankEnrichment implements EnrichmentInterface
private function collectNotes(): void private function collectNotes(): void
{ {
$notes = Note::query()->whereIn('noteable_id', $this->ids) $notes = Note::query()->whereIn('noteable_id', $this->ids)
->whereNotNull('notes.text') ->whereNotNull('notes.text')
->where('notes.text', '!=', '') ->where('notes.text', '!=', '')
->where('noteable_type', PiggyBank::class)->get(['notes.noteable_id', 'notes.text'])->toArray() ->where('noteable_type', PiggyBank::class)->get(['notes.noteable_id', 'notes.text'])->toArray();
;
foreach ($notes as $note) { foreach ($notes as $note) {
$this->notes[(int)$note['noteable_id']] = (string)$note['text']; $this->notes[(int)$note['noteable_id']] = (string)$note['text'];
} }
@@ -219,13 +220,12 @@ class PiggyBankEnrichment implements EnrichmentInterface
private function collectObjectGroups(): void private function collectObjectGroups(): void
{ {
$set = DB::table('object_groupables') $set = DB::table('object_groupables')
->whereIn('object_groupable_id', $this->ids) ->whereIn('object_groupable_id', $this->ids)
->where('object_groupable_type', PiggyBank::class) ->where('object_groupable_type', PiggyBank::class)
->get(['object_groupable_id', 'object_group_id']) ->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) { 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;
@@ -239,7 +239,9 @@ class PiggyBankEnrichment implements EnrichmentInterface
} }
} }
private function collectCurrentAmounts(): void {} private function collectCurrentAmounts(): void
{
}
/** /**
* Returns the suggested amount the user should save per month, or "". * Returns the suggested amount the user should save per month, or "".