Skip to content

Firefly III: a personal finances manager

Convert to primary currency for reportPeriodChart

Khoa Nguyenc06d8263d8b5
diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php
index d7a45b12e6..6ff25bc252 100644
--- a/app/Http/Controllers/Chart/CategoryController.php
+++ b/app/Http/Controllers/Chart/CategoryController.php
@@ -34,10 +34,10 @@ use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Chart\Category\FrontpageChartGenerator;
use FireflyIII\Support\Chart\Category\WholePeriodChartGenerator;
use FireflyIII\Support\Facades\Navigation;
-use FireflyIII\Support\Facades\Steam;
use FireflyIII\Support\Http\Controllers\AugumentData;
use FireflyIII\Support\Http\Controllers\ChartGeneration;
use FireflyIII\Support\Http\Controllers\DateCalculation;
+use FireflyIII\Support\Http\Controllers\ResolvesJournalAmountAndCurrency;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Psr\Container\ContainerExceptionInterface;
@@ -51,6 +51,7 @@ class CategoryController extends Controller
use AugumentData;
use ChartGeneration;
use DateCalculation;
+ use ResolvesJournalAmountAndCurrency;
protected GeneratorInterface $generator;
@@ -267,7 +268,8 @@ class CategoryController extends Controller
// loop income and expenses for this category.:
$outSet = $expenses[$currencyId]['categories'][$categoryId] ?? ['transaction_journals' => []];
foreach ($outSet['transaction_journals'] as $journal) {
- $amount = Steam::positive($journal['amount']);
+ $journalData = $this->resolveJournalAmountAndCurrency($journal, $currencyInfo);
+ $amount = $journalData['amount'];
$date = $journal['date']->isoFormat($format);
$chartData[$outKey]['entries'][$date] ??= '0';
@@ -276,7 +278,8 @@ class CategoryController extends Controller
$inSet = $income[$currencyId]['categories'][$categoryId] ?? ['transaction_journals' => []];
foreach ($inSet['transaction_journals'] as $journal) {
- $amount = Steam::positive($journal['amount']);
+ $journalData = $this->resolveJournalAmountAndCurrency($journal, $currencyInfo);
+ $amount = $journalData['amount'];
$date = $journal['date']->isoFormat($format);
$chartData[$inKey]['entries'][$date] ??= '0';
$chartData[$inKey]['entries'][$date] = bcadd($amount, $chartData[$inKey]['entries'][$date]);
diff --git a/app/Repositories/Category/NoCategoryRepository.php b/app/Repositories/Category/NoCategoryRepository.php
index 491ece11d0..17c59c0af6 100644
--- a/app/Repositories/Category/NoCategoryRepository.php
+++ b/app/Repositories/Category/NoCategoryRepository.php
@@ -78,8 +78,16 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface, UserGroupIn
// only a subset of the fields.
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['categories'][0]['transaction_journals'][$journalId] = [
- 'amount' => Steam::negative($journal['amount']),
- 'date' => $journal['date'],
+ 'amount' => Steam::negative($journal['amount']),
+ 'currency_id' => (int) $journal['currency_id'],
+ 'currency_name' => (string) $journal['currency_name'],
+ 'currency_symbol' => (string) $journal['currency_symbol'],
+ 'currency_code' => (string) $journal['currency_code'],
+ 'currency_decimal_places' => (int) $journal['currency_decimal_places'],
+ 'foreign_currency_id' => (int) ($journal['foreign_currency_id'] ?? 0),
+ 'foreign_amount' => isset($journal['foreign_amount']) ? Steam::negative((string) $journal['foreign_amount']) : null,
+ 'pc_amount' => isset($journal['pc_amount']) ? Steam::negative((string) $journal['pc_amount']) : null,
+ 'date' => $journal['date'],
];
}
@@ -124,8 +132,16 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface, UserGroupIn
// only a subset of the fields.
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['categories'][0]['transaction_journals'][$journalId] = [
- 'amount' => Steam::positive($journal['amount']),
- 'date' => $journal['date'],
+ 'amount' => Steam::positive($journal['amount']),
+ 'currency_id' => (int) $journal['currency_id'],
+ 'currency_name' => (string) $journal['currency_name'],
+ 'currency_symbol' => (string) $journal['currency_symbol'],
+ 'currency_code' => (string) $journal['currency_code'],
+ 'currency_decimal_places' => (int) $journal['currency_decimal_places'],
+ 'foreign_currency_id' => (int) ($journal['foreign_currency_id'] ?? 0),
+ 'foreign_amount' => isset($journal['foreign_amount']) ? Steam::positive((string) $journal['foreign_amount']) : null,
+ 'pc_amount' => isset($journal['pc_amount']) ? Steam::positive((string) $journal['pc_amount']) : null,
+ 'date' => $journal['date'],
];
}
diff --git a/app/Repositories/Category/OperationsRepository.php b/app/Repositories/Category/OperationsRepository.php
index ba53a9627d..663f96f9f7 100644
--- a/app/Repositories/Category/OperationsRepository.php
+++ b/app/Repositories/Category/OperationsRepository.php
@@ -153,6 +153,14 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['categories'][$categoryId]['transaction_journals'][$journalId] = [
'amount' => Steam::negative($journal['amount']),
+ 'currency_id' => (int) $journal['currency_id'],
+ 'currency_name' => (string) $journal['currency_name'],
+ 'currency_symbol' => (string) $journal['currency_symbol'],
+ 'currency_code' => (string) $journal['currency_code'],
+ 'currency_decimal_places' => (int) $journal['currency_decimal_places'],
+ 'foreign_currency_id' => (int) ($journal['foreign_currency_id'] ?? 0),
+ 'foreign_amount' => isset($journal['foreign_amount']) ? Steam::negative((string) $journal['foreign_amount']) : null,
+ 'pc_amount' => isset($journal['pc_amount']) ? Steam::negative((string) $journal['pc_amount']) : null,
'date' => $journal['date'],
'source_account_id' => (string) $journal['source_account_id'],
'budget_name' => $journal['budget_name'],
@@ -223,6 +231,14 @@ class OperationsRepository implements OperationsRepositoryInterface, UserGroupIn
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['categories'][$categoryId]['transaction_journals'][$journalId] = [
'amount' => Steam::positive($journal['amount']),
+ 'currency_id' => (int) $journal['currency_id'],
+ 'currency_name' => (string) $journal['currency_name'],
+ 'currency_symbol' => (string) $journal['currency_symbol'],
+ 'currency_code' => (string) $journal['currency_code'],
+ 'currency_decimal_places' => (int) $journal['currency_decimal_places'],
+ 'foreign_currency_id' => (int) ($journal['foreign_currency_id'] ?? 0),
+ 'foreign_amount' => isset($journal['foreign_amount']) ? Steam::positive((string) $journal['foreign_amount']) : null,
+ 'pc_amount' => isset($journal['pc_amount']) ? Steam::positive((string) $journal['pc_amount']) : null,
'date' => $journal['date'],
'source_account_id' => (string) $journal['source_account_id'],
'destination_account_id' => (string) $journal['destination_account_id'],