Rearrange some code.

This commit is contained in:
James Cole
2025-09-26 05:33:35 +02:00
parent 66d09450d3
commit 08879d31ba
19 changed files with 407 additions and 416 deletions

View File

@@ -56,10 +56,10 @@ trait AugumentData
/** @var Account $expenseAccount */
foreach ($accounts as $expenseAccount) {
$collection = new Collection();
$collection = new Collection();
$collection->push($expenseAccount);
$revenue = $repository->findByName($expenseAccount->name, [AccountTypeEnum::REVENUE->value]);
$revenue = $repository->findByName($expenseAccount->name, [AccountTypeEnum::REVENUE->value]);
if (null !== $revenue) {
$collection->push($revenue);
}
@@ -110,13 +110,13 @@ trait AugumentData
$grouped = $accounts->groupBy('id')->toArray();
$return = [];
foreach ($accountIds as $combinedId) {
$parts = explode('-', (string) $combinedId);
$accountId = (int) $parts[0];
$parts = explode('-', (string)$combinedId);
$accountId = (int)$parts[0];
if (array_key_exists($accountId, $grouped)) {
$return[$accountId] = $grouped[$accountId][0]['name'];
}
}
$return[0] = '(no name)';
$return[0] = '(no name)';
return $return;
}
@@ -136,7 +136,7 @@ trait AugumentData
$return[$budgetId] = $grouped[$budgetId][0]['name'];
}
}
$return[0] = (string) trans('firefly.no_budget');
$return[0] = (string)trans('firefly.no_budget');
return $return;
}
@@ -152,13 +152,13 @@ trait AugumentData
$grouped = $categories->groupBy('id')->toArray();
$return = [];
foreach ($categoryIds as $combinedId) {
$parts = explode('-', (string) $combinedId);
$categoryId = (int) $parts[0];
$parts = explode('-', (string)$combinedId);
$categoryId = (int)$parts[0];
if (array_key_exists($categoryId, $grouped)) {
$return[$categoryId] = $grouped[$categoryId][0]['name'];
}
}
$return[0] = (string) trans('firefly.no_category');
$return[0] = (string)trans('firefly.no_category');
return $return;
}
@@ -171,14 +171,14 @@ trait AugumentData
Log::debug('In getLimits');
/** @var OperationsRepositoryInterface $opsRepository */
$opsRepository = app(OperationsRepositoryInterface::class);
$opsRepository = app(OperationsRepositoryInterface::class);
/** @var BudgetLimitRepositoryInterface $blRepository */
$blRepository = app(BudgetLimitRepositoryInterface::class);
$blRepository = app(BudgetLimitRepositoryInterface::class);
$end->endOfMonth();
// properties for cache
$cache = new CacheProperties();
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($budget->id);
@@ -189,25 +189,25 @@ trait AugumentData
return $cache->get();
}
$set = $blRepository->getBudgetLimits($budget, $start, $end);
$set = $blRepository->getBudgetLimits($budget, $start, $end);
$budgetCollection = new Collection()->push($budget);
// merge sets based on a key, in case of convert to primary currency
$limits = new Collection();
$limits = new Collection();
/** @var BudgetLimit $entry */
foreach ($set as $entry) {
Log::debug(sprintf('Now at budget limit #%d', $entry->id));
$currency = $entry->transactionCurrency;
$currency = $entry->transactionCurrency;
if ($this->convertToPrimary) {
// the sumExpenses method already handles this.
$currency = $this->primaryCurrency;
}
// clone because these objects change each other.
$currentStart = clone $entry->start_date;
$currentEnd = null === $entry->end_date ? null : clone $entry->end_date;
$currentStart = clone $entry->start_date;
$currentEnd = null === $entry->end_date ? null : clone $entry->end_date;
if (null === $currentEnd) {
$currentEnd = clone $currentStart;
@@ -219,9 +219,9 @@ trait AugumentData
$entry->pc_spent = $spent;
// normal amount:
$expenses = $opsRepository->sumExpenses($currentStart, $currentEnd, null, $budgetCollection, $entry->transactionCurrency, false);
$spent = $expenses[$entry->transactionCurrency->id]['sum'] ?? '0';
$entry->spent = $spent;
$expenses = $opsRepository->sumExpenses($currentStart, $currentEnd, null, $budgetCollection, $entry->transactionCurrency, false);
$spent = $expenses[$entry->transactionCurrency->id]['sum'] ?? '0';
$entry->spent = $spent;
$limits->push($entry);
}
@@ -240,7 +240,7 @@ trait AugumentData
/** @var array $journal */
foreach ($array as $journal) {
$name = '(no name)';
$name = '(no name)';
if (TransactionTypeEnum::WITHDRAWAL->value === $journal['transaction_type_type']) {
$name = $journal['destination_account_name'];
}
@@ -249,7 +249,7 @@ trait AugumentData
}
$grouped[$name] ??= '0';
$grouped[$name] = bcadd((string) $journal['amount'], $grouped[$name]);
$grouped[$name] = bcadd((string)$journal['amount'], $grouped[$name]);
}
return $grouped;
@@ -263,16 +263,16 @@ trait AugumentData
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$total = $assets->merge($opposing);
$total = $assets->merge($opposing);
$collector->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value])->setAccounts($total);
$journals = $collector->getExtractedJournals();
$sum = [
$journals = $collector->getExtractedJournals();
$sum = [
'grand_sum' => '0',
'per_currency' => [],
];
// loop to support multi currency
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
// if not set, set to zero:
if (!array_key_exists($currencyId, $sum['per_currency'])) {
@@ -287,8 +287,8 @@ trait AugumentData
}
// add amount
$sum['per_currency'][$currencyId]['sum'] = bcadd($sum['per_currency'][$currencyId]['sum'], (string) $journal['amount']);
$sum['grand_sum'] = bcadd($sum['grand_sum'], (string) $journal['amount']);
$sum['per_currency'][$currencyId]['sum'] = bcadd($sum['per_currency'][$currencyId]['sum'], (string)$journal['amount']);
$sum['grand_sum'] = bcadd($sum['grand_sum'], (string)$journal['amount']);
}
return $sum;