mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 08:35:00 +00:00
Code cleanup
This commit is contained in:
@@ -44,10 +44,10 @@ trait UserGroupDetectable
|
||||
$userGroup = request()->route()?->parameter('userGroup');
|
||||
if (null === $userGroup) {
|
||||
app('log')->debug('Request class has no userGroup parameter, but perhaps there is a parameter.');
|
||||
$userGroupId = (int)request()->get('user_group_id');
|
||||
$userGroupId = (int) request()->get('user_group_id');
|
||||
if (0 === $userGroupId) {
|
||||
app('log')->debug(sprintf('Request class has no user_group_id parameter, grab default from user (group #%d).', $user->user_group_id));
|
||||
$userGroupId = (int)$user->user_group_id;
|
||||
$userGroupId = (int) $user->user_group_id;
|
||||
}
|
||||
$userGroup = UserGroup::find($userGroupId);
|
||||
if (null === $userGroup) {
|
||||
|
@@ -44,17 +44,16 @@ use Illuminate\Support\Facades\Log;
|
||||
*/
|
||||
class AccountEnrichment implements EnrichmentInterface
|
||||
{
|
||||
private array $balances;
|
||||
private Collection $collection;
|
||||
private array $currencies;
|
||||
private array $objectGroups;
|
||||
private array $grouped;
|
||||
private array $balances;
|
||||
private TransactionCurrency $default;
|
||||
private ?Carbon $start;
|
||||
private ?Carbon $end;
|
||||
|
||||
private AccountRepositoryInterface $repository;
|
||||
private CurrencyRepositoryInterface $currencyRepository;
|
||||
private TransactionCurrency $default;
|
||||
private ?Carbon $end;
|
||||
private array $grouped;
|
||||
private array $objectGroups;
|
||||
private AccountRepositoryInterface $repository;
|
||||
private ?Carbon $start;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -64,6 +63,16 @@ class AccountEnrichment implements EnrichmentInterface
|
||||
$this->end = null;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function enrichSingle(Model $model): Account
|
||||
{
|
||||
Log::debug(__METHOD__);
|
||||
$collection = new Collection([$model]);
|
||||
$collection = $this->enrich($collection);
|
||||
|
||||
return $collection->first();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
/**
|
||||
* Do the actual enrichment.
|
||||
@@ -111,6 +120,53 @@ class AccountEnrichment implements EnrichmentInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO this method refers to a single-use method inside Steam that could be moved here.
|
||||
*/
|
||||
private function collectAccountTypes(): void
|
||||
{
|
||||
$accountTypes = $this->repository->getAccountTypes($this->collection);
|
||||
$types = [];
|
||||
|
||||
/** @var AccountType $row */
|
||||
foreach ($accountTypes as $row) {
|
||||
$types[$row->id] = $row->type;
|
||||
}
|
||||
$this->collection->transform(function (Account $account) use ($types) {
|
||||
$account->account_type_string = $types[$account->id];
|
||||
|
||||
return $account;
|
||||
});
|
||||
}
|
||||
|
||||
private function collectMetaData(): void
|
||||
{
|
||||
$metaFields = $this->repository->getMetaValues($this->collection, ['is_multi_currency', 'currency_id', 'account_role', 'account_number', 'liability_direction', 'interest', 'interest_period', 'current_debt']);
|
||||
$currencyIds = $metaFields->where('name', 'currency_id')->pluck('data')->toArray();
|
||||
|
||||
$currencies = [];
|
||||
foreach ($this->currencyRepository->getByIds($currencyIds) as $currency) {
|
||||
$id = $currency->id;
|
||||
$currencies[$id] = $currency;
|
||||
}
|
||||
|
||||
$this->collection->transform(function (Account $account) use ($metaFields, $currencies) {
|
||||
$set = $metaFields->where('account_id', $account->id);
|
||||
foreach ($set as $entry) {
|
||||
$account->{$entry->name} = $entry->data;
|
||||
if ('currency_id' === $entry->name) {
|
||||
$id = (int) $entry->data;
|
||||
$account->currency_name = $currencies[$id]?->name;
|
||||
$account->currency_code = $currencies[$id]?->code;
|
||||
$account->currency_symbol = $currencies[$id]?->symbol;
|
||||
$account->currency_decimal_places = $currencies[$id]?->decimal_places;
|
||||
}
|
||||
}
|
||||
|
||||
return $account;
|
||||
});
|
||||
}
|
||||
|
||||
private function getMetaBalances(): void
|
||||
{
|
||||
$this->balances = Balance::getAccountBalances($this->collection, today());
|
||||
@@ -181,73 +237,6 @@ class AccountEnrichment implements EnrichmentInterface
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO this method refers to a single-use method inside Steam that could be moved here.
|
||||
*/
|
||||
private function collectAccountTypes(): void
|
||||
{
|
||||
$accountTypes = $this->repository->getAccountTypes($this->collection);
|
||||
$types = [];
|
||||
|
||||
/** @var AccountType $row */
|
||||
foreach ($accountTypes as $row) {
|
||||
$types[$row->id] = $row->type;
|
||||
}
|
||||
$this->collection->transform(function (Account $account) use ($types) {
|
||||
$account->account_type_string = $types[$account->id];
|
||||
|
||||
return $account;
|
||||
});
|
||||
}
|
||||
|
||||
private function collectMetaData(): void
|
||||
{
|
||||
$metaFields = $this->repository->getMetaValues($this->collection, ['is_multi_currency', 'currency_id', 'account_role', 'account_number', 'liability_direction', 'interest', 'interest_period', 'current_debt']);
|
||||
$currencyIds = $metaFields->where('name', 'currency_id')->pluck('data')->toArray();
|
||||
|
||||
$currencies = [];
|
||||
foreach ($this->currencyRepository->getByIds($currencyIds) as $currency) {
|
||||
$id = $currency->id;
|
||||
$currencies[$id] = $currency;
|
||||
}
|
||||
|
||||
$this->collection->transform(function (Account $account) use ($metaFields, $currencies) {
|
||||
$set = $metaFields->where('account_id', $account->id);
|
||||
foreach ($set as $entry) {
|
||||
$account->{$entry->name} = $entry->data;
|
||||
if ('currency_id' === $entry->name) {
|
||||
$id = (int) $entry->data;
|
||||
$account->currency_name = $currencies[$id]?->name;
|
||||
$account->currency_code = $currencies[$id]?->code;
|
||||
$account->currency_symbol = $currencies[$id]?->symbol;
|
||||
$account->currency_decimal_places = $currencies[$id]?->decimal_places;
|
||||
}
|
||||
}
|
||||
|
||||
return $account;
|
||||
});
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function enrichSingle(Model $model): Account
|
||||
{
|
||||
Log::debug(__METHOD__);
|
||||
$collection = new Collection([$model]);
|
||||
$collection = $this->enrich($collection);
|
||||
|
||||
return $collection->first();
|
||||
}
|
||||
|
||||
public function setStart(?Carbon $start): void
|
||||
{
|
||||
$this->start = $start;
|
||||
}
|
||||
|
||||
public function setEnd(?Carbon $end): void
|
||||
{
|
||||
$this->end = $end;
|
||||
}
|
||||
|
||||
private function getObjectGroups(): void
|
||||
{
|
||||
$set = \DB::table('object_groupables')
|
||||
@@ -279,4 +268,14 @@ class AccountEnrichment implements EnrichmentInterface
|
||||
return $account;
|
||||
});
|
||||
}
|
||||
|
||||
public function setEnd(?Carbon $end): void
|
||||
{
|
||||
$this->end = $end;
|
||||
}
|
||||
|
||||
public function setStart(?Carbon $start): void
|
||||
{
|
||||
$this->start = $start;
|
||||
}
|
||||
}
|
||||
|
@@ -35,72 +35,6 @@ trait ExpandsQuery
|
||||
{
|
||||
use AccountFilter;
|
||||
|
||||
final protected function addPagination(Builder $query, array $pagination): Builder
|
||||
{
|
||||
$skip = ($pagination['number'] - 1) * $pagination['size'];
|
||||
|
||||
return $query->skip($skip)->take($pagination['size']);
|
||||
}
|
||||
|
||||
final protected function addSortParams(string $class, Builder $query, ?SortFields $sort): Builder
|
||||
{
|
||||
$config = config('api.valid_query_sort')[$class] ?? [];
|
||||
if (null === $sort) {
|
||||
return $query;
|
||||
}
|
||||
foreach ($sort->all() as $sortField) {
|
||||
if (in_array($sortField->name(), $config, true)) {
|
||||
$query->orderBy($sortField->name(), $sortField->isAscending() ? 'ASC' : 'DESC');
|
||||
}
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
private function parseAccountTypeFilter(array $value): array
|
||||
{
|
||||
$return = [];
|
||||
foreach ($value as $entry) {
|
||||
$return = array_merge($return, $this->mapAccountTypes($entry));
|
||||
}
|
||||
|
||||
return array_unique($return);
|
||||
}
|
||||
|
||||
private function parseAllFilters(string $class, FilterParameters $filters): array
|
||||
{
|
||||
$config = config('api.valid_api_filters')[$class];
|
||||
$parsed = [];
|
||||
foreach ($filters->all() as $filter) {
|
||||
$key = $filter->key();
|
||||
if (!in_array($key, $config, true)) {
|
||||
continue;
|
||||
}
|
||||
// make array if not array:
|
||||
$value = $filter->value();
|
||||
if (null === $value) {
|
||||
continue;
|
||||
}
|
||||
if (!is_array($value)) {
|
||||
$value = [$value];
|
||||
}
|
||||
|
||||
switch ($filter->key()) {
|
||||
case 'name':
|
||||
$parsed['name'] = $value;
|
||||
|
||||
break;
|
||||
|
||||
case 'type':
|
||||
$parsed['type'] = $this->parseAccountTypeFilter($value);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
final protected function addFilterParams(string $class, Builder $query, ?FilterParameters $filters): Builder
|
||||
{
|
||||
Log::debug(__METHOD__);
|
||||
@@ -137,4 +71,70 @@ trait ExpandsQuery
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
private function parseAllFilters(string $class, FilterParameters $filters): array
|
||||
{
|
||||
$config = config('api.valid_api_filters')[$class];
|
||||
$parsed = [];
|
||||
foreach ($filters->all() as $filter) {
|
||||
$key = $filter->key();
|
||||
if (!in_array($key, $config, true)) {
|
||||
continue;
|
||||
}
|
||||
// make array if not array:
|
||||
$value = $filter->value();
|
||||
if (null === $value) {
|
||||
continue;
|
||||
}
|
||||
if (!is_array($value)) {
|
||||
$value = [$value];
|
||||
}
|
||||
|
||||
switch ($filter->key()) {
|
||||
case 'name':
|
||||
$parsed['name'] = $value;
|
||||
|
||||
break;
|
||||
|
||||
case 'type':
|
||||
$parsed['type'] = $this->parseAccountTypeFilter($value);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
private function parseAccountTypeFilter(array $value): array
|
||||
{
|
||||
$return = [];
|
||||
foreach ($value as $entry) {
|
||||
$return = array_merge($return, $this->mapAccountTypes($entry));
|
||||
}
|
||||
|
||||
return array_unique($return);
|
||||
}
|
||||
|
||||
final protected function addPagination(Builder $query, array $pagination): Builder
|
||||
{
|
||||
$skip = ($pagination['number'] - 1) * $pagination['size'];
|
||||
|
||||
return $query->skip($skip)->take($pagination['size']);
|
||||
}
|
||||
|
||||
final protected function addSortParams(string $class, Builder $query, ?SortFields $sort): Builder
|
||||
{
|
||||
$config = config('api.valid_query_sort')[$class] ?? [];
|
||||
if (null === $sort) {
|
||||
return $query;
|
||||
}
|
||||
foreach ($sort->all() as $sortField) {
|
||||
if (in_array($sortField->name(), $config, true)) {
|
||||
$query->orderBy($sortField->name(), $sortField->isAscending() ? 'ASC' : 'DESC');
|
||||
}
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
|
@@ -82,8 +82,8 @@ trait SortsQueryResults
|
||||
if (Account::class === $class && 'last_activity' === $field->name()) {
|
||||
$ascending = $field->isAscending();
|
||||
$collection = $collection->sort(function (Account $left, Account $right) use ($ascending): int {
|
||||
$leftNr = (int)$left->last_activity?->format('U');
|
||||
$rightNr = (int)$right->last_activity?->format('U');
|
||||
$leftNr = (int) $left->last_activity?->format('U');
|
||||
$rightNr = (int) $right->last_activity?->format('U');
|
||||
if ($ascending) {
|
||||
return $leftNr <=> $rightNr;
|
||||
}
|
||||
|
Reference in New Issue
Block a user