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

301 lines
11 KiB
PHP
Raw Normal View History

<?php
/**
* CategoryReportController.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/>.
*/
2017-03-25 13:41:17 +01:00
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
2016-12-15 13:47:28 +01:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Chart\MetaPieChartInterface;
use FireflyIII\Http\Controllers\Controller;
2016-11-12 20:29:16 +01:00
use FireflyIII\Models\Category;
2016-12-15 13:47:28 +01:00
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Controllers\AugumentData;
use FireflyIII\Support\Http\Controllers\TransactionCalculation;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
/**
* Separate controller because many helper functions are shared.
*
* Class CategoryReportController
*/
class CategoryReportController extends Controller
{
use AugumentData, TransactionCalculation;
2018-07-21 08:06:24 +02:00
/** @var GeneratorInterface Chart generation methods. */
private $generator;
/**
2018-07-21 08:06:24 +02:00
* CategoryReportController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2017-03-25 13:41:17 +01:00
$this->generator = app(GeneratorInterface::class);
return $next($request);
}
);
}
2018-07-08 12:08:53 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
/**
2018-07-21 08:06:24 +02:00
* Chart for expenses grouped by expense account.
*
* TODO this chart is not multi-currency aware.
*
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
* @param string $others
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2018-07-17 22:21:03 +02:00
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
2018-07-08 12:28:42 +02:00
public function accountExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
{
/** @var MetaPieChartInterface $helper */
$helper = app(MetaPieChartInterface::class);
2018-04-02 15:10:40 +02:00
$helper->setAccounts($accounts)->setCategories($categories)->setStart($start)->setEnd($end)->setCollectOtherObjects(1 === (int)$others);
2017-03-12 09:22:33 +01:00
$chartData = $helper->generate('expense', 'account');
$data = $this->generator->pieChart($chartData);
2018-03-10 20:30:09 +01:00
return response()->json($data);
}
2018-07-08 12:08:53 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
/**
2018-07-21 08:06:24 +02:00
* Chart for income grouped by revenue account.
*
* TODO this chart is not multi-currency aware.
*
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
* @param string $others
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2018-07-17 22:21:03 +02:00
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
2018-07-08 12:28:42 +02:00
public function accountIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
{
/** @var MetaPieChartInterface $helper */
$helper = app(MetaPieChartInterface::class);
$helper->setAccounts($accounts);
$helper->setCategories($categories);
$helper->setStart($start);
$helper->setEnd($end);
2018-04-02 15:10:40 +02:00
$helper->setCollectOtherObjects(1 === (int)$others);
$chartData = $helper->generate('income', 'account');
$data = $this->generator->pieChart($chartData);
2018-03-10 20:30:09 +01:00
return response()->json($data);
}
2018-07-08 12:08:53 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
/**
2018-07-21 08:06:24 +02:00
* Chart for expenses grouped by expense account.
*
* TODO this chart is not multi-currency aware.
*
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
* @param string $others
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2018-07-17 22:21:03 +02:00
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
2018-07-08 12:28:42 +02:00
public function categoryExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
{
/** @var MetaPieChartInterface $helper */
$helper = app(MetaPieChartInterface::class);
$helper->setAccounts($accounts);
$helper->setCategories($categories);
$helper->setStart($start);
$helper->setEnd($end);
2018-04-02 15:10:40 +02:00
$helper->setCollectOtherObjects(1 === (int)$others);
$chartData = $helper->generate('expense', 'category');
$data = $this->generator->pieChart($chartData);
2018-03-10 20:30:09 +01:00
return response()->json($data);
}
2018-07-08 12:08:53 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
/**
2018-07-21 08:06:24 +02:00
* Piechart for income grouped by account.
*
* TODO this chart is not multi-currency aware.
*
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
* @param string $others
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2018-07-17 22:21:03 +02:00
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
2018-07-08 12:28:42 +02:00
public function categoryIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
{
/** @var MetaPieChartInterface $helper */
$helper = app(MetaPieChartInterface::class);
$helper->setAccounts($accounts);
$helper->setCategories($categories);
$helper->setStart($start);
$helper->setEnd($end);
2018-04-02 15:10:40 +02:00
$helper->setCollectOtherObjects(1 === (int)$others);
$chartData = $helper->generate('income', 'category');
$data = $this->generator->pieChart($chartData);
2018-03-10 20:30:09 +01:00
return response()->json($data);
}
2018-07-08 12:08:53 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
2016-11-12 20:29:16 +01:00
/**
2018-07-21 08:06:24 +02:00
* Main report category chart.
*
* TODO this chart is not multi-currency aware.
*
2016-11-12 20:29:16 +01:00
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2018-07-17 22:21:03 +02:00
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2016-11-12 20:29:16 +01:00
*/
2018-07-08 12:28:42 +02:00
public function mainChart(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
2016-11-12 20:29:16 +01:00
{
2016-12-15 13:47:28 +01:00
$cache = new CacheProperties;
$cache->addProperty('chart.category.report.main');
$cache->addProperty($accounts);
$cache->addProperty($categories);
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
2018-03-10 20:30:09 +01:00
return response()->json($cache->get()); // @codeCoverageIgnore
2016-11-12 20:29:16 +01:00
}
2016-12-15 13:47:28 +01:00
$format = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
$function = app('navigation')->preferredEndOfPeriod($start, $end);
2016-12-15 13:47:28 +01:00
$chartData = [];
2016-11-25 16:52:43 +01:00
$currentStart = clone $start;
2016-12-15 13:47:28 +01:00
// prep chart data:
foreach ($categories as $category) {
$chartData[$category->id . '-in'] = [
2018-04-02 15:10:40 +02:00
'label' => $category->name . ' (' . strtolower((string)trans('firefly.income')) . ')',
2016-12-15 13:47:28 +01:00
'type' => 'bar',
2016-12-23 07:20:47 +01:00
'yAxisID' => 'y-axis-0',
2016-12-15 13:47:28 +01:00
'entries' => [],
];
$chartData[$category->id . '-out'] = [
2018-04-02 15:10:40 +02:00
'label' => $category->name . ' (' . strtolower((string)trans('firefly.expenses')) . ')',
2016-12-15 13:47:28 +01:00
'type' => 'bar',
2016-12-23 07:20:47 +01:00
'yAxisID' => 'y-axis-0',
'entries' => [],
];
// total in, total out:
$chartData[$category->id . '-total-in'] = [
2018-04-02 15:10:40 +02:00
'label' => $category->name . ' (' . strtolower((string)trans('firefly.sum_of_income')) . ')',
2016-12-23 07:20:47 +01:00
'type' => 'line',
'fill' => false,
'yAxisID' => 'y-axis-1',
'entries' => [],
];
$chartData[$category->id . '-total-out'] = [
2018-04-02 15:10:40 +02:00
'label' => $category->name . ' (' . strtolower((string)trans('firefly.sum_of_expenses')) . ')',
2016-12-23 07:20:47 +01:00
'type' => 'line',
'fill' => false,
'yAxisID' => 'y-axis-1',
2016-12-15 13:47:28 +01:00
'entries' => [],
];
}
2016-12-23 07:20:47 +01:00
$sumOfIncome = [];
$sumOfExpense = [];
2016-12-15 13:47:28 +01:00
2016-11-25 16:52:43 +01:00
while ($currentStart < $end) {
$currentEnd = clone $currentStart;
$currentEnd = $currentEnd->$function();
$expenses = $this->groupByCategory($this->getExpensesInCategories($accounts, $categories, $currentStart, $currentEnd));
$income = $this->groupByCategory($this->getIncomeForCategories($accounts, $categories, $currentStart, $currentEnd));
2016-12-15 13:47:28 +01:00
$label = $currentStart->formatLocalized($format);
2016-11-12 20:29:16 +01:00
/** @var Category $category */
foreach ($categories as $category) {
2016-12-23 07:20:47 +01:00
$labelIn = $category->id . '-in';
$labelOut = $category->id . '-out';
$labelSumIn = $category->id . '-total-in';
$labelSumOut = $category->id . '-total-out';
$currentIncome = $income[$category->id] ?? '0';
$currentExpense = $expenses[$category->id] ?? '0';
// add to sum:
$sumOfIncome[$category->id] = $sumOfIncome[$category->id] ?? '0';
$sumOfExpense[$category->id] = $sumOfExpense[$category->id] ?? '0';
$sumOfIncome[$category->id] = bcadd($sumOfIncome[$category->id], $currentIncome);
$sumOfExpense[$category->id] = bcadd($sumOfExpense[$category->id], $currentExpense);
// add to chart:
$chartData[$labelIn]['entries'][$label] = $currentIncome;
$chartData[$labelOut]['entries'][$label] = $currentExpense;
$chartData[$labelSumIn]['entries'][$label] = $sumOfIncome[$category->id];
$chartData[$labelSumOut]['entries'][$label] = $sumOfExpense[$category->id];
2016-11-12 20:29:16 +01:00
}
2018-07-09 19:24:08 +02:00
/** @var Carbon $currentStart */
2016-11-25 16:52:43 +01:00
$currentStart = clone $currentEnd;
2016-11-26 09:01:00 +01:00
$currentStart->addDay();
2016-11-12 20:29:16 +01:00
}
2016-12-23 07:20:47 +01:00
// remove all empty entries to prevent cluttering:
2016-12-23 15:52:12 +01:00
$newSet = [];
2016-12-23 07:20:47 +01:00
foreach ($chartData as $key => $entry) {
2017-11-15 12:25:49 +01:00
if (0 === !array_sum($entry['entries'])) {
2016-12-23 15:52:12 +01:00
$newSet[$key] = $chartData[$key];
2016-12-23 07:20:47 +01:00
}
}
2018-04-28 06:23:13 +02:00
if (0 === \count($newSet)) {
2016-12-23 15:52:12 +01:00
$newSet = $chartData;
}
$data = $this->generator->multiSet($newSet);
2016-12-15 13:47:28 +01:00
$cache->store($data);
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
}
}