Code cleanup

This commit is contained in:
James Cole
2024-12-22 08:43:12 +01:00
parent 5751f7e5a3
commit 565bd87959
574 changed files with 4600 additions and 4604 deletions

View File

@@ -38,6 +38,7 @@ class AccountBalanceGrouped
{
private array $accountIds;
private string $carbonFormat;
private ExchangeRateConverter $converter;
private array $currencies = [];
private array $data = [];
private TransactionCurrency $default;
@@ -45,7 +46,6 @@ class AccountBalanceGrouped
private array $journals = [];
private string $preferredRange;
private Carbon $start;
private ExchangeRateConverter $converter;
public function __construct()
{
@@ -139,51 +139,6 @@ class AccountBalanceGrouped
$converter->summarize();
}
public function setAccounts(Collection $accounts): void
{
$this->accountIds = $accounts->pluck('id')->toArray();
}
public function setDefault(TransactionCurrency $default): void
{
$this->default = $default;
$defaultCurrencyId = $default->id;
$this->currencies = [$default->id => $default]; // currency cache
$this->data[$defaultCurrencyId] = [
'currency_id' => (string) $defaultCurrencyId,
'currency_symbol' => $default->symbol,
'currency_code' => $default->code,
'currency_name' => $default->name,
'currency_decimal_places' => $default->decimal_places,
'native_currency_id' => (string) $defaultCurrencyId,
'native_currency_symbol' => $default->symbol,
'native_currency_code' => $default->code,
'native_currency_name' => $default->name,
'native_currency_decimal_places' => $default->decimal_places,
];
}
public function setEnd(Carbon $end): void
{
$this->end = $end;
}
public function setJournals(array $journals): void
{
$this->journals = $journals;
}
public function setPreferredRange(string $preferredRange): void
{
$this->preferredRange = $preferredRange;
$this->carbonFormat = app('navigation')->preferredCarbonFormatByPeriod($preferredRange);
}
public function setStart(Carbon $start): void
{
$this->start = $start;
}
private function processJournal(array $journal): void
{
// format the date according to the period
@@ -292,4 +247,49 @@ class AccountBalanceGrouped
return $rate;
}
public function setAccounts(Collection $accounts): void
{
$this->accountIds = $accounts->pluck('id')->toArray();
}
public function setDefault(TransactionCurrency $default): void
{
$this->default = $default;
$defaultCurrencyId = $default->id;
$this->currencies = [$default->id => $default]; // currency cache
$this->data[$defaultCurrencyId] = [
'currency_id' => (string) $defaultCurrencyId,
'currency_symbol' => $default->symbol,
'currency_code' => $default->code,
'currency_name' => $default->name,
'currency_decimal_places' => $default->decimal_places,
'native_currency_id' => (string) $defaultCurrencyId,
'native_currency_symbol' => $default->symbol,
'native_currency_code' => $default->code,
'native_currency_name' => $default->name,
'native_currency_decimal_places' => $default->decimal_places,
];
}
public function setEnd(Carbon $end): void
{
$this->end = $end;
}
public function setJournals(array $journals): void
{
$this->journals = $journals;
}
public function setPreferredRange(string $preferredRange): void
{
$this->preferredRange = $preferredRange;
$this->carbonFormat = app('navigation')->preferredCarbonFormatByPeriod($preferredRange);
}
public function setStart(Carbon $start): void
{
$this->start = $start;
}
}

View File

@@ -32,57 +32,58 @@ use FireflyIII\Models\AccountType;
*/
trait AccountFilter
{
protected array $types = [
'all' => [
AccountTypeEnum::DEFAULT->value,
AccountType::CASH,
AccountType::ASSET,
AccountType::EXPENSE,
AccountType::REVENUE,
AccountType::INITIAL_BALANCE,
AccountType::BENEFICIARY,
AccountType::IMPORT,
AccountType::RECONCILIATION,
AccountType::LOAN,
AccountType::DEBT,
AccountType::MORTGAGE,
],
'asset' => [AccountType::DEFAULT, AccountType::ASSET],
'cash' => [AccountType::CASH],
'expense' => [AccountType::EXPENSE, AccountType::BENEFICIARY],
'revenue' => [AccountType::REVENUE],
'special' => [AccountType::CASH, AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION],
'hidden' => [AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION],
'liability' => [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD],
'liabilities' => [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD],
AccountType::DEFAULT => [AccountType::DEFAULT],
AccountType::CASH => [AccountType::CASH],
AccountType::ASSET => [AccountType::ASSET],
AccountType::EXPENSE => [AccountType::EXPENSE],
AccountType::REVENUE => [AccountType::REVENUE],
AccountType::INITIAL_BALANCE => [AccountType::INITIAL_BALANCE],
AccountType::BENEFICIARY => [AccountType::BENEFICIARY],
AccountType::IMPORT => [AccountType::IMPORT],
AccountType::RECONCILIATION => [AccountType::RECONCILIATION],
AccountType::LOAN => [AccountType::LOAN],
AccountType::MORTGAGE => [AccountType::MORTGAGE],
AccountType::DEBT => [AccountType::DEBT],
AccountType::CREDITCARD => [AccountType::CREDITCARD],
'default account' => [AccountType::DEFAULT],
'cash account' => [AccountType::CASH],
'asset account' => [AccountType::ASSET],
'expense account' => [AccountType::EXPENSE],
'revenue account' => [AccountType::REVENUE],
'initial balance account' => [AccountType::INITIAL_BALANCE],
'reconciliation' => [AccountType::RECONCILIATION],
'loan' => [AccountType::LOAN],
'mortgage' => [AccountType::MORTGAGE],
'debt' => [AccountType::DEBT],
'credit card' => [AccountType::CREDITCARD],
'credit-card' => [AccountType::CREDITCARD],
'creditcard' => [AccountType::CREDITCARD],
'cc' => [AccountType::CREDITCARD],
];
protected array $types
= [
'all' => [
AccountTypeEnum::DEFAULT->value,
AccountType::CASH,
AccountType::ASSET,
AccountType::EXPENSE,
AccountType::REVENUE,
AccountType::INITIAL_BALANCE,
AccountType::BENEFICIARY,
AccountType::IMPORT,
AccountType::RECONCILIATION,
AccountType::LOAN,
AccountType::DEBT,
AccountType::MORTGAGE,
],
'asset' => [AccountType::DEFAULT, AccountType::ASSET],
'cash' => [AccountType::CASH],
'expense' => [AccountType::EXPENSE, AccountType::BENEFICIARY],
'revenue' => [AccountType::REVENUE],
'special' => [AccountType::CASH, AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION],
'hidden' => [AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION],
'liability' => [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD],
'liabilities' => [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD],
AccountType::DEFAULT => [AccountType::DEFAULT],
AccountType::CASH => [AccountType::CASH],
AccountType::ASSET => [AccountType::ASSET],
AccountType::EXPENSE => [AccountType::EXPENSE],
AccountType::REVENUE => [AccountType::REVENUE],
AccountType::INITIAL_BALANCE => [AccountType::INITIAL_BALANCE],
AccountType::BENEFICIARY => [AccountType::BENEFICIARY],
AccountType::IMPORT => [AccountType::IMPORT],
AccountType::RECONCILIATION => [AccountType::RECONCILIATION],
AccountType::LOAN => [AccountType::LOAN],
AccountType::MORTGAGE => [AccountType::MORTGAGE],
AccountType::DEBT => [AccountType::DEBT],
AccountType::CREDITCARD => [AccountType::CREDITCARD],
'default account' => [AccountType::DEFAULT],
'cash account' => [AccountType::CASH],
'asset account' => [AccountType::ASSET],
'expense account' => [AccountType::EXPENSE],
'revenue account' => [AccountType::REVENUE],
'initial balance account' => [AccountType::INITIAL_BALANCE],
'reconciliation' => [AccountType::RECONCILIATION],
'loan' => [AccountType::LOAN],
'mortgage' => [AccountType::MORTGAGE],
'debt' => [AccountType::DEBT],
'credit card' => [AccountType::CREDITCARD],
'credit-card' => [AccountType::CREDITCARD],
'creditcard' => [AccountType::CREDITCARD],
'cc' => [AccountType::CREDITCARD],
];
/**
* All the available types.

View File

@@ -48,10 +48,10 @@ trait CleansChartData
*/
foreach ($data as $index => $array) {
if (array_key_exists('currency_id', $array)) {
$array['currency_id'] = (string)$array['currency_id'];
$array['currency_id'] = (string) $array['currency_id'];
}
if (array_key_exists('native_currency_id', $array)) {
$array['native_currency_id'] = (string)$array['native_currency_id'];
$array['native_currency_id'] = (string) $array['native_currency_id'];
}
if (!array_key_exists('start', $array)) {
throw new FireflyException(sprintf('Data-set "%s" is missing the "start"-variable.', $index));

View File

@@ -56,9 +56,9 @@ trait ConvertsExchangeRates
/** @var TransactionCurrency $native */
$native = app('amount')->getDefaultCurrency();
$currency = $this->getCurrency((int)$set['currency_id']);
$currency = $this->getCurrency((int) $set['currency_id']);
if ($native->id === $currency->id) {
$set['native_currency_id'] = (string)$currency->id;
$set['native_currency_id'] = (string) $currency->id;
$set['native_currency_code'] = $currency->code;
$set['native_currency_symbol'] = $currency->symbol;
$set['native_currency_decimal_places'] = $currency->decimal_places;
@@ -69,8 +69,8 @@ trait ConvertsExchangeRates
$carbon = Carbon::createFromFormat(\DateTimeInterface::ATOM, $date);
$rate = $this->getRate($currency, $native, $carbon);
$rate = '0' === $rate ? '1' : $rate;
app('log')->debug(sprintf('bcmul("%s", "%s")', (string)$entry, $rate));
$set['entries'][$date] = (float)bcmul((string)$entry, $rate);
app('log')->debug(sprintf('bcmul("%s", "%s")', (string) $entry, $rate));
$set['entries'][$date] = (float) bcmul((string) $entry, $rate);
}
return $set;
@@ -128,12 +128,12 @@ trait ConvertsExchangeRates
/** @var array $entry */
foreach ($entries as $entry) {
$currency = $this->getCurrency((int)$entry['id']);
$currency = $this->getCurrency((int) $entry['id']);
if ($currency->id !== $native->id) {
$amount = $this->convertAmount($entry['sum'], $currency, $native);
$entry['converted'] = true;
$entry['native_sum'] = $amount;
$entry['native_currency_id'] = (string)$native->id;
$entry['native_currency_id'] = (string) $native->id;
$entry['native_currency_name'] = $native->name;
$entry['native_currency_symbol'] = $native->symbol;
$entry['native_currency_code'] = $native->code;
@@ -142,7 +142,7 @@ trait ConvertsExchangeRates
if ($currency->id === $native->id) {
$entry['converted'] = false;
$entry['native_sum'] = $entry['sum'];
$entry['native_currency_id'] = (string)$native->id;
$entry['native_currency_id'] = (string) $native->id;
$entry['native_currency_name'] = $native->name;
$entry['native_currency_symbol'] = $native->symbol;
$entry['native_currency_code'] = $native->code;

View File

@@ -39,21 +39,11 @@ class ExchangeRateConverter
{
// use ConvertsExchangeRates;
private array $fallback = [];
private bool $ignoreSettings = false;
private bool $isPrepared = false;
private bool $noPreparedRates = false;
private array $prepared = [];
private int $queryCount = 0;
private bool $ignoreSettings = false;
public function setIgnoreSettings(bool $ignoreSettings): void
{
$this->ignoreSettings = $ignoreSettings;
}
public function enabled(): bool
{
return false !== config('cer.enabled') || true === $this->ignoreSettings;
}
/**
* @throws FireflyException
@@ -70,6 +60,11 @@ class ExchangeRateConverter
return bcmul($amount, $rate);
}
public function enabled(): bool
{
return false !== config('cer.enabled') || true === $this->ignoreSettings;
}
/**
* @throws FireflyException
*/
@@ -138,6 +133,11 @@ class ExchangeRateConverter
return $rate;
}
private function getCacheKey(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string
{
return sprintf('cer-%d-%d-%s', $from->id, $to->id, $date->format('Y-m-d'));
}
private function getFromDB(int $from, int $to, string $date): ?string
{
if ($from === $to) {
@@ -338,6 +338,11 @@ class ExchangeRateConverter
Log::debug(sprintf('Fallback rate %s > %s = %s', $to->code, $from->code, bcdiv('1', $fallback)));
}
public function setIgnoreSettings(bool $ignoreSettings): void
{
$this->ignoreSettings = $ignoreSettings;
}
public function summarize(): void
{
if (false === $this->enabled()) {
@@ -345,9 +350,4 @@ class ExchangeRateConverter
}
Log::debug(sprintf('ExchangeRateConverter ran %d queries.', $this->queryCount));
}
private function getCacheKey(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string
{
return sprintf('cer-%d-%d-%s', $from->id, $to->id, $date->format('Y-m-d'));
}
}

View File

@@ -31,6 +31,13 @@ use LaravelJsonApi\Core\Query\QueryParameters;
trait ParsesQueryFilters
{
private function arrayOfStrings(QueryParameters $parameters, string $field): array
{
$array = $parameters->filter()?->value($field, []) ?? [];
return is_string($array) ? [$array] : $array;
}
private function dateOrToday(QueryParameters $parameters, string $field): Carbon
{
$date = today();
@@ -51,25 +58,18 @@ trait ParsesQueryFilters
return $date;
}
private function arrayOfStrings(QueryParameters $parameters, string $field): array
{
$array = $parameters->filter()?->value($field, []) ?? [];
return is_string($array) ? [$array] : $array;
}
private function integerFromQueryParams(QueryParameters $parameters, string $field, int $default): int
{
return (int) ($parameters->page()[$field] ?? $default);
}
private function stringFromFilterParams(QueryParameters $parameters, string $field, string $default): string
{
return (string) $parameters->filter()?->value($field, $default) ?? $default;
}
private function stringFromQueryParams(QueryParameters $parameters, string $field, string $default): string
{
return (string) ($parameters->page()[$field] ?? $default);
}
private function stringFromFilterParams(QueryParameters $parameters, string $field, string $default): string
{
return (string)$parameters->filter()?->value($field, $default) ?? $default;
}
}

View File

@@ -59,7 +59,7 @@ class SummaryBalanceGrouped
$return[] = [
'key' => sprintf('%s-in-native', $title),
'value' => $this->amounts[$key]['native'] ?? '0',
'currency_id' => (string)$this->default->id,
'currency_id' => (string) $this->default->id,
'currency_code' => $this->default->code,
'currency_symbol' => $this->default->symbol,
'currency_decimal_places' => $this->default->decimal_places,
@@ -72,7 +72,7 @@ class SummaryBalanceGrouped
// skip native entries.
continue;
}
$currencyId = (int)$currencyId;
$currencyId = (int) $currencyId;
$currency = $this->currencies[$currencyId] ?? $this->currencyRepository->find($currencyId);
$this->currencies[$currencyId] = $currency;
// create objects for big array.
@@ -86,7 +86,7 @@ class SummaryBalanceGrouped
$return[] = [
'key' => sprintf('%s-in-%s', $title, $currency->code),
'value' => $this->amounts[$key][$currencyId] ?? '0',
'currency_id' => (string)$currency->id,
'currency_id' => (string) $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
@@ -108,12 +108,12 @@ class SummaryBalanceGrouped
/** @var array $journal */
foreach ($journals as $journal) {
// transaction info:
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$amount = bcmul($journal['amount'], $multiplier);
$currency = $this->currencies[$currencyId] ?? TransactionCurrency::find($currencyId);
$this->currencies[$currencyId] = $currency;
$nativeAmount = $converter->convert($currency, $this->default, $journal['date'], $amount);
if ((int)$journal['foreign_currency_id'] === $this->default->id) {
if ((int) $journal['foreign_currency_id'] === $this->default->id) {
// use foreign amount instead
$nativeAmount = $journal['foreign_amount'];
}