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

265 lines
10 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;
2025-07-27 07:36:23 +02:00
use FireflyIII\Exceptions\ValidationException;
2019-01-18 05:38:23 +01:00
use FireflyIII\Models\Account;
use FireflyIII\Models\Preference;
2025-07-27 07:36:23 +02:00
use FireflyIII\Models\TransactionCurrency;
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;
2025-06-09 07:06:42 +02:00
use FireflyIII\Support\Facades\Preferences;
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;
2025-07-27 07:36:23 +02:00
use Illuminate\Support\Facades\Log;
2019-01-18 05:38:23 +01:00
/**
* 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();
Log::debug(sprintf('dashboard(), convert to primary: %s', var_export($this->convertToPrimary, true)));
2025-03-25 17:27:59 +01:00
// loop each account, and collect info:
/** @var Account $account */
foreach ($accounts as $account) {
2025-07-27 07:36:23 +02:00
Log::debug(sprintf('Account #%d ("%s")', $account->id, $account->name));
2025-03-25 17:27:59 +01:00
$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
{
2025-07-27 07:36:23 +02:00
Log::debug(sprintf('Now in %s(array, #%d)', __METHOD__, $account->id));
$currency = $this->repository->getAccountCurrency($account);
$currentStart = clone $params['start'];
$range = Steam::finalAccountBalanceInRange($account, $params['start'], clone $params['end'], $this->convertToPrimary);
2025-07-27 07:36:23 +02:00
$previous = array_values($range)[0]['balance'];
$pcPrevious = null;
2025-05-24 16:39:20 +02:00
if (!$currency instanceof TransactionCurrency) {
2025-05-04 17:41:26 +02:00
$currency = $this->default;
}
$currentSet = [
2025-05-04 17:41:26 +02:00
'label' => $account->name,
// the currency that belongs to the account.
2025-07-27 07:36:23 +02:00
'currency_id' => (string)$currency->id,
2025-05-04 17:41:26 +02:00
'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' => [],
];
2025-07-31 20:38:57 +02:00
if ($this->convertToPrimary) {
$currentSet['pc_entries'] = [];
$currentSet['primary_currency_id'] = (string)$this->primaryCurrency->id;
$currentSet['primary_currency_code'] = $this->primaryCurrency->code;
$currentSet['primary_currency_symbol'] = $this->primaryCurrency->symbol;
$currentSet['primary_currency_decimal_places'] = $this->primaryCurrency->decimal_places;
$pcPrevious = array_values($range)[0]['pc_balance'];
2025-07-27 07:36:23 +02:00
}
2025-05-04 17:41:26 +02:00
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;
2025-07-27 07:36:23 +02:00
$currentSet['entries'][$label] = $balance;
// do the same for the primary currency balance, if relevant:
$pcBalance = null;
2025-07-31 20:38:57 +02:00
if ($this->convertToPrimary) {
$pcBalance = array_key_exists($format, $range) ? $range[$format]['pc_balance'] : $pcPrevious;
$pcPrevious = $pcBalance;
$currentSet['pc_entries'][$label] = $pcBalance;
2025-07-27 07:36:23 +02:00
}
2025-05-04 17:41:26 +02:00
$currentStart->addDay();
}
$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
*
2025-06-09 07:06:42 +02:00
* @throws ValidationException
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();
2025-07-27 07:36:23 +02:00
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();
2025-07-27 07:36:23 +02:00
$frontPageIds = $this->getFrontPageAccountIds();
$accounts = $this->repository->getAccountsById($frontPageIds);
$chartData = [];
2023-12-20 19:35:52 +01:00
2019-02-13 17:38:41 +01:00
/** @var Account $account */
foreach ($accounts as $account) {
2025-07-27 07:36:23 +02:00
Log::debug(sprintf('Rendering chart data for account %s (%d)', $account->name, $account->id));
$currency = $this->repository->getAccountCurrency($account) ?? $this->primaryCurrency;
$currentStart = clone $start;
$range = Steam::finalAccountBalanceInRange($account, $start, clone $end, $this->convertToPrimary);
$previous = array_values($range)[0]['balance'];
$pcPrevious = null;
$currentSet = [
2019-02-13 17:38:41 +01:00
'label' => $account->name,
2025-07-27 07:36:23 +02: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' => [],
];
2025-07-27 07:36:23 +02:00
// add "pc_entries" if convertToPrimary is true:
2025-07-31 20:38:57 +02:00
if ($this->convertToPrimary) {
$currentSet['pc_entries'] = [];
$currentSet['primary_currency_id'] = (string)$this->primaryCurrency->id;
$currentSet['primary_currency_code'] = $this->primaryCurrency->code;
$currentSet['primary_currency_symbol'] = $this->primaryCurrency->symbol;
$currentSet['primary_currency_decimal_places'] = $this->primaryCurrency->decimal_places;
$pcPrevious = array_values($range)[0]['pc_balance'];
2025-07-27 07:36:23 +02:00
}
// also get the primary balance if convertToPrimary is true:
2019-02-13 17:38:41 +01:00
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->toAtomString();
2025-07-27 07:36:23 +02:00
// balance is based on "balance" from the $range variable.
$balance = array_key_exists($format, $range) ? $range[$format]['balance'] : $previous;
$previous = $balance;
2019-02-13 17:38:41 +01:00
$currentSet['entries'][$label] = $balance;
2025-07-27 07:36:23 +02:00
// do the same for the primary balance, if relevant:
$pcBalance = null;
2025-07-31 20:38:57 +02:00
if ($this->convertToPrimary) {
$pcBalance = array_key_exists($format, $range) ? $range[$format]['pc_balance'] : $pcPrevious;
$pcPrevious = $pcBalance;
$currentSet['pc_entries'][$label] = $pcBalance;
2025-07-27 07:36:23 +02:00
}
$currentStart->addDay();
2019-02-13 17:38:41 +01:00
}
$chartData[] = $currentSet;
2019-02-13 17:38:41 +01:00
}
return response()->json($chartData);
}
2025-07-27 07:36:23 +02:00
private function getFrontPageAccountIds(): array
{
$defaultSet = $this->repository->getAccountsByType([AccountTypeEnum::ASSET->value])->pluck('id')->toArray();
/** @var Preference $frontpage */
$frontpage = Preferences::get('frontpageAccounts', $defaultSet);
2025-07-27 07:36:23 +02:00
if (!(is_array($frontpage->data) && count($frontpage->data) > 0)) {
$frontpage->data = $defaultSet;
$frontpage->save();
}
2025-07-27 07:36:23 +02:00
return $frontpage->data ?? $defaultSet;
}
2019-02-09 10:36:59 +01:00
}