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

259 lines
9.5 KiB
PHP
Raw Normal View History

<?php
/**
* CategoryController.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Report;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
2016-12-03 20:38:13 +01:00
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Controllers\BasicDataSupport;
use Illuminate\Support\Collection;
2018-07-20 14:34:56 +02:00
use Log;
use Throwable;
/**
2017-11-15 12:25:49 +01:00
* Class CategoryController.
*/
class CategoryController extends Controller
{
use BasicDataSupport;
2018-07-08 12:08:53 +02:00
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
* @param Collection $accounts
2016-12-03 20:38:13 +01:00
* @param Carbon $start
* @param Carbon $end
*
2016-12-06 07:48:41 +01:00
* @return mixed|string
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
{
2016-12-03 21:24:55 +01:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category-period-expenses-report');
2016-12-03 21:24:55 +01:00
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
2017-03-04 11:19:44 +01:00
return $cache->get(); // @codeCoverageIgnore
2016-12-03 21:24:55 +01:00
}
2016-12-03 20:38:13 +01:00
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
$data = $repository->periodExpenses($categories, $accounts, $start, $end);
$data[0] = $repository->periodExpensesNoCategory($accounts, $start, $end);
$report = $this->filterPeriodReport($data);
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.
$range = app('navigation')->preferredCarbonFormat($start, $end);
if ('Y' === $range) {
$start->startOfYear();
}
if ('Y-m' === $range) {
$start->startOfMonth();
}
$periods = app('navigation')->listOfPeriods($start, $end);
2018-07-20 14:34:56 +02:00
try {
$result = view('reports.partials.category-period', compact('report', 'periods'))->render();
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreStart
2018-07-20 14:34:56 +02:00
} catch (Throwable $e) {
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());
2018-07-20 14:34:56 +02:00
}
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreEnd
2016-12-03 20:38:13 +01:00
2016-12-03 21:24:55 +01:00
$cache->store($result);
2016-12-03 20:38:13 +01:00
return $result;
}
2018-07-08 12:08:53 +02:00
/**
2018-07-21 08:55:32 +02:00
* Show overview of income in category.
*
2018-04-28 06:23:13 +02:00
* @param Collection $accounts
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
2018-07-08 12:28:42 +02: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()) {
2017-03-04 11:19:44 +01:00
return $cache->get(); // @codeCoverageIgnore
}
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
$data = $repository->periodIncome($categories, $accounts, $start, $end);
$data[0] = $repository->periodIncomeNoCategory($accounts, $start, $end);
$report = $this->filterPeriodReport($data);
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.
$range = app('navigation')->preferredCarbonFormat($start, $end);
if ('Y' === $range) {
$start->startOfYear();
}
if ('Y-m' === $range) {
$start->startOfMonth();
}
$periods = app('navigation')->listOfPeriods($start, $end);
2018-07-20 14:34:56 +02:00
try {
$result = view('reports.partials.category-period', compact('report', 'periods'))->render();
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreStart
2018-07-20 14:34:56 +02:00
} catch (Throwable $e) {
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());
2018-07-20 14:34:56 +02:00
}
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreEnd
$cache->store($result);
return $result;
}
2018-07-08 12:08:53 +02:00
2016-12-06 08:59:08 +01:00
/**
2018-07-21 08:55:32 +02:00
* Show overview of operations.
*
2016-12-15 17:16:46 +01:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-12-06 08:59:08 +01:00
*
* @return mixed|string
2017-11-15 12:25:49 +01:00
*
2018-07-20 14:34:56 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2016-12-06 08:59:08 +01: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()) {
//return $cache->get(); // @codeCoverageIgnore
2016-12-06 08:59:08 +01:00
}
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
2019-08-16 17:54:38 +02:00
$report = [
'categories' => [],
'sums' => [],
];
2016-12-06 08:59:08 +01:00
/** @var Category $category */
foreach ($categories as $category) {
$spent = $repository->spentInPeriod($category, $accounts, $start, $end);
$earned = $repository->earnedInPeriod($category, $accounts, $start, $end);
2019-08-16 17:54:38 +02:00
if (0 === count($spent) && 0 === count($earned)) {
continue;
}
$currencies = array_unique(array_merge(array_keys($spent), array_keys($earned)));
foreach ($currencies as $code) {
2019-08-16 17:54:38 +02:00
$currencyInfo = $spent[$code] ?? $earned[$code];
$key = sprintf('%s-%s', $category->id, $code);
$report['categories'][$key] = [
'name' => $category->name,
'spent' => $spent[$code]['spent'] ?? '0',
'earned' => $earned[$code]['earned'] ?? '0',
'id' => $category->id,
'currency_id' => $currencyInfo['currency_id'],
'currency_code' => $currencyInfo['currency_code'],
'currency_symbol' => $currencyInfo['currency_symbol'],
2019-08-16 17:54:38 +02:00
'currency_name' => $currencyInfo['currency_name'],
'currency_decimal_places' => $currencyInfo['currency_decimal_places'],
];
2019-08-16 17:54:38 +02:00
2016-12-06 10:42:13 +01:00
}
}
$sum = [];
2019-08-16 17:54:38 +02:00
/**
* @var string $categoryId
* @var array $row
*/
foreach ($report['categories'] as $categoryId => $row) {
$sum[$categoryId] = (float)$row['spent'];
}
array_multisort($sum, SORT_ASC, $report['categories']);
// get sums:
foreach ($report['categories'] as $entry) {
$currencyId = $entry['currency_id'];
$report['sums'][$currencyId] = $report['sums'][$currencyId] ?? [
'spent' => '0',
'earned' => '0',
'currency_id' => $entry['currency_id'],
'currency_code' => $entry['currency_code'],
'currency_symbol' => $entry['currency_symbol'],
'currency_name' => $entry['currency_name'],
'cyrrency_decimal_places' => $entry['currency_decimal_places'],
];
$report['sums'][$currencyId]['spent'] = bcadd($report['sums'][$currencyId]['spent'], $entry['spent']);
$report['sums'][$currencyId]['earned'] = bcadd($report['sums'][$currencyId]['earned'], $entry['earned']);
2016-12-06 08:59:08 +01:00
}
2019-07-24 19:02:41 +02:00
// @codeCoverageIgnoreStart
2018-07-20 14:34:56 +02:00
try {
$result = view('reports.partials.categories', compact('report'))->render();
$cache->store($result);
} catch (Throwable $e) {
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());
2018-07-20 14:34:56 +02:00
}
2019-02-13 17:38:41 +01:00
2018-09-02 20:13:25 +02:00
// @codeCoverageIgnoreEnd
2016-12-06 08:59:08 +01:00
return $result;
}
/**
* @param array $array
*
* @return bool
*/
private function noAmountInArray(array $array): bool
{
if (0 === count($array)) {
return true;
}
return false;
}
2016-12-03 20:38:13 +01:00
}