Multi currency net worth box.

This commit is contained in:
James Cole 2018-01-13 18:40:28 +01:00
parent edd5215b21
commit 5c27c8e633
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 25 additions and 9 deletions

View File

@ -25,11 +25,13 @@ namespace FireflyIII\Http\Controllers\Json;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface; use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\CacheProperties; use FireflyIII\Support\CacheProperties;
use Response; use Response;
@ -170,7 +172,7 @@ class BoxController extends Controller
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function netWorth(AccountRepositoryInterface $repository) public function netWorth(AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepos)
{ {
$date = new Carbon(date('Y-m-d')); // needed so its per day. $date = new Carbon(date('Y-m-d')); // needed so its per day.
/** @var Carbon $start */ /** @var Carbon $start */
@ -193,16 +195,32 @@ class BoxController extends Controller
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore return Response::json($cache->get()); // @codeCoverageIgnore
} }
$netWorth = [];
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); $accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$currency = app('amount')->getDefaultCurrency(); $currency = app('amount')->getDefaultCurrency();
$balances = app('steam')->balancesByAccounts($accounts, $date); $balances = app('steam')->balancesByAccounts($accounts, $date);
$sum = '0';
foreach ($balances as $entry) { /** @var Account $account */
$sum = bcadd($sum, $entry); foreach ($accounts as $account) {
$accountCurrency = $currency;
$balance = $balances[$account->id] ?? '0';
$currencyId = intval($account->getMeta('currency_id'));
if ($currencyId !== 0) {
$accountCurrency = $currencyRepos->find($currencyId);
}
if (!isset($netWorth[$accountCurrency->id])) {
$netWorth[$accountCurrency->id]['currency'] = $accountCurrency;
$netWorth[$accountCurrency->id]['sum'] = '0';
}
$netWorth[$accountCurrency->id]['sum'] = bcadd($netWorth[$accountCurrency->id]['sum'], $balance);
} }
$return = [];
foreach ($netWorth as $currencyId => $data) {
$return[$currencyId] = app('amount')->formatAnything($data['currency'], $data['sum'], false);
}
$return = [ $return = [
'net_worth' => app('amount')->formatAnything($currency, $sum, false), 'net_worths' => array_values($return),
]; ];
$cache->store($return); $cache->store($return);

View File

@ -67,7 +67,7 @@ function getPiggyBanks() {
function getNetWorthBox() { function getNetWorthBox() {
// box-net-worth // box-net-worth
$.getJSON('json/box/net-worth').done(function (data) { $.getJSON('json/box/net-worth').done(function (data) {
$('#box-net-worth').html(data.net_worth); $('#box-net-worth').html(data.net_worths.join(', '));
}); });
} }

View File

@ -70,9 +70,7 @@
<div class="progress"> <div class="progress">
<div class="progress-bar" style="width: 0%"></div> <div class="progress-bar" style="width: 0%"></div>
</div> </div>
<span class="progress-description"> <span class="progress-description"></span>
<span title="{{ 'net_worth'|_ }}" id="box-clear"></span>
</span>
</div> </div>
</div> </div>
</div> </div>