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

783 lines
38 KiB
PHP
Raw Normal View History

<?php
/**
* CategoryController.php
2020-01-31 00:32:04 -06: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 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05: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 01:40:00 -05: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\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\NoCategoryRepositoryInterface;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Controllers\BasicDataSupport;
2020-10-19 21:36:36 -05:00
use FireflyIII\Support\Report\Category\CategoryReportGenerator;
2019-09-04 10:39:39 -05:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Collection;
2019-09-04 10:39:39 -05:00
use Illuminate\View\View;
2018-07-20 07:34:56 -05:00
use Log;
use Throwable;
/**
2017-11-15 05:25:49 -06:00
* Class CategoryController.
*/
class CategoryController extends Controller
{
use BasicDataSupport;
2018-07-08 05:08:53 -05:00
2020-10-12 23:35:33 -05:00
private NoCategoryRepositoryInterface $noCatRepository;
2021-03-28 04:46:23 -05:00
private OperationsRepositoryInterface $opsRepository;
/**
* ExpenseReportController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2020-10-12 23:35:33 -05:00
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->noCatRepository = app(NoCategoryRepositoryInterface::class);
2020-12-20 23:18:59 -06:00
return $next($request);
}
);
}
/**
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
*
2019-09-04 10:39:39 -05:00
* @return Factory|View
*/
public function accountPerCategory(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$report = [];
/** @var Account $account */
foreach ($accounts as $account) {
$accountId = $account->id;
$report[$accountId] = $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) {
$sourceAccountId = $journal['source_account_id'];
$report[$sourceAccountId]['currencies'][$currencyId] = $report[$sourceAccountId]['currencies'][$currencyId] ?? [
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'categories' => [],
];
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]
= $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'],
$journal['amount']
);
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]['sum'] = bcadd(
$report[$sourceAccountId]['currencies'][$currencyId]['categories'][$category['id']]['sum'],
$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) {
2021-03-28 04:46:23 -05:00
$destinationId = $journal['destination_account_id'];
$report[$destinationId]['currencies'][$currencyId]
2021-03-28 04:46:23 -05:00
= $report[$destinationId]['currencies'][$currencyId]
?? [
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'categories' => [],
];
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]
= $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'],
$journal['amount']
);
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]['sum'] = bcadd(
$report[$destinationId]['currencies'][$currencyId]['categories'][$category['id']]['sum'],
$journal['amount']
);
}
}
}
return prefixView('reports.category.partials.account-per-category', compact('report', 'categories'));
}
2019-09-02 15:31:07 -05:00
/**
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
*
2019-09-04 10:39:39 -05:00
* @return Factory|View
2019-09-02 15:31:07 -05:00
*/
public function accounts(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$report = [];
$sums = [];
/** @var Account $account */
foreach ($accounts as $account) {
$accountId = $account->id;
$report[$accountId] = $report[$accountId] ?? [
'name' => $account->name,
'id' => $account->id,
'iban' => $account->iban,
'currencies' => [],
];
}
// loop expenses.
foreach ($spent as $currency) {
$currencyId = $currency['currency_id'];
$sums[$currencyId] = $sums[$currencyId] ?? [
'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',
'total_sum' => '0',
];
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$sourceAccountId = $journal['source_account_id'];
$report[$sourceAccountId]['currencies'][$currencyId] = $report[$sourceAccountId]['currencies'][$currencyId] ?? [
'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',
'sum' => '0',
];
$report[$sourceAccountId]['currencies'][$currencyId]['spent'] = bcadd(
$report[$sourceAccountId]['currencies'][$currencyId]['spent'],
$journal['amount']
2019-09-02 15:31:07 -05:00
);
$report[$sourceAccountId]['currencies'][$currencyId]['sum'] = bcadd(
$report[$sourceAccountId]['currencies'][$currencyId]['sum'],
$journal['amount']
2019-09-02 15:31:07 -05:00
);
$sums[$currencyId]['spent_sum'] = bcadd($sums[$currencyId]['spent_sum'], $journal['amount']);
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], $journal['amount']);
}
}
}
// loop income.
foreach ($earned as $currency) {
$currencyId = $currency['currency_id'];
$sums[$currencyId] = $sums[$currencyId] ?? [
'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',
'total_sum' => '0',
];
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$destinationAccountId = $journal['destination_account_id'];
$report[$destinationAccountId]['currencies'][$currencyId] = $report[$destinationAccountId]['currencies'][$currencyId] ?? [
'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',
'sum' => '0',
];
$report[$destinationAccountId]['currencies'][$currencyId]['earned'] = bcadd(
$report[$destinationAccountId]['currencies'][$currencyId]['earned'],
$journal['amount']
2019-09-02 15:31:07 -05:00
);
$report[$destinationAccountId]['currencies'][$currencyId]['sum'] = bcadd(
$report[$destinationAccountId]['currencies'][$currencyId]['sum'],
$journal['amount']
2019-09-02 15:31:07 -05:00
);
$sums[$currencyId]['earned_sum'] = bcadd($sums[$currencyId]['earned_sum'], $journal['amount']);
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], $journal['amount']);
2019-09-02 15:31:07 -05:00
}
}
}
return prefixView('reports.category.partials.accounts', compact('sums', 'report'));
2019-09-02 15:31:07 -05:00
}
/**
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
*
2021-05-24 01:54:58 -05:00
* @return string
2019-09-04 10:39:39 -05:00
*/
public function avgExpenses(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$result = [];
foreach ($spent as $currency) {
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$destinationId = $journal['destination_account_id'];
$key = sprintf('%d-%d', $destinationId, $currency['currency_id']);
$result[$key] = $result[$key] ?? [
'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'],
'currency_decimal_places' => $currency['currency_decimal_places'],
];
$result[$key]['transactions']++;
$result[$key]['sum'] = bcadd($journal['amount'], $result[$key]['sum']);
2020-12-20 23:18:59 -06:00
$result[$key]['avg'] = bcdiv($result[$key]['sum'], (string)$result[$key]['transactions']);
$result[$key]['avg_float'] = (float)$result[$key]['avg'];
2019-09-04 10:39:39 -05:00
}
}
}
// sort by amount_float
// sort temp array by amount.
$amounts = array_column($result, 'avg_float');
array_multisort($amounts, SORT_ASC, $result);
try {
$result = prefixView('reports.category.partials.avg-expenses', compact('result'))->render();
2021-04-07 00:28:43 -05:00
} catch (Throwable $e) { // @phpstan-ignore-line
2019-09-04 10:39:39 -05:00
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
$result = sprintf('Could not render view: %s', $e->getMessage());
}
return $result;
}
/**
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
*
2021-05-24 01:54:58 -05:00
* @return string
2019-09-04 10:39:39 -05:00
*/
public function avgIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
$spent = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$result = [];
foreach ($spent as $currency) {
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$sourceId = $journal['source_account_id'];
$key = sprintf('%d-%d', $sourceId, $currency['currency_id']);
$result[$key] = $result[$key] ?? [
'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'],
'currency_decimal_places' => $currency['currency_decimal_places'],
];
$result[$key]['transactions']++;
$result[$key]['sum'] = bcadd($journal['amount'], $result[$key]['sum']);
2020-12-20 23:18:59 -06:00
$result[$key]['avg'] = bcdiv($result[$key]['sum'], (string)$result[$key]['transactions']);
$result[$key]['avg_float'] = (float)$result[$key]['avg'];
2019-09-04 10:39:39 -05:00
}
}
}
// sort by amount_float
// sort temp array by amount.
$amounts = array_column($result, 'avg_float');
array_multisort($amounts, SORT_DESC, $result);
try {
$result = prefixView('reports.category.partials.avg-income', compact('result'))->render();
2021-04-07 00:28:43 -05:00
} catch (Throwable $e) { // @phpstan-ignore-line
2019-09-04 10:39:39 -05:00
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
$result = sprintf('Could not render view: %s', $e->getMessage());
}
return $result;
}
/**
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
*
* @return Factory|View
*/
public function categories(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$sums = [];
$report = [];
/** @var Category $category */
foreach ($categories as $category) {
$categoryId = $category->id;
$report[$categoryId] = $report[$categoryId] ?? [
'name' => $category->name,
'id' => $category->id,
'currencies' => [],
];
}
foreach ($spent as $currency) {
$currencyId = $currency['currency_id'];
$sums[$currencyId] = $sums[$currencyId] ?? [
'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',
'total_sum' => '0',
];
/** @var array $category */
foreach ($currency['categories'] as $category) {
$categoryId = $category['id'];
foreach ($category['transaction_journals'] as $journal) {
// add currency info to report array:
$report[$categoryId]['currencies'][$currencyId] = $report[$categoryId]['currencies'][$currencyId] ?? [
'spent' => '0',
'earned' => '0',
'sum' => '0',
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
];
$report[$categoryId]['currencies'][$currencyId]['spent'] = bcadd(
$report[$categoryId]['currencies'][$currencyId]['spent'],
$journal['amount']
);
$report[$categoryId]['currencies'][$currencyId]['sum'] = bcadd(
$report[$categoryId]['currencies'][$currencyId]['sum'],
$journal['amount']
);
$sums[$currencyId]['spent_sum'] = bcadd($sums[$currencyId]['spent_sum'], $journal['amount']);
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], $journal['amount']);
}
}
}
foreach ($earned as $currency) {
$currencyId = $currency['currency_id'];
$sums[$currencyId] = $sums[$currencyId] ?? [
'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',
'total_sum' => '0',
];
/** @var array $category */
foreach ($currency['categories'] as $category) {
$categoryId = $category['id'];
foreach ($category['transaction_journals'] as $journal) {
// add currency info to report array:
$report[$categoryId]['currencies'][$currencyId] = $report[$categoryId]['currencies'][$currencyId] ?? [
'spent' => '0',
'earned' => '0',
'sum' => '0',
'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'],
'currency_name' => $currency['currency_name'],
'currency_decimal_places' => $currency['currency_decimal_places'],
];
$report[$categoryId]['currencies'][$currencyId]['earned'] = bcadd(
$report[$categoryId]['currencies'][$currencyId]['earned'],
$journal['amount']
);
$report[$categoryId]['currencies'][$currencyId]['sum'] = bcadd(
$report[$categoryId]['currencies'][$currencyId]['sum'],
$journal['amount']
);
$sums[$currencyId]['earned_sum'] = bcadd($sums[$currencyId]['earned_sum'], $journal['amount']);
$sums[$currencyId]['total_sum'] = bcadd($sums[$currencyId]['total_sum'], $journal['amount']);
}
}
}
return prefixView('reports.category.partials.categories', compact('sums', 'report'));
}
2016-12-03 13:38:13 -06:00
/**
2018-07-21 01:55:32 -05:00
* Show overview of expenses in category.
*
2016-12-06 00:48:41 -06:00
* @param Collection $accounts
2016-12-03 13:38:13 -06:00
* @param Carbon $start
* @param Carbon $end
*
2016-12-06 00:48:41 -06:00
* @return mixed|string
2016-12-03 13:38:13 -06:00
*/
2016-12-06 00:48:41 -06:00
public function expenses(Collection $accounts, Carbon $start, Carbon $end)
2016-12-03 13:38:13 -06:00
{
2016-12-03 14:24:55 -06:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category-period-expenses-report');
2016-12-03 14:24:55 -06:00
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
2021-04-07 00:28:43 -05:00
return $cache->get();
2016-12-03 14:24:55 -06:00
}
2019-08-27 07:45:14 -05:00
2019-08-01 22:24:51 -05: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.
2019-08-28 05:28:23 -05:00
$format = app('navigation')->preferredCarbonFormat($start, $end);
2019-08-01 22:24:51 -05:00
2019-08-28 05:28:23 -05:00
if ('Y' === $format) {
2019-08-01 22:24:51 -05:00
$start->startOfYear();
}
2019-08-28 05:28:23 -05:00
if ('Y-m' === $format) {
2019-08-01 22:24:51 -05:00
$start->startOfMonth();
}
2019-08-28 05:28:23 -05:00
$periods = app('navigation')->listOfPeriods($start, $end);
$data = [];
2020-10-12 23:35:33 -05: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) {
$key = sprintf('%d-%d', $currencyId, $categoryId);
$data[$key] = $data[$key] ?? [
'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',
'entries' => [],
2019-08-28 05:28:23 -05:00
];
2020-10-24 00:40:56 -05:00
foreach ($categoryRow['transaction_journals'] as $journal) {
$date = $journal['date']->format($format);
$data[$key]['entries'][$date] = $data[$key]['entries'][$date] ?? '0';
$data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date], $journal['amount']);
$data[$key]['sum'] = bcadd($data[$key]['sum'], $journal['amount']);
}
2019-08-28 05:28:23 -05:00
}
}
}
2020-02-01 08:54:26 -06:00
2019-08-28 05:28:23 -05:00
$cache->store($data);
$report = $data;
2019-08-27 07:45:14 -05:00
2018-07-20 07:34:56 -05:00
try {
$result = prefixView('reports.partials.category-period', compact('report', 'periods'))->render();
2021-04-07 00:28:43 -05:00
} catch (Throwable $e) { // @phpstan-ignore-line
2018-07-20 07:34:56 -05:00
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
2019-05-31 06:35:33 -05:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2018-07-20 07:34:56 -05:00
}
2021-04-07 00:28:43 -05:00
2016-12-03 13:38:13 -06:00
2016-12-03 14:24:55 -06:00
$cache->store($result);
2016-12-03 13:38:13 -06:00
return $result;
}
/**
2018-07-21 01:55:32 -05:00
* Show overview of income in category.
*
2018-04-27 23:23:13 -05:00
* @param Collection $accounts
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
2018-07-08 05:28:42 -05:00
public function income(Collection $accounts, Carbon $start, Carbon $end): string
{
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category-period-income-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
2021-04-07 00:28:43 -05:00
return $cache->get();
}
2019-08-01 22:24:51 -05: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.
2019-08-28 05:28:23 -05:00
$format = app('navigation')->preferredCarbonFormat($start, $end);
2019-08-01 22:24:51 -05:00
2019-08-28 05:28:23 -05:00
if ('Y' === $format) {
2019-08-01 22:24:51 -05:00
$start->startOfYear();
}
2019-08-28 05:28:23 -05:00
if ('Y-m' === $format) {
2019-08-01 22:24:51 -05:00
$start->startOfMonth();
}
2019-08-27 07:45:14 -05:00
$periods = app('navigation')->listOfPeriods($start, $end);
2019-08-28 05:28:23 -05:00
$data = [];
2020-10-12 23:35:33 -05:00
$with = $this->opsRepository->listIncome($start, $end, $accounts);
$without = $this->noCatRepository->listIncome($start, $end, $accounts);
foreach ([$with, $without] as $set) {
2020-02-01 08:54:26 -06:00
foreach ($set as $currencyId => $currencyRow) {
foreach ($currencyRow['categories'] as $categoryId => $categoryRow) {
$key = sprintf('%d-%d', $currencyId, $categoryId);
$data[$key] = $data[$key] ?? [
'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',
'entries' => [],
2019-08-28 05:28:23 -05:00
2020-02-01 08:54:26 -06:00
];
2020-10-24 00:40:56 -05:00
foreach ($categoryRow['transaction_journals'] as $journal) {
2020-02-01 08:54:26 -06:00
$date = $journal['date']->format($format);
$data[$key]['entries'][$date] = $data[$key]['entries'][$date] ?? '0';
$data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date], $journal['amount']);
$data[$key]['sum'] = bcadd($data[$key]['sum'], $journal['amount']);
}
2019-08-28 05:28:23 -05:00
}
}
}
$cache->store($data);
$report = $data;
2018-07-20 07:34:56 -05:00
try {
$result = prefixView('reports.partials.category-period', compact('report', 'periods'))->render();
2021-04-07 00:28:43 -05:00
} catch (Throwable $e) { // @phpstan-ignore-line
2018-07-20 07:34:56 -05:00
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
2019-05-31 06:35:33 -05:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2018-07-20 07:34:56 -05:00
}
2021-04-07 00:28:43 -05:00
2019-08-28 05:28:23 -05:00
$cache->store($result);
return $result;
}
2016-12-06 01:59:08 -06:00
/**
2020-10-19 21:36:36 -05:00
* Show overview of category transactions on the default report.
2018-07-21 01:55:32 -05:00
*
2016-12-15 10:16:46 -06:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-12-06 01:59:08 -06:00
*
* @return mixed|string
2017-11-15 05:25:49 -06:00
*
2016-12-06 01:59:08 -06:00
*/
public function operations(Collection $accounts, Carbon $start, Carbon $end)
{
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
2021-04-07 00:28:43 -05:00
return $cache->get();
2016-12-06 01:59:08 -06:00
}
2020-10-19 21:36:36 -05:00
/** @var CategoryReportGenerator $generator */
$generator = app(CategoryReportGenerator::class);
$generator->setAccounts($accounts);
$generator->setStart($start);
$generator->setEnd($end);
$generator->operations();
$report = $generator->getReport();
2021-04-07 00:28:43 -05:00
2018-07-20 07:34:56 -05:00
try {
$result = prefixView('reports.partials.categories', compact('report'))->render();
2018-07-20 07:34:56 -05:00
$cache->store($result);
2021-04-07 00:28:43 -05:00
} catch (Throwable $e) { // @phpstan-ignore-line
2018-07-20 07:34:56 -05:00
Log::error(sprintf('Could not render category::expenses: %s', $e->getMessage()));
2019-05-31 06:35:33 -05:00
$result = sprintf('An error prevented Firefly III from rendering: %s. Apologies.', $e->getMessage());
2018-07-20 07:34:56 -05:00
}
2016-12-06 01:59:08 -06:00
return $result;
}
/**
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
*
2021-05-24 01:54:58 -05:00
* @return string
*/
public function topExpenses(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$result = [];
foreach ($spent as $currency) {
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$result[] = [
'description' => $journal['description'],
'transaction_group_id' => $journal['transaction_group_id'],
2020-12-20 23:18:59 -06:00
'amount_float' => (float)$journal['amount'],
'amount' => $journal['amount'],
'date' => $journal['date']->formatLocalized($this->monthAndDayFormat),
2020-12-20 23:18:59 -06: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'],
'category_name' => $category['name'],
];
}
}
}
// sort by amount_float
// sort temp array by amount.
$amounts = array_column($result, 'amount_float');
array_multisort($amounts, SORT_ASC, $result);
try {
$result = prefixView('reports.category.partials.top-expenses', compact('result'))->render();
2021-04-07 00:28:43 -05:00
} catch (Throwable $e) { // @phpstan-ignore-line
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
$result = sprintf('Could not render view: %s', $e->getMessage());
}
return $result;
}
/**
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
*
2021-05-24 01:54:58 -05:00
* @return string
*/
public function topIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
$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 10:39:39 -05:00
'description' => $journal['description'],
'transaction_group_id' => $journal['transaction_group_id'],
2020-12-20 23:18:59 -06:00
'amount_float' => (float)$journal['amount'],
2019-09-04 10:39:39 -05:00
'amount' => $journal['amount'],
'date' => $journal['date']->formatLocalized($this->monthAndDayFormat),
2020-12-20 23:18:59 -06:00
'date_sort' => $journal['date']->format('Y-m-d'),
2019-09-04 10:39:39 -05: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'],
'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 {
$result = prefixView('reports.category.partials.top-income', compact('result'))->render();
2021-04-07 00:28:43 -05:00
} catch (Throwable $e) { // @phpstan-ignore-line
Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage()));
$result = sprintf('Could not render view: %s', $e->getMessage());
}
return $result;
}
}