From ea808b441b56a8cf17c624c09d3486c722bf394a Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 23 Jul 2022 20:13:24 +0200 Subject: [PATCH] Add first budget view --- .../Model/Budget/ListController.php | 45 ++++++ app/Transformers/V2/BudgetTransformer.php | 133 ++++++++++++++++++ frontend/src/api/v2/budgets/list.js | 28 ++++ .../src/components/dashboard/BudgetBox.vue | 110 +++++++++++++++ frontend/src/pages/dashboard/Dashboard.vue | 28 ++-- routes/api.php | 1 + 6 files changed, 334 insertions(+), 11 deletions(-) create mode 100644 app/Api/V2/Controllers/Model/Budget/ListController.php create mode 100644 app/Transformers/V2/BudgetTransformer.php create mode 100644 frontend/src/api/v2/budgets/list.js create mode 100644 frontend/src/components/dashboard/BudgetBox.vue diff --git a/app/Api/V2/Controllers/Model/Budget/ListController.php b/app/Api/V2/Controllers/Model/Budget/ListController.php new file mode 100644 index 0000000000..8218695ebc --- /dev/null +++ b/app/Api/V2/Controllers/Model/Budget/ListController.php @@ -0,0 +1,45 @@ +. + */ + +namespace FireflyIII\Api\V2\Controllers\Model\Budget; + +use FireflyIII\Api\V2\Controllers\Controller; +use FireflyIII\Transformers\V2\BudgetTransformer; +use Illuminate\Http\JsonResponse; +use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Collection; + +class ListController extends Controller +{ + /** + * @return JsonResponse + */ + public function index(): JsonResponse + { + $collection = new Collection; + $paginator = new LengthAwarePaginator($collection, 0, 50, 1); + $transformer = new BudgetTransformer(); + return response() + ->api($this->jsonApiList('budgets', $paginator, $transformer)) + ->header('Content-Type', self::CONTENT_TYPE); + } + +} diff --git a/app/Transformers/V2/BudgetTransformer.php b/app/Transformers/V2/BudgetTransformer.php new file mode 100644 index 0000000000..9f38d43a30 --- /dev/null +++ b/app/Transformers/V2/BudgetTransformer.php @@ -0,0 +1,133 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Transformers\V2; + +use FireflyIII\Models\Budget; +use Illuminate\Support\Collection; + +/** + * Class BudgetTransformer + */ +class BudgetTransformer extends AbstractTransformer +{ + //private OperationsRepositoryInterface $opsRepository; + //private BudgetRepositoryInterface $repository; + + /** + * BudgetTransformer constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + //$this->opsRepository = app(OperationsRepositoryInterface::class); + //$this->repository = app(BudgetRepositoryInterface::class); + //$this->parameters = new ParameterBag(); + } + + /** + * Transform a budget. + * + * @param Budget $budget + * + * @return array + */ + public function transform(Budget $budget): array + { + //$this->opsRepository->setUser($budget->user); + $start = $this->parameters->get('start'); + $end = $this->parameters->get('end'); + //$autoBudget = $this->repository->getAutoBudget($budget); +// $spent = []; +// if (null !== $start && null !== $end) { +// $spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget]))); +// } + +// $abCurrencyId = null; +// $abCurrencyCode = null; +// $abType = null; +// $abAmount = null; +// $abPeriod = null; +// $notes = $this->repository->getNoteText($budget); +// +// $types = [ +// AutoBudget::AUTO_BUDGET_RESET => 'reset', +// AutoBudget::AUTO_BUDGET_ROLLOVER => 'rollover', +// ]; +// +// if (null !== $autoBudget) { +// $abCurrencyId = (string) $autoBudget->transactionCurrency->id; +// $abCurrencyCode = $autoBudget->transactionCurrency->code; +// $abType = $types[$autoBudget->auto_budget_type]; +// $abAmount = number_format((float) $autoBudget->amount, $autoBudget->transactionCurrency->decimal_places, '.', ''); +// $abPeriod = $autoBudget->period; +// } + + return [ + 'id' => (string) $budget->id, + 'created_at' => $budget->created_at->toAtomString(), + 'updated_at' => $budget->updated_at->toAtomString(), + 'active' => $budget->active, + 'name' => $budget->name, + 'order' => $budget->order, + // 'notes' => $notes, + // 'auto_budget_type' => $abType, + // 'auto_budget_period' => $abPeriod, + // 'auto_budget_currency_id' => $abCurrencyId, + // 'auto_budget_currency_code' => $abCurrencyCode, + // 'auto_budget_amount' => $abAmount, + // 'spent' => $spent, + 'links' => [ + [ + 'rel' => 'self', + 'uri' => sprintf('/budgets/%d', $budget->id), + ], + ], + ]; + } + + /** + * @param array $array + * + * @return array + */ + private function beautify(array $array): array + { + $return = []; + foreach ($array as $data) { + $data['sum'] = number_format((float) $data['sum'], (int) $data['currency_decimal_places'], '.', ''); + $return[] = $data; + } + + return $return; + } + + /** + * @inheritDoc + */ + public function collectMetaData(Collection $objects): void + { + // TODO: Implement collectMetaData() method. + } +} diff --git a/frontend/src/api/v2/budgets/list.js b/frontend/src/api/v2/budgets/list.js new file mode 100644 index 0000000000..122c817e98 --- /dev/null +++ b/frontend/src/api/v2/budgets/list.js @@ -0,0 +1,28 @@ +/* + * list.js + * Copyright (c) 2022 james@firefly-iii.org + * + * This file is part of Firefly III (https://github.com/firefly-iii). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import {api} from "boot/axios"; + +export default class List { + list(page) { + let url = '/api/v2/budgets'; + return api.get(url, {params: {page: page}}); + } +} diff --git a/frontend/src/components/dashboard/BudgetBox.vue b/frontend/src/components/dashboard/BudgetBox.vue new file mode 100644 index 0000000000..8c743a4438 --- /dev/null +++ b/frontend/src/components/dashboard/BudgetBox.vue @@ -0,0 +1,110 @@ + + + + + + + diff --git a/frontend/src/pages/dashboard/Dashboard.vue b/frontend/src/pages/dashboard/Dashboard.vue index 943d4db1b4..f04c67a104 100644 --- a/frontend/src/pages/dashboard/Dashboard.vue +++ b/frontend/src/pages/dashboard/Dashboard.vue @@ -24,28 +24,28 @@
- +
- +
- +
- +
- +
- Budget box +
Category box @@ -77,11 +77,16 @@ icon="fas fa-chevron-up" direction="up" > - - - - - + + + + +
@@ -98,6 +103,7 @@ export default { NetWorthInsightBox: defineAsyncComponent(() => import('../../components/dashboard/NetWorthInsightBox.vue')), BillInsightBox: defineAsyncComponent(() => import('../../components/dashboard/BillInsightBox.vue')), SpendInsightBox: defineAsyncComponent(() => import('../../components/dashboard/SpendInsightBox.vue')), + BudgetBox: defineAsyncComponent(() => import('../../components/dashboard/BudgetBox.vue')), } } diff --git a/routes/api.php b/routes/api.php index e5a17250d4..781c229c52 100644 --- a/routes/api.php +++ b/routes/api.php @@ -96,6 +96,7 @@ Route::group( ['namespace' => 'FireflyIII\Api\V2\Controllers\Model\Budget', 'prefix' => 'v2/budgets', 'as' => 'api.v2.budgets',], static function () { + Route::get('', ['uses' => 'ListController@index', 'as' => 'index']); Route::get('sum/budgeted', ['uses' => 'SumController@budgeted', 'as' => 'sum.budgeted']); Route::get('sum/spent', ['uses' => 'SumController@spent', 'as' => 'sum.spent']); }