Various code cleanup and sorting.

This commit is contained in:
James Cole
2025-03-08 19:55:21 +01:00
parent 2d3d3bc0a4
commit 30b7e17b6f
60 changed files with 1402 additions and 1475 deletions

View File

@@ -63,8 +63,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
$search->whereLike('name', sprintf('%%%s', $query));
}
$search->orderBy('order', 'ASC')
->orderBy('name', 'ASC')->where('active', true)
;
->orderBy('name', 'ASC')->where('active', true);
return $search->take($limit)->get();
}
@@ -76,8 +75,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
$search->whereLike('name', sprintf('%s%%', $query));
}
$search->orderBy('order', 'ASC')
->orderBy('name', 'ASC')->where('active', true)
;
->orderBy('name', 'ASC')->where('active', true);
return $search->take($limit)->get();
}
@@ -85,7 +83,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
public function budgetedInPeriod(Carbon $start, Carbon $end): array
{
app('log')->debug(sprintf('Now in budgetedInPeriod("%s", "%s")', $start->format('Y-m-d'), $end->format('Y-m-d')));
$return = [];
$return = [];
/** @var BudgetLimitRepository $limitRepository */
$limitRepository = app(BudgetLimitRepository::class);
@@ -102,9 +100,9 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
/** @var BudgetLimit $limit */
foreach ($limits as $limit) {
app('log')->debug(sprintf('Budget limit #%d', $limit->id));
$currency = $limit->transactionCurrency;
$rate = $converter->getCurrencyRate($currency, $defaultCurrency, $end);
$currencyCode = $currency->code;
$currency = $limit->transactionCurrency;
$rate = $converter->getCurrencyRate($currency, $defaultCurrency, $end);
$currencyCode = $currency->code;
$return[$currencyCode] ??= [
'currency_id' => (string) $currency->id,
'currency_name' => $currency->name,
@@ -159,10 +157,9 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
public function getActiveBudgets(): Collection
{
return $this->user->budgets()->where('active', true)
->orderBy('order', 'ASC')
->orderBy('name', 'ASC')
->get()
;
->orderBy('order', 'ASC')
->orderBy('name', 'ASC')
->get();
}
/**
@@ -202,19 +199,19 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
public function budgetedInPeriodForBudget(Budget $budget, Carbon $start, Carbon $end): array
{
app('log')->debug(sprintf('Now in budgetedInPeriod(#%d, "%s", "%s")', $budget->id, $start->format('Y-m-d'), $end->format('Y-m-d')));
$return = [];
$return = [];
/** @var BudgetLimitRepository $limitRepository */
$limitRepository = app(BudgetLimitRepository::class);
$limitRepository->setUser($this->user);
app('log')->debug(sprintf('Budget #%d: "%s"', $budget->id, $budget->name));
$limits = $limitRepository->getBudgetLimits($budget, $start, $end);
$limits = $limitRepository->getBudgetLimits($budget, $start, $end);
/** @var BudgetLimit $limit */
foreach ($limits as $limit) {
app('log')->debug(sprintf('Budget limit #%d', $limit->id));
$currency = $limit->transactionCurrency;
$currency = $limit->transactionCurrency;
$return[$currency->id] ??= [
'id' => (string) $currency->id,
'name' => $currency->name,
@@ -283,7 +280,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
{
app('log')->debug('Now in update()');
$oldName = $budget->name;
$oldName = $budget->name;
if (array_key_exists('name', $data)) {
$budget->name = $data['name'];
$this->updateRuleActions($oldName, $budget->name);
@@ -298,7 +295,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
$budget->save();
// update or create auto-budget:
$autoBudget = $this->getAutoBudget($budget);
$autoBudget = $this->getAutoBudget($budget);
// first things first: delete when no longer required:
$autoBudgetType = array_key_exists('auto_budget_type', $data) ? $data['auto_budget_type'] : null;
@@ -324,11 +321,10 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
{
$types = ['set_budget'];
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*'])
;
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*']);
app('log')->debug(sprintf('Found %d actions to update.', $actions->count()));
/** @var RuleAction $action */
@@ -343,11 +339,10 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
{
$types = ['budget_is'];
$triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_triggers.trigger_type', $types)
->where('rule_triggers.trigger_value', $oldName)
->get(['rule_triggers.*'])
;
->where('rules.user_id', $this->user->id)
->whereIn('rule_triggers.trigger_type', $types)
->where('rule_triggers.trigger_value', $oldName)
->get(['rule_triggers.*']);
app('log')->debug(sprintf('Found %d triggers to update.', $triggers->count()));
/** @var RuleTrigger $trigger */
@@ -391,7 +386,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
$autoBudget = $this->getAutoBudget($budget);
// grab default currency:
$currency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
$currency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
if (null === $autoBudget) {
// at this point it's a blind assumption auto_budget_type is 1 or 2.
@@ -471,8 +466,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
public function getBudgets(): Collection
{
return $this->user->budgets()->orderBy('order', 'ASC')
->orderBy('name', 'ASC')->get()
;
->orderBy('name', 'ASC')->get();
}
public function destroyAutoBudget(Budget $budget): void
@@ -530,7 +524,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
public function getAttachments(Budget $budget): Collection
{
$set = $budget->attachments()->get();
$set = $budget->attachments()->get();
$disk = Storage::disk('upload');
@@ -556,9 +550,8 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
public function getInactiveBudgets(): Collection
{
return $this->user->budgets()
->orderBy('order', 'ASC')
->orderBy('name', 'ASC')->where('active', 0)->get()
;
->orderBy('order', 'ASC')
->orderBy('name', 'ASC')->where('active', 0)->get();
}
public function getNoteText(Budget $budget): ?string
@@ -578,8 +571,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
$search->whereLike('name', sprintf('%%%s%%', $query));
}
$search->orderBy('order', 'ASC')
->orderBy('name', 'ASC')->where('active', true)
;
->orderBy('name', 'ASC')->where('active', true);
return $search->take($limit)->get();
}
@@ -599,8 +591,8 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
// exclude specific liabilities
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($this->user);
$subset = $repository->getAccountsByType(config('firefly.valid_liabilities'));
$selection = new Collection();
$subset = $repository->getAccountsByType(config('firefly.valid_liabilities'));
$selection = new Collection();
/** @var Account $account */
foreach ($subset as $account) {
@@ -611,20 +603,19 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
// start collecting:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)
->setRange($start, $end)
->excludeDestinationAccounts($selection)
->setTypes([TransactionTypeEnum::WITHDRAWAL->value])
->setBudgets($this->getActiveBudgets())
;
->setRange($start, $end)
->excludeDestinationAccounts($selection)
->setTypes([TransactionTypeEnum::WITHDRAWAL->value])
->setBudgets($this->getActiveBudgets());
$journals = $collector->getExtractedJournals();
$array = [];
$journals = $collector->getExtractedJournals();
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
$array[$currencyId] ??= [
'id' => (string) $currencyId,
'name' => $journal['currency_name'],
'symbol' => $journal['currency_symbol'],
@@ -635,9 +626,9 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
// also do foreign amount:
$foreignId = (int) $journal['foreign_currency_id'];
$foreignId = (int) $journal['foreign_currency_id'];
if (0 !== $foreignId) {
$array[$foreignId] ??= [
$array[$foreignId] ??= [
'id' => (string) $foreignId,
'name' => $journal['foreign_currency_name'],
'symbol' => $journal['foreign_currency_symbol'],
@@ -661,8 +652,8 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
// exclude specific liabilities
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($this->user);
$subset = $repository->getAccountsByType(config('firefly.valid_liabilities'));
$selection = new Collection();
$subset = $repository->getAccountsByType(config('firefly.valid_liabilities'));
$selection = new Collection();
/** @var Account $account */
foreach ($subset as $account) {
@@ -673,20 +664,19 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
// start collecting:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)
->setRange($start, $end)
->excludeDestinationAccounts($selection)
->setTypes([TransactionTypeEnum::WITHDRAWAL->value])
->setBudget($budget)
;
->setRange($start, $end)
->excludeDestinationAccounts($selection)
->setTypes([TransactionTypeEnum::WITHDRAWAL->value])
->setBudget($budget);
$journals = $collector->getExtractedJournals();
$array = [];
$journals = $collector->getExtractedJournals();
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
$array[$currencyId] ??= [
'id' => (string) $currencyId,
'name' => $journal['currency_name'],
'symbol' => $journal['currency_symbol'],
@@ -697,9 +687,9 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
// also do foreign amount:
$foreignId = (int) $journal['foreign_currency_id'];
$foreignId = (int) $journal['foreign_currency_id'];
if (0 !== $foreignId) {
$array[$foreignId] ??= [
$array[$foreignId] ??= [
'id' => (string) $foreignId,
'name' => $journal['foreign_currency_name'],
'symbol' => $journal['foreign_currency_symbol'],
@@ -721,7 +711,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
*/
public function store(array $data): Budget
{
$order = $this->getMaxOrder();
$order = $this->getMaxOrder();
try {
$newBudget = Budget::create(
@@ -748,7 +738,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
if (!array_key_exists('auto_budget_type', $data) || !array_key_exists('auto_budget_amount', $data) || !array_key_exists('auto_budget_period', $data)) {
return $newBudget;
}
$type = $data['auto_budget_type'];
$type = $data['auto_budget_type'];
if ('none' === $type) {
return $newBudget;
}
@@ -767,8 +757,8 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
}
/** @var CurrencyRepositoryInterface $repos */
$repos = app(CurrencyRepositoryInterface::class);
$currency = null;
$repos = app(CurrencyRepositoryInterface::class);
$currency = null;
if (array_key_exists('currency_id', $data)) {
$currency = $repos->find((int) $data['currency_id']);
}
@@ -779,7 +769,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
$currency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
}
$autoBudget = new AutoBudget();
$autoBudget = new AutoBudget();
$autoBudget->budget()->associate($newBudget);
$autoBudget->transaction_currency_id = $currency->id;
$autoBudget->auto_budget_type = $type;
@@ -788,11 +778,11 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface
$autoBudget->save();
// create initial budget limit.
$today = today(config('app.timezone'));
$start = app('navigation')->startOfPeriod($today, $autoBudget->period);
$end = app('navigation')->endOfPeriod($start, $autoBudget->period);
$today = today(config('app.timezone'));
$start = app('navigation')->startOfPeriod($today, $autoBudget->period);
$end = app('navigation')->endOfPeriod($start, $autoBudget->period);
$limitRepos = app(BudgetLimitRepositoryInterface::class);
$limitRepos = app(BudgetLimitRepositoryInterface::class);
$limitRepos->setUser($this->user);
$limitRepos->store(
[