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

@@ -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;
}
/**

View File

@@ -64,7 +64,7 @@ class AccountTransformer extends AbstractTransformer
$this->repository->setUser($account->user);
// get account type:
$fullType = $this->repository->getAccountType($account);
$fullType = $account->accountType->type;
$accountType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $fullType));
$liabilityType = (string) config(sprintf('firefly.shortLiabilityNameByFullName.%s', $fullType));
$liabilityType = '' === $liabilityType ? null : $liabilityType;
@@ -181,17 +181,15 @@ 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) {
// 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);