Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Code for #1236
James Colea436c55c50ad
diff --git a/app/Http/Controllers/Json/BoxController.php b/app/Http/Controllers/Json/BoxController.phpindex 9a7511cea3..f380655390 100644--- a/app/Http/Controllers/Json/BoxController.php+++ b/app/Http/Controllers/Json/BoxController.php@@ -22,11 +22,13 @@ declare(strict_types=1);namespace FireflyIII\Http\Controllers\Json;+use Amount;use Carbon\Carbon;use FireflyIII\Helpers\Collector\JournalCollectorInterface;use FireflyIII\Http\Controllers\Controller;use FireflyIII\Models\Account;use FireflyIII\Models\AccountType;+use FireflyIII\Models\Transaction;use FireflyIII\Models\TransactionType;use FireflyIII\Repositories\Account\AccountRepositoryInterface;use FireflyIII\Repositories\Bill\BillRepositoryInterface;@@ -88,13 +90,15 @@ class BoxController extends Controller}/**+ * @param CurrencyRepositoryInterface $repository+ ** @return \Illuminate\Http\JsonResponse*/- public function balance()+ public function balance(CurrencyRepositoryInterface $repository){+ // Cache result, return cache if present.$start = session('start', Carbon::now()->startOfMonth());$end = session('end', Carbon::now()->endOfMonth());-$cache = new CacheProperties;$cache->addProperty($start);$cache->addProperty($end);@@ -102,29 +106,56 @@ class BoxController extends Controllerif ($cache->has()) {return Response::json($cache->get()); // @codeCoverageIgnore}+ // prep some arrays:+ $incomes = [];+ $expenses = [];+ $sums = [];- // try a collector for income:+ // collect income of user:/** @var JournalCollectorInterface $collector */$collector = app(JournalCollectorInterface::class);$collector->setAllAssetAccounts()->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])->withOpposingAccount();- $income = strval($collector->getJournals()->sum('transaction_amount'));- $currency = app('amount')->getDefaultCurrency();+ $set = $collector->getJournals();+ /** @var Transaction $transaction */+ foreach ($set as $transaction) {+ $currencyId = intval($transaction->transaction_currency_id);+ $incomes[$currencyId] = $incomes[$currencyId] ?? '0';+ $incomes[$currencyId] = bcadd($incomes[$currencyId], $transaction->transaction_amount);+ $sums[$currencyId] = $sums[$currencyId] ?? '0';+ $sums[$currencyId] = bcadd($sums[$currencyId], $transaction->transaction_amount);+ }- // expense:- // try a collector for expenses:+ // collect expenses/** @var JournalCollectorInterface $collector */$collector = app(JournalCollectorInterface::class);$collector->setAllAssetAccounts()->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withOpposingAccount();- $expense = strval($collector->getJournals()->sum('transaction_amount'));+ $set = $collector->getJournals();+ /** @var Transaction $transaction */+ foreach ($set as $transaction) {+ $currencyId = intval($transaction->transaction_currency_id);+ $expenses[$currencyId] = $expenses[$currencyId] ?? '0';+ $expenses[$currencyId] = bcadd($expenses[$currencyId], $transaction->transaction_amount);+ $sums[$currencyId] = $sums[$currencyId] ?? '0';+ $sums[$currencyId] = bcadd($sums[$currencyId], $transaction->transaction_amount);+ }++ // format amounts:+ foreach ($sums as $currencyId => $amount) {+ $currency = $repository->findNull($currencyId);+ $sums[$currencyId] = Amount::formatAnything($currency, $sums[$currencyId], false);+ $incomes[$currencyId] = Amount::formatAnything($currency, $incomes[$currencyId] ?? '0', false);+ $expenses[$currencyId] = Amount::formatAnything($currency, $expenses[$currencyId] ?? '0', false);+ }$response = [- 'income' => app('amount')->formatAnything($currency, $income, false),- 'expense' => app('amount')->formatAnything($currency, $expense, false),- 'combined' => app('amount')->formatAnything($currency, bcadd($income, $expense), false),+ 'incomes' => $incomes,+ 'expenses' => $expenses,+ 'sums' => $sums,+ 'size' => count($sums),];$cache->store($response);diff --git a/public/js/ff/index.js b/public/js/ff/index.jsindex 38765985d1..a5698cea55 100644--- a/public/js/ff/index.js+++ b/public/js/ff/index.js@@ -99,12 +99,34 @@ function getBillsBox() {**/function getBalanceBox() {- // box-balance-total- // box-balance-out- // box-balance-in+ // box-balance-sums+ // box-balance-list$.getJSON('json/box/balance').done(function (data) {- $('#box-balance-total').html(data.combined);- $('#box-balance-in').html(data.income);- $('#box-balance-out').html(data.expense);+ if (data.size === 1) {+ // show balance in "sums", show single entry in list.+ for (x in data.sums) {+ $('#box-balance-sums').html(data.sums[x]);+ $('#box-balance-list').html(data.incomes[x] + ' / ' + data.expenses[x]);+ }+ return;+ }+ // do not use "sums", only use list.+ $('#box-balance-progress').remove();+ var expense, string, sum, income, current;+ var count = 0;+ for (x in data.sums) {+ if (count > 1) {+ return;+ }+ current = $('#box-balance-list').html();+ sum = data.sums[x];+ expense = data.expenses[x];+ income = data.incomes[x];+ string = income + ' / ' + expense + ': ' + sum;++ $('#box-balance-list').html(current + '<span title="' + string + '">' + string + '</span>' + '<br>');+ count++;+ }+ return;});}\ No newline at end of file