mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Replace native with primary where possible
This commit is contained in:
@@ -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,
|
||||
];
|
||||
}
|
||||
|
||||
|
@@ -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));
|
||||
|
@@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user