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

161 lines
6.1 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 Carbon\Carbon;
use FireflyIII\Api\V2\Controllers\Controller;
use FireflyIII\Api\V2\Request\Generic\DateRequest;
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;
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-02-22 18:03:31 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* 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;
/**
*
*/
public function __construct()
{
2023-02-12 06:53:36 +01:00
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(AccountRepositoryInterface::class);
2023-09-21 16:26:07 +02:00
$userGroup = $this->validateUserGroup($request);
if (null !== $userGroup) {
$this->repository->setUserGroup($userGroup);
}
return $next($request);
}
);
}
/**
2023-02-12 06:53:36 +01:00
* This endpoint is documented at
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v2)#/charts/getChartAccountOverview
*
2023-07-25 09:01:44 +02:00
* The native currency is the preferred currency on the page /currencies.
*
2023-08-01 12:28:11 +02:00
* If a transaction has foreign currency = native currency, the foreign amount will be used, no conversion
* will take place.
*
* TODO validate and set user_group_id from request
2023-08-09 14:14:33 +02:00
*
2023-06-21 12:34:58 +02:00
* @param DateRequest $request
2023-05-29 13:56:55 +02:00
*
* @return JsonResponse
2023-02-22 18:03:31 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2023-08-01 12:28:11 +02:00
* @throws FireflyException
2023-11-26 12:50:54 +01:00
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function dashboard(DateRequest $request): JsonResponse
{
/** @var Carbon $start */
2023-08-09 14:14:33 +02:00
$start = $this->parameters->get('start');
/** @var Carbon $end */
2023-08-09 14:14:33 +02:00
$end = $this->parameters->get('end');
2023-08-01 11:15:19 +02:00
$end->endOfDay();
2023-07-25 09:01:44 +02:00
// user's preferences
$defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray();
$frontPage = app('preferences')->get('frontPageAccounts', $defaultSet);
2022-10-30 14:44:49 +01:00
if (!(is_array($frontPage->data) && count($frontPage->data) > 0)) {
$frontPage->data = $defaultSet;
$frontPage->save();
}
2023-11-04 14:09:51 +01:00
/** @var TransactionCurrency $default */
$default = app('amount')->getDefaultCurrency();
$accounts = $this->repository->getAccountsById($frontPage->data);
$chartData = [];
/** @var Account $account */
foreach ($accounts as $account) {
$currency = $this->repository->getAccountCurrency($account);
if (null === $currency) {
$currency = $default;
}
2023-07-25 09:01:44 +02:00
$currentSet = [
'label' => $account->name,
2023-07-25 09:01:44 +02:00
// the currency that belongs to the account.
2022-12-29 19:41:57 +01:00
'currency_id' => (string)$currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
2023-07-25 09:01:44 +02:00
2023-08-01 09:42:59 +02:00
// the default currency of the user (could be the same!)
2023-09-09 15:12:24 +02:00
'native_id' => (string)$default->id,
2023-07-25 09:01:44 +02:00
'native_code' => $default->code,
'native_symbol' => $default->symbol,
2023-11-26 12:10:42 +01:00
'native_decimal_places' => $default->decimal_places,
2023-08-01 11:15:19 +02:00
'start' => $start->toAtomString(),
'end' => $end->toAtomString(),
'period' => '1D',
'entries' => [],
2023-08-01 10:26:26 +02:00
'native_entries' => [],
];
2023-07-25 09:01:44 +02:00
$currentStart = clone $start;
$range = app('steam')->balanceInRange($account, $start, clone $end, $currency);
$rangeConverted = app('steam')->balanceInRangeConverted($account, $start, clone $end, $default);
2022-10-16 14:44:11 +02:00
2023-07-25 09:01:44 +02:00
$previous = array_values($range)[0];
$previousConverted = array_values($rangeConverted)[0];
while ($currentStart <= $end) {
2023-07-25 09:01:44 +02:00
$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();
2023-08-01 10:26:26 +02:00
$currentSet['entries'][$label] = $balance;
$currentSet['native_entries'][$label] = $balanceConverted;
}
$chartData[] = $currentSet;
}
2023-08-01 11:15:19 +02:00
return response()->json($this->clean($chartData));
}
2023-08-06 19:37:51 +02:00
}