Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -35,7 +35,6 @@ use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use JsonException;
/**
* Class BudgetReportGenerator
@@ -74,6 +73,7 @@ class BudgetReportGenerator
{
$spent = $this->opsRepository->listExpenses($this->start, $this->end, $this->accounts, $this->budgets);
$this->report = [];
/** @var Account $account */
foreach ($this->accounts as $account) {
$accountId = $account->id;
@@ -91,48 +91,6 @@ class BudgetReportGenerator
}
}
/**
* Process each row of expenses collected for the "Account per budget" partial
*
* @param array $expenses
*/
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.
*
* @param array $expenses
* @param array $budget
*/
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], $journal['amount']);
}
}
/**
* Generates the data necessary to create the card that displays
* the budget overview in the general report.
@@ -149,12 +107,88 @@ class BudgetReportGenerator
$this->percentageReport();
}
public function getReport(): array
{
return $this->report;
}
public function setAccounts(Collection $accounts): void
{
$this->accounts = $accounts;
}
public function setBudgets(Collection $budgets): void
{
$this->budgets = $budgets;
}
public function setEnd(Carbon $end): void
{
$this->end = $end;
}
public function setStart(Carbon $start): void
{
$this->start = $start;
}
/**
* @throws FireflyException
* @throws \JsonException
*/
public function setUser(User $user): void
{
$this->repository->setUser($user);
$this->blRepository->setUser($user);
$this->opsRepository->setUser($user);
$this->nbRepository->setUser($user);
$this->currency = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
}
/**
* 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], $journal['amount']);
}
}
/**
* 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);
@@ -163,8 +197,6 @@ class BudgetReportGenerator
/**
* Process expenses etc. for a single budget for the budgets block on the default report.
*
* @param Budget $budget
*/
private function processBudget(Budget $budget): void
{
@@ -178,6 +210,7 @@ class BudgetReportGenerator
// 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);
@@ -186,9 +219,6 @@ class BudgetReportGenerator
/**
* Process a single budget limit for the budgets block on the default report.
*
* @param Budget $budget
* @param BudgetLimit $limit
*/
private function processLimit(Budget $budget, BudgetLimit $limit): void
{
@@ -221,16 +251,16 @@ class BudgetReportGenerator
// 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,
];
'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($this->report['sums'][$currencyId]['budgeted'], $limit->amount);
$this->report['sums'][$currencyId]['spent'] = bcadd($this->report['sums'][$currencyId]['spent'], $spent);
$this->report['sums'][$currencyId]['left'] = bcadd($this->report['sums'][$currencyId]['left'], bcadd($limit->amount, $spent));
@@ -320,59 +350,4 @@ class BudgetReportGenerator
}
}
}
/**
* @return array
*/
public function getReport(): array
{
return $this->report;
}
/**
* @param Collection $accounts
*/
public function setAccounts(Collection $accounts): void
{
$this->accounts = $accounts;
}
/**
* @param Collection $budgets
*/
public function setBudgets(Collection $budgets): void
{
$this->budgets = $budgets;
}
/**
* @param Carbon $end
*/
public function setEnd(Carbon $end): void
{
$this->end = $end;
}
/**
* @param Carbon $start
*/
public function setStart(Carbon $start): void
{
$this->start = $start;
}
/**
* @param User $user
*
* @throws FireflyException
* @throws JsonException
*/
public function setUser(User $user): void
{
$this->repository->setUser($user);
$this->blRepository->setUser($user);
$this->opsRepository->setUser($user);
$this->nbRepository->setUser($user);
$this->currency = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
}
}

View File

@@ -50,9 +50,6 @@ class CategoryReportGenerator
$this->noCatRepository = app(NoCategoryRepositoryInterface::class);
}
/**
* @return array
*/
public function getReport(): array
{
return $this->report;
@@ -85,10 +82,29 @@ class CategoryReportGenerator
}
}
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);
}
/**
* Process one of the spent arrays from the operations method.
*
* @param array $data
*/
private function processOpsArray(array $data): void
{
@@ -101,10 +117,6 @@ class CategoryReportGenerator
}
}
/**
* @param int $currencyId
* @param array $currencyRow
*/
private function processCurrencyArray(int $currencyId, array $currencyRow): void
{
$this->report['sums'][$currencyId] ??= [
@@ -127,12 +139,6 @@ class CategoryReportGenerator
}
}
/**
* @param int $currencyId
* @param array $currencyRow
* @param int $categoryId
* @param array $categoryRow
*/
private function processCategoryRow(int $currencyId, array $currencyRow, int $categoryId, array $categoryRow): void
{
$key = sprintf('%s-%s', $currencyId, $categoryId);
@@ -177,37 +183,4 @@ class CategoryReportGenerator
) : $this->report['categories'][$key]['earned'];
}
}
/**
* @param Collection $accounts
*/
public function setAccounts(Collection $accounts): void
{
$this->accounts = $accounts;
}
/**
* @param Carbon $end
*/
public function setEnd(Carbon $end): void
{
$this->end = $end;
}
/**
* @param Carbon $start
*/
public function setStart(Carbon $start): void
{
$this->start = $start;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->noCatRepository->setUser($user);
$this->opsRepository->setUser($user);
}
}