Skip to content

Firefly III: a personal finances manager

Move a selection of code to group logic.

James Cole02961105338c
diff --git a/app/Api/V1/Controllers/Chart/AccountController.php b/app/Api/V1/Controllers/Chart/AccountController.php
index 06380836b0..fc6e25ade5 100644
--- a/app/Api/V1/Controllers/Chart/AccountController.php
+++ b/app/Api/V1/Controllers/Chart/AccountController.php
@@ -77,7 +77,8 @@ final class AccountController extends Controller
public function overview(ChartRequest $request): JsonResponse
{
$queryParameters = $request->getParameters();
- $accounts = $this->getAccountList($queryParameters);
+ $accounts = $this->getAccountListForGroup($queryParameters);
+
// move date to end of day
$queryParameters['start']->startOfDay();
diff --git a/app/Api/V1/Controllers/Summary/BasicController.php b/app/Api/V1/Controllers/Summary/BasicController.php
index 5ba1809629..daa01b06c7 100644
--- a/app/Api/V1/Controllers/Summary/BasicController.php
+++ b/app/Api/V1/Controllers/Summary/BasicController.php
@@ -53,6 +53,7 @@ use Illuminate\Support\Facades\Log;
*/
final class BasicController extends Controller
{
+ protected array $acceptedRoles = [];
private AvailableBudgetRepositoryInterface $abRepository;
private AccountRepositoryInterface $accountRepository;
private BillRepositoryInterface $billRepository;
diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php
index 415cd5331a..68c293e4ed 100644
--- a/app/Http/Controllers/HomeController.php
+++ b/app/Http/Controllers/HomeController.php
@@ -146,7 +146,7 @@ final class HomeController extends Controller
$subTitle = (string) trans('firefly.welcome_back');
$subTitleIcon = 'bi-piggy-bank';
$transactions = [];
- $frontpage = Preferences::getFresh('frontpageAccounts', $repository->getAccountsByType([AccountTypeEnum::ASSET->value])->pluck('id')->toArray());
+ $frontpage = Preferences::getFresh('frontpageAccounts', $repository->getAccountsByTypeForGroup([AccountTypeEnum::ASSET->value])->pluck('id')->toArray());
$frontpageArray = $frontpage->data;
if (!is_array($frontpageArray)) {
$frontpageArray = [];
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index 85443dfde2..1b71b46f0c 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -105,11 +105,19 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
return $result;
}
+ /**
+ * @deprecated
+ */
public function find(int $accountId): ?Account
{
/** @var null|Account */
return $this->user->accounts()->find($accountId);
}
+ public function findForGroup(int $accountId): ?Account
+ {
+ /** @var null|Account */
+ return $this->userGroup->accounts()->find($accountId);
+ }
public function findByAccountNumber(string $number, array $types): ?Account
{
@@ -200,6 +208,20 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
return $query->get(['accounts.*']);
}
+ public function getAccountsByIdForGroup(array $accountIds): Collection
+ {
+ $query = $this->userGroup->accounts();
+
+ if (0 !== count($accountIds)) {
+ $query->whereIn('accounts.id', $accountIds);
+ }
+ $query->orderBy('accounts.order', 'ASC');
+ $query->orderBy('accounts.active', 'DESC');
+ $query->orderBy('accounts.name', 'ASC');
+
+ return $query->get(['accounts.*']);
+ }
+
public function getAccountsByType(array $types, ?array $sort = []): Collection
{
$res = array_intersect([
@@ -238,6 +260,44 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
return $query->get(['accounts.*']);
}
+ public function getAccountsByTypeForGroup(array $types, ?array $sort = []): Collection
+ {
+ $res = array_intersect([
+ AccountTypeEnum::ASSET->value,
+ AccountTypeEnum::MORTGAGE->value,
+ AccountTypeEnum::LOAN->value,
+ AccountTypeEnum::DEBT->value,
+ ], $types);
+ $query = $this->userGroup->accounts();
+ if (0 !== count($types)) {
+ $query->accountTypeIn($types);
+ }
+
+ // add sort parameters
+ $allowed = config('firefly.allowed_db_sort_parameters.Account', []);
+ $sorted = 0;
+ if (0 !== count($sort)) {
+ foreach ($sort as $param) {
+ if (in_array($param[0], $allowed, true)) {
+ $query->orderBy($param[0], $param[1]);
+ ++$sorted;
+ }
+ }
+ }
+
+ if (0 === $sorted) {
+ if (0 !== count($res)) {
+ $query->orderBy('accounts.order', 'ASC');
+ }
+ $query->orderBy('accounts.active', 'DESC');
+ $query->orderBy('accounts.name', 'ASC');
+ $query->orderBy('accounts.account_type_id', 'ASC');
+ $query->orderBy('accounts.id', 'ASC');
+ }
+
+ return $query->get(['accounts.*']);
+ }
+
/**
* Return account type or null if not found.
*/
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index 86ccfdaedf..8169896478 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -63,7 +63,11 @@ interface AccountRepositoryInterface
*/
public function expandWithDoubles(Collection $accounts): Collection;
+ /**
+ * @deprecated
+ */
public function find(int $accountId): ?Account;
+ public function findForGroup(int $accountId): ?Account;
public function findByAccountNumber(string $number, array $types): ?Account;
@@ -73,12 +77,18 @@ interface AccountRepositoryInterface
public function getAccountCurrency(Account $account): ?TransactionCurrency;
+ /**
+ * @deprecated
+ */
public function getAccountsById(array $accountIds): Collection;
+ public function getAccountsByIdForGroup(array $accountIds): Collection;
/**
* @param array<int, int|string> $types
+ * @deprecated
*/
public function getAccountsByType(array $types, ?array $sort = []): Collection;
+ public function getAccountsByTypeForGroup(array $types, ?array $sort = []): Collection;
/**
* Return account type or null if not found.
diff --git a/app/Support/Http/Api/CollectsAccountsFromFilter.php b/app/Support/Http/Api/CollectsAccountsFromFilter.php
index cd063afdb2..a52aacb695 100644
--- a/app/Support/Http/Api/CollectsAccountsFromFilter.php
+++ b/app/Support/Http/Api/CollectsAccountsFromFilter.php
@@ -83,4 +83,58 @@ trait CollectsAccountsFromFilter
return $collection;
}
+
+ /**
+ * Get the account list for the associated GROUP, not the USER
+ */
+ private function getAccountListForGroup(array $queryParameters): Collection
+ {
+ $collection = new Collection();
+
+ // always collect from the query parameter, even when it's empty.
+ if (null !== $queryParameters['accounts']) {
+ foreach ($queryParameters['accounts'] as $accountId) {
+ $account = $this->repository->findForGroup((int) $accountId);
+ if (null !== $account) {
+ $collection->push($account);
+ }
+ }
+ }
+
+ // if no "preselected", and found accounts
+ if ('empty' === $queryParameters['preselected'] && $collection->count() > 0) {
+ return $collection;
+ }
+ // if no preselected, but no accounts:
+ if ('empty' === $queryParameters['preselected'] && 0 === $collection->count()) {
+ $defaultSet = $this->repository->getAccountsByTypeForGroup([AccountTypeEnum::ASSET->value, AccountTypeEnum::DEFAULT->value])->pluck('id')->toArray();
+ $frontpage = Preferences::get('frontpageAccounts', $defaultSet);
+
+ if (!(is_array($frontpage->data) && count($frontpage->data) > 0)) {
+ $frontpage->data = $defaultSet;
+ $frontpage->save();
+ }
+
+ return $this->repository->getAccountsByIdForGroup($frontpage->data);
+ }
+
+ // both options are overruled by "preselected"
+ if ('all' === $queryParameters['preselected']) {
+ return $this->repository->getAccountsByTypeForGroup([
+ AccountTypeEnum::ASSET->value,
+ AccountTypeEnum::DEFAULT->value,
+ AccountTypeEnum::LOAN->value,
+ AccountTypeEnum::DEBT->value,
+ AccountTypeEnum::MORTGAGE->value,
+ ]);
+ }
+ if ('assets' === $queryParameters['preselected'] || 'Asset account' === $queryParameters['preselected']) {
+ return $this->repository->getAccountsByTypeForGroup([AccountTypeEnum::ASSET->value, AccountTypeEnum::DEFAULT->value]);
+ }
+ if ('liabilities' === $queryParameters['preselected']) {
+ return $this->repository->getAccountsByTypeForGroup([AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value]);
+ }
+
+ return $collection;
+ }
}