Files
firefly-iii/app/Api/V1/Controllers/Chart/AccountController.php

146 lines
5.1 KiB
PHP
Raw Normal View History

2019-01-18 05:38:23 +01:00
<?php
2019-02-09 10:36:59 +01:00
/**
* AccountController.php
* Copyright (c) 2019 james@firefly-iii.org
2019-02-09 10:36:59 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-02-09 10:36:59 +01:00
*
* 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.
2019-02-09 10:36:59 +01:00
*
* This program is distributed in the hope that it will be useful,
2019-02-09 10:36:59 +01:00
* 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.
2019-02-09 10:36:59 +01:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-02-09 10:36:59 +01:00
*/
2019-01-18 05:38:23 +01:00
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Api\V1\Controllers\Controller;
2021-03-12 18:31:19 +01:00
use FireflyIII\Api\V1\Requests\Data\DateRequest;
2021-09-18 05:26:31 +02:00
use FireflyIII\Exceptions\FireflyException;
2019-01-18 05:38:23 +01:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2019-01-18 05:38:23 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2019-06-04 20:42:11 +02:00
use FireflyIII\Support\Http\Api\ApiSupport;
2019-01-18 05:38:23 +01:00
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
2022-03-30 20:09:19 +02:00
use JsonException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2019-01-18 05:38:23 +01:00
/**
* Class AccountController
*/
class AccountController extends Controller
{
2019-06-04 20:42:11 +02:00
use ApiSupport;
2020-07-31 09:42:00 +02:00
2020-07-01 15:33:06 +02:00
private CurrencyRepositoryInterface $currencyRepository;
2021-03-12 18:31:19 +01:00
private AccountRepositoryInterface $repository;
2019-01-18 05:38:23 +01:00
/**
* AccountController constructor.
2019-09-04 17:39:39 +02:00
*
2019-06-09 15:28:54 +02:00
* @codeCoverageIgnore
2019-01-18 05:38:23 +01:00
*/
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);
2019-01-18 05:38:23 +01:00
return $next($request);
}
);
}
2019-02-13 17:38:41 +01:00
/**
2021-09-18 05:26:31 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/charts/getChartAccountOverview
2021-09-18 05:26:31 +02:00
*
2022-12-29 19:41:57 +01:00
* @param DateRequest $request
2019-02-13 17:38:41 +01:00
*
* @return JsonResponse
2021-09-18 05:26:31 +02:00
* @throws FireflyException
2022-03-30 20:09:19 +02:00
* @throws JsonException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2019-02-13 17:38:41 +01:00
*/
2019-06-09 15:28:54 +02:00
public function overview(DateRequest $request): JsonResponse
2019-02-13 17:38:41 +01:00
{
// parameters for chart:
2019-06-09 15:28:54 +02:00
$dates = $request->getAll();
/** @var Carbon $start */
$start = $dates['start'];
/** @var Carbon $end */
$end = $dates['end'];
2019-02-13 17:38:41 +01:00
// user's preferences
$defaultSet = $this->repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray();
2019-06-04 20:42:11 +02:00
$frontPage = app('preferences')->get('frontPageAccounts', $defaultSet);
$default = app('amount')->getDefaultCurrency();
2021-04-07 07:28:43 +02:00
2022-10-30 14:34:36 +01:00
if (!(is_array($frontPage->data) && count($frontPage->data) > 0)) {
2019-02-13 17:38:41 +01:00
$frontPage->data = $defaultSet;
$frontPage->save();
}
2021-04-07 07:28:43 +02:00
2019-02-13 17:38:41 +01:00
// 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;
2019-02-13 17:38:41 +01:00
}
$currentSet = [
2019-02-13 17:38:41 +01:00
'label' => $account->name,
2022-12-29 19:41:57 +01:00
'currency_id' => (string)$currency->id,
2019-02-13 17:38:41 +01:00
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
2021-04-04 07:25:42 +02:00
'start_date' => $start->toAtomString(),
'end_date' => $end->toAtomString(),
2019-02-13 17:38:41 +01:00
'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-16 14:44:11 +02:00
// 2022-10-11 this method no longer converts to float.
2022-12-29 19:41:57 +01:00
$previous = array_values($range)[0];
2019-02-13 17:38:41 +01:00
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
2021-04-04 07:25:42 +02:00
$label = $currentStart->toAtomString();
2022-10-16 14:44:11 +02:00
$balance = array_key_exists($format, $range) ? $range[$format] : $previous;
2019-02-13 17:38:41 +01:00
$previous = $balance;
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
}
$chartData[] = $currentSet;
}
return response()->json($chartData);
}
2019-02-09 10:36:59 +01:00
}