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

117 lines
4.3 KiB
PHP
Raw Normal View History

2023-08-01 09:27:39 +02:00
<?php
2023-08-06 07:04:09 +02:00
2023-08-01 09:27:39 +02:00
/*
* BalanceController.php
* Copyright (c) 2023 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/>.
*/
2023-09-20 06:36:43 +02:00
declare(strict_types=1);
2023-08-01 09:27:39 +02:00
namespace FireflyIII\Api\V2\Controllers\Chart;
use FireflyIII\Api\V2\Controllers\Controller;
2024-05-20 07:30:41 +02:00
use FireflyIII\Api\V2\Request\Chart\ChartRequest;
2024-11-25 07:14:14 +01:00
use FireflyIII\Enums\TransactionTypeEnum;
2023-08-01 12:28:11 +02:00
use FireflyIII\Exceptions\FireflyException;
2023-08-01 09:27:39 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2024-05-20 07:30:41 +02:00
use FireflyIII\Support\Chart\ChartData;
2023-12-20 06:27:10 +01:00
use FireflyIII\Support\Http\Api\AccountBalanceGrouped;
2023-08-01 11:15:19 +02:00
use FireflyIII\Support\Http\Api\CleansChartData;
2024-05-20 07:30:41 +02:00
use FireflyIII\Support\Http\Api\CollectsAccountsFromFilter;
2023-08-01 09:47:06 +02:00
use Illuminate\Http\JsonResponse;
2023-08-01 09:27:39 +02:00
/**
* Class BalanceController
*/
class BalanceController extends Controller
{
2023-08-01 11:15:19 +02:00
use CleansChartData;
2024-05-20 07:30:41 +02:00
use CollectsAccountsFromFilter;
private ChartData $chartData;
2024-12-22 08:43:12 +01:00
private GroupCollectorInterface $collector;
private AccountRepositoryInterface $repository;
2024-07-31 13:09:55 +02:00
// private TransactionCurrency $default;
2024-05-20 07:30:41 +02:00
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(AccountRepositoryInterface::class);
$this->collector = app(GroupCollectorInterface::class);
2024-05-20 07:30:41 +02:00
$userGroup = $this->validateUserGroup($request);
$this->repository->setUserGroup($userGroup);
$this->collector->setUserGroup($userGroup);
$this->chartData = new ChartData();
2025-01-19 19:07:19 +01:00
// $this->default = app('amount')->getNativeCurrency();
2024-05-20 07:30:41 +02:00
return $next($request);
}
);
}
2024-04-10 19:45:08 +02:00
2023-08-01 09:27:39 +02:00
/**
* The code is practically a duplicate of ReportController::operations.
*
* Currency is up to the account/transactions in question, but conversion to the default
* currency is possible.
*
2023-08-01 12:28:11 +02:00
* If the transaction being processed is already in native currency OR if the
* foreign amount is in the native currency, the amount will not be converted.
2023-08-01 09:27:39 +02:00
*
2023-08-01 12:28:11 +02:00
* @throws FireflyException
2023-08-01 09:27:39 +02:00
*/
2024-05-20 07:30:41 +02:00
public function balance(ChartRequest $request): JsonResponse
2023-08-01 09:27:39 +02:00
{
2024-05-20 07:30:41 +02:00
$queryParameters = $request->getParameters();
$accounts = $this->getAccountList($queryParameters);
2023-12-20 19:35:52 +01:00
2023-08-01 09:27:39 +02:00
// prepare for currency conversion and data collection:
/** @var TransactionCurrency $default */
2025-01-19 19:07:19 +01:00
$default = app('amount')->getNativeCurrency();
2023-08-01 09:27:39 +02:00
// get journals for entire period:
2024-05-20 07:30:41 +02:00
$this->collector->setRange($queryParameters['start'], $queryParameters['end'])
->withAccountInformation()
->setXorAccounts($accounts)
2024-11-25 07:14:14 +01:00
->setTypes([TransactionTypeEnum::WITHDRAWAL->value, TransactionTypeEnum::DEPOSIT->value, TransactionTypeEnum::RECONCILIATION->value, TransactionTypeEnum::TRANSFER->value])
;
$journals = $this->collector->getExtractedJournals();
2024-05-20 07:30:41 +02:00
$object = new AccountBalanceGrouped();
2024-05-20 07:30:41 +02:00
$object->setPreferredRange($queryParameters['period']);
2023-12-20 06:27:10 +01:00
$object->setDefault($default);
$object->setAccounts($accounts);
$object->setJournals($journals);
2024-05-20 07:30:41 +02:00
$object->setStart($queryParameters['start']);
$object->setEnd($queryParameters['end']);
2023-12-20 06:27:10 +01:00
$object->groupByCurrencyAndPeriod();
$data = $object->convertToChartData();
foreach ($data as $entry) {
2024-05-20 07:30:41 +02:00
$this->chartData->add($entry);
}
2023-08-01 09:27:39 +02:00
2024-05-20 07:30:41 +02:00
return response()->json($this->chartData->render());
2023-08-01 09:27:39 +02:00
}
}