mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Optimise transformer.
This commit is contained in:
parent
cdf42e2a37
commit
10c9118f49
@ -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');
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user