Fix display of foreign currencies in charts.

This commit is contained in:
James Cole 2017-06-05 22:11:54 +02:00
parent b69a2ef0cd
commit 17b6cc43d5
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
8 changed files with 67 additions and 50 deletions

View File

@ -219,8 +219,8 @@ class AccountController extends Controller
$start->subDay();
$ids = $accounts->pluck('id')->toArray();
$startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end);
$startBalances = Steam::balancesByAccounts($accounts, $start);
$endBalances = Steam::balancesByAccounts($accounts, $end);
$activities = Steam::getLastActivities($ids);
$accounts->each(

View File

@ -114,9 +114,8 @@ class AccountController extends Controller
$start->subDay();
$accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
$ids = $accounts->pluck('id')->toArray();
$startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end);
$startBalances = Steam::balancesByAccounts($accounts, $start);
$endBalances = Steam::balancesByAccounts($accounts, $end);
$chartData = [];
foreach ($accounts as $account) {
@ -350,7 +349,7 @@ class AccountController extends Controller
$cache->addProperty('chart.account.period');
$cache->addProperty($account->id);
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
//return Response::json($cache->get()); // @codeCoverageIgnore
}
$format = (string)trans('config.month_and_day');
@ -410,9 +409,8 @@ class AccountController extends Controller
$accounts = $repository->getAccountsByType([AccountType::REVENUE]);
$start->subDay();
$ids = $accounts->pluck('id')->toArray();
$startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end);
$startBalances = Steam::balancesByAccounts($accounts, $start);
$endBalances = Steam::balancesByAccounts($accounts, $end);
foreach ($accounts as $account) {
$id = $account->id;

View File

@ -67,11 +67,10 @@ class ReportController extends Controller
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
$ids = $accounts->pluck('id')->toArray();
$current = clone $start;
$chartData = [];
while ($current < $end) {
$balances = Steam::balancesById($ids, $current);
$balances = Steam::balancesByAccounts($accounts, $current);
$sum = $this->arraySum($balances);
$label = $current->formatLocalized(strval(trans('config.month_and_day')));
$chartData[$label] = $sum;

View File

@ -41,11 +41,10 @@ class AccountTasker implements AccountTaskerInterface
*/
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array
{
$ids = $accounts->pluck('id')->toArray();
$yesterday = clone $start;
$yesterday->subDay();
$startSet = Steam::balancesById($ids, $yesterday);
$endSet = Steam::balancesById($ids, $end);
$startSet = Steam::balancesByAccounts($accounts, $yesterday);
$endSet = Steam::balancesByAccounts($accounts, $end);
Log::debug('Start of accountreport');

View File

@ -18,6 +18,7 @@ use Crypt;
use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use Illuminate\Support\Collection;
/**
* Class Steam
@ -107,7 +108,7 @@ class Steam
->where('transactions.foreign_currency_id', $currencyId)
->sum('transactions.foreign_amount')
);
$balance = bcadd($nativeBalance, $foreignBalance);
$balance = bcadd($nativeBalance, $foreignBalance);
$cache->store($balance);
@ -134,30 +135,59 @@ class Steam
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
//return $cache->get(); // @codeCoverageIgnore
}
$balances = [];
$start->subDay();
$end->addDay();
$startBalance = $this->balance($account, $start);
$balances[$start->format('Y-m-d')] = $startBalance;
$balances = [];
$formatted = $start->format('Y-m-d');
$startBalance = $this->balance($account, $start);
$balances[$formatted] = $startBalance;
$currencyId = intval($account->getMeta('currency_id'));
$start->addDay();
// query!
$set = $account->transactions()
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
->groupBy('transaction_journals.date')
->orderBy('transaction_journals.date', 'ASC')
->whereNull('transaction_journals.deleted_at')
->get(['transaction_journals.date', DB::raw('SUM(transactions.amount) AS modified')]);
$set = $account->transactions()
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
->groupBy('transaction_journals.date')
->groupBy('transactions.transaction_currency_id')
->groupBy('transactions.foreign_currency_id')
->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;
/** @var Transaction $entry */
foreach ($set as $entry) {
// normal amount and foreign amount
$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);
$date = $carbon->format('Y-m-d');
$balances[$date] = $currentBalance;
@ -173,38 +203,30 @@ class Steam
/**
* This method always ignores the virtual balance.
*
* @param array $ids
* @param \Carbon\Carbon $date
* @param \Illuminate\Support\Collection $accounts
* @param \Carbon\Carbon $date
*
* @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 = new CacheProperties;
$cache->addProperty($ids);
$cache->addProperty('balances');
$cache->addProperty($date);
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
//return $cache->get(); // @codeCoverageIgnore
}
$balances = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->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')]);
// need to do this per account.
$result = [];
foreach ($balances as $entry) {
$accountId = intval($entry->account_id);
$balance = $entry->aggregate;
$result[$accountId] = $balance;
/** @var Account $account */
foreach ($accounts as $account) {
$result[$account->id] = $this->balance($account, $date);
}
$cache->store($result);
return $result;

View File

@ -132,7 +132,7 @@ class AccountControllerTest extends TestCase
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->andReturn(new Collection([$account]));
$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([]);
$this->be($this->user());

View File

@ -68,7 +68,7 @@ class AccountControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::EXPENSE, AccountType::BENEFICIARY]])->andReturn(new Collection([$account]));
$generator->shouldReceive('singleSet')->andReturn([]);
Steam::shouldReceive('balancesById')->twice()->andReturn([]);
Steam::shouldReceive('balancesByAccounts')->twice()->andReturn([]);
$this->be($this->user());
@ -333,7 +333,7 @@ class AccountControllerTest extends TestCase
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::REVENUE]])->andReturn(new Collection([$account]));
$generator->shouldReceive('singleSet')->andReturn([]);
Steam::shouldReceive('balancesById')->twice()->andReturn([]);
Steam::shouldReceive('balancesByAccounts')->twice()->andReturn([]);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);

View File

@ -33,9 +33,8 @@ class ReportControllerTest extends TestCase
public function testNetWorth()
{
$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([]);
$this->be($this->user());