diff --git a/app/Api/V2/Controllers/Model/Account/IndexController.php b/app/Api/V2/Controllers/Model/Account/IndexController.php index 23e8c2fc0c..d175fe66c5 100644 --- a/app/Api/V2/Controllers/Model/Account/IndexController.php +++ b/app/Api/V2/Controllers/Model/Account/IndexController.php @@ -73,7 +73,7 @@ class IndexController extends Controller // depending on the sort parameters, this list must not be split, because the // order is calculated in the account transformer and by that time it's too late. $first = array_key_first($sorting); - $disablePagination = in_array($first, ['last_activity', 'balance_difference'], true); + $disablePagination = in_array($first, ['last_activity','balance', 'balance_difference'], true); if (!$disablePagination) { $accounts = $accounts->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); } diff --git a/app/Transformers/V2/AccountTransformer.php b/app/Transformers/V2/AccountTransformer.php index db5f2065c5..a5b4022049 100644 --- a/app/Transformers/V2/AccountTransformer.php +++ b/app/Transformers/V2/AccountTransformer.php @@ -87,14 +87,18 @@ class AccountTransformer extends AbstractTransformer // get object groups $this->getObjectGroups($objects); + // sort: + $objects = $this->sortAccounts($objects); + // if pagination is disabled, do it now: - if(true === $this->parameters->get('disablePagination')) { - $page = (int) $this->parameters->get('page'); - $size = (int) $this->parameters->get('pageSize'); - $objects = $objects->slice(($page-1) * $size, $size); + if (true === $this->parameters->get('disablePagination')) { + $page = (int) $this->parameters->get('page'); + $size = (int) $this->parameters->get('pageSize'); + $objects = $objects->slice(($page - 1) * $size, $size); } - return $this->sortAccounts($objects); + return $objects; + } private function getDate(): Carbon @@ -112,12 +116,12 @@ class AccountTransformer extends AbstractTransformer */ public function transform(Account $account): array { - $id = $account->id; + $id = $account->id; // various meta - $accountRole = $this->accountMeta[$id]['account_role'] ?? null; - $accountType = $this->accountTypes[$id]; - $order = $account->order; + $accountRole = $this->accountMeta[$id]['account_role'] ?? null; + $accountType = $this->accountTypes[$id]; + $order = $account->order; // liability type $liabilityType = 'liabilities' === $accountType ? $this->fullTypes[$id] : null; @@ -127,13 +131,13 @@ class AccountTransformer extends AbstractTransformer $currentDebt = $this->accountMeta[$id]['current_debt'] ?? null; // no currency? use default - $currency = $this->default; + $currency = $this->default; if (array_key_exists($id, $this->accountMeta) && 0 !== (int) ($this->accountMeta[$id]['currency_id'] ?? 0)) { $currency = $this->currencies[(int) $this->accountMeta[$id]['currency_id']]; } // amounts and calculation. - $balance = $this->balances[$id]['balance'] ?? null; - $nativeBalance = $this->convertedBalances[$id]['native_balance'] ?? null; + $balance = $this->balances[$id]['balance'] ?? null; + $nativeBalance = $this->convertedBalances[$id]['native_balance'] ?? null; // no order for some accounts: if (!in_array(strtolower($accountType), ['liability', 'liabilities', 'asset'], true)) { @@ -141,15 +145,15 @@ class AccountTransformer extends AbstractTransformer } // object group - $objectGroupId = $this->objectGroups[$id]['id'] ?? null; - $objectGroupOrder = $this->objectGroups[$id]['order'] ?? null; - $objectGroupTitle = $this->objectGroups[$id]['title'] ?? null; + $objectGroupId = $this->objectGroups[$id]['id'] ?? null; + $objectGroupOrder = $this->objectGroups[$id]['order'] ?? null; + $objectGroupTitle = $this->objectGroups[$id]['title'] ?? null; // balance difference - $diffStart = null; - $diffEnd = null; - $balanceDiff = null; - $nativeBalanceDiff = null; + $diffStart = null; + $diffEnd = null; + $balanceDiff = null; + $nativeBalanceDiff = null; if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { $diffStart = $this->parameters->get('start')->toAtomString(); $diffEnd = $this->parameters->get('end')->toAtomString(); @@ -158,20 +162,20 @@ class AccountTransformer extends AbstractTransformer } return [ - 'id' => (string) $account->id, - 'created_at' => $account->created_at->toAtomString(), - 'updated_at' => $account->updated_at->toAtomString(), - 'active' => $account->active, - 'order' => $order, - 'name' => $account->name, - 'iban' => '' === (string) $account->iban ? null : $account->iban, - 'account_number' => $this->accountMeta[$id]['account_number'] ?? null, - 'type' => strtolower($accountType), - 'account_role' => $accountRole, - 'currency_id' => (string) $currency->id, - 'currency_code' => $currency->code, - 'currency_symbol' => $currency->symbol, - 'currency_decimal_places' => $currency->decimal_places, + 'id' => (string) $account->id, + 'created_at' => $account->created_at->toAtomString(), + 'updated_at' => $account->updated_at->toAtomString(), + 'active' => $account->active, + 'order' => $order, + 'name' => $account->name, + 'iban' => '' === (string) $account->iban ? null : $account->iban, + 'account_number' => $this->accountMeta[$id]['account_number'] ?? null, + 'type' => strtolower($accountType), + 'account_role' => $accountRole, + 'currency_id' => (string) $currency->id, + 'currency_code' => $currency->code, + 'currency_symbol' => $currency->symbol, + 'currency_decimal_places' => $currency->decimal_places, 'native_currency_id' => (string) $this->default->id, 'native_currency_code' => $this->default->code, @@ -218,7 +222,7 @@ class AccountTransformer extends AbstractTransformer 'links' => [ [ 'rel' => 'self', - 'uri' => '/accounts/'.$account->id, + 'uri' => '/accounts/' . $account->id, ], ], ]; @@ -241,14 +245,14 @@ class AccountTransformer extends AbstractTransformer private function collectAccountMetaData(Collection $accounts): void { /** @var CurrencyRepositoryInterface $repository */ - $repository = app(CurrencyRepositoryInterface::class); + $repository = app(CurrencyRepositoryInterface::class); /** @var AccountRepositoryInterface $accountRepository */ $accountRepository = app(AccountRepositoryInterface::class); $metaFields = $accountRepository->getMetaValues($accounts, ['currency_id', 'account_role', 'account_number', 'liability_direction', 'interest', 'interest_period', 'current_debt']); $currencyIds = $metaFields->where('name', 'currency_id')->pluck('data')->toArray(); - $currencies = $repository->getByIds($currencyIds); + $currencies = $repository->getByIds($currencyIds); foreach ($currencies as $currency) { $id = $currency->id; $this->currencies[$id] = $currency;