Replace native with primary where possible

This commit is contained in:
Sander Dorigo
2025-08-01 12:31:01 +02:00
parent 65dcad6898
commit 6278662014
59 changed files with 430 additions and 430 deletions

View File

@@ -41,7 +41,7 @@ class AccountBalanceGrouped
private readonly ExchangeRateConverter $converter;
private array $currencies = [];
private array $data = [];
private TransactionCurrency $default;
private TransactionCurrency $primary;
private Carbon $end;
private array $journals = [];
private string $preferredRange;
@@ -70,16 +70,16 @@ class AccountBalanceGrouped
'currency_symbol' => $currency['currency_symbol'],
'currency_code' => $currency['currency_code'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'native_currency_id' => (string) $currency['native_currency_id'],
'native_currency_symbol' => $currency['native_currency_symbol'],
'native_currency_code' => $currency['native_currency_code'],
'native_currency_decimal_places' => $currency['native_currency_decimal_places'],
'primary_currency_id' => (string) $currency['primary_currency_id'],
'primary_currency_symbol' => $currency['primary_currency_symbol'],
'primary_currency_code' => $currency['primary_currency_code'],
'primary_currency_decimal_places' => $currency['primary_currency_decimal_places'],
'date' => $this->start->toAtomString(),
'start' => $this->start->toAtomString(),
'end' => $this->end->toAtomString(),
'period' => $this->preferredRange,
'entries' => [],
'native_entries' => [],
'primary_entries' => [],
];
$expense = [
'label' => 'spent',
@@ -87,16 +87,16 @@ class AccountBalanceGrouped
'currency_symbol' => $currency['currency_symbol'],
'currency_code' => $currency['currency_code'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'native_currency_id' => (string) $currency['native_currency_id'],
'native_currency_symbol' => $currency['native_currency_symbol'],
'native_currency_code' => $currency['native_currency_code'],
'native_currency_decimal_places' => $currency['native_currency_decimal_places'],
'primary_currency_id' => (string) $currency['primary_currency_id'],
'primary_currency_symbol' => $currency['primary_currency_symbol'],
'primary_currency_code' => $currency['primary_currency_code'],
'primary_currency_decimal_places' => $currency['primary_currency_decimal_places'],
'date' => $this->start->toAtomString(),
'start' => $this->start->toAtomString(),
'end' => $this->end->toAtomString(),
'period' => $this->preferredRange,
'entries' => [],
'native_entries' => [],
'pc_entries' => [],
];
// loop all possible periods between $start and $end, and add them to the correct dataset.
$currentStart = clone $this->start;
@@ -108,8 +108,8 @@ class AccountBalanceGrouped
$expense['entries'][$label] = app('steam')->bcround($currency[$key]['spent'] ?? '0', $currency['currency_decimal_places']);
// converted entries
$income['native_entries'][$label] = app('steam')->bcround($currency[$key]['native_earned'] ?? '0', $currency['native_currency_decimal_places']);
$expense['native_entries'][$label] = app('steam')->bcround($currency[$key]['native_spent'] ?? '0', $currency['native_currency_decimal_places']);
$income['pc_entries'][$label] = app('steam')->bcround($currency[$key]['pc_earned'] ?? '0', $currency['primary_currency_decimal_places']);
$expense['pc_entries'][$label] = app('steam')->bcround($currency[$key]['pc_spent'] ?? '0', $currency['primary_currency_decimal_places']);
// next loop
$currentStart = app('navigation')->addPeriod($currentStart, $this->preferredRange, 0);
@@ -159,8 +159,8 @@ class AccountBalanceGrouped
$rate = $this->getRate($currency, $journal['date']);
$amountConverted = bcmul((string) $amount, $rate);
// perhaps transaction already has the foreign amount in the native currency.
if ((int) $journal['foreign_currency_id'] === $this->default->id) {
// perhaps transaction already has the foreign amount in the primary currency.
if ((int) $journal['foreign_currency_id'] === $this->primary->id) {
$amountConverted = $journal['foreign_amount'] ?? '0';
$amountConverted = 'earned' === $key ? app('steam')->positive($amountConverted) : app('steam')->negative($amountConverted);
}
@@ -169,7 +169,7 @@ class AccountBalanceGrouped
$this->data[$currencyId][$period][$key] = bcadd((string) $this->data[$currencyId][$period][$key], (string) $amount);
// add converted entry
$convertedKey = sprintf('native_%s', $key);
$convertedKey = sprintf('pc_%s', $key);
$this->data[$currencyId][$period][$convertedKey] = bcadd((string) $this->data[$currencyId][$period][$convertedKey], (string) $amountConverted);
}
@@ -192,11 +192,11 @@ class AccountBalanceGrouped
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_decimal_places' => $journal['currency_decimal_places'],
// native currency info (could be the same)
'native_currency_id' => (string) $this->default->id,
'native_currency_code' => $this->default->code,
'native_currency_symbol' => $this->default->symbol,
'native_currency_decimal_places' => $this->default->decimal_places,
// primary currency info (could be the same)
'primary_currency_id' => (string) $this->primary->id,
'primary_currency_code' => $this->primary->code,
'primary_currency_symbol' => $this->primary->symbol,
'primary_currency_decimal_places' => $this->primary->decimal_places,
];
}
@@ -208,8 +208,8 @@ class AccountBalanceGrouped
'period' => $period,
'spent' => '0',
'earned' => '0',
'native_spent' => '0',
'native_earned' => '0',
'pc_spent' => '0',
'pc_earned' => '0',
];
}
@@ -238,7 +238,7 @@ class AccountBalanceGrouped
private function getRate(TransactionCurrency $currency, Carbon $date): string
{
try {
$rate = $this->converter->getCurrencyRate($currency, $this->default, $date);
$rate = $this->converter->getCurrencyRate($currency, $this->primary, $date);
} catch (FireflyException $e) {
app('log')->error($e->getMessage());
$rate = '1';
@@ -252,22 +252,22 @@ class AccountBalanceGrouped
$this->accountIds = $accounts->pluck('id')->toArray();
}
public function setDefault(TransactionCurrency $default): void
public function setPrimary(TransactionCurrency $primary): 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,
$this->primary = $primary;
$primaryCurrencyId = $primary->id;
$this->currencies = [$primary->id => $primary]; // currency cache
$this->data[$primaryCurrencyId] = [
'currency_id' => (string) $primaryCurrencyId,
'currency_symbol' => $primary->symbol,
'currency_code' => $primary->code,
'currency_name' => $primary->name,
'currency_decimal_places' => $primary->decimal_places,
'primary_currency_id' => (string) $primaryCurrencyId,
'primary_currency_symbol' => $primary->symbol,
'primary_currency_code' => $primary->code,
'primary_currency_name' => $primary->name,
'primary_currency_decimal_places' => $primary->decimal_places,
];
}

View File

@@ -50,8 +50,8 @@ trait CleansChartData
if (array_key_exists('currency_id', $array)) {
$array['currency_id'] = (string) $array['currency_id'];
}
if (array_key_exists('native_currency_id', $array)) {
$array['native_currency_id'] = (string) $array['native_currency_id'];
if (array_key_exists('primary_currency_id', $array)) {
$array['primary_currency_id'] = (string) $array['primary_currency_id'];
}
if (!array_key_exists('start', $array)) {
throw new FireflyException(sprintf('Data-set "%s" is missing the "start"-variable.', $index));

View File

@@ -57,8 +57,8 @@ class SummaryBalanceGrouped
};
$return[] = [
'key' => sprintf('%s-in-native', $title),
'value' => $this->amounts[$key]['native'] ?? '0',
'key' => sprintf('%s-in-pc', $title),
'value' => $this->amounts[$key]['primary'] ?? '0',
'currency_id' => (string) $this->default->id,
'currency_code' => $this->default->code,
'currency_symbol' => $this->default->symbol,
@@ -68,8 +68,8 @@ class SummaryBalanceGrouped
// loop 3: format amounts:
$currencyIds = array_keys($this->amounts[self::SUM] ?? []);
foreach ($currencyIds as $currencyId) {
if ('native' === $currencyId) {
// skip native entries.
if ('primary' === $currencyId) {
// skip primary entries.
continue;
}
$currencyId = (int) $currencyId;
@@ -112,23 +112,23 @@ class SummaryBalanceGrouped
$amount = bcmul((string) $journal['amount'], $multiplier);
$currency = $this->currencies[$currencyId] ?? TransactionCurrency::find($currencyId);
$this->currencies[$currencyId] = $currency;
$nativeAmount = $converter->convert($currency, $this->default, $journal['date'], $amount);
$pcAmount = $converter->convert($currency, $this->default, $journal['date'], $amount);
if ((int) $journal['foreign_currency_id'] === $this->default->id) {
// use foreign amount instead
$nativeAmount = $journal['foreign_amount'];
$pcAmount = $journal['foreign_amount'];
}
// prep the arrays
$this->amounts[$key] ??= [];
$this->amounts[$key][$currencyId] ??= '0';
$this->amounts[$key]['native'] ??= '0';
$this->amounts[$key]['primary'] ??= '0';
$this->amounts[self::SUM][$currencyId] ??= '0';
$this->amounts[self::SUM]['native'] ??= '0';
$this->amounts[self::SUM]['primary'] ??= '0';
// add values:
$this->amounts[$key][$currencyId] = bcadd((string) $this->amounts[$key][$currencyId], $amount);
$this->amounts[self::SUM][$currencyId] = bcadd((string) $this->amounts[self::SUM][$currencyId], $amount);
$this->amounts[$key]['native'] = bcadd((string) $this->amounts[$key]['native'], (string) $nativeAmount);
$this->amounts[self::SUM]['native'] = bcadd((string) $this->amounts[self::SUM]['native'], (string) $nativeAmount);
$this->amounts[$key]['primary'] = bcadd((string) $this->amounts[$key]['primary'], (string) $pcAmount);
$this->amounts[self::SUM]['primary'] = bcadd((string) $this->amounts[self::SUM]['primary'], (string) $pcAmount);
}
$converter->summarize();
}

View File

@@ -182,7 +182,7 @@ trait AugumentData
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($budget->id);
$cache->addProperty($this->convertToNative);
$cache->addProperty($this->convertToPrimary);
$cache->addProperty('get-limits');
if ($cache->has()) {
@@ -193,14 +193,14 @@ trait AugumentData
$budgetCollection = new Collection([$budget]);
// merge sets based on a key, in case of convert to native
// merge sets based on a key, in case of convert to primary currency
$limits = new Collection();
/** @var BudgetLimit $entry */
foreach ($set as $entry) {
Log::debug(sprintf('Now at budget limit #%d', $entry->id));
$currency = $entry->transactionCurrency;
if ($this->convertToNative) {
if ($this->convertToPrimary) {
// the sumExpenses method already handles this.
$currency = $this->defaultCurrency;
}
@@ -213,10 +213,10 @@ trait AugumentData
$currentEnd = clone $currentStart;
$currentEnd->addMonth();
}
// native amount.
$expenses = $opsRepository->sumExpenses($currentStart, $currentEnd, null, $budgetCollection, $entry->transactionCurrency, $this->convertToNative);
// primary currency amount.
$expenses = $opsRepository->sumExpenses($currentStart, $currentEnd, null, $budgetCollection, $entry->transactionCurrency, $this->convertToPrimary);
$spent = $expenses[$currency->id]['sum'] ?? '0';
$entry->native_spent = $spent;
$entry->pc_spent = $spent;
// normal amount:
$expenses = $opsRepository->sumExpenses($currentStart, $currentEnd, null, $budgetCollection, $entry->transactionCurrency, false);

View File

@@ -38,7 +38,7 @@ trait BasicDataSupport
*/
protected function isInArray(array $array, int $entryId)
{
$key = $this->convertToNative ? 'native_balance' : 'balance';
$key = $this->convertToPrimary ? 'pc_balance' : 'balance';
return $array[$entryId][$key] ?? '0';
}

View File

@@ -48,13 +48,13 @@ trait ChartGeneration
protected function accountBalanceChart(Collection $accounts, Carbon $start, Carbon $end): array // chart helper method.
{
// chart properties for cache:
$convertToNative = Amount::convertToPrimary();
$convertToPrimary = Amount::convertToPrimary();
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('chart.account.account-balance-chart');
$cache->addProperty($accounts);
$cache->addProperty($convertToNative);
$cache->addProperty($convertToPrimary);
if ($cache->has()) {
return $cache->get();
}
@@ -76,9 +76,9 @@ trait ChartGeneration
foreach ($accounts as $account) {
Log::debug(sprintf('Now at account #%d ("%s)', $account->id, $account->name));
$currency = $accountRepos->getAccountCurrency($account) ?? $default;
$useNative = $convertToNative && $default->id !== $currency->id;
$field = $convertToNative ? 'native_balance' : 'balance';
$currency = $useNative ? $default : $currency;
$usePrimary = $convertToPrimary && $default->id !== $currency->id;
$field = $convertToPrimary ? 'pc_balance' : 'balance';
$currency = $usePrimary ? $default : $currency;
Log::debug(sprintf('Will use field %s', $field));
$currentSet = [
'label' => $account->name,
@@ -87,7 +87,7 @@ trait ChartGeneration
];
$currentStart = clone $start;
$range = Steam::finalAccountBalanceInRange($account, clone $start, clone $end, $this->convertToNative);
$range = Steam::finalAccountBalanceInRange($account, clone $start, clone $end, $this->convertToPrimary);
$previous = array_values($range)[0];
Log::debug(sprintf('Start balance for account #%d ("%s) is', $account->id, $account->name), $previous);
while ($currentStart <= $end) {

View File

@@ -194,15 +194,15 @@ trait PeriodOverview
$foreignCurrencyId = $journal['foreign_currency_id'];
$amount = $journal['amount'] ?? '0';
if ($this->convertToNative && $currencyId !== $this->defaultCurrency->id && $foreignCurrencyId !== $this->defaultCurrency->id) {
$amount = $journal['native_amount'] ?? '0';
$currencyId = $this->defaultCurrency->id;
$currencyCode = $this->defaultCurrency->code;
$currencyName = $this->defaultCurrency->name;
$currencySymbol = $this->defaultCurrency->symbol;
$currencyDecimalPlaces = $this->defaultCurrency->decimal_places;
if ($this->convertToPrimary && $currencyId !== $this->primaryCurrency->id && $foreignCurrencyId !== $this->primaryCurrency->id) {
$amount = $journal['pc_amount'] ?? '0';
$currencyId = $this->primaryCurrency->id;
$currencyCode = $this->primaryCurrency->code;
$currencyName = $this->primaryCurrency->name;
$currencySymbol = $this->primaryCurrency->symbol;
$currencyDecimalPlaces = $this->primaryCurrency->decimal_places;
}
if ($this->convertToNative && $currencyId !== $this->defaultCurrency->id && $foreignCurrencyId === $this->defaultCurrency->id) {
if ($this->convertToPrimary && $currencyId !== $this->primaryCurrency->id && $foreignCurrencyId === $this->primaryCurrency->id) {
$currencyId = (int) $foreignCurrencyId;
$currencyCode = $journal['foreign_currency_code'];
$currencyName = $journal['foreign_currency_name'];
@@ -334,7 +334,7 @@ trait PeriodOverview
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($this->convertToNative);
$cache->addProperty($this->convertToPrimary);
$cache->addProperty('no-budget-period-entries');
if ($cache->has()) {