diff --git a/app/Http/Controllers/Json/BudgetController.php b/app/Http/Controllers/Json/BudgetController.php new file mode 100644 index 0000000000..6e70d19f17 --- /dev/null +++ b/app/Http/Controllers/Json/BudgetController.php @@ -0,0 +1,99 @@ +. + */ + +namespace FireflyIII\Http\Controllers\Json; + + +use Carbon\Carbon; +use FireflyIII\Http\Controllers\Controller; +use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface; +use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; +use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; +use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use FireflyIII\Support\Http\Controllers\DateCalculation; +use Illuminate\Http\JsonResponse; + +/** + * Class BudgetController + */ +class BudgetController extends Controller +{ + use DateCalculation; + /** @var AvailableBudgetRepositoryInterface */ + private $abRepository; + /** @var BudgetLimitRepositoryInterface */ + private $blRepository; + /** @var CurrencyRepositoryInterface */ + private $currencyRepository; + /** @var OperationsRepositoryInterface */ + private $opsRepository; + /** @var BudgetRepositoryInterface The budget repository */ + private $repository; + + /** + * IndexController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + + $this->middleware( + function ($request, $next) { + app('view')->share('title', (string) trans('firefly.budgets')); + app('view')->share('mainTitleIcon', 'fa-tasks'); + $this->repository = app(BudgetRepositoryInterface::class); + $this->opsRepository = app(OperationsRepositoryInterface::class); + $this->abRepository = app(AvailableBudgetRepositoryInterface::class); + $this->currencyRepository = app(CurrencyRepositoryInterface::class); + $this->blRepository = app(BudgetLimitRepositoryInterface::class); + $this->repository->cleanupBudgets(); + + return $next($request); + } + ); + } + + /** + * @return JsonResponse + */ + public function getBudgetInformation(TransactionCurrency $currency, Carbon $start, Carbon $end): JsonResponse + { + $budgeted = $this->blRepository->budgeted($start, $end, $currency,); + + return response()->json( + [ + 'budgeted' => $budgeted, + 'budgeted_formatted' => app('amount')->formatAnything($currency, $budgeted, true), + 'currency_id' => $currency->id, + 'currency_code' => $currency->code, + 'currency_symbol' => $currency->symbol, + 'currency_name' => $currency->name, + 'currency_decimal_places' => $currency->decimal_places, + ] + ); + } + +} \ No newline at end of file diff --git a/public/v1/js/ff/budgets/index.js b/public/v1/js/ff/budgets/index.js index 7a57008a43..d20c631870 100644 --- a/public/v1/js/ff/budgets/index.js +++ b/public/v1/js/ff/budgets/index.js @@ -23,16 +23,10 @@ */ $(function () { "use strict"; - - //$('.updateIncome').on('click', updateIncome); - /* On start, fill the "spent"-bar using the content from the page. */ - //drawSpentBar(); drawSpentBars(); - //drawBudgetedBar(); - drawBudgetedBars(); $('.update_ab').on('click', updateAvailableBudget); @@ -45,8 +39,6 @@ $(function () { /* When the input changes, update the percentages for the budgeted bar: */ - //$('input[type="number"]').on('change', updateBudgetedAmounts); - $('.selectPeriod').change(function (e) { var selected = $(e.currentTarget); if (selected.find(":selected").val() !== "x") { @@ -107,7 +99,8 @@ function updateBudgetedAmount(e) { if (data.left_per_day > 0) { $('.left_span[data-limit="0"][data-id="' + budgetId + '"]').html(data.left_formatted + '(' + data.left_per_day_formatted + ')'); } - //$('.left_span[data-limit="0"][data-id="' + budgetId + '"]').text('XXXXX'); + // update budgeted amount + updateTotalBudgetedAmount(data.transaction_currency_id); }).fail(function () { alert('I failed :('); @@ -118,10 +111,12 @@ function updateBudgetedAmount(e) { amount: input.val(), }).done(function (data) { input.prop('disabled', false); - $('.left_span[data-limit="'+budgetLimitId+'"]').html(data.left_formatted); + $('.left_span[data-limit="' + budgetLimitId + '"]').html(data.left_formatted); if (data.left_per_day > 0) { - $('.left_span[data-limit="'+budgetLimitId+'"]').html(data.left_formatted + '(' + data.left_per_day_formatted + ')'); + $('.left_span[data-limit="' + budgetLimitId + '"]').html(data.left_formatted + '(' + data.left_per_day_formatted + ')'); } + updateTotalBudgetedAmount(data.transaction_currency_id); + // update budgeted amount }).fail(function () { alert('I failed :('); @@ -129,6 +124,22 @@ function updateBudgetedAmount(e) { } } +function updateTotalBudgetedAmount(currencyId) { + // fade info away: + $('span.budgeted_amount[data-currency="' + currencyId + '"]') + .fadeTo(100, 0.1, function () { + //$(this).fadeTo(500, 1.0); + }); + + // get new amount: + $.get(totalBudgetedUri.replace('REPLACEME',currencyId)).done(function (data) { + $('span.budgeted_amount[data-currency="' + currencyId + '"]') + .html(data.budgeted_formatted) + // fade back: + .fadeTo(300, 1.0); + }); +} + var fixHelper = function (e, tr) { "use strict"; var $originals = tr.children(); diff --git a/resources/views/v1/budgets/index.twig b/resources/views/v1/budgets/index.twig index f0a23462c1..3f82626b07 100644 --- a/resources/views/v1/budgets/index.twig +++ b/resources/views/v1/budgets/index.twig @@ -65,7 +65,7 @@ {# info about the amount budgeted #}
{{ 'budgeted'|_ }} ({{ 'see_below'|_ }}): - + {{ formatAmountBySymbol(budgeted, defaultCurrency.symbol, defaultCurrency.decimal_places) }} @@ -75,7 +75,7 @@ {{ trans('firefly.available_between', {start: start.formatLocalized(monthAndDayFormat), end: end.formatLocalized(monthAndDayFormat) }) }} : - {{ formatAmountBySymbol(0, defaultCurrency.symbol, defaultCurrency.decimal_places, true) }} @@ -124,7 +124,7 @@ {# info about the amount budgeted #}
{{ 'budgeted'|_ }}: - + {{ formatAmountBySymbol(budget.budgeted, budget.transaction_currency.symbol, budget.transaction_currency.decimal_places, false) }} @@ -429,7 +429,7 @@ var createBudgetLimitUri = "{{ route('budget-limits.create', ['REPLACEME', start.format('Y-m-d'), end.format('Y-m-d')]) }}"; var storeBudgetLimitUri = "{{ route('budget-limits.store') }}"; var updateBudgetLimitUri = "{{ route('budget-limits.update', ['REPLACEME']) }}"; - + var totalBudgetedUri = "{{ route('json.budget.total-budgeted', ['REPLACEME', start.format('Y-m-d'), end.format('Y-m-d')]) }}"; {#var budgetAmountUri = "{{ route('budgets.amount','REPLACE') }}";#} {#var updateIncomeUri = "{{ route('budgets.income',[start.format('Y-m-d'),end.format('Y-m-d')]) }}";#} var periodStart = "{{ start.format('Y-m-d') }}"; diff --git a/routes/web.php b/routes/web.php index afbec8ddb7..0efb443300 100644 --- a/routes/web.php +++ b/routes/web.php @@ -626,9 +626,12 @@ Route::group( Route::get('transaction-journals/all', ['uses' => 'Json\AutoCompleteController@allJournals', 'as' => 'autocomplete.all-journals']); Route::get('transaction-journals/with-id', ['uses' => 'Json\AutoCompleteController@allJournalsWithID', 'as' => 'autocomplete.all-journals-with-id']); Route::get('currency-names', ['uses' => 'Json\AutoCompleteController@currencyNames', 'as' => 'autocomplete.currency-names']); - Route::get('transaction-types', ['uses' => 'Json\AutoCompleteController@transactionTypes', 'as' => 'transaction-types']); + // budgets: + Route::get('budget/total-budgeted/{currency}/{start_date}/{end_date}', ['uses' => 'Json\BudgetController@getBudgetInformation', 'as' => 'budget.total-budgeted']); + + // boxes Route::get('box/balance', ['uses' => 'Json\BoxController@balance', 'as' => 'box.balance']); Route::get('box/bills', ['uses' => 'Json\BoxController@bills', 'as' => 'box.bills']);