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

@@ -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);