Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Multi currency net worth box.
James Cole5c27c8e63355
diff --git a/app/Http/Controllers/Json/BoxController.php b/app/Http/Controllers/Json/BoxController.phpindex 7c6810613e..c3f3485677 100644--- a/app/Http/Controllers/Json/BoxController.php+++ b/app/Http/Controllers/Json/BoxController.php@@ -25,11 +25,13 @@ namespace FireflyIII\Http\Controllers\Json;use Carbon\Carbon;use FireflyIII\Helpers\Collector\JournalCollectorInterface;use FireflyIII\Http\Controllers\Controller;+use FireflyIII\Models\Account;use FireflyIII\Models\AccountType;use FireflyIII\Models\TransactionType;use FireflyIII\Repositories\Account\AccountRepositoryInterface;use FireflyIII\Repositories\Bill\BillRepositoryInterface;use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;use FireflyIII\Support\CacheProperties;use Response;@@ -170,7 +172,7 @@ class BoxController extends Controller** @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./** @var Carbon $start */@@ -193,16 +195,32 @@ class BoxController extends Controllerif ($cache->has()) {return Response::json($cache->get()); // @codeCoverageIgnore}+ $netWorth = [];$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);$currency = app('amount')->getDefaultCurrency();$balances = app('steam')->balancesByAccounts($accounts, $date);- $sum = '0';- foreach ($balances as $entry) {- $sum = bcadd($sum, $entry);++ /** @var Account $account */+ 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 = [- 'net_worth' => app('amount')->formatAnything($currency, $sum, false),+ 'net_worths' => array_values($return),];$cache->store($return);diff --git a/public/js/ff/index.js b/public/js/ff/index.jsindex ca69033a7d..38765985d1 100644--- a/public/js/ff/index.js+++ b/public/js/ff/index.js@@ -67,7 +67,7 @@ function getPiggyBanks() {function getNetWorthBox() {// box-net-worth$.getJSON('json/box/net-worth').done(function (data) {- $('#box-net-worth').html(data.net_worth);+ $('#box-net-worth').html(data.net_worths.join(', '));});}diff --git a/resources/views/partials/boxes.twig b/resources/views/partials/boxes.twigindex 03abe8e3d7..d381b2fff7 100644--- a/resources/views/partials/boxes.twig+++ b/resources/views/partials/boxes.twig@@ -70,9 +70,7 @@<div class="progress"><div class="progress-bar" style="width: 0%"></div></div>- <span class="progress-description">- <span title="{{ 'net_worth'|_ }}" id="box-clear"></span>- </span>+ <span class="progress-description"></span></div></div></div>