diff --git a/app/Http/Controllers/Json/FrontpageController.php b/app/Http/Controllers/Json/FrontpageController.php new file mode 100644 index 0000000000..f82d6aacb7 --- /dev/null +++ b/app/Http/Controllers/Json/FrontpageController.php @@ -0,0 +1,74 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Http\Controllers\Json; + +use FireflyIII\Http\Controllers\Controller; +use FireflyIII\Models\PiggyBank; +use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; +use Response; + +/** + * Class FrontpageController + * + * @package FireflyIII\Http\Controllers\Json + */ +class FrontpageController extends Controller +{ + /** + * @return \Illuminate\Http\JsonResponse + */ + public function piggyBanks(PiggyBankRepositoryInterface $repository) + { + $set = $repository->getPiggyBanks(); + $info = []; + /** @var PiggyBank $piggyBank */ + foreach ($set as $piggyBank) { + $rep = $piggyBank->currentRelevantRep(); + $amount = strval($rep->currentamount); + if (!is_null($rep->id) && bccomp($amount, '0') === 1) { + + // percentage! + $pct = round(($amount / $piggyBank->targetamount) * 100); + + $entry = [ + 'id' => $piggyBank->id, + 'name' => $piggyBank->name, + 'amount' => $amount, + 'target' => $piggyBank->targetamount, + 'percentage' => $pct, + ]; + + $info[] = $entry; + } + + } + $html = ''; + if (count($info) > 0) { + $html = view('json.piggy-banks', compact('info'))->render(); + } + + return Response::json(['html' => $html]); + } + +} \ No newline at end of file diff --git a/public/js/ff/index.js b/public/js/ff/index.js index 2c104f73e7..fcfb57f14f 100644 --- a/public/js/ff/index.js +++ b/public/js/ff/index.js @@ -43,13 +43,25 @@ function drawChart() { getBillsBox(); getAvailableBox(); getNetWorthBox(); + getPiggyBanks(); //getBoxAmounts(); } +/** + * + */ +function getPiggyBanks() { + $.getJSON(piggyInfoUri).done(function (data) { + if (data.html.length > 0) { + $('#piggy_bank_overview').html(data.html); + } + }); +} + function getNetWorthBox() { // 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); }); } @@ -60,7 +72,7 @@ function getNetWorthBox() { function getAvailableBox() { // box-left-to-spend // box-left-per-day - $.getJSON('json/box/available').done(function(data) { + $.getJSON('json/box/available').done(function (data) { $('#box-left-to-spend').html(data.left); $('#box-left-per-day').html(data.perDay); }); @@ -72,7 +84,7 @@ function getAvailableBox() { function getBillsBox() { // box-bills-unpaid // box-bills-paid - $.getJSON('json/box/bills').done(function(data) { + $.getJSON('json/box/bills').done(function (data) { $('#box-bills-paid').html(data.paid); $('#box-bills-unpaid').html(data.unpaid); }); @@ -85,7 +97,7 @@ function getBalanceBox() { // box-balance-total // box-balance-out // box-balance-in - $.getJSON('json/box/balance').done(function(data) { + $.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); @@ -93,7 +105,6 @@ function getBalanceBox() { } - function getBoxAmounts() { "use strict"; var boxes = ['in', 'out', 'bills-unpaid', 'bills-paid']; diff --git a/resources/views/index.twig b/resources/views/index.twig index e3462baea5..bc62cd0881 100644 --- a/resources/views/index.twig +++ b/resources/views/index.twig @@ -21,7 +21,7 @@ {# BUDGETS #}