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

185 lines
7.3 KiB
PHP
Raw Normal View History

<?php
2022-10-16 19:22:26 +02:00
/*
* AccountController.php
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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/>.
*/
2022-10-16 19:22:26 +02:00
declare(strict_types=1);
namespace FireflyIII\Api\V2\Controllers\Chart;
use FireflyIII\Api\V2\Controllers\Controller;
2024-05-20 06:49:42 +02:00
use FireflyIII\Api\V2\Request\Chart\ChartRequest;
2023-08-01 12:28:11 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2023-08-01 09:42:59 +02:00
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
2024-05-20 06:49:42 +02:00
use FireflyIII\Support\Chart\ChartData;
2023-08-01 11:15:19 +02:00
use FireflyIII\Support\Http\Api\CleansChartData;
2023-09-21 16:26:07 +02:00
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
use Illuminate\Http\JsonResponse;
2023-12-02 15:56:17 +01:00
use Illuminate\Support\Collection;
/**
* Class AccountController
*/
class AccountController extends Controller
{
2023-08-01 11:15:19 +02:00
use CleansChartData;
2023-09-21 16:26:07 +02:00
use ValidatesUserGroupTrait;
private AccountRepositoryInterface $repository;
2024-05-20 06:49:42 +02:00
private ChartData $chartData;
private TransactionCurrency $default;
public function __construct()
{
2023-02-12 06:53:36 +01:00
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(AccountRepositoryInterface::class);
2024-04-23 19:40:48 +02:00
$this->repository->setUserGroup($this->validateUserGroup($request));
2024-05-20 06:49:42 +02:00
$this->chartData = new ChartData();
$this->default = app('amount')->getDefaultCurrency();
2023-09-21 16:26:07 +02:00
return $next($request);
}
);
}
/**
2024-05-20 06:49:42 +02:00
* TODO fix documentation
2023-08-01 12:28:11 +02:00
* @throws FireflyException
*/
2024-05-20 06:49:42 +02:00
public function dashboard(ChartRequest $request): JsonResponse
{
2024-05-20 06:49:42 +02:00
$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);
}
2023-12-20 19:35:52 +01:00
2024-05-20 06:49:42 +02:00
return response()->json($this->chartData->render());
}
2023-07-25 09:01:44 +02:00
2024-05-20 06:49:42 +02:00
/**
* TODO Duplicate function but I think it belongs here or in a separate trait
*
*/
private function getAccountList(array $queryParameters): Collection
{
$collection = new Collection();
2023-12-20 19:35:52 +01:00
2024-05-20 06:49:42 +02:00
// always collect from the query parameter, even when it's empty.
foreach ($queryParameters['accounts'] as $accountId) {
$account = $this->repository->find((int) $accountId);
if (null !== $account) {
$collection->push($account);
}
}
2023-12-02 15:56:17 +01:00
2024-05-20 06:49:42 +02:00
// if no "preselected", and found accounts
if ('empty' === $queryParameters['preselected'] && $collection->count() > 0) {
return $collection;
}
// if no preselected, but no accounts:
if ('empty' === $queryParameters['preselected'] && 0 === $collection->count()) {
2023-12-02 15:56:17 +01:00
$defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray();
2024-04-01 20:26:02 +02:00
$frontpage = app('preferences')->get('frontpageAccounts', $defaultSet);
2024-04-01 20:26:02 +02:00
if (!(is_array($frontpage->data) && count($frontpage->data) > 0)) {
$frontpage->data = $defaultSet;
$frontpage->save();
2023-12-02 15:56:17 +01:00
}
2023-11-04 14:09:51 +01:00
2024-05-20 06:49:42 +02:00
return $this->repository->getAccountsById($frontpage->data);
2023-12-02 15:56:17 +01:00
}
2023-11-04 14:09:51 +01:00
// both options are overruled by "preselected"
2024-05-20 06:49:42 +02:00
if ('all' === $queryParameters['preselected']) {
return $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]);
}
2024-05-20 06:49:42 +02:00
if ('assets' === $queryParameters['preselected']) {
return $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
}
2024-05-20 06:49:42 +02:00
if ('liabilities' === $queryParameters['preselected']) {
return $this->repository->getAccountsByType([AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]);
}
2024-05-20 06:49:42 +02:00
return $collection;
}
2024-05-20 06:49:42 +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!)
'native_currency_id' => (string) $this->default->id,
'native_currency_code' => $this->default->code,
'native_currency_symbol' => $this->default->symbol,
'native_currency_decimal_places' => $this->default->decimal_places,
'start' => $params['start']->toAtomString(),
'end' => $params['end']->toAtomString(),
'period' => '1D',
'entries' => [],
'native_entries' => [],
];
$currentStart = clone $params['start'];
$range = app('steam')->balanceInRange($account, $params['start'], clone $params['end'], $currency);
$rangeConverted = app('steam')->balanceInRangeConverted($account, $params['start'], clone $params['end'], $this->default);
$previous = array_values($range)[0];
$previousConverted = array_values($rangeConverted)[0];
while ($currentStart <= $params['end']) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->toAtomString();
$balance = array_key_exists($format, $range) ? $range[$format] : $previous;
$balanceConverted = array_key_exists($format, $rangeConverted) ? $rangeConverted[$format] : $previousConverted;
$previous = $balance;
$previousConverted = $balanceConverted;
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
$currentSet['native_entries'][$label] = $balanceConverted;
}
$this->chartData->add($currentSet);
}
}