. */ declare(strict_types=1); namespace FireflyIII\Api\V1\Controllers\Chart; use Carbon\Carbon; use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Requests\Data\DateRequest; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Support\Http\Api\ApiSupport; use FireflyIII\User; use Illuminate\Http\JsonResponse; use JsonException; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; /** * Class AccountController */ class AccountController extends Controller { use ApiSupport; private CurrencyRepositoryInterface $currencyRepository; private AccountRepositoryInterface $repository; /** * AccountController constructor. * * @codeCoverageIgnore */ public function __construct() { parent::__construct(); $this->middleware( function ($request, $next) { /** @var User $user */ $user = auth()->user(); $this->repository = app(AccountRepositoryInterface::class); $this->repository->setUser($user); $this->currencyRepository = app(CurrencyRepositoryInterface::class); $this->currencyRepository->setUser($user); return $next($request); } ); } /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/charts/getChartAccountOverview * * @param DateRequest $request * * @return JsonResponse * @throws FireflyException * @throws JsonException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function overview(DateRequest $request): JsonResponse { // parameters for chart: $dates = $request->getAll(); /** @var Carbon $start */ $start = $dates['start']; /** @var Carbon $end */ $end = $dates['end']; // user's preferences $defaultSet = $this->repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray(); $frontPage = app('preferences')->get('frontPageAccounts', $defaultSet); $default = app('amount')->getDefaultCurrency(); if (!(is_array($frontPage->data) && count($frontPage->data) > 0)) { $frontPage->data = $defaultSet; $frontPage->save(); } // get accounts: $accounts = $this->repository->getAccountsById($frontPage->data); $chartData = []; /** @var Account $account */ foreach ($accounts as $account) { $currency = $this->repository->getAccountCurrency($account); if (null === $currency) { $currency = $default; } $currentSet = [ 'label' => $account->name, 'currency_id' => (string)$currency->id, 'currency_code' => $currency->code, 'currency_symbol' => $currency->symbol, 'currency_decimal_places' => $currency->decimal_places, 'start_date' => $start->toAtomString(), 'end_date' => $end->toAtomString(), 'type' => 'line', // line, area or bar 'yAxisID' => 0, // 0, 1, 2 'entries' => [], ]; $currentStart = clone $start; $range = app('steam')->balanceInRange($account, $start, clone $end); // 2022-10-11 this method no longer converts to float. $previous = array_values($range)[0]; while ($currentStart <= $end) { $format = $currentStart->format('Y-m-d'); $label = $currentStart->toAtomString(); $balance = array_key_exists($format, $range) ? $range[$format] : $previous; $previous = $balance; $currentStart->addDay(); $currentSet['entries'][$label] = $balance; } $chartData[] = $currentSet; } return response()->json($chartData); } }