mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fix display of foreign currencies in charts.
This commit is contained in:
parent
b69a2ef0cd
commit
17b6cc43d5
@ -219,8 +219,8 @@ class AccountController extends Controller
|
|||||||
$start->subDay();
|
$start->subDay();
|
||||||
|
|
||||||
$ids = $accounts->pluck('id')->toArray();
|
$ids = $accounts->pluck('id')->toArray();
|
||||||
$startBalances = Steam::balancesById($ids, $start);
|
$startBalances = Steam::balancesByAccounts($accounts, $start);
|
||||||
$endBalances = Steam::balancesById($ids, $end);
|
$endBalances = Steam::balancesByAccounts($accounts, $end);
|
||||||
$activities = Steam::getLastActivities($ids);
|
$activities = Steam::getLastActivities($ids);
|
||||||
|
|
||||||
$accounts->each(
|
$accounts->each(
|
||||||
|
@ -114,9 +114,8 @@ class AccountController extends Controller
|
|||||||
$start->subDay();
|
$start->subDay();
|
||||||
|
|
||||||
$accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
|
$accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
|
||||||
$ids = $accounts->pluck('id')->toArray();
|
$startBalances = Steam::balancesByAccounts($accounts, $start);
|
||||||
$startBalances = Steam::balancesById($ids, $start);
|
$endBalances = Steam::balancesByAccounts($accounts, $end);
|
||||||
$endBalances = Steam::balancesById($ids, $end);
|
|
||||||
$chartData = [];
|
$chartData = [];
|
||||||
|
|
||||||
foreach ($accounts as $account) {
|
foreach ($accounts as $account) {
|
||||||
@ -350,7 +349,7 @@ class AccountController extends Controller
|
|||||||
$cache->addProperty('chart.account.period');
|
$cache->addProperty('chart.account.period');
|
||||||
$cache->addProperty($account->id);
|
$cache->addProperty($account->id);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
//return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$format = (string)trans('config.month_and_day');
|
$format = (string)trans('config.month_and_day');
|
||||||
@ -410,9 +409,8 @@ class AccountController extends Controller
|
|||||||
$accounts = $repository->getAccountsByType([AccountType::REVENUE]);
|
$accounts = $repository->getAccountsByType([AccountType::REVENUE]);
|
||||||
|
|
||||||
$start->subDay();
|
$start->subDay();
|
||||||
$ids = $accounts->pluck('id')->toArray();
|
$startBalances = Steam::balancesByAccounts($accounts, $start);
|
||||||
$startBalances = Steam::balancesById($ids, $start);
|
$endBalances = Steam::balancesByAccounts($accounts, $end);
|
||||||
$endBalances = Steam::balancesById($ids, $end);
|
|
||||||
|
|
||||||
foreach ($accounts as $account) {
|
foreach ($accounts as $account) {
|
||||||
$id = $account->id;
|
$id = $account->id;
|
||||||
|
@ -67,11 +67,10 @@ class ReportController extends Controller
|
|||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
$ids = $accounts->pluck('id')->toArray();
|
|
||||||
$current = clone $start;
|
$current = clone $start;
|
||||||
$chartData = [];
|
$chartData = [];
|
||||||
while ($current < $end) {
|
while ($current < $end) {
|
||||||
$balances = Steam::balancesById($ids, $current);
|
$balances = Steam::balancesByAccounts($accounts, $current);
|
||||||
$sum = $this->arraySum($balances);
|
$sum = $this->arraySum($balances);
|
||||||
$label = $current->formatLocalized(strval(trans('config.month_and_day')));
|
$label = $current->formatLocalized(strval(trans('config.month_and_day')));
|
||||||
$chartData[$label] = $sum;
|
$chartData[$label] = $sum;
|
||||||
|
@ -41,11 +41,10 @@ class AccountTasker implements AccountTaskerInterface
|
|||||||
*/
|
*/
|
||||||
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array
|
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array
|
||||||
{
|
{
|
||||||
$ids = $accounts->pluck('id')->toArray();
|
|
||||||
$yesterday = clone $start;
|
$yesterday = clone $start;
|
||||||
$yesterday->subDay();
|
$yesterday->subDay();
|
||||||
$startSet = Steam::balancesById($ids, $yesterday);
|
$startSet = Steam::balancesByAccounts($accounts, $yesterday);
|
||||||
$endSet = Steam::balancesById($ids, $end);
|
$endSet = Steam::balancesByAccounts($accounts, $end);
|
||||||
|
|
||||||
Log::debug('Start of accountreport');
|
Log::debug('Start of accountreport');
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ use Crypt;
|
|||||||
use DB;
|
use DB;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Steam
|
* Class Steam
|
||||||
@ -107,7 +108,7 @@ class Steam
|
|||||||
->where('transactions.foreign_currency_id', $currencyId)
|
->where('transactions.foreign_currency_id', $currencyId)
|
||||||
->sum('transactions.foreign_amount')
|
->sum('transactions.foreign_amount')
|
||||||
);
|
);
|
||||||
$balance = bcadd($nativeBalance, $foreignBalance);
|
$balance = bcadd($nativeBalance, $foreignBalance);
|
||||||
|
|
||||||
$cache->store($balance);
|
$cache->store($balance);
|
||||||
|
|
||||||
@ -134,30 +135,59 @@ class Steam
|
|||||||
$cache->addProperty($start);
|
$cache->addProperty($start);
|
||||||
$cache->addProperty($end);
|
$cache->addProperty($end);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return $cache->get(); // @codeCoverageIgnore
|
//return $cache->get(); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$balances = [];
|
|
||||||
$start->subDay();
|
$start->subDay();
|
||||||
$end->addDay();
|
$end->addDay();
|
||||||
$startBalance = $this->balance($account, $start);
|
$balances = [];
|
||||||
$balances[$start->format('Y-m-d')] = $startBalance;
|
$formatted = $start->format('Y-m-d');
|
||||||
|
$startBalance = $this->balance($account, $start);
|
||||||
|
$balances[$formatted] = $startBalance;
|
||||||
|
$currencyId = intval($account->getMeta('currency_id'));
|
||||||
$start->addDay();
|
$start->addDay();
|
||||||
|
|
||||||
// query!
|
// query!
|
||||||
$set = $account->transactions()
|
$set = $account->transactions()
|
||||||
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||||
->groupBy('transaction_journals.date')
|
->groupBy('transaction_journals.date')
|
||||||
->orderBy('transaction_journals.date', 'ASC')
|
->groupBy('transactions.transaction_currency_id')
|
||||||
->whereNull('transaction_journals.deleted_at')
|
->groupBy('transactions.foreign_currency_id')
|
||||||
->get(['transaction_journals.date', DB::raw('SUM(transactions.amount) AS modified')]);
|
->orderBy('transaction_journals.date', 'ASC')
|
||||||
|
->whereNull('transaction_journals.deleted_at')
|
||||||
|
->get(
|
||||||
|
[
|
||||||
|
'transaction_journals.date',
|
||||||
|
'transactions.transaction_currency_id',
|
||||||
|
DB::raw('SUM(transactions.amount) AS modified'),
|
||||||
|
'transactions.foreign_currency_id',
|
||||||
|
DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// echo '<pre>';
|
||||||
|
// var_dump($set->toArray());
|
||||||
|
// exit;
|
||||||
|
|
||||||
$currentBalance = $startBalance;
|
$currentBalance = $startBalance;
|
||||||
/** @var Transaction $entry */
|
/** @var Transaction $entry */
|
||||||
foreach ($set as $entry) {
|
foreach ($set as $entry) {
|
||||||
|
// normal amount and foreign amount
|
||||||
$modified = is_null($entry->modified) ? '0' : strval($entry->modified);
|
$modified = is_null($entry->modified) ? '0' : strval($entry->modified);
|
||||||
$currentBalance = bcadd($currentBalance, $modified);
|
$foreignModified = is_null($entry->modified_foreign) ? '0' : strval($entry->modified_foreign);
|
||||||
|
$amount = '0';
|
||||||
|
if ($currencyId === $entry->transaction_currency_id) {
|
||||||
|
// use normal amount:
|
||||||
|
$amount = $modified;
|
||||||
|
}
|
||||||
|
if ($currencyId === $entry->foreign_currency_id) {
|
||||||
|
// use normal amount:
|
||||||
|
$amount = $foreignModified;
|
||||||
|
}
|
||||||
|
|
||||||
|
$currentBalance = bcadd($currentBalance, $amount);
|
||||||
$carbon = new Carbon($entry->date);
|
$carbon = new Carbon($entry->date);
|
||||||
$date = $carbon->format('Y-m-d');
|
$date = $carbon->format('Y-m-d');
|
||||||
$balances[$date] = $currentBalance;
|
$balances[$date] = $currentBalance;
|
||||||
@ -173,38 +203,30 @@ class Steam
|
|||||||
/**
|
/**
|
||||||
* This method always ignores the virtual balance.
|
* This method always ignores the virtual balance.
|
||||||
*
|
*
|
||||||
* @param array $ids
|
* @param \Illuminate\Support\Collection $accounts
|
||||||
* @param \Carbon\Carbon $date
|
* @param \Carbon\Carbon $date
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function balancesById(array $ids, Carbon $date): array
|
public function balancesByAccounts(Collection $accounts, Carbon $date): array
|
||||||
{
|
{
|
||||||
|
$ids = $accounts->pluck('id')->toArray();
|
||||||
// cache this property.
|
// cache this property.
|
||||||
$cache = new CacheProperties;
|
$cache = new CacheProperties;
|
||||||
$cache->addProperty($ids);
|
$cache->addProperty($ids);
|
||||||
$cache->addProperty('balances');
|
$cache->addProperty('balances');
|
||||||
$cache->addProperty($date);
|
$cache->addProperty($date);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return $cache->get(); // @codeCoverageIgnore
|
//return $cache->get(); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$balances = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
// need to do this per account.
|
||||||
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
|
|
||||||
->groupBy('transactions.account_id')
|
|
||||||
->whereIn('transactions.account_id', $ids)
|
|
||||||
->whereNull('transaction_journals.deleted_at')
|
|
||||||
->get(['transactions.account_id', DB::raw('sum(transactions.amount) AS aggregate')]);
|
|
||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
foreach ($balances as $entry) {
|
/** @var Account $account */
|
||||||
$accountId = intval($entry->account_id);
|
foreach ($accounts as $account) {
|
||||||
$balance = $entry->aggregate;
|
$result[$account->id] = $this->balance($account, $date);
|
||||||
$result[$accountId] = $balance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$cache->store($result);
|
$cache->store($result);
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
@ -132,7 +132,7 @@ class AccountControllerTest extends TestCase
|
|||||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||||
$repository->shouldReceive('getAccountsByType')->andReturn(new Collection([$account]));
|
$repository->shouldReceive('getAccountsByType')->andReturn(new Collection([$account]));
|
||||||
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
||||||
Steam::shouldReceive('balancesById')->andReturn([$account->id => '100']);
|
Steam::shouldReceive('balancesByAccounts')->andReturn([$account->id => '100']);
|
||||||
Steam::shouldReceive('getLastActivities')->andReturn([]);
|
Steam::shouldReceive('getLastActivities')->andReturn([]);
|
||||||
|
|
||||||
$this->be($this->user());
|
$this->be($this->user());
|
||||||
|
@ -68,7 +68,7 @@ class AccountControllerTest extends TestCase
|
|||||||
|
|
||||||
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::EXPENSE, AccountType::BENEFICIARY]])->andReturn(new Collection([$account]));
|
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::EXPENSE, AccountType::BENEFICIARY]])->andReturn(new Collection([$account]));
|
||||||
$generator->shouldReceive('singleSet')->andReturn([]);
|
$generator->shouldReceive('singleSet')->andReturn([]);
|
||||||
Steam::shouldReceive('balancesById')->twice()->andReturn([]);
|
Steam::shouldReceive('balancesByAccounts')->twice()->andReturn([]);
|
||||||
|
|
||||||
|
|
||||||
$this->be($this->user());
|
$this->be($this->user());
|
||||||
@ -333,7 +333,7 @@ class AccountControllerTest extends TestCase
|
|||||||
|
|
||||||
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::REVENUE]])->andReturn(new Collection([$account]));
|
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::REVENUE]])->andReturn(new Collection([$account]));
|
||||||
$generator->shouldReceive('singleSet')->andReturn([]);
|
$generator->shouldReceive('singleSet')->andReturn([]);
|
||||||
Steam::shouldReceive('balancesById')->twice()->andReturn([]);
|
Steam::shouldReceive('balancesByAccounts')->twice()->andReturn([]);
|
||||||
|
|
||||||
$this->be($this->user());
|
$this->be($this->user());
|
||||||
$this->changeDateRange($this->user(), $range);
|
$this->changeDateRange($this->user(), $range);
|
||||||
|
@ -33,9 +33,8 @@ class ReportControllerTest extends TestCase
|
|||||||
public function testNetWorth()
|
public function testNetWorth()
|
||||||
{
|
{
|
||||||
$generator = $this->mock(GeneratorInterface::class);
|
$generator = $this->mock(GeneratorInterface::class);
|
||||||
$tasker = $this->mock(AccountTaskerInterface::class);
|
|
||||||
|
|
||||||
Steam::shouldReceive('balancesById')->andReturn(['5', '10']);
|
Steam::shouldReceive('balancesByAccounts')->andReturn(['5', '10']);
|
||||||
$generator->shouldReceive('singleSet')->andReturn([]);
|
$generator->shouldReceive('singleSet')->andReturn([]);
|
||||||
|
|
||||||
$this->be($this->user());
|
$this->be($this->user());
|
||||||
|
Loading…
Reference in New Issue
Block a user