Files
firefly-iii/app/Http/Controllers/Chart/CategoryReportController.php
T

344 lines
16 KiB
PHP
Raw Normal View History

2016-11-12 10:14:20 +01:00
<?php
2022-12-29 19:41:57 +01:00
2016-11-12 10:14:20 +01:00
/**
* CategoryReportController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2016-11-12 10:14:20 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2016-11-12 10:14:20 +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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02: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.
2017-10-21 08:40:00 +02: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/>.
2016-11-12 10:14:20 +01:00
*/
2017-03-25 13:41:17 +01:00
declare(strict_types=1);
2016-11-12 10:14:20 +01:00
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
2016-12-15 13:47:28 +01:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
2016-11-12 10:14:20 +01:00
use FireflyIII\Http\Controllers\Controller;
2016-11-12 20:29:16 +01:00
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
2026-01-23 15:09:50 +01:00
use FireflyIII\Support\Facades\Navigation;
use FireflyIII\Support\Http\Controllers\AugumentData;
use FireflyIII\Support\Http\Controllers\ResolvesJournalAmountAndCurrency;
use FireflyIII\Support\Http\Controllers\TransactionCalculation;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
2016-11-12 10:14:20 +01:00
use Illuminate\Support\Collection;
/**
* Separate controller because many helper functions are shared.
*
* Class CategoryReportController
*/
2026-03-03 20:33:02 +01:00
final class CategoryReportController extends Controller
2016-11-12 10:14:20 +01:00
{
2022-10-30 14:24:19 +01:00
use AugumentData;
use ResolvesJournalAmountAndCurrency;
2022-10-30 14:24:19 +01:00
use TransactionCalculation;
2026-02-20 06:04:21 +01:00
private GeneratorInterface $generator;
2023-12-20 19:35:52 +01:00
2026-02-20 06:04:21 +01:00
private OperationsRepositoryInterface $opsRepository;
2016-11-12 10:14:20 +01:00
/**
2018-07-21 08:06:24 +02:00
* CategoryReportController constructor.
2016-11-12 10:14:20 +01:00
*/
public function __construct()
{
parent::__construct();
2026-01-23 15:09:50 +01:00
$this->middleware(function ($request, $next) {
2026-01-23 15:14:29 +01:00
$this->generator = app(GeneratorInterface::class);
2026-01-23 15:09:50 +01:00
$this->opsRepository = app(OperationsRepositoryInterface::class);
2016-11-12 10:14:20 +01:00
2026-01-23 15:09:50 +01:00
return $next($request);
});
2016-11-12 10:14:20 +01:00
}
public function budgetExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
2016-11-12 10:14:20 +01:00
{
$result = [];
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
2017-03-12 09:22:33 +01:00
// loop expenses.
foreach ($spent as $currency) {
/** @var array $category */
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$journalData = $this->resolveJournalAmountAndCurrency($journal, $currency);
2026-01-23 15:14:29 +01:00
$objectName = $journal['budget_name'] ?? trans('firefly.no_budget');
$title = sprintf('%s (%s)', $objectName, $journalData['currency_name']);
2024-01-01 14:43:56 +01:00
$result[$title] ??= [
2022-12-29 19:41:57 +01:00
'amount' => '0',
'currency_symbol' => $journalData['currency_symbol'],
'currency_code' => $journalData['currency_code'],
2022-12-29 19:41:57 +01:00
];
$result[$title]['amount'] = bcadd($result[$title]['amount'], $journalData['amount']);
}
}
}
2026-01-23 15:14:29 +01:00
$data = $this->generator->multiCurrencyPieChart($result);
2016-11-12 10:14:20 +01:00
2018-03-10 20:30:09 +01:00
return response()->json($data);
2016-11-12 10:14:20 +01:00
}
public function categoryExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
2016-11-12 10:14:20 +01:00
{
$result = [];
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
// loop expenses.
foreach ($spent as $currency) {
/** @var array $category */
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
2026-03-01 08:08:15 +01:00
$journalData = $this->resolveJournalAmountAndCurrency($journal, $currency);
$title = sprintf('%s (%s)', $category['name'], $journalData['currency_name']);
$result[$title] ??= [
'amount' => '0',
'currency_symbol' => $journalData['currency_symbol'],
'currency_code' => $journalData['currency_code'],
];
$result[$title]['amount'] = bcadd($result[$title]['amount'], $journalData['amount']);
}
}
}
2026-01-23 15:14:29 +01:00
$data = $this->generator->multiCurrencyPieChart($result);
2016-11-12 10:14:20 +01:00
2018-03-10 20:30:09 +01:00
return response()->json($data);
2016-11-12 10:14:20 +01:00
}
public function categoryIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
2016-11-12 10:14:20 +01:00
{
$result = [];
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
// loop expenses.
foreach ($earned as $currency) {
/** @var array $category */
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
2026-03-01 08:08:15 +01:00
$journalData = $this->resolveJournalAmountAndCurrency($journal, $currency);
$title = sprintf('%s (%s)', $category['name'], $journalData['currency_name']);
$result[$title] ??= [
'amount' => '0',
'currency_symbol' => $journalData['currency_symbol'],
'currency_code' => $journalData['currency_code'],
];
$result[$title]['amount'] = bcadd($result[$title]['amount'], $journalData['amount']);
}
}
}
2026-01-23 15:14:29 +01:00
$data = $this->generator->multiCurrencyPieChart($result);
2016-11-12 10:14:20 +01:00
2018-03-10 20:30:09 +01:00
return response()->json($data);
2016-11-12 10:14:20 +01:00
}
public function destinationExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
2016-11-12 10:14:20 +01:00
{
$result = [];
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
// loop expenses.
foreach ($spent as $currency) {
/** @var array $category */
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$journalData = $this->resolveJournalAmountAndCurrency($journal, $currency);
2026-01-23 15:14:29 +01:00
$objectName = $journal['destination_account_name'] ?? trans('firefly.empty');
$title = sprintf('%s (%s)', $objectName, $journalData['currency_name']);
2024-01-01 14:43:56 +01:00
$result[$title] ??= [
2022-12-29 19:41:57 +01:00
'amount' => '0',
'currency_symbol' => $journalData['currency_symbol'],
'currency_code' => $journalData['currency_code'],
2022-12-29 19:41:57 +01:00
];
$result[$title]['amount'] = bcadd($result[$title]['amount'], $journalData['amount']);
}
}
}
2026-01-23 15:14:29 +01:00
$data = $this->generator->multiCurrencyPieChart($result);
2016-11-12 10:14:20 +01:00
2018-03-10 20:30:09 +01:00
return response()->json($data);
2016-11-12 10:14:20 +01:00
}
public function destinationIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
{
$result = [];
$spent = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
// loop expenses.
foreach ($spent as $currency) {
/** @var array $category */
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$journalData = $this->resolveJournalAmountAndCurrency($journal, $currency);
2026-01-23 15:14:29 +01:00
$objectName = $journal['destination_account_name'] ?? trans('firefly.empty');
$title = sprintf('%s (%s)', $objectName, $journalData['currency_name']);
2024-01-01 14:43:56 +01:00
$result[$title] ??= [
2022-12-29 19:41:57 +01:00
'amount' => '0',
'currency_symbol' => $journalData['currency_symbol'],
'currency_code' => $journalData['currency_code'],
2022-12-29 19:41:57 +01:00
];
$result[$title]['amount'] = bcadd($result[$title]['amount'], $journalData['amount']);
}
}
}
2026-01-23 15:14:29 +01:00
$data = $this->generator->multiCurrencyPieChart($result);
2018-07-08 12:08:53 +02:00
return response()->json($data);
}
public function mainChart(Collection $accounts, Category $category, Carbon $start, Carbon $end): JsonResponse
2016-11-12 20:29:16 +01:00
{
$chartData = [];
2025-09-10 16:07:19 +02:00
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, new Collection()->push($category));
$earned = $this->opsRepository->listIncome($start, $end, $accounts, new Collection()->push($category));
2025-11-25 20:12:23 +01:00
$format = Navigation::preferredCarbonLocalizedFormat($start, $end);
// loop expenses.
foreach ($spent as $currency) {
// add things to chart Data for each currency:
foreach ($currency['categories'] as $currentCategory) {
foreach ($currentCategory['transaction_journals'] as $journal) {
2026-03-01 08:08:15 +01:00
$journalData = $this->resolveJournalAmountAndCurrency($journal, $currency);
$spentKey = sprintf('%d-spent', $journalData['currency_id']);
$chartData[$spentKey] ??= [
'label' => sprintf(
'%s (%s)',
(string) trans('firefly.spent_in_specific_category', ['category' => $category->name]),
$journalData['currency_name']
),
'type' => 'bar',
'currency_symbol' => $journalData['currency_symbol'],
'currency_code' => $journalData['currency_code'],
'currency_id' => $journalData['currency_id'],
'entries' => $this->makeEntries($start, $end),
];
2026-03-01 08:08:15 +01:00
$key = $journal['date']->isoFormat($format);
2023-12-10 06:45:59 +01:00
$chartData[$spentKey]['entries'][$key] ??= '0';
$chartData[$spentKey]['entries'][$key] = bcadd($chartData[$spentKey]['entries'][$key], $journalData['amount']);
}
}
2016-12-15 13:47:28 +01:00
}
// loop income.
foreach ($earned as $currency) {
// add things to chart Data for each currency:
foreach ($currency['categories'] as $currentCategory) {
foreach ($currentCategory['transaction_journals'] as $journal) {
2026-03-01 08:08:15 +01:00
$journalData = $this->resolveJournalAmountAndCurrency($journal, $currency);
$spentKey = sprintf('%d-earned', $journalData['currency_id']);
$chartData[$spentKey] ??= [
'label' => sprintf(
'%s (%s)',
(string) trans('firefly.earned_in_specific_category', ['category' => $category->name]),
$journalData['currency_name']
),
'type' => 'bar',
'currency_symbol' => $journalData['currency_symbol'],
'currency_code' => $journalData['currency_code'],
'currency_id' => $journalData['currency_id'],
'entries' => $this->makeEntries($start, $end),
];
2026-03-01 08:08:15 +01:00
$key = $journal['date']->isoFormat($format);
2023-12-10 06:45:59 +01:00
$chartData[$spentKey]['entries'][$key] ??= '0';
$chartData[$spentKey]['entries'][$key] = bcadd($chartData[$spentKey]['entries'][$key], $journalData['amount']);
}
2016-11-12 20:29:16 +01:00
}
}
2026-01-23 15:14:29 +01:00
$data = $this->generator->multiSet($chartData);
return response()->json($data);
}
public function sourceExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
{
$result = [];
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
// loop expenses.
foreach ($spent as $currency) {
/** @var array $category */
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$journalData = $this->resolveJournalAmountAndCurrency($journal, $currency);
2026-01-23 15:14:29 +01:00
$objectName = $journal['source_account_name'] ?? trans('firefly.empty');
$title = sprintf('%s (%s)', $objectName, $journalData['currency_name']);
2024-01-01 14:43:56 +01:00
$result[$title] ??= [
2022-12-29 19:41:57 +01:00
'amount' => '0',
'currency_symbol' => $journalData['currency_symbol'],
'currency_code' => $journalData['currency_code'],
2022-12-29 19:41:57 +01:00
];
$result[$title]['amount'] = bcadd($result[$title]['amount'], $journalData['amount']);
}
2016-12-23 07:20:47 +01:00
}
}
2026-01-23 15:14:29 +01:00
$data = $this->generator->multiCurrencyPieChart($result);
return response()->json($data);
}
public function sourceIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
{
$result = [];
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
// loop expenses.
foreach ($earned as $currency) {
/** @var array $category */
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$journalData = $this->resolveJournalAmountAndCurrency($journal, $currency);
2026-01-23 15:14:29 +01:00
$objectName = $journal['source_account_name'] ?? trans('firefly.empty');
$title = sprintf('%s (%s)', $objectName, $journalData['currency_name']);
2024-01-01 14:43:56 +01:00
$result[$title] ??= [
2022-12-29 19:41:57 +01:00
'amount' => '0',
'currency_symbol' => $journalData['currency_symbol'],
'currency_code' => $journalData['currency_code'],
2022-12-29 19:41:57 +01:00
];
$result[$title]['amount'] = bcadd($result[$title]['amount'], $journalData['amount']);
}
}
2016-12-23 15:52:12 +01:00
}
2026-01-23 15:14:29 +01:00
$data = $this->generator->multiCurrencyPieChart($result);
2016-11-20 11:43:19 +01:00
2018-03-10 20:30:09 +01:00
return response()->json($data);
2016-11-12 20:29:16 +01:00
}
2026-02-06 13:55:17 +01:00
/**
* TODO duplicate function
*/
private function makeEntries(Carbon $start, Carbon $end): array
{
$return = [];
$format = Navigation::preferredCarbonLocalizedFormat($start, $end);
$preferredRange = Navigation::preferredRangeFormat($start, $end);
$currentStart = clone $start;
while ($currentStart <= $end) {
$currentEnd = Navigation::endOfPeriod($currentStart, $preferredRange);
$key = $currentStart->isoFormat($format);
$return[$key] = '0';
$currentStart = clone $currentEnd;
$currentStart->addDay()->startOfDay();
}
return $return;
}
}