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 #}
-

{{ 'budgetsAndSpending'|_ }}

+

{{ 'budgetsAndSpending'|_ }}

@@ -30,7 +30,7 @@ {# CATEGORIES #}
-

{{ 'categories'|_ }}

+

{{ 'categories'|_ }}

@@ -39,22 +39,6 @@
- {% if billCount > 0 %} - {# BILLS #} -
-
-

{{ 'bills'|_ }}

- -
-
-
- -
- -
-
- {% endif %} - {# TRANSACTIONS #}
{% for data in transactions %} @@ -100,6 +84,27 @@
{% endfor %}
+ + {% if billCount > 0 %} + {# BILLS #} +
+ +
+
+ +
+ +
+
+ {% endif %} + + {# box for piggy bank data (JSON) #} +
+ +
@@ -107,7 +112,7 @@ {# EXPENSE ACCOUNTS #}
-

{{ 'expense_accounts'|_ }}

+

{{ 'expense_accounts'|_ }}

@@ -118,7 +123,7 @@ {% if showDeps %}
-

{{ 'revenue_accounts'|_ }}

+

{{ 'revenue_accounts'|_ }}

@@ -136,6 +141,7 @@ var accountFrontpageUri = '{{ route('chart.account.frontpage') }}'; var accountRevenueUri = '{{ route('chart.account.revenue') }}'; var accountExpenseUri = '{{ route('chart.account.expense') }}'; + var piggyInfoUri = '{{ route('json.fp.piggy-banks') }}'; diff --git a/resources/views/json/piggy-banks.twig b/resources/views/json/piggy-banks.twig new file mode 100644 index 0000000000..2e0e9214c0 --- /dev/null +++ b/resources/views/json/piggy-banks.twig @@ -0,0 +1,16 @@ +
+ +
+ {% for entry in info %} + {{ entry.name }}
+
+
+ {{ entry.amount|formatAmountPlain }} +
+
+ {% endfor %} +
+
\ No newline at end of file