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

207 lines
7.8 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;
2025-03-25 17:27:59 +01:00
use FireflyIII\Api\V1\Requests\Chart\ChartRequest;
2025-05-04 17:41:26 +02:00
use FireflyIII\Api\V1\Requests\Data\DateRequest;
2024-12-29 05:58:12 +01:00
use FireflyIII\Enums\AccountTypeEnum;
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\Preference;
2019-01-18 05:38:23 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2025-03-25 17:27:59 +01:00
use FireflyIII\Support\Chart\ChartData;
use FireflyIII\Support\Facades\Steam;
2019-06-04 20:42:11 +02:00
use FireflyIII\Support\Http\Api\ApiSupport;
2025-03-25 17:27:59 +01:00
use FireflyIII\Support\Http\Api\CollectsAccountsFromFilter;
2019-01-18 05:38:23 +01:00
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
/**
* Class AccountController
*/
class AccountController extends Controller
{
2019-06-04 20:42:11 +02:00
use ApiSupport;
2025-03-25 17:27:59 +01:00
use CollectsAccountsFromFilter;
2020-07-31 09:42:00 +02:00
2025-03-25 17:27:59 +01:00
private ChartData $chartData;
2025-05-04 17:41:26 +02:00
private AccountRepositoryInterface $repository;
2019-01-18 05:38:23 +01:00
/**
* AccountController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $user */
$user = auth()->user();
2025-03-25 17:27:59 +01:00
$this->chartData = new ChartData();
2019-01-18 05:38:23 +01:00
$this->repository = app(AccountRepositoryInterface::class);
$this->repository->setUser($user);
return $next($request);
}
);
}
2025-03-25 17:27:59 +01:00
/**
* TODO fix documentation
*
* @throws FireflyException
*/
public function dashboard(ChartRequest $request): JsonResponse
{
$queryParameters = $request->getParameters();
$accounts = $this->getAccountList($queryParameters);
// move date to end of day
$queryParameters['start']->startOfDay();
$queryParameters['end']->endOfDay();
// loop each account, and collect info:
/** @var Account $account */
foreach ($accounts as $account) {
$this->renderAccountData($queryParameters, $account);
}
return response()->json($this->chartData->render());
}
2025-05-04 17:41:26 +02:00
/**
* @throws FireflyException
*/
private function renderAccountData(array $params, Account $account): void
{
$currency = $this->repository->getAccountCurrency($account);
if (null === $currency) {
$currency = $this->default;
}
$currentSet = [
'label' => $account->name,
// the currency that belongs to the account.
'currency_id' => (string) $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
// the default currency of the user (could be the same!)
'date' => $params['start']->toAtomString(),
'start' => $params['start']->toAtomString(),
'end' => $params['end']->toAtomString(),
'period' => '1D',
'entries' => [],
];
$currentStart = clone $params['start'];
$range = Steam::finalAccountBalanceInRange($account, $params['start'], clone $params['end'], $this->convertToNative);
$previous = array_values($range)[0]['balance'];
while ($currentStart <= $params['end']) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->toAtomString();
$balance = array_key_exists($format, $range) ? $range[$format]['balance'] : $previous;
$previous = $balance;
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
}
$this->chartData->add($currentSet);
}
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
*
* @throws FireflyException
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:
$dates = $request->getAll();
2023-12-20 19:35:52 +01:00
2019-06-09 15:28:54 +02:00
/** @var Carbon $start */
$start = $dates['start'];
2023-12-20 19:35:52 +01:00
2019-06-09 15:28:54 +02:00
/** @var Carbon $end */
$end = $dates['end'];
2019-02-13 17:38:41 +01:00
// set dates to end of day + start of day:
$start->startOfDay();
$end->endOfDay();
2019-02-13 17:38:41 +01:00
// user's preferences
2024-12-29 05:58:12 +01:00
$defaultSet = $this->repository->getAccountsByType([AccountTypeEnum::ASSET->value])->pluck('id')->toArray();
2023-12-20 19:35:52 +01:00
2024-04-01 20:26:02 +02:00
/** @var Preference $frontpage */
$frontpage = app('preferences')->get('frontpageAccounts', $defaultSet);
2021-04-07 07:28:43 +02:00
2024-04-01 20:26:02 +02:00
if (!(is_array($frontpage->data) && count($frontpage->data) > 0)) {
$frontpage->data = $defaultSet;
$frontpage->save();
2019-02-13 17:38:41 +01:00
}
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 = [];
2023-12-20 19:35:52 +01:00
2019-02-13 17:38:41 +01:00
/** @var Account $account */
foreach ($accounts as $account) {
$currency = $this->repository->getAccountCurrency($account) ?? $this->nativeCurrency;
$field = $this->convertToNative && $currency->id !== $this->nativeCurrency->id ? 'native_balance' : 'balance';
$currentSet = [
2019-02-13 17:38:41 +01:00
'label' => $account->name,
2024-12-22 08:43:12 +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' => [],
];
2023-06-04 06:30:22 +02:00
// TODO this code is also present in the V2 chart account controller so this method is due to be deprecated.
2019-02-13 17:38:41 +01:00
$currentStart = clone $start;
$range = Steam::finalAccountBalanceInRange($account, $start, clone $end, $this->convertToNative);
2024-12-29 05:58:12 +01:00
$previous = array_values($range)[0][$field];
2019-02-13 17:38:41 +01:00
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->toAtomString();
$balance = array_key_exists($format, $range) ? $range[$format][$field] : $previous;
$previous = $balance;
2019-02-13 17:38:41 +01:00
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
}
$chartData[] = $currentSet;
2019-02-13 17:38:41 +01:00
}
return response()->json($chartData);
}
2019-02-09 10:36:59 +01:00
}