Optimize queries for statistics.

This commit is contained in:
James Cole
2025-09-26 06:05:37 +02:00
parent 08879d31ba
commit 4ec2fcdb8a
92 changed files with 6499 additions and 6514 deletions

View File

@@ -76,7 +76,7 @@ class BudgetReportGenerator
/** @var Account $account */
foreach ($this->accounts as $account) {
$accountId = $account->id;
$accountId = $account->id;
$this->report[$accountId] ??= [
'name' => $account->name,
'id' => $account->id,
@@ -91,43 +91,6 @@ class BudgetReportGenerator
}
}
/**
* Process each row of expenses collected for the "Account per budget" partial
*/
private function processExpenses(array $expenses): void
{
foreach ($expenses['budgets'] as $budget) {
$this->processBudgetExpenses($expenses, $budget);
}
}
/**
* Process each set of transactions for each row of expenses.
*/
private function processBudgetExpenses(array $expenses, array $budget): void
{
$budgetId = (int) $budget['id'];
$currencyId = (int) $expenses['currency_id'];
foreach ($budget['transaction_journals'] as $journal) {
$sourceAccountId = $journal['source_account_id'];
$this->report[$sourceAccountId]['currencies'][$currencyId]
??= [
'currency_id' => $expenses['currency_id'],
'currency_symbol' => $expenses['currency_symbol'],
'currency_name' => $expenses['currency_name'],
'currency_decimal_places' => $expenses['currency_decimal_places'],
'budgets' => [],
];
$this->report[$sourceAccountId]['currencies'][$currencyId]['budgets'][$budgetId]
??= '0';
$this->report[$sourceAccountId]['currencies'][$currencyId]['budgets'][$budgetId]
= bcadd($this->report[$sourceAccountId]['currencies'][$currencyId]['budgets'][$budgetId], (string) $journal['amount']);
}
}
/**
* Generates the data necessary to create the card that displays
* the budget overview in the general report.
@@ -144,175 +107,6 @@ class BudgetReportGenerator
$this->percentageReport();
}
/**
* Start the budgets block on the default report by processing every budget.
*/
private function generalBudgetReport(): void
{
$budgetList = $this->repository->getBudgets();
/** @var Budget $budget */
foreach ($budgetList as $budget) {
$this->processBudget($budget);
}
}
/**
* Process expenses etc. for a single budget for the budgets block on the default report.
*/
private function processBudget(Budget $budget): void
{
$budgetId = $budget->id;
$this->report['budgets'][$budgetId] ??= [
'budget_id' => $budgetId,
'budget_name' => $budget->name,
'no_budget' => false,
'budget_limits' => [],
];
// get all budget limits for budget in period:
$limits = $this->blRepository->getBudgetLimits($budget, $this->start, $this->end);
/** @var BudgetLimit $limit */
foreach ($limits as $limit) {
$this->processLimit($budget, $limit);
}
}
/**
* Process a single budget limit for the budgets block on the default report.
*/
private function processLimit(Budget $budget, BudgetLimit $limit): void
{
$budgetId = $budget->id;
$limitId = $limit->id;
$limitCurrency = $limit->transactionCurrency ?? $this->currency;
$currencyId = $limitCurrency->id;
$expenses = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, $this->accounts, new Collection()->push($budget));
$spent = $expenses[$currencyId]['sum'] ?? '0';
$left = -1 === bccomp(bcadd($limit->amount, $spent), '0') ? '0' : bcadd($limit->amount, $spent);
$overspent = 1 === bccomp(bcmul($spent, '-1'), $limit->amount) ? bcadd($spent, $limit->amount) : '0';
$this->report['budgets'][$budgetId]['budget_limits'][$limitId] ??= [
'budget_limit_id' => $limitId,
'start_date' => $limit->start_date,
'end_date' => $limit->end_date,
'budgeted' => $limit->amount,
'budgeted_pct' => '0',
'spent' => $spent,
'spent_pct' => '0',
'left' => $left,
'overspent' => $overspent,
'currency_id' => $currencyId,
'currency_code' => $limitCurrency->code,
'currency_name' => $limitCurrency->name,
'currency_symbol' => $limitCurrency->symbol,
'currency_decimal_places' => $limitCurrency->decimal_places,
];
// make sum information:
$this->report['sums'][$currencyId]
??= [
'budgeted' => '0',
'spent' => '0',
'left' => '0',
'overspent' => '0',
'currency_id' => $currencyId,
'currency_code' => $limitCurrency->code,
'currency_name' => $limitCurrency->name,
'currency_symbol' => $limitCurrency->symbol,
'currency_decimal_places' => $limitCurrency->decimal_places,
];
$this->report['sums'][$currencyId]['budgeted'] = bcadd((string) $this->report['sums'][$currencyId]['budgeted'], $limit->amount);
$this->report['sums'][$currencyId]['spent'] = bcadd((string) $this->report['sums'][$currencyId]['spent'], $spent);
$this->report['sums'][$currencyId]['left'] = bcadd((string) $this->report['sums'][$currencyId]['left'], bcadd($limit->amount, $spent));
$this->report['sums'][$currencyId]['overspent'] = bcadd((string) $this->report['sums'][$currencyId]['overspent'], $overspent);
}
/**
* Calculate the expenses for transactions without a budget. Part of the "budgets" block of the default report.
*/
private function noBudgetReport(): void
{
// add no budget info.
$this->report['budgets'][0] = [
'budget_id' => null,
'budget_name' => null,
'no_budget' => true,
'budget_limits' => [],
];
$noBudget = $this->nbRepository->sumExpenses($this->start, $this->end, $this->accounts);
foreach ($noBudget as $noBudgetEntry) {
// currency information:
$nbCurrencyId = (int) ($noBudgetEntry['currency_id'] ?? $this->currency->id);
$nbCurrencyCode = $noBudgetEntry['currency_code'] ?? $this->currency->code;
$nbCurrencyName = $noBudgetEntry['currency_name'] ?? $this->currency->name;
$nbCurrencySymbol = $noBudgetEntry['currency_symbol'] ?? $this->currency->symbol;
$nbCurrencyDp = $noBudgetEntry['currency_decimal_places'] ?? $this->currency->decimal_places;
$this->report['budgets'][0]['budget_limits'][] = [
'budget_limit_id' => null,
'start_date' => $this->start,
'end_date' => $this->end,
'budgeted' => '0',
'budgeted_pct' => '0',
'spent' => $noBudgetEntry['sum'],
'spent_pct' => '0',
'left' => '0',
'overspent' => '0',
'currency_id' => $nbCurrencyId,
'currency_code' => $nbCurrencyCode,
'currency_name' => $nbCurrencyName,
'currency_symbol' => $nbCurrencySymbol,
'currency_decimal_places' => $nbCurrencyDp,
];
$this->report['sums'][$nbCurrencyId]['spent'] = bcadd($this->report['sums'][$nbCurrencyId]['spent'] ?? '0', (string) $noBudgetEntry['sum']);
// append currency info because it may be missing:
$this->report['sums'][$nbCurrencyId]['currency_id'] = $nbCurrencyId;
$this->report['sums'][$nbCurrencyId]['currency_code'] = $nbCurrencyCode;
$this->report['sums'][$nbCurrencyId]['currency_name'] = $nbCurrencyName;
$this->report['sums'][$nbCurrencyId]['currency_symbol'] = $nbCurrencySymbol;
$this->report['sums'][$nbCurrencyId]['currency_decimal_places'] = $nbCurrencyDp;
// append other sums because they might be missing:
$this->report['sums'][$nbCurrencyId]['overspent'] ??= '0';
$this->report['sums'][$nbCurrencyId]['left'] ??= '0';
$this->report['sums'][$nbCurrencyId]['budgeted'] ??= '0';
}
}
/**
* Calculate the percentages for each budget. Part of the "budgets" block on the default report.
*/
private function percentageReport(): void
{
// make percentages based on total amount.
foreach ($this->report['budgets'] as $budgetId => $data) {
foreach ($data['budget_limits'] as $limitId => $entry) {
$budgetId = (int) $budgetId;
$limitId = (int) $limitId;
$currencyId = (int) $entry['currency_id'];
$spent = $entry['spent'];
$totalSpent = $this->report['sums'][$currencyId]['spent'] ?? '0';
$spentPct = '0';
$budgeted = $entry['budgeted'];
$totalBudgeted = $this->report['sums'][$currencyId]['budgeted'] ?? '0';
$budgetedPct = '0';
if (0 !== bccomp((string) $spent, '0') && 0 !== bccomp($totalSpent, '0')) {
$spentPct = round((float) bcmul(bcdiv((string) $spent, $totalSpent), '100'));
}
if (0 !== bccomp((string) $budgeted, '0') && 0 !== bccomp($totalBudgeted, '0')) {
$budgetedPct = round((float) bcmul(bcdiv((string) $budgeted, $totalBudgeted), '100'));
}
$this->report['sums'][$currencyId]['budgeted'] ??= '0';
$this->report['budgets'][$budgetId]['budget_limits'][$limitId]['spent_pct'] = $spentPct;
$this->report['budgets'][$budgetId]['budget_limits'][$limitId]['budgeted_pct'] = $budgetedPct;
}
}
}
public function getReport(): array
{
return $this->report;
@@ -349,4 +143,210 @@ class BudgetReportGenerator
$this->nbRepository->setUser($user);
$this->currency = app('amount')->getPrimaryCurrencyByUserGroup($user->userGroup);
}
/**
* Start the budgets block on the default report by processing every budget.
*/
private function generalBudgetReport(): void
{
$budgetList = $this->repository->getBudgets();
/** @var Budget $budget */
foreach ($budgetList as $budget) {
$this->processBudget($budget);
}
}
/**
* Calculate the expenses for transactions without a budget. Part of the "budgets" block of the default report.
*/
private function noBudgetReport(): void
{
// add no budget info.
$this->report['budgets'][0] = [
'budget_id' => null,
'budget_name' => null,
'no_budget' => true,
'budget_limits' => [],
];
$noBudget = $this->nbRepository->sumExpenses($this->start, $this->end, $this->accounts);
foreach ($noBudget as $noBudgetEntry) {
// currency information:
$nbCurrencyId = (int)($noBudgetEntry['currency_id'] ?? $this->currency->id);
$nbCurrencyCode = $noBudgetEntry['currency_code'] ?? $this->currency->code;
$nbCurrencyName = $noBudgetEntry['currency_name'] ?? $this->currency->name;
$nbCurrencySymbol = $noBudgetEntry['currency_symbol'] ?? $this->currency->symbol;
$nbCurrencyDp = $noBudgetEntry['currency_decimal_places'] ?? $this->currency->decimal_places;
$this->report['budgets'][0]['budget_limits'][] = [
'budget_limit_id' => null,
'start_date' => $this->start,
'end_date' => $this->end,
'budgeted' => '0',
'budgeted_pct' => '0',
'spent' => $noBudgetEntry['sum'],
'spent_pct' => '0',
'left' => '0',
'overspent' => '0',
'currency_id' => $nbCurrencyId,
'currency_code' => $nbCurrencyCode,
'currency_name' => $nbCurrencyName,
'currency_symbol' => $nbCurrencySymbol,
'currency_decimal_places' => $nbCurrencyDp,
];
$this->report['sums'][$nbCurrencyId]['spent'] = bcadd($this->report['sums'][$nbCurrencyId]['spent'] ?? '0', (string)$noBudgetEntry['sum']);
// append currency info because it may be missing:
$this->report['sums'][$nbCurrencyId]['currency_id'] = $nbCurrencyId;
$this->report['sums'][$nbCurrencyId]['currency_code'] = $nbCurrencyCode;
$this->report['sums'][$nbCurrencyId]['currency_name'] = $nbCurrencyName;
$this->report['sums'][$nbCurrencyId]['currency_symbol'] = $nbCurrencySymbol;
$this->report['sums'][$nbCurrencyId]['currency_decimal_places'] = $nbCurrencyDp;
// append other sums because they might be missing:
$this->report['sums'][$nbCurrencyId]['overspent'] ??= '0';
$this->report['sums'][$nbCurrencyId]['left'] ??= '0';
$this->report['sums'][$nbCurrencyId]['budgeted'] ??= '0';
}
}
/**
* Calculate the percentages for each budget. Part of the "budgets" block on the default report.
*/
private function percentageReport(): void
{
// make percentages based on total amount.
foreach ($this->report['budgets'] as $budgetId => $data) {
foreach ($data['budget_limits'] as $limitId => $entry) {
$budgetId = (int)$budgetId;
$limitId = (int)$limitId;
$currencyId = (int)$entry['currency_id'];
$spent = $entry['spent'];
$totalSpent = $this->report['sums'][$currencyId]['spent'] ?? '0';
$spentPct = '0';
$budgeted = $entry['budgeted'];
$totalBudgeted = $this->report['sums'][$currencyId]['budgeted'] ?? '0';
$budgetedPct = '0';
if (0 !== bccomp((string)$spent, '0') && 0 !== bccomp($totalSpent, '0')) {
$spentPct = round((float)bcmul(bcdiv((string)$spent, $totalSpent), '100'));
}
if (0 !== bccomp((string)$budgeted, '0') && 0 !== bccomp($totalBudgeted, '0')) {
$budgetedPct = round((float)bcmul(bcdiv((string)$budgeted, $totalBudgeted), '100'));
}
$this->report['sums'][$currencyId]['budgeted'] ??= '0';
$this->report['budgets'][$budgetId]['budget_limits'][$limitId]['spent_pct'] = $spentPct;
$this->report['budgets'][$budgetId]['budget_limits'][$limitId]['budgeted_pct'] = $budgetedPct;
}
}
}
/**
* Process expenses etc. for a single budget for the budgets block on the default report.
*/
private function processBudget(Budget $budget): void
{
$budgetId = $budget->id;
$this->report['budgets'][$budgetId] ??= [
'budget_id' => $budgetId,
'budget_name' => $budget->name,
'no_budget' => false,
'budget_limits' => [],
];
// get all budget limits for budget in period:
$limits = $this->blRepository->getBudgetLimits($budget, $this->start, $this->end);
/** @var BudgetLimit $limit */
foreach ($limits as $limit) {
$this->processLimit($budget, $limit);
}
}
/**
* Process each set of transactions for each row of expenses.
*/
private function processBudgetExpenses(array $expenses, array $budget): void
{
$budgetId = (int)$budget['id'];
$currencyId = (int)$expenses['currency_id'];
foreach ($budget['transaction_journals'] as $journal) {
$sourceAccountId = $journal['source_account_id'];
$this->report[$sourceAccountId]['currencies'][$currencyId]
??= [
'currency_id' => $expenses['currency_id'],
'currency_symbol' => $expenses['currency_symbol'],
'currency_name' => $expenses['currency_name'],
'currency_decimal_places' => $expenses['currency_decimal_places'],
'budgets' => [],
];
$this->report[$sourceAccountId]['currencies'][$currencyId]['budgets'][$budgetId]
??= '0';
$this->report[$sourceAccountId]['currencies'][$currencyId]['budgets'][$budgetId]
= bcadd($this->report[$sourceAccountId]['currencies'][$currencyId]['budgets'][$budgetId], (string)$journal['amount']);
}
}
/**
* Process each row of expenses collected for the "Account per budget" partial
*/
private function processExpenses(array $expenses): void
{
foreach ($expenses['budgets'] as $budget) {
$this->processBudgetExpenses($expenses, $budget);
}
}
/**
* Process a single budget limit for the budgets block on the default report.
*/
private function processLimit(Budget $budget, BudgetLimit $limit): void
{
$budgetId = $budget->id;
$limitId = $limit->id;
$limitCurrency = $limit->transactionCurrency ?? $this->currency;
$currencyId = $limitCurrency->id;
$expenses = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, $this->accounts, new Collection()->push($budget));
$spent = $expenses[$currencyId]['sum'] ?? '0';
$left = -1 === bccomp(bcadd($limit->amount, $spent), '0') ? '0' : bcadd($limit->amount, $spent);
$overspent = 1 === bccomp(bcmul($spent, '-1'), $limit->amount) ? bcadd($spent, $limit->amount) : '0';
$this->report['budgets'][$budgetId]['budget_limits'][$limitId] ??= [
'budget_limit_id' => $limitId,
'start_date' => $limit->start_date,
'end_date' => $limit->end_date,
'budgeted' => $limit->amount,
'budgeted_pct' => '0',
'spent' => $spent,
'spent_pct' => '0',
'left' => $left,
'overspent' => $overspent,
'currency_id' => $currencyId,
'currency_code' => $limitCurrency->code,
'currency_name' => $limitCurrency->name,
'currency_symbol' => $limitCurrency->symbol,
'currency_decimal_places' => $limitCurrency->decimal_places,
];
// make sum information:
$this->report['sums'][$currencyId]
??= [
'budgeted' => '0',
'spent' => '0',
'left' => '0',
'overspent' => '0',
'currency_id' => $currencyId,
'currency_code' => $limitCurrency->code,
'currency_name' => $limitCurrency->name,
'currency_symbol' => $limitCurrency->symbol,
'currency_decimal_places' => $limitCurrency->decimal_places,
];
$this->report['sums'][$currencyId]['budgeted'] = bcadd((string)$this->report['sums'][$currencyId]['budgeted'], $limit->amount);
$this->report['sums'][$currencyId]['spent'] = bcadd((string)$this->report['sums'][$currencyId]['spent'], $spent);
$this->report['sums'][$currencyId]['left'] = bcadd((string)$this->report['sums'][$currencyId]['left'], bcadd($limit->amount, $spent));
$this->report['sums'][$currencyId]['overspent'] = bcadd((string)$this->report['sums'][$currencyId]['overspent'], $overspent);
}
}

View File

@@ -62,17 +62,17 @@ class CategoryReportGenerator
*/
public function operations(): void
{
$earnedWith = $this->opsRepository->listIncome($this->start, $this->end, $this->accounts);
$spentWith = $this->opsRepository->listExpenses($this->start, $this->end, $this->accounts);
$earnedWith = $this->opsRepository->listIncome($this->start, $this->end, $this->accounts);
$spentWith = $this->opsRepository->listExpenses($this->start, $this->end, $this->accounts);
// also transferred out and transferred into these accounts in this category:
$transferredIn = $this->opsRepository->listTransferredIn($this->start, $this->end, $this->accounts);
$transferredOut = $this->opsRepository->listTransferredOut($this->start, $this->end, $this->accounts);
$earnedWithout = $this->noCatRepository->listIncome($this->start, $this->end, $this->accounts);
$spentWithout = $this->noCatRepository->listExpenses($this->start, $this->end, $this->accounts);
$earnedWithout = $this->noCatRepository->listIncome($this->start, $this->end, $this->accounts);
$spentWithout = $this->noCatRepository->listExpenses($this->start, $this->end, $this->accounts);
$this->report = [
$this->report = [
'categories' => [],
'sums' => [],
];
@@ -83,17 +83,69 @@ class CategoryReportGenerator
}
}
/**
* Process one of the spent arrays from the operations method.
*/
private function processOpsArray(array $data): void
public function setAccounts(Collection $accounts): void
{
/**
* @var int $currencyId
* @var array $currencyRow
*/
foreach ($data as $currencyId => $currencyRow) {
$this->processCurrencyArray($currencyId, $currencyRow);
$this->accounts = $accounts;
}
public function setEnd(Carbon $end): void
{
$this->end = $end;
}
public function setStart(Carbon $start): void
{
$this->start = $start;
}
public function setUser(User $user): void
{
$this->noCatRepository->setUser($user);
$this->opsRepository->setUser($user);
}
private function processCategoryRow(int $currencyId, array $currencyRow, int $categoryId, array $categoryRow): void
{
$key = sprintf('%s-%s', $currencyId, $categoryId);
$this->report['categories'][$key] ??= [
'id' => $categoryId,
'title' => $categoryRow['name'],
'currency_id' => $currencyRow['currency_id'],
'currency_symbol' => $currencyRow['currency_symbol'],
'currency_name' => $currencyRow['currency_name'],
'currency_code' => $currencyRow['currency_code'],
'currency_decimal_places' => $currencyRow['currency_decimal_places'],
'spent' => '0',
'earned' => '0',
'sum' => '0',
];
// loop journals:
foreach ($categoryRow['transaction_journals'] as $journal) {
// sum of sums
$this->report['sums'][$currencyId]['sum'] = bcadd((string)$this->report['sums'][$currencyId]['sum'], (string)$journal['amount']);
// sum of spent:
$this->report['sums'][$currencyId]['spent'] = -1 === bccomp((string)$journal['amount'], '0') ? bcadd(
(string)$this->report['sums'][$currencyId]['spent'],
(string)$journal['amount']
) : $this->report['sums'][$currencyId]['spent'];
// sum of earned
$this->report['sums'][$currencyId]['earned'] = 1 === bccomp((string)$journal['amount'], '0') ? bcadd(
(string)$this->report['sums'][$currencyId]['earned'],
(string)$journal['amount']
) : $this->report['sums'][$currencyId]['earned'];
// sum of category
$this->report['categories'][$key]['sum'] = bcadd((string)$this->report['categories'][$key]['sum'], (string)$journal['amount']);
// total spent in category
$this->report['categories'][$key]['spent'] = -1 === bccomp((string)$journal['amount'], '0') ? bcadd(
(string)$this->report['categories'][$key]['spent'],
(string)$journal['amount']
) : $this->report['categories'][$key]['spent'];
// total earned in category
$this->report['categories'][$key]['earned'] = 1 === bccomp((string)$journal['amount'], '0') ? bcadd(
(string)$this->report['categories'][$key]['earned'],
(string)$journal['amount']
) : $this->report['categories'][$key]['earned'];
}
}
@@ -119,69 +171,17 @@ class CategoryReportGenerator
}
}
private function processCategoryRow(int $currencyId, array $currencyRow, int $categoryId, array $categoryRow): void
/**
* Process one of the spent arrays from the operations method.
*/
private function processOpsArray(array $data): void
{
$key = sprintf('%s-%s', $currencyId, $categoryId);
$this->report['categories'][$key] ??= [
'id' => $categoryId,
'title' => $categoryRow['name'],
'currency_id' => $currencyRow['currency_id'],
'currency_symbol' => $currencyRow['currency_symbol'],
'currency_name' => $currencyRow['currency_name'],
'currency_code' => $currencyRow['currency_code'],
'currency_decimal_places' => $currencyRow['currency_decimal_places'],
'spent' => '0',
'earned' => '0',
'sum' => '0',
];
// loop journals:
foreach ($categoryRow['transaction_journals'] as $journal) {
// sum of sums
$this->report['sums'][$currencyId]['sum'] = bcadd((string) $this->report['sums'][$currencyId]['sum'], (string) $journal['amount']);
// sum of spent:
$this->report['sums'][$currencyId]['spent'] = -1 === bccomp((string) $journal['amount'], '0') ? bcadd(
(string) $this->report['sums'][$currencyId]['spent'],
(string) $journal['amount']
) : $this->report['sums'][$currencyId]['spent'];
// sum of earned
$this->report['sums'][$currencyId]['earned'] = 1 === bccomp((string) $journal['amount'], '0') ? bcadd(
(string) $this->report['sums'][$currencyId]['earned'],
(string) $journal['amount']
) : $this->report['sums'][$currencyId]['earned'];
// sum of category
$this->report['categories'][$key]['sum'] = bcadd((string) $this->report['categories'][$key]['sum'], (string) $journal['amount']);
// total spent in category
$this->report['categories'][$key]['spent'] = -1 === bccomp((string) $journal['amount'], '0') ? bcadd(
(string) $this->report['categories'][$key]['spent'],
(string) $journal['amount']
) : $this->report['categories'][$key]['spent'];
// total earned in category
$this->report['categories'][$key]['earned'] = 1 === bccomp((string) $journal['amount'], '0') ? bcadd(
(string) $this->report['categories'][$key]['earned'],
(string) $journal['amount']
) : $this->report['categories'][$key]['earned'];
/**
* @var int $currencyId
* @var array $currencyRow
*/
foreach ($data as $currencyId => $currencyRow) {
$this->processCurrencyArray($currencyId, $currencyRow);
}
}
public function setAccounts(Collection $accounts): void
{
$this->accounts = $accounts;
}
public function setEnd(Carbon $end): void
{
$this->end = $end;
}
public function setStart(Carbon $start): void
{
$this->start = $start;
}
public function setUser(User $user): void
{
$this->noCatRepository->setUser($user);
$this->opsRepository->setUser($user);
}
}

View File

@@ -43,26 +43,19 @@ class TransactionSummarizer
}
}
public function setUser(User $user): void
{
$this->user = $user;
$this->default = Amount::getPrimaryCurrencyByUserGroup($user->userGroup);
$this->convertToPrimary = Amount::convertToPrimary($user);
}
public function groupByCurrencyId(array $journals, string $method = 'negative', bool $includeForeign = true): array
{
Log::debug(sprintf('Now in groupByCurrencyId([%d journals], "%s", %s)', count($journals), $method, var_export($includeForeign, true)));
$array = [];
foreach ($journals as $journal) {
$field = 'amount';
$field = 'amount';
// grab default currency information.
$currencyId = (int) $journal['currency_id'];
$currencyName = $journal['currency_name'];
$currencySymbol = $journal['currency_symbol'];
$currencyCode = $journal['currency_code'];
$currencyDecimalPlaces = $journal['currency_decimal_places'];
$currencyId = (int)$journal['currency_id'];
$currencyName = $journal['currency_name'];
$currencySymbol = $journal['currency_symbol'];
$currencyCode = $journal['currency_code'];
$currencyDecimalPlaces = $journal['currency_decimal_places'];
// prepare foreign currency info:
$foreignCurrencyId = 0;
@@ -74,8 +67,8 @@ class TransactionSummarizer
if ($this->convertToPrimary) {
// Log::debug('convertToPrimary is true.');
// if convert to primary currency, use the primary currency amount yes or no?
$usePrimary = $this->default->id !== (int) $journal['currency_id'];
$useForeign = $this->default->id === (int) $journal['foreign_currency_id'];
$usePrimary = $this->default->id !== (int)$journal['currency_id'];
$useForeign = $this->default->id === (int)$journal['foreign_currency_id'];
if ($usePrimary) {
// Log::debug(sprintf('Journal #%d switches to primary currency amount (original is %s)', $journal['transaction_journal_id'], $journal['currency_code']));
$field = 'pc_amount';
@@ -88,7 +81,7 @@ class TransactionSummarizer
if ($useForeign) {
// Log::debug(sprintf('Journal #%d switches to foreign amount (foreign is %s)', $journal['transaction_journal_id'], $journal['foreign_currency_code']));
$field = 'foreign_amount';
$currencyId = (int) $journal['foreign_currency_id'];
$currencyId = (int)$journal['foreign_currency_id'];
$currencyName = $journal['foreign_currency_name'];
$currencySymbol = $journal['foreign_currency_symbol'];
$currencyCode = $journal['foreign_currency_code'];
@@ -98,7 +91,7 @@ class TransactionSummarizer
if (!$this->convertToPrimary) {
// Log::debug('convertToPrimary is false.');
// use foreign amount?
$foreignCurrencyId = (int) $journal['foreign_currency_id'];
$foreignCurrencyId = (int)$journal['foreign_currency_id'];
if (0 !== $foreignCurrencyId) {
Log::debug(sprintf('Journal #%d also includes foreign amount (foreign is "%s")', $journal['transaction_journal_id'], $journal['foreign_currency_code']));
$foreignCurrencyName = $journal['foreign_currency_name'];
@@ -109,7 +102,7 @@ class TransactionSummarizer
}
// first process normal amount
$amount = (string) ($journal[$field] ?? '0');
$amount = (string)($journal[$field] ?? '0');
$array[$currencyId] ??= [
'sum' => '0',
'currency_id' => $currencyId,
@@ -128,7 +121,7 @@ class TransactionSummarizer
// then process foreign amount, if it exists.
if (0 !== $foreignCurrencyId && true === $includeForeign) {
$amount = (string) ($journal['foreign_amount'] ?? '0');
$amount = (string)($journal['foreign_amount'] ?? '0');
$array[$foreignCurrencyId] ??= [
'sum' => '0',
'currency_id' => $foreignCurrencyId,
@@ -186,7 +179,7 @@ class TransactionSummarizer
if ($convertToPrimary && $journal['currency_id'] !== $primary->id && $primary->id === $journal['foreign_currency_id']) {
$field = 'foreign_amount';
}
$key = sprintf('%s-%s', $journal[$idKey], $currencyId);
$key = sprintf('%s-%s', $journal[$idKey], $currencyId);
// sum it all up or create a new array.
$array[$key] ??= [
'id' => $journal[$idKey],
@@ -200,15 +193,15 @@ class TransactionSummarizer
];
// add the data from the $field to the array.
$array[$key]['sum'] = bcadd($array[$key]['sum'], Steam::{$method}((string) ($journal[$field] ?? '0'))); // @phpstan-ignore-line
$array[$key]['sum'] = bcadd($array[$key]['sum'], Steam::{$method}((string)($journal[$field] ?? '0'))); // @phpstan-ignore-line
Log::debug(sprintf('Field for transaction #%d is "%s" (%s). Sum: %s', $journal['transaction_group_id'], $currencyCode, $field, $array[$key]['sum']));
// also do foreign amount, but only when convertToPrimary is false (otherwise we have it already)
// or when convertToPrimary is true and the foreign currency is ALSO not the default currency.
if ((!$convertToPrimary || $journal['foreign_currency_id'] !== $primary->id) && 0 !== (int) $journal['foreign_currency_id']) {
if ((!$convertToPrimary || $journal['foreign_currency_id'] !== $primary->id) && 0 !== (int)$journal['foreign_currency_id']) {
Log::debug(sprintf('Use foreign amount from transaction #%d: %s %s. Sum: %s', $journal['transaction_group_id'], $currencyCode, $journal['foreign_amount'], $array[$key]['sum']));
$key = sprintf('%s-%s', $journal[$idKey], $journal['foreign_currency_id']);
$array[$key] ??= [
$array[$key] ??= [
'id' => $journal[$idKey],
'name' => $journal[$nameKey],
'sum' => '0',
@@ -218,7 +211,7 @@ class TransactionSummarizer
'currency_code' => $journal['foreign_currency_code'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
];
$array[$key]['sum'] = bcadd($array[$key]['sum'], Steam::{$method}((string) $journal['foreign_amount'])); // @phpstan-ignore-line
$array[$key]['sum'] = bcadd($array[$key]['sum'], Steam::{$method}((string)$journal['foreign_amount'])); // @phpstan-ignore-line
}
}
@@ -230,4 +223,11 @@ class TransactionSummarizer
Log::debug(sprintf('Overrule convertToPrimary to become %s', var_export($convertToPrimary, true)));
$this->convertToPrimary = $convertToPrimary;
}
public function setUser(User $user): void
{
$this->user = $user;
$this->default = Amount::getPrimaryCurrencyByUserGroup($user->userGroup);
$this->convertToPrimary = Amount::convertToPrimary($user);
}
}