Files
firefly-iii/app/Http/Controllers/Report/CategoryController.php
T

735 lines
35 KiB
PHP
Raw Normal View History

<?php
2022-12-29 19:42:26 +01:00
/**
* CategoryController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 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.
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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Report;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\Category;
2019-08-27 10:52:07 +02:00
use FireflyIII\Repositories\Category\NoCategoryRepositoryInterface;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
use FireflyIII\Support\CacheProperties;
2026-01-23 15:09:50 +01:00
use FireflyIII\Support\Facades\Navigation;
use FireflyIII\Support\Http\Controllers\BasicDataSupport;
2020-10-20 04:36:36 +02:00
use FireflyIII\Support\Report\Category\CategoryReportGenerator;
2019-09-04 17:39:39 +02:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Collection;
2026-01-23 15:09:50 +01:00
use Illuminate\Support\Facades\Log;
2023-05-29 13:56:55 +02:00
use Illuminate\View\View;
2025-10-05 12:59:43 +02:00
use Throwable;
/**
2017-11-15 12:25:49 +01:00
* Class CategoryController.
*/
2026-03-03 20:33:02 +01:00
final class CategoryController extends Controller
{
use BasicDataSupport;
2018-07-08 12:08:53 +02:00
2020-10-13 06:35:33 +02:00
private NoCategoryRepositoryInterface $noCatRepository;
2021-03-28 11:46:23 +02:00
private OperationsRepositoryInterface $opsRepository;
/**
* ExpenseReportController constructor.
*/
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->opsRepository = app(OperationsRepositoryInterface::class);
2026-01-23 15:09:50 +01:00
$this->noCatRepository = app(NoCategoryRepositoryInterface::class);
2020-12-21 06:18:59 +01:00
2026-01-23 15:09:50 +01:00
return $next($request);
});
}
/**
2019-09-04 17:39:39 +02:00
* @return Factory|View
*/
2025-11-09 09:07:14 +01:00
public function accountPerCategory(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): Factory|\Illuminate\Contracts\View\View
{
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$report = [];
2023-12-20 19:35:52 +01:00
/** @var Account $account */
foreach ($accounts as $account) {
2024-01-01 14:43:56 +01:00
$accountId = $account->id;
2026-03-13 03:55:47 +01:00
$report[$accountId] ??= ['name' => $account->name, 'id' => $account->id, 'iban' => $account->iban, 'currencies' => []];
}
// loop expenses.
foreach ($spent as $currency) {
$currencyId = $currency['currency_id'];
/** @var array $category */
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
2026-01-23 15:14:29 +01:00
$sourceAccountId = $journal['source_account_id'];
$report[$sourceAccountId]['currencies'][$currencyId] ??= [
2022-12-29 19:42:26 +01:00
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
2026-01-23 15:14:29 +01:00
'categories' => [],
2022-12-29 19:42:26 +01:00
];
2026-01-25 10:55:27 +01:00
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']] ??= [
'spent' => '0',
'earned' => '0',
'sum' => '0',
];
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]['spent'] = bcadd(
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]['spent'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
);
2026-01-23 15:14:29 +01:00
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]['sum'] = bcadd(
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]['sum'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
);
}
}
}
// loop income.
foreach ($earned as $currency) {
$currencyId = $currency['currency_id'];
/** @var array $category */
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
2026-01-23 15:14:29 +01:00
$destinationId = $journal['destination_account_id'];
$report[$destinationId]['currencies'][$currencyId] ??= [
2026-01-23 15:09:50 +01:00
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
2026-01-23 15:14:29 +01:00
'categories' => [],
2026-01-23 15:09:50 +01:00
];
2026-01-25 10:55:27 +01:00
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']] ??= [
'spent' => '0',
'earned' => '0',
'sum' => '0',
];
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]['earned'] = bcadd(
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]['earned'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
);
2026-01-23 15:14:29 +01:00
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]['sum'] = bcadd(
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]['sum'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
);
}
}
}
2026-03-13 03:55:47 +01:00
return view('reports.category.partials.account-per-category', ['report' => $report, 'categories' => $categories]);
}
2019-09-02 22:31:07 +02:00
/**
2019-09-04 17:39:39 +02:00
* @return Factory|View
2023-12-22 17:28:42 +01:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.ExcessiveMethodLength")
2019-09-02 22:31:07 +02:00
*/
2025-11-09 09:07:14 +01:00
public function accounts(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): Factory|\Illuminate\Contracts\View\View
2019-09-02 22:31:07 +02:00
{
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$report = [];
$sums = [];
2023-12-20 19:35:52 +01:00
2019-09-02 22:31:07 +02:00
/** @var Account $account */
foreach ($accounts as $account) {
2024-01-01 14:43:56 +01:00
$accountId = $account->id;
2026-03-13 03:55:47 +01:00
$report[$accountId] ??= ['name' => $account->name, 'id' => $account->id, 'iban' => $account->iban, 'currencies' => []];
2019-09-02 22:31:07 +02:00
}
// loop expenses.
foreach ($spent as $currency) {
2024-01-01 14:43:56 +01:00
$currencyId = $currency['currency_id'];
2023-12-10 06:45:59 +01:00
$sums[$currencyId] ??= [
2022-12-29 19:42:26 +01:00
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'spent_sum' => '0',
'earned_sum' => '0',
2026-01-23 15:14:29 +01:00
'total_sum' => '0',
2022-12-29 19:42:26 +01:00
];
2019-09-02 22:31:07 +02:00
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
2026-01-23 15:14:29 +01:00
$sourceAccountId = $journal['source_account_id'];
2024-01-01 14:43:56 +01:00
$report[$sourceAccountId]['currencies'][$currencyId] ??= [
2022-12-29 19:42:26 +01:00
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'spent' => '0',
'earned' => '0',
2026-01-23 15:14:29 +01:00
'sum' => '0',
2022-12-29 19:42:26 +01:00
];
2019-09-02 22:31:07 +02:00
$report[$sourceAccountId]['currencies'][$currencyId]['spent'] = bcadd(
$report[$sourceAccountId]['currencies'][$currencyId]['spent'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
2019-09-02 22:31:07 +02:00
);
2026-01-23 15:14:29 +01:00
$report[$sourceAccountId]['currencies'][$currencyId]['sum'] = bcadd(
$report[$sourceAccountId]['currencies'][$currencyId]['sum'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
2019-09-02 22:31:07 +02:00
);
2026-01-23 15:14:29 +01:00
$sums[$currencyId]['spent_sum'] = bcadd($sums[$currencyId]['spent_sum'], (string) $journal['amount']);
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], (string) $journal['amount']);
2019-09-02 22:31:07 +02:00
}
}
}
// loop income.
foreach ($earned as $currency) {
2024-01-01 14:43:56 +01:00
$currencyId = $currency['currency_id'];
2023-12-10 06:45:59 +01:00
$sums[$currencyId] ??= [
2022-12-29 19:42:26 +01:00
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'spent_sum' => '0',
'earned_sum' => '0',
2026-01-23 15:14:29 +01:00
'total_sum' => '0',
2022-12-29 19:42:26 +01:00
];
2019-09-02 22:31:07 +02:00
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
2026-01-23 15:14:29 +01:00
$destinationAccountId = $journal['destination_account_id'];
2024-01-01 14:43:56 +01:00
$report[$destinationAccountId]['currencies'][$currencyId] ??= [
2022-12-29 19:42:26 +01:00
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'spent' => '0',
'earned' => '0',
2026-01-23 15:14:29 +01:00
'sum' => '0',
2022-12-29 19:42:26 +01:00
];
2019-09-02 22:31:07 +02:00
$report[$destinationAccountId]['currencies'][$currencyId]['earned'] = bcadd(
$report[$destinationAccountId]['currencies'][$currencyId]['earned'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
2019-09-02 22:31:07 +02:00
);
2026-01-23 15:14:29 +01:00
$report[$destinationAccountId]['currencies'][$currencyId]['sum'] = bcadd(
$report[$destinationAccountId]['currencies'][$currencyId]['sum'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
2019-09-02 22:31:07 +02:00
);
2026-01-23 15:14:29 +01:00
$sums[$currencyId]['earned_sum'] = bcadd($sums[$currencyId]['earned_sum'], (string) $journal['amount']);
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], (string) $journal['amount']);
2019-09-02 22:31:07 +02:00
}
}
}
2026-03-13 03:55:47 +01:00
return view('reports.category.partials.accounts', ['sums' => $sums, 'report' => $report]);
2019-09-02 22:31:07 +02:00
}
/**
2021-05-24 08:54:58 +02:00
* @return string
2023-12-20 19:35:52 +01:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2019-09-04 17:39:39 +02:00
*/
public function avgExpenses(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
2026-01-23 15:14:29 +01:00
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$result = [];
2019-09-04 17:39:39 +02:00
foreach ($spent as $currency) {
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
2026-01-23 15:14:29 +01:00
$destinationId = $journal['destination_account_id'];
$key = sprintf('%d-%d', $destinationId, $currency['currency_id']);
2024-01-01 14:43:56 +01:00
$result[$key] ??= [
2022-12-29 19:42:26 +01:00
'transactions' => 0,
'sum' => '0',
'avg' => '0',
'avg_float' => 0,
'destination_account_name' => $journal['destination_account_name'],
'destination_account_id' => $journal['destination_account_id'],
'currency_id' => $currency['currency_id'],
'currency_name' => $currency['currency_name'],
'currency_symbol' => $currency['currency_symbol'],
2026-01-23 15:14:29 +01:00
'currency_decimal_places' => $currency['currency_decimal_places'],
2022-12-29 19:42:26 +01:00
];
2023-12-20 19:35:52 +01:00
++$result[$key]['transactions'];
2026-01-23 15:14:29 +01:00
$result[$key]['sum'] = bcadd((string) $journal['amount'], $result[$key]['sum']);
$result[$key]['avg'] = bcdiv($result[$key]['sum'], (string) $result[$key]['transactions']);
2024-12-22 08:43:12 +01:00
$result[$key]['avg_float'] = (float) $result[$key]['avg']; // intentional float
2019-09-04 17:39:39 +02:00
}
}
}
// sort by amount_float
// sort temp array by amount.
$amounts = array_column($result, 'avg_float');
array_multisort($amounts, SORT_ASC, $result);
try {
2025-11-09 09:07:14 +01:00
$result = view('reports.category.partials.avg-expenses', ['result' => $result])->render();
2025-05-27 16:53:48 +02:00
} catch (Throwable $e) {
2025-11-09 09:07:14 +01:00
Log::error(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
2019-09-04 17:39:39 +02:00
$result = sprintf('Could not render view: %s', $e->getMessage());
2023-12-20 19:35:52 +01:00
throw new FireflyException($result, 0, $e);
2019-09-04 17:39:39 +02:00
}
return $result;
}
/**
2021-05-24 08:54:58 +02:00
* @return string
2023-12-20 19:35:52 +01:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2019-09-04 17:39:39 +02:00
*/
public function avgIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
2026-01-23 15:14:29 +01:00
$spent = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$result = [];
2019-09-04 17:39:39 +02:00
foreach ($spent as $currency) {
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
2026-01-23 15:14:29 +01:00
$sourceId = $journal['source_account_id'];
$key = sprintf('%d-%d', $sourceId, $currency['currency_id']);
2023-12-10 06:45:59 +01:00
$result[$key] ??= [
2022-12-29 19:42:26 +01:00
'transactions' => 0,
'sum' => '0',
'avg' => '0',
'avg_float' => 0,
'source_account_name' => $journal['source_account_name'],
'source_account_id' => $journal['source_account_id'],
'currency_id' => $currency['currency_id'],
'currency_name' => $currency['currency_name'],
'currency_symbol' => $currency['currency_symbol'],
2026-01-23 15:14:29 +01:00
'currency_decimal_places' => $currency['currency_decimal_places'],
2022-12-29 19:42:26 +01:00
];
2023-12-20 19:35:52 +01:00
++$result[$key]['transactions'];
2026-01-23 15:14:29 +01:00
$result[$key]['sum'] = bcadd((string) $journal['amount'], $result[$key]['sum']);
$result[$key]['avg'] = bcdiv($result[$key]['sum'], (string) $result[$key]['transactions']);
2024-12-22 08:43:12 +01:00
$result[$key]['avg_float'] = (float) $result[$key]['avg'];
2019-09-04 17:39:39 +02:00
}
}
}
// sort by amount_float
// sort temp array by amount.
$amounts = array_column($result, 'avg_float');
array_multisort($amounts, SORT_DESC, $result);
try {
2025-11-09 09:07:14 +01:00
$result = view('reports.category.partials.avg-income', ['result' => $result])->render();
2025-05-27 16:53:48 +02:00
} catch (Throwable $e) {
2025-11-09 09:07:14 +01:00
Log::error(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
2019-09-04 17:39:39 +02:00
$result = sprintf('Could not render view: %s', $e->getMessage());
2023-12-20 19:35:52 +01:00
throw new FireflyException($result, 0, $e);
2019-09-04 17:39:39 +02:00
}
return $result;
}
/**
* @return Factory|View
2023-12-22 17:28:42 +01:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.ExcessiveMethodLength")
*/
2025-11-09 09:07:14 +01:00
public function categories(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): Factory|\Illuminate\Contracts\View\View
{
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$sums = [];
$report = [];
2023-12-20 19:35:52 +01:00
/** @var Category $category */
foreach ($categories as $category) {
2024-01-01 14:43:56 +01:00
$categoryId = $category->id;
2026-03-13 03:55:47 +01:00
$report[$categoryId] ??= ['name' => $category->name, 'id' => $category->id, 'currencies' => []];
}
foreach ($spent as $currency) {
2024-01-01 14:43:56 +01:00
$currencyId = $currency['currency_id'];
2023-12-10 06:45:59 +01:00
$sums[$currencyId] ??= [
2022-12-29 19:42:26 +01:00
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'earned_sum' => '0',
'spent_sum' => '0',
2026-01-23 15:14:29 +01:00
'total_sum' => '0',
2022-12-29 19:42:26 +01:00
];
2023-12-20 19:35:52 +01:00
/** @var array $category */
foreach ($currency['categories'] as $category) {
$categoryId = $category['id'];
foreach ($category['transaction_journals'] as $journal) {
// add currency info to report array:
2024-01-01 14:43:56 +01:00
$report[$categoryId]['currencies'][$currencyId] ??= [
2022-12-29 19:42:26 +01:00
'spent' => '0',
'earned' => '0',
'sum' => '0',
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
2026-01-23 15:14:29 +01:00
'currency_decimal_places' => $currency['currency_decimal_places'],
2022-12-29 19:42:26 +01:00
];
$report[$categoryId]['currencies'][$currencyId]['spent'] = bcadd(
$report[$categoryId]['currencies'][$currencyId]['spent'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
);
2026-01-23 15:14:29 +01:00
$report[$categoryId]['currencies'][$currencyId]['sum'] = bcadd(
$report[$categoryId]['currencies'][$currencyId]['sum'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
);
2026-01-23 15:14:29 +01:00
$sums[$currencyId]['spent_sum'] = bcadd($sums[$currencyId]['spent_sum'], (string) $journal['amount']);
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], (string) $journal['amount']);
}
}
}
foreach ($earned as $currency) {
2024-01-01 14:43:56 +01:00
$currencyId = $currency['currency_id'];
2023-12-10 06:45:59 +01:00
$sums[$currencyId] ??= [
2022-12-29 19:42:26 +01:00
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'earned_sum' => '0',
'spent_sum' => '0',
2026-01-23 15:14:29 +01:00
'total_sum' => '0',
2022-12-29 19:42:26 +01:00
];
2023-12-20 19:35:52 +01:00
/** @var array $category */
foreach ($currency['categories'] as $category) {
$categoryId = $category['id'];
foreach ($category['transaction_journals'] as $journal) {
// add currency info to report array:
2024-01-01 14:43:56 +01:00
$report[$categoryId]['currencies'][$currencyId] ??= [
2022-12-29 19:42:26 +01:00
'spent' => '0',
'earned' => '0',
'sum' => '0',
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
2026-01-23 15:14:29 +01:00
'currency_decimal_places' => $currency['currency_decimal_places'],
2022-12-29 19:42:26 +01:00
];
$report[$categoryId]['currencies'][$currencyId]['earned'] = bcadd(
$report[$categoryId]['currencies'][$currencyId]['earned'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
);
2026-01-23 15:14:29 +01:00
$report[$categoryId]['currencies'][$currencyId]['sum'] = bcadd(
$report[$categoryId]['currencies'][$currencyId]['sum'],
2025-05-04 13:47:00 +02:00
(string) $journal['amount']
);
2026-01-23 15:14:29 +01:00
$sums[$currencyId]['earned_sum'] = bcadd($sums[$currencyId]['earned_sum'], (string) $journal['amount']);
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], (string) $journal['amount']);
}
}
}
2026-03-13 03:55:47 +01:00
return view('reports.category.partials.categories', ['sums' => $sums, 'report' => $report]);
}
2016-12-03 20:38:13 +01:00
/**
2018-07-21 08:55:32 +02:00
* Show overview of expenses in category.
*
2016-12-06 07:48:41 +01:00
* @return mixed|string
2023-12-20 19:35:52 +01:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2016-12-03 20:38:13 +01:00
*/
2016-12-06 07:48:41 +01:00
public function expenses(Collection $accounts, Carbon $start, Carbon $end)
2016-12-03 20:38:13 +01:00
{
2026-01-23 15:14:29 +01:00
$cache = new CacheProperties();
2016-12-03 21:24:55 +01:00
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-04 18:02:19 +01:00
$cache->addProperty('category-period-expenses-report');
2016-12-03 21:24:55 +01:00
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
return $cache->get();
2016-12-03 21:24:55 +01:00
}
2019-08-27 14:45:14 +02:00
2019-08-02 05:24:51 +02:00
// depending on the carbon format (a reliable way to determine the general date difference)
// change the "listOfPeriods" call so the entire period gets included correctly.
2026-01-23 15:14:29 +01:00
$format = Navigation::preferredCarbonFormat($start, $end);
2019-08-02 05:24:51 +02:00
2019-08-28 12:28:23 +02:00
if ('Y' === $format) {
2019-08-02 05:24:51 +02:00
$start->startOfYear();
}
2019-08-28 12:28:23 +02:00
if ('Y-m' === $format) {
2019-08-02 05:24:51 +02:00
$start->startOfMonth();
}
2025-11-25 20:12:23 +01:00
$periods = Navigation::listOfPeriods($start, $end);
2019-08-28 12:28:23 +02:00
$data = [];
2020-10-13 06:35:33 +02:00
$with = $this->opsRepository->listExpenses($start, $end, $accounts);
$without = $this->noCatRepository->listExpenses($start, $end, $accounts);
foreach ([$with, $without] as $set) {
foreach ($set as $currencyId => $currencyRow) {
foreach ($currencyRow['categories'] as $categoryId => $categoryRow) {
2024-01-01 14:43:56 +01:00
$key = sprintf('%d-%d', $currencyId, $categoryId);
2023-12-10 06:45:59 +01:00
$data[$key] ??= [
2022-12-29 19:42:26 +01:00
'id' => $categoryRow['id'],
'title' => sprintf('%s (%s)', $categoryRow['name'], $currencyRow['currency_name']),
'currency_id' => $currencyRow['currency_id'],
'currency_symbol' => $currencyRow['currency_symbol'],
'currency_name' => $currencyRow['currency_name'],
'currency_code' => $currencyRow['currency_code'],
'currency_decimal_places' => $currencyRow['currency_decimal_places'],
'sum' => '0',
2026-01-23 15:14:29 +01:00
'entries' => [],
2022-12-29 19:42:26 +01:00
];
2020-10-24 07:40:56 +02:00
foreach ($categoryRow['transaction_journals'] as $journal) {
2026-01-23 15:14:29 +01:00
$date = $journal['date']->format($format);
2023-12-10 06:45:59 +01:00
$data[$key]['entries'][$date] ??= '0';
2025-05-04 13:47:00 +02:00
$data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date], (string) $journal['amount']);
2026-01-23 15:14:29 +01:00
$data[$key]['sum'] = bcadd($data[$key]['sum'], (string) $journal['amount']);
}
2019-08-28 12:28:23 +02:00
}
}
}
2020-02-01 15:54:26 +01:00
2019-08-28 12:28:23 +02:00
$cache->store($data);
2026-01-23 15:14:29 +01:00
$report = $data;
2019-08-27 14:45:14 +02:00
2018-07-20 14:34:56 +02:00
try {
2026-03-13 03:55:47 +01:00
$result = view('reports.partials.category-period', ['report' => $report, 'periods' => $periods])->render();
2025-05-27 16:53:48 +02:00
} catch (Throwable $e) {
2025-11-09 09:07:14 +01:00
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
2019-05-31 13:35:33 +02:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2023-12-20 19:35:52 +01:00
throw new FireflyException($result, 0, $e);
2018-07-20 14:34:56 +02:00
}
2021-04-07 07:28:43 +02:00
2016-12-03 21:24:55 +01:00
$cache->store($result);
2016-12-03 20:38:13 +01:00
return $result;
}
/**
2018-07-21 08:55:32 +02:00
* Show overview of income in category.
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
*/
2018-07-08 12:28:42 +02:00
public function income(Collection $accounts, Carbon $start, Carbon $end): string
{
2026-01-23 15:14:29 +01:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-04 18:02:19 +01:00
$cache->addProperty('category-period-income-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
return $cache->get();
}
2019-08-27 10:52:07 +02:00
2019-08-02 05:24:51 +02:00
// depending on the carbon format (a reliable way to determine the general date difference)
// change the "listOfPeriods" call so the entire period gets included correctly.
2026-01-23 15:14:29 +01:00
$format = Navigation::preferredCarbonFormat($start, $end);
2019-08-02 05:24:51 +02:00
2019-08-28 12:28:23 +02:00
if ('Y' === $format) {
2019-08-02 05:24:51 +02:00
$start->startOfYear();
}
2019-08-28 12:28:23 +02:00
if ('Y-m' === $format) {
2019-08-02 05:24:51 +02:00
$start->startOfMonth();
}
2025-11-25 20:12:23 +01:00
$periods = Navigation::listOfPeriods($start, $end);
2019-08-28 12:28:23 +02:00
$data = [];
2020-10-13 06:35:33 +02:00
$with = $this->opsRepository->listIncome($start, $end, $accounts);
$without = $this->noCatRepository->listIncome($start, $end, $accounts);
foreach ([$with, $without] as $set) {
2020-02-01 15:54:26 +01:00
foreach ($set as $currencyId => $currencyRow) {
foreach ($currencyRow['categories'] as $categoryId => $categoryRow) {
2024-01-01 14:43:56 +01:00
$key = sprintf('%d-%d', $currencyId, $categoryId);
2023-12-10 06:45:59 +01:00
$data[$key] ??= [
2022-12-29 19:42:26 +01:00
'id' => $categoryRow['id'],
'title' => sprintf('%s (%s)', $categoryRow['name'], $currencyRow['currency_name']),
'currency_id' => $currencyRow['currency_id'],
'currency_symbol' => $currencyRow['currency_symbol'],
'currency_name' => $currencyRow['currency_name'],
'currency_code' => $currencyRow['currency_code'],
'currency_decimal_places' => $currencyRow['currency_decimal_places'],
'sum' => '0',
2026-01-23 15:14:29 +01:00
'entries' => [],
2022-12-29 19:42:26 +01:00
];
2020-10-24 07:40:56 +02:00
foreach ($categoryRow['transaction_journals'] as $journal) {
2026-01-23 15:14:29 +01:00
$date = $journal['date']->format($format);
2023-12-10 06:45:59 +01:00
$data[$key]['entries'][$date] ??= '0';
2025-05-04 13:47:00 +02:00
$data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date], (string) $journal['amount']);
2026-01-23 15:14:29 +01:00
$data[$key]['sum'] = bcadd($data[$key]['sum'], (string) $journal['amount']);
2020-02-01 15:54:26 +01:00
}
2019-08-28 12:28:23 +02:00
}
}
}
2026-01-23 15:14:29 +01:00
$report = $data;
2019-08-28 12:28:23 +02:00
2018-07-20 14:34:56 +02:00
try {
2026-03-13 03:55:47 +01:00
$result = view('reports.partials.category-period', ['report' => $report, 'periods' => $periods])->render();
2025-05-27 16:53:48 +02:00
} catch (Throwable $e) {
2025-11-09 09:07:14 +01:00
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
2019-05-31 13:35:33 +02:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2023-12-20 19:35:52 +01:00
throw new FireflyException($result, 0, $e);
2018-07-20 14:34:56 +02:00
}
2021-04-07 07:28:43 +02:00
$cache->store($result);
return $result;
}
2016-12-06 08:59:08 +01:00
/**
2020-10-20 04:36:36 +02:00
* Show overview of category transactions on the default report.
2018-07-21 08:55:32 +02:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2016-12-06 08:59:08 +01:00
*/
2021-10-30 11:42:35 +02:00
public function operations(Collection $accounts, Carbon $start, Carbon $end): string
2016-12-06 08:59:08 +01:00
{
2026-07-09 19:10:33 +02:00
$incomeTopLength = 0;
2016-12-06 08:59:08 +01:00
// chart properties for cache:
2026-07-09 19:57:19 +02:00
$cache = new CacheProperties();
2016-12-06 08:59:08 +01:00
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
return $cache->get();
2016-12-06 08:59:08 +01:00
}
2020-10-20 04:36:36 +02:00
/** @var CategoryReportGenerator $generator */
2026-07-09 19:57:19 +02:00
$generator = app(CategoryReportGenerator::class);
2020-10-20 04:36:36 +02:00
$generator->setAccounts($accounts);
$generator->setStart($start);
$generator->setEnd($end);
$generator->operations();
2026-07-09 19:57:19 +02:00
$report = $generator->getReport();
2019-08-12 18:18:51 +02:00
2018-07-20 14:34:56 +02:00
try {
2026-07-09 19:57:19 +02:00
$result = view('reports.partials.categories', ['report' => $report, 'incomeTopLength' => $incomeTopLength])->render();
2018-07-20 14:34:56 +02:00
$cache->store($result);
2025-05-27 16:53:48 +02:00
} catch (Throwable $e) {
2025-11-09 09:07:14 +01:00
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
2019-05-31 13:35:33 +02:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2023-12-20 19:35:52 +01:00
throw new FireflyException($result, 0, $e);
2018-07-20 14:34:56 +02:00
}
2016-12-06 08:59:08 +01:00
return $result;
}
2016-12-04 18:02:19 +01:00
/**
2021-05-24 08:54:58 +02:00
* @return string
2023-12-20 19:35:52 +01:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
*/
public function topExpenses(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
2026-07-09 19:57:19 +02:00
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
2026-06-27 16:47:20 +02:00
$incomeTopLength = 0;
2026-07-09 19:57:19 +02:00
$result = [];
foreach ($spent as $currency) {
foreach ($currency['categories'] as $category) {
2026-07-09 19:57:19 +02:00
++$incomeTopLength;
foreach ($category['transaction_journals'] as $journal) {
$result[] = [
'description' => $journal['description'],
'transaction_group_id' => $journal['transaction_group_id'],
2024-12-22 08:43:12 +01:00
'amount_float' => (float) $journal['amount'],
'amount' => $journal['amount'],
2022-03-27 20:24:13 +02:00
'date' => $journal['date']->isoFormat($this->monthAndDayFormat),
2020-12-21 06:18:59 +01:00
'date_sort' => $journal['date']->format('Y-m-d'),
'destination_account_name' => $journal['destination_account_name'],
'destination_account_id' => $journal['destination_account_id'],
'currency_id' => $currency['currency_id'],
'currency_name' => $currency['currency_name'],
'currency_symbol' => $currency['currency_symbol'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'category_id' => $category['id'],
2026-01-23 15:14:29 +01:00
'category_name' => $category['name'],
];
}
}
}
// sort by amount_float
// sort temp array by amount.
2026-07-09 19:57:19 +02:00
$amounts = array_column($result, 'amount_float');
array_multisort($amounts, SORT_ASC, $result);
try {
2026-06-27 16:47:20 +02:00
$result = view('reports.category.partials.top-expenses', ['result' => $result, 'incomeTopLength' => $incomeTopLength])->render();
2025-05-27 16:53:48 +02:00
} catch (Throwable $e) {
2025-11-09 09:07:14 +01:00
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
$result = sprintf('Could not render view: %s', $e->getMessage());
2023-12-20 19:35:52 +01:00
2022-11-04 05:11:05 +01:00
throw new FireflyException($e->getMessage(), 0, $e);
}
return $result;
}
/**
2021-05-24 08:54:58 +02:00
* @return string
2023-12-20 19:35:52 +01:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
*/
public function topIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
2026-01-23 15:14:29 +01:00
$spent = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$result = [];
foreach ($spent as $currency) {
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$result[] = [
2019-09-04 17:39:39 +02:00
'description' => $journal['description'],
'transaction_group_id' => $journal['transaction_group_id'],
2024-12-22 08:43:12 +01:00
'amount_float' => (float) $journal['amount'],
2019-09-04 17:39:39 +02:00
'amount' => $journal['amount'],
2022-03-27 20:24:13 +02:00
'date' => $journal['date']->isoFormat($this->monthAndDayFormat),
2020-12-21 06:18:59 +01:00
'date_sort' => $journal['date']->format('Y-m-d'),
2019-09-04 17:39:39 +02:00
'source_account_name' => $journal['source_account_name'],
'source_account_id' => $journal['source_account_id'],
'currency_id' => $currency['currency_id'],
'currency_name' => $currency['currency_name'],
'currency_symbol' => $currency['currency_symbol'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'category_id' => $category['id'],
2026-01-23 15:14:29 +01:00
'category_name' => $category['name'],
];
}
}
}
// sort by amount_float
// sort temp array by amount.
$amounts = array_column($result, 'amount_float');
array_multisort($amounts, SORT_DESC, $result);
try {
2025-11-09 09:07:14 +01:00
$result = view('reports.category.partials.top-income', ['result' => $result])->render();
2025-05-27 16:53:48 +02:00
} catch (Throwable $e) {
2025-11-09 09:07:14 +01:00
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
$result = sprintf('Could not render view: %s', $e->getMessage());
2023-12-20 19:35:52 +01:00
2022-11-04 05:11:05 +01:00
throw new FireflyException($e->getMessage(), 0, $e);
}
return $result;
}
}