From e3433aa95b5ce5e326a479b9e44e709613758e9f Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 26 Jan 2019 12:10:35 +0100 Subject: [PATCH] Add pie chart for available budget API --- .../Chart/AvailableBudgetController.php | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 app/Api/V1/Controllers/Chart/AvailableBudgetController.php diff --git a/app/Api/V1/Controllers/Chart/AvailableBudgetController.php b/app/Api/V1/Controllers/Chart/AvailableBudgetController.php new file mode 100644 index 0000000000..3391f17eb2 --- /dev/null +++ b/app/Api/V1/Controllers/Chart/AvailableBudgetController.php @@ -0,0 +1,97 @@ +middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + $this->repository = app(BudgetRepositoryInterface::class); + $this->repository->setUser($user); + + return $next($request); + } + ); + } + + /** + * @param Request $request + * + * @param AvailableBudget $availableBudget + * + * @return JsonResponse + */ + public function overview(Request $request, AvailableBudget $availableBudget): JsonResponse + { + $currency = $availableBudget->transactionCurrency; + $budgets = $this->repository->getActiveBudgets(); + $budgetInformation = $this->repository->spentInPeriodMc($budgets, new Collection, $availableBudget->start_date, $availableBudget->end_date); + $spent = 0.0; + + // get for current currency + foreach ($budgetInformation as $spentInfo) { + if ($spentInfo['currency_id'] === $availableBudget->transaction_currency_id) { + $spent = $spentInfo['amount']; + } + } + $left = bcadd($availableBudget->amount, (string)$spent); + // left less than zero? Set to zero. + if (bccomp($left, '0') === -1) { + $left = '0'; + } + + $chartData = [ + [ + 'label' => trans('firefly.spent'), + 'currency_id' => $currency->id, + 'currency_code' => $currency->code, + 'currency_symbol' => $currency->symbol, + 'currency_decimal_places' => $currency->decimal_places, + 'type' => 'pie', + 'yAxisID' => 0, // 0, 1, 2 + 'fill' => null, // true, false, null + 'backgroundColor' => null, // null or hex + 'entries' => [$spent * -1], + ], + [ + 'label' => trans('firefly.left'), + 'currency_id' => $currency->id, + 'currency_code' => $currency->code, + 'currency_symbol' => $currency->symbol, + 'currency_decimal_places' => $currency->decimal_places, + 'type' => 'line', // line, area or bar + 'yAxisID' => 0, // 0, 1, 2 + 'fill' => null, // true, false, null + 'backgroundColor' => null, // null or hex + 'entries' => [round($left, $currency->decimal_places)], + ], + ]; + + return response()->json($chartData); + } + +} \ No newline at end of file