Skip to content

Firefly III: a personal finances manager

Fix all sorts for accounts.

James Coleacae2d3dfcac
diff --git a/app/Api/V1/Controllers/Models/Account/ShowController.php b/app/Api/V1/Controllers/Models/Account/ShowController.php
index f87116e9b8..3e878a0145 100644
--- a/app/Api/V1/Controllers/Models/Account/ShowController.php
+++ b/app/Api/V1/Controllers/Models/Account/ShowController.php
@@ -63,12 +63,31 @@ final class ShowController extends Controller
});
}
+ private function isAllDatabaseSort(array $instructions, string $model): bool
+ {
+ if (0 === count($instructions)) {
+ return true;
+ }
+ $config = config(sprintf('firefly.allowed_db_sort_parameters.%s', $model));
+ if (null === $config) {
+ return true;
+ }
+ foreach ($instructions as $item) {
+ $field = $item[0];
+ if (!in_array($field, $config)) {
+ return false;
+ }
+ }
+ return true;
+
+ }
+
/**
* Display a listing of the resource.
*/
public function index(ShowRequest $request): JsonResponse
{
- $manager = $this->getManager();
+ $manager = $this->getManager();
[
'types' => $types,
'page' => $page,
@@ -79,36 +98,42 @@ final class ShowController extends Controller
'end' => $end,
'active' => $active,
'date' => $date,
- ] = $request->attributes->all();
+ ]
+ = $request->attributes->all();
// get list of accounts. Count it and split it.
$this->repository->resetAccountOrder();
- $collection = $this->repository->getAccountsByType($types, $sort, $active);
- $count = $collection->count();
-
- // continue sort:
- // TODO if the user sorts on DB dependent field there must be no slice before enrichment, only after.
- // TODO still need to figure out how to do this easily.
- $accounts = $collection->slice($offset, $limit);
+ $collection = $this->repository->getAccountsByType($types, $sort, $active);
+ $count = $collection->count();
+ $dbFields = $this->isAllDatabaseSort($sort, 'Account');
+ $accounts = $collection;
+ if ($dbFields) {
+ $accounts = $collection->slice($offset, $limit);
+ }
// enrich
/** @var User $admin */
- $admin = auth()->user();
- $enrichment = new AccountEnrichment();
+ $admin = auth()->user();
+ $enrichment = new AccountEnrichment();
$enrichment->setSort($sort);
$enrichment->setDate($date);
$enrichment->setStart($start);
$enrichment->setEnd($end);
$enrichment->setUser($admin);
- $accounts = $enrichment->enrich($accounts);
+ $accounts = $enrichment->enrich($accounts);
+
+ if (!$dbFields) {
+ // now do the slicing.
+ $accounts = $accounts->slice($offset, $limit);
+ }
// make paginator:
- $paginator = new LengthAwarePaginator($accounts, $count, $limit, $page);
- $paginator->setPath(route('api.v1.accounts.index').$this->buildParams());
+ $paginator = new LengthAwarePaginator($accounts, $count, $limit, $page);
+ $paginator->setPath(route('api.v1.accounts.index') . $this->buildParams());
/** @var AccountTransformer $transformer */
$transformer = app(AccountTransformer::class);
- $resource = new FractalCollection($accounts, $transformer, self::RESOURCE_KEY);
+ $resource = new FractalCollection($accounts, $transformer, self::RESOURCE_KEY);
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
@@ -125,22 +150,22 @@ final class ShowController extends Controller
// get list of accounts. Count it and split it.
$this->repository->resetAccountOrder();
$account->refresh();
- $manager = $this->getManager();
+ $manager = $this->getManager();
['start' => $start, 'end' => $end, 'date' => $date] = $request->attributes->all();
// enrich
/** @var User $admin */
- $admin = auth()->user();
- $enrichment = new AccountEnrichment();
+ $admin = auth()->user();
+ $enrichment = new AccountEnrichment();
$enrichment->setDate($date);
$enrichment->setStart($start);
$enrichment->setEnd($end);
$enrichment->setUser($admin);
- $account = $enrichment->enrichSingle($account);
+ $account = $enrichment->enrichSingle($account);
/** @var AccountTransformer $transformer */
- $transformer = app(AccountTransformer::class);
- $resource = new Item($account, $transformer, self::RESOURCE_KEY);
+ $transformer = app(AccountTransformer::class);
+ $resource = new Item($account, $transformer, self::RESOURCE_KEY);
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
diff --git a/app/Support/JsonApi/Enrichments/AccountEnrichment.php b/app/Support/JsonApi/Enrichments/AccountEnrichment.php
index d722b672fc..01f5ccbddb 100644
--- a/app/Support/JsonApi/Enrichments/AccountEnrichment.php
+++ b/app/Support/JsonApi/Enrichments/AccountEnrichment.php
@@ -473,7 +473,11 @@ class AccountEnrichment implements EnrichmentInterface
default:
throw new FireflyException(sprintf('Account enrichment cannot sort on field "%s"', $field));
case 'account_number':
- $this->collection = $this->collection->sortBy(static fn (Account $account) => $account->meta['account_number'] ?? '', SORT_REGULAR, 'desc' === $parameter[1]);
+ case 'liability_direction':
+ $this->collection = $this->collection->sortBy(static fn (Account $account) => $account->meta[$field] ?? '', SORT_REGULAR, 'desc' === $parameter[1]);
+ break;
+ case 'liability_interest':
+ $this->collection = $this->collection->sortBy(static fn (Account $account) => (float) ($account->meta['interest'] ?? 0.0), SORT_NUMERIC, 'desc' === $parameter[1]);
break;
case 'role':
$this->collection = $this->collection->sortBy(static fn (Account $account) => $account->meta['account_role'] ?? '', SORT_REGULAR, 'desc' === $parameter[1]);
@@ -481,9 +485,15 @@ class AccountEnrichment implements EnrichmentInterface
case 'account_number_and_iban':
$this->collection = $this->collection->sortBy(static fn (Account $account) => sprintf('%s-%s', $account->iban, $account->meta['account_number']), SORT_REGULAR, 'desc' === $parameter[1]);
break;
-
+ case 'last_activity':
+ $this->collection = $this->collection->sortBy(static fn (Account $account) => null === $account->meta['last_activity'] ? '0000-00-00 00:00:00' : $account->meta['last_activity']->format('Y-m-d H:i:s'), SORT_REGULAR, 'desc' === $parameter[1]);
+ break;
case 'current_balance':
case 'pc_current_balance':
+ case 'debt_amount':
+ case 'pc_debt_amount':
+ case 'balance_difference':
+ case 'pc_balance_difference':
$this->collection = $this->collection->sortBy(
static fn (Account $account) => $account->meta['balances'][$parameter[0]] ?? '0',
SORT_NUMERIC,
diff --git a/config/firefly.php b/config/firefly.php
index 60b59c5554..c4d85abb7c 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -861,6 +861,7 @@ return [
'liability_interest',
'active',
'role',
+ 'last_activity',
'account_type_id',
'current_balance',
'pc_current_balance',
diff --git a/resources/assets/v3/js/pages/accounts/index.js b/resources/assets/v3/js/pages/accounts/index.js
index c67da547a1..51ceab895d 100644
--- a/resources/assets/v3/js/pages/accounts/index.js
+++ b/resources/assets/v3/js/pages/accounts/index.js
@@ -88,7 +88,7 @@ let index = function () {
this.downloadAccounts();
// get accounts by initial sort.
document.addEventListener('alpine:initialized', () => {
- document.querySelectorAll("table.sortable th").forEach((el) => {
+ document.querySelectorAll("table.sortable th.sortable").forEach((el) => {
el.addEventListener("click", (event) => {
let newColumn = event.currentTarget.dataset.column;
if (newColumn === this.sortColumn) {
diff --git a/resources/views/accounts/index.blade.php b/resources/views/accounts/index.blade.php
index 3cba867abe..b6e40f066b 100644
--- a/resources/views/accounts/index.blade.php
+++ b/resources/views/accounts/index.blade.php
@@ -39,16 +39,18 @@
<th data-column="current_balance" :class="{'text-end': true, 'sortable': true, 'sortable_sorted': 'current_balance' === sortColumn, 'sortable_sorted_asc': 'asc' === sortDirection, 'sortable_sorted_desc': 'desc' === sortDirection }">{{ trans('list.currentBalance') }}</th>
</template>
<template x-if="'liabilities' === objectType">
- <th class="text-end">
+ <th data-column="debt_amount" :class="{'text-end': true, 'sortable': true, 'sortable_sorted': 'debt_amount' === sortColumn, 'sortable_sorted_asc': 'asc' === sortDirection, 'sortable_sorted_desc': 'desc' === sortDirection }">
{{ trans('firefly.left_in_debt') }}
</th>
</template>
<th {{-- hide on SM --}} class="d-md-table-cell d-none">{{ trans('list.active') }}</th>
{{-- hide last activity to make room for other stuff --}}
<template x-if="'liabilities' !== objectType">
- <th {{-- hide on LG and smaller. --}} class="d-lg-table-cell d-none">{{ trans('list.lastActivity') }}</th>
+ {{-- hide on LG and smaller. --}}
+ <th data-column="last_activity" :class="{'d-lg-table-cell': true, 'd-none': true, 'sortable': true, 'sortable_sorted': 'last_activity' === sortColumn, 'sortable_sorted_asc': 'asc' === sortDirection, 'sortable_sorted_desc': 'desc' === sortDirection }">{{ trans('list.lastActivity') }}</th>
</template>
- <th {{-- hide on SM --}} class="w-15 d-md-table-cell d-none text-end">{{ trans('list.balanceDiff') }}</th>
+ {{-- hide on SM --}}
+ <th data-column="balance_difference" :class="{'w-15': true,'text-end': true, 'd-lg-table-cell': true, 'd-none': true, 'sortable': true, 'sortable_sorted': 'balance_difference' === sortColumn, 'sortable_sorted_asc': 'asc' === sortDirection, 'sortable_sorted_desc': 'desc' === sortDirection }">{{ trans('list.balanceDiff') }}</th>
<th>&nbsp;</th>
</tr>
</thead>