Optimise transformer.

This commit is contained in:
James Cole
2020-08-09 08:56:15 +02:00
parent cdf42e2a37
commit 10c9118f49
2 changed files with 35 additions and 31 deletions

View File

@@ -231,7 +231,7 @@ class AccountRepository implements AccountRepositoryInterface
*/ */
public function getAccountCurrency(Account $account): ?TransactionCurrency public function getAccountCurrency(Account $account): ?TransactionCurrency
{ {
$currencyId = (int)$this->getMetaValue($account, 'currency_id'); $currencyId = (int) $this->getMetaValue($account, 'currency_id');
if ($currencyId > 0) { if ($currencyId > 0) {
return TransactionCurrency::find($currencyId); return TransactionCurrency::find($currencyId);
} }
@@ -351,13 +351,16 @@ class AccountRepository implements AccountRepositoryInterface
*/ */
public function getMetaValue(Account $account, string $field): ?string public function getMetaValue(Account $account, string $field): ?string
{ {
/** @var AccountMeta $meta */ $result = $account->accountMeta->filter(function (AccountMeta $meta) use ($field) {
$meta = $account->accountMeta()->where('name', $field)->first(); return strtolower($meta->name) === strtolower($field);
if (null === $meta) { });
if (0 === $result->count()) {
return null; return null;
} }
if (1 === $result->count()) {
return (string)$meta->data; return (string)$result->first()->data;
}
return null;
} }
/** /**
@@ -414,7 +417,7 @@ class AccountRepository implements AccountRepositoryInterface
return null; 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)); throw new FireflyException(sprintf('%s is not an asset account.', $account->name));
} }
$currency = $this->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency(); $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 */ /** @var AccountType $type */
$type = AccountType::where('type', AccountType::RECONCILIATION)->first(); $type = AccountType::where('type', AccountType::RECONCILIATION)->first();
$current = $this->user->accounts()->where('account_type_id', $type->id) $current = $this->user->accounts()->where('account_type_id', $type->id)
->where('name', $name) ->where('name', $name)
->first(); ->first();
/** @var Account $current */ /** @var Account $current */
if (null !== $current) { if (null !== $current) {
@@ -532,7 +535,7 @@ class AccountRepository implements AccountRepositoryInterface
->orderBy('transaction_journals.id', 'ASC') ->orderBy('transaction_journals.id', 'ASC')
->first(['transaction_journals.id']); ->first(['transaction_journals.id']);
if (null !== $first) { if (null !== $first) {
return TransactionJournal::find((int)$first->id); return TransactionJournal::find((int) $first->id);
} }
return null; return null;
@@ -559,7 +562,7 @@ class AccountRepository implements AccountRepositoryInterface
/** /**
* @param string $query * @param string $query
* @param array $types * @param array $types
* @param int $limit * @param int $limit
* *
* @return Collection * @return Collection
*/ */
@@ -574,7 +577,7 @@ class AccountRepository implements AccountRepositoryInterface
if ('' !== $query) { if ('' !== $query) {
// split query on spaces just in case: // split query on spaces just in case:
$parts = explode(' ', $query); $parts = explode(' ', $query);
foreach($parts as $part) { foreach ($parts as $part) {
$search = sprintf('%%%s%%', $part); $search = sprintf('%%%s%%', $part);
$dbQuery->where('name', 'LIKE', $search); $dbQuery->where('name', 'LIKE', $search);
} }
@@ -664,7 +667,7 @@ class AccountRepository implements AccountRepositoryInterface
*/ */
public function getAttachments(Account $account): Collection public function getAttachments(Account $account): Collection
{ {
$set = $account->attachments()->get(); $set = $account->attachments()->get();
/** @var Storage $disk */ /** @var Storage $disk */
$disk = Storage::disk('upload'); $disk = Storage::disk('upload');

View File

@@ -64,9 +64,9 @@ class AccountTransformer extends AbstractTransformer
$this->repository->setUser($account->user); $this->repository->setUser($account->user);
// get account type: // get account type:
$fullType = $this->repository->getAccountType($account); $fullType = $account->accountType->type;
$accountType = (string)config(sprintf('firefly.shortNamesByFullName.%s', $fullType)); $accountType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $fullType));
$liabilityType = (string)config(sprintf('firefly.shortLiabilityNameByFullName.%s', $fullType)); $liabilityType = (string) config(sprintf('firefly.shortLiabilityNameByFullName.%s', $fullType));
$liabilityType = '' === $liabilityType ? null : $liabilityType; $liabilityType = '' === $liabilityType ? null : $liabilityType;
// get account role (will only work if the type is asset. // 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); [$openingBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType, $decimalPlaces);
[$interest, $interestPeriod] = $this->getInterest($account, $accountType); [$interest, $interestPeriod] = $this->getInterest($account, $accountType);
$openingBalance = number_format((float) $openingBalance, $decimalPlaces, '.', ''); $openingBalance = number_format((float) $openingBalance, $decimalPlaces, '.', '');
$liabilityAmount = null; $liabilityAmount = null;
$liabilityStart = null; $liabilityStart = null;
if (null !== $liabilityType) { if (null !== $liabilityType) {
@@ -147,7 +147,7 @@ class AccountTransformer extends AbstractTransformer
private function getAccountRole(Account $account, string $accountType): ?string private function getAccountRole(Account $account, string $accountType): ?string
{ {
$accountRole = $this->repository->getMetaValue($account, 'account_role'); $accountRole = $this->repository->getMetaValue($account, 'account_role');
if ('asset' !== $accountType || '' === (string)$accountRole) { if ('asset' !== $accountType || '' === (string) $accountRole) {
$accountRole = null; $accountRole = null;
} }
@@ -180,18 +180,16 @@ class AccountTransformer extends AbstractTransformer
*/ */
private function getCurrency(Account $account): array private function getCurrency(Account $account): array
{ {
$currency = $this->repository->getAccountCurrency($account); $currency = $this->repository->getAccountCurrency($account);
$default = app('amount')->getDefaultCurrencyByUser($account->user);
$currencyId = (int) $default->id; // only grab default when result is null:
$currencyCode = $default->code; if (null === $currency) {
$decimalPlaces = $default->decimal_places; $currency = app('amount')->getDefaultCurrencyByUser($account->user);
$currencySymbol = $default->symbol;
if (null !== $currency) {
$currencyId = (int) $currency->id;
$currencyCode = $currency->code;
$decimalPlaces = $currency->decimal_places;
$currencySymbol = $currency->symbol;
} }
$currencyId = (int) $currency->id;
$currencyCode = $currency->code;
$decimalPlaces = $currency->decimal_places;
$currencySymbol = $currency->symbol;
return [$currencyId, $currencyCode, $currencySymbol, $decimalPlaces]; return [$currencyId, $currencyCode, $currencySymbol, $decimalPlaces];
} }
@@ -201,7 +199,7 @@ class AccountTransformer extends AbstractTransformer
*/ */
private function getDate(): Carbon private function getDate(): Carbon
{ {
$date = new Carbon; $date = today(config('app.timezone'));
if (null !== $this->parameters->get('date')) { if (null !== $this->parameters->get('date')) {
$date = $this->parameters->get('date'); $date = $this->parameters->get('date');
} }
@@ -234,12 +232,15 @@ class AccountTransformer extends AbstractTransformer
* @param int $decimalPlaces * @param int $decimalPlaces
* *
* @return array * @return array
*
* TODO refactor call to getOpeningBalanceAmount / Date because its extra queries.
*/ */
private function getOpeningBalance(Account $account, string $accountType, int $decimalPlaces): array private function getOpeningBalance(Account $account, string $accountType, int $decimalPlaces): array
{ {
$openingBalance = null; $openingBalance = null;
$openingBalanceDate = null; $openingBalanceDate = null;
if (in_array($accountType, ['asset', 'liabilities'], true)) { if (in_array($accountType, ['asset', 'liabilities'], true)) {
//$journal = $this->repository->getOpeningBalance($account);
$amount = $this->repository->getOpeningBalanceAmount($account); $amount = $this->repository->getOpeningBalanceAmount($account);
$openingBalance = $amount; $openingBalance = $amount;
$openingBalanceDate = $this->repository->getOpeningBalanceDate($account); $openingBalanceDate = $this->repository->getOpeningBalanceDate($account);