From 10c9118f492892faf558dd4f04cf331b3561248f Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 9 Aug 2020 08:56:15 +0200 Subject: [PATCH] Optimise transformer. --- .../Account/AccountRepository.php | 31 ++++++++-------- app/Transformers/AccountTransformer.php | 35 ++++++++++--------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index f1bd4621da..2c21024f88 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -231,7 +231,7 @@ class AccountRepository implements AccountRepositoryInterface */ public function getAccountCurrency(Account $account): ?TransactionCurrency { - $currencyId = (int)$this->getMetaValue($account, 'currency_id'); + $currencyId = (int) $this->getMetaValue($account, 'currency_id'); if ($currencyId > 0) { return TransactionCurrency::find($currencyId); } @@ -351,13 +351,16 @@ class AccountRepository implements AccountRepositoryInterface */ public function getMetaValue(Account $account, string $field): ?string { - /** @var AccountMeta $meta */ - $meta = $account->accountMeta()->where('name', $field)->first(); - if (null === $meta) { + $result = $account->accountMeta->filter(function (AccountMeta $meta) use ($field) { + return strtolower($meta->name) === strtolower($field); + }); + if (0 === $result->count()) { return null; } - - return (string)$meta->data; + if (1 === $result->count()) { + return (string)$result->first()->data; + } + return null; } /** @@ -414,7 +417,7 @@ class AccountRepository implements AccountRepositoryInterface return null; } - return (string)$transaction->amount; + return (string) $transaction->amount; } /** @@ -476,13 +479,13 @@ class AccountRepository implements AccountRepositoryInterface throw new FireflyException(sprintf('%s is not an asset account.', $account->name)); } $currency = $this->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency(); - $name = trans('firefly.reconciliation_account_name', ['name' => $account->name, 'currency' => $currency->code]); + $name = trans('firefly.reconciliation_account_name', ['name' => $account->name, 'currency' => $currency->code]); /** @var AccountType $type */ $type = AccountType::where('type', AccountType::RECONCILIATION)->first(); $current = $this->user->accounts()->where('account_type_id', $type->id) - ->where('name', $name) - ->first(); + ->where('name', $name) + ->first(); /** @var Account $current */ if (null !== $current) { @@ -532,7 +535,7 @@ class AccountRepository implements AccountRepositoryInterface ->orderBy('transaction_journals.id', 'ASC') ->first(['transaction_journals.id']); if (null !== $first) { - return TransactionJournal::find((int)$first->id); + return TransactionJournal::find((int) $first->id); } return null; @@ -559,7 +562,7 @@ class AccountRepository implements AccountRepositoryInterface /** * @param string $query * @param array $types - * @param int $limit + * @param int $limit * * @return Collection */ @@ -574,7 +577,7 @@ class AccountRepository implements AccountRepositoryInterface if ('' !== $query) { // split query on spaces just in case: $parts = explode(' ', $query); - foreach($parts as $part) { + foreach ($parts as $part) { $search = sprintf('%%%s%%', $part); $dbQuery->where('name', 'LIKE', $search); } @@ -664,7 +667,7 @@ class AccountRepository implements AccountRepositoryInterface */ public function getAttachments(Account $account): Collection { - $set = $account->attachments()->get(); + $set = $account->attachments()->get(); /** @var Storage $disk */ $disk = Storage::disk('upload'); diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index 6b4fa49009..65b3b718a0 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -64,9 +64,9 @@ class AccountTransformer extends AbstractTransformer $this->repository->setUser($account->user); // get account type: - $fullType = $this->repository->getAccountType($account); - $accountType = (string)config(sprintf('firefly.shortNamesByFullName.%s', $fullType)); - $liabilityType = (string)config(sprintf('firefly.shortLiabilityNameByFullName.%s', $fullType)); + $fullType = $account->accountType->type; + $accountType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $fullType)); + $liabilityType = (string) config(sprintf('firefly.shortLiabilityNameByFullName.%s', $fullType)); $liabilityType = '' === $liabilityType ? null : $liabilityType; // get account role (will only work if the type is asset. @@ -78,7 +78,7 @@ class AccountTransformer extends AbstractTransformer [$openingBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType, $decimalPlaces); [$interest, $interestPeriod] = $this->getInterest($account, $accountType); - $openingBalance = number_format((float) $openingBalance, $decimalPlaces, '.', ''); + $openingBalance = number_format((float) $openingBalance, $decimalPlaces, '.', ''); $liabilityAmount = null; $liabilityStart = null; if (null !== $liabilityType) { @@ -147,7 +147,7 @@ class AccountTransformer extends AbstractTransformer private function getAccountRole(Account $account, string $accountType): ?string { $accountRole = $this->repository->getMetaValue($account, 'account_role'); - if ('asset' !== $accountType || '' === (string)$accountRole) { + if ('asset' !== $accountType || '' === (string) $accountRole) { $accountRole = null; } @@ -180,18 +180,16 @@ class AccountTransformer extends AbstractTransformer */ private function getCurrency(Account $account): array { - $currency = $this->repository->getAccountCurrency($account); - $default = app('amount')->getDefaultCurrencyByUser($account->user); - $currencyId = (int) $default->id; - $currencyCode = $default->code; - $decimalPlaces = $default->decimal_places; - $currencySymbol = $default->symbol; - if (null !== $currency) { - $currencyId = (int) $currency->id; - $currencyCode = $currency->code; - $decimalPlaces = $currency->decimal_places; - $currencySymbol = $currency->symbol; + $currency = $this->repository->getAccountCurrency($account); + + // only grab default when result is null: + if (null === $currency) { + $currency = app('amount')->getDefaultCurrencyByUser($account->user); } + $currencyId = (int) $currency->id; + $currencyCode = $currency->code; + $decimalPlaces = $currency->decimal_places; + $currencySymbol = $currency->symbol; return [$currencyId, $currencyCode, $currencySymbol, $decimalPlaces]; } @@ -201,7 +199,7 @@ class AccountTransformer extends AbstractTransformer */ private function getDate(): Carbon { - $date = new Carbon; + $date = today(config('app.timezone')); if (null !== $this->parameters->get('date')) { $date = $this->parameters->get('date'); } @@ -234,12 +232,15 @@ class AccountTransformer extends AbstractTransformer * @param int $decimalPlaces * * @return array + * + * TODO refactor call to getOpeningBalanceAmount / Date because its extra queries. */ private function getOpeningBalance(Account $account, string $accountType, int $decimalPlaces): array { $openingBalance = null; $openingBalanceDate = null; if (in_array($accountType, ['asset', 'liabilities'], true)) { + //$journal = $this->repository->getOpeningBalance($account); $amount = $this->repository->getOpeningBalanceAmount($account); $openingBalance = $amount; $openingBalanceDate = $this->repository->getOpeningBalanceDate($account);