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

303 lines
11 KiB
PHP
Raw Normal View History

<?php
/**
* CategoryReportController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05: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 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2017-03-25 07:41:17 -05:00
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
2016-12-15 06:47:28 -06:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Chart\MetaPieChartInterface;
use FireflyIII\Http\Controllers\Controller;
2016-11-12 13:29:16 -06:00
use FireflyIII\Models\Category;
2016-12-15 06:47:28 -06:00
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Controllers\AugumentData;
use FireflyIII\Support\Http\Controllers\TransactionCalculation;
2018-07-08 05:28:42 -05: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 01:06:24 -05:00
/** @var GeneratorInterface Chart generation methods. */
private $generator;
/**
2018-07-21 01:06:24 -05:00
* CategoryReportController constructor.
2019-06-29 01:14:28 -05:00
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2017-03-25 07:41:17 -05:00
$this->generator = app(GeneratorInterface::class);
return $next($request);
}
);
}
2018-07-08 05:08:53 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
/**
2018-07-21 01:06:24 -05: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 05:28:42 -05:00
* @return JsonResponse
2018-07-17 15:21:03 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
2018-07-08 05:28:42 -05: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 08:10:40 -05:00
$helper->setAccounts($accounts)->setCategories($categories)->setStart($start)->setEnd($end)->setCollectOtherObjects(1 === (int)$others);
2017-03-12 03:22:33 -05:00
$chartData = $helper->generate('expense', 'account');
$data = $this->generator->pieChart($chartData);
2018-03-10 13:30:09 -06:00
return response()->json($data);
}
2018-07-08 05:08:53 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
/**
2018-07-21 01:06:24 -05: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 05:28:42 -05:00
* @return JsonResponse
2018-07-17 15:21:03 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
2018-07-08 05:28:42 -05: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 08:10:40 -05:00
$helper->setCollectOtherObjects(1 === (int)$others);
$chartData = $helper->generate('income', 'account');
$data = $this->generator->pieChart($chartData);
2018-03-10 13:30:09 -06:00
return response()->json($data);
}
2018-07-08 05:08:53 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
/**
2018-07-21 01:06:24 -05: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 05:28:42 -05:00
* @return JsonResponse
2018-07-17 15:21:03 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
2018-07-08 05:28:42 -05: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 08:10:40 -05:00
$helper->setCollectOtherObjects(1 === (int)$others);
$chartData = $helper->generate('expense', 'category');
$data = $this->generator->pieChart($chartData);
2018-03-10 13:30:09 -06:00
return response()->json($data);
}
2018-07-08 05:08:53 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
/**
2018-07-21 01:06:24 -05: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 05:28:42 -05:00
* @return JsonResponse
2018-07-17 15:21:03 -05:00
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
2018-07-08 05:28:42 -05: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 08:10:40 -05:00
$helper->setCollectOtherObjects(1 === (int)$others);
$chartData = $helper->generate('income', 'category');
$data = $this->generator->pieChart($chartData);
2018-03-10 13:30:09 -06:00
return response()->json($data);
}
2018-07-08 05:08:53 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
2016-11-12 13:29:16 -06:00
/**
2018-07-21 01:06:24 -05:00
* Main report category chart.
*
* TODO this chart is not multi-currency aware.
*
2016-11-12 13:29:16 -06:00
* @param Collection $accounts
* @param Collection $categories
* @param Carbon $start
* @param Carbon $end
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2018-07-17 15:21:03 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2016-11-12 13:29:16 -06:00
*/
2018-07-08 05:28:42 -05:00
public function mainChart(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
2016-11-12 13:29:16 -06:00
{
2016-12-15 06:47:28 -06: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()) {
2019-05-29 23:23:25 -05:00
//return response()->json($cache->get()); // @codeCoverageIgnore
2016-11-12 13:29:16 -06:00
}
2016-12-15 06:47:28 -06:00
$format = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
$function = app('navigation')->preferredEndOfPeriod($start, $end);
2016-12-15 06:47:28 -06:00
$chartData = [];
2016-11-25 09:52:43 -06:00
$currentStart = clone $start;
2016-12-15 06:47:28 -06:00
// prep chart data:
foreach ($categories as $category) {
$chartData[$category->id . '-in'] = [
2018-04-02 08:10:40 -05:00
'label' => $category->name . ' (' . strtolower((string)trans('firefly.income')) . ')',
2016-12-15 06:47:28 -06:00
'type' => 'bar',
2016-12-23 00:20:47 -06:00
'yAxisID' => 'y-axis-0',
2016-12-15 06:47:28 -06:00
'entries' => [],
];
$chartData[$category->id . '-out'] = [
2018-04-02 08:10:40 -05:00
'label' => $category->name . ' (' . strtolower((string)trans('firefly.expenses')) . ')',
2016-12-15 06:47:28 -06:00
'type' => 'bar',
2016-12-23 00:20:47 -06:00
'yAxisID' => 'y-axis-0',
'entries' => [],
];
// total in, total out:
$chartData[$category->id . '-total-in'] = [
2018-04-02 08:10:40 -05:00
'label' => $category->name . ' (' . strtolower((string)trans('firefly.sum_of_income')) . ')',
2016-12-23 00:20:47 -06:00
'type' => 'line',
'fill' => false,
'yAxisID' => 'y-axis-1',
'entries' => [],
];
$chartData[$category->id . '-total-out'] = [
2018-04-02 08:10:40 -05:00
'label' => $category->name . ' (' . strtolower((string)trans('firefly.sum_of_expenses')) . ')',
2016-12-23 00:20:47 -06:00
'type' => 'line',
'fill' => false,
'yAxisID' => 'y-axis-1',
2016-12-15 06:47:28 -06:00
'entries' => [],
];
}
2016-12-23 00:20:47 -06:00
$sumOfIncome = [];
$sumOfExpense = [];
2016-12-15 06:47:28 -06:00
2016-11-25 09:52:43 -06: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 06:47:28 -06:00
$label = $currentStart->formatLocalized($format);
2016-11-12 13:29:16 -06:00
/** @var Category $category */
foreach ($categories as $category) {
2016-12-23 00:20:47 -06:00
$labelIn = $category->id . '-in';
$labelOut = $category->id . '-out';
$labelSumIn = $category->id . '-total-in';
$labelSumOut = $category->id . '-total-out';
2019-05-29 23:23:25 -05:00
$currentIncome = bcmul($income[$category->id] ?? '0','-1');
2016-12-23 00:20:47 -06:00
$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 13:29:16 -06:00
}
2018-07-09 12:24:08 -05:00
/** @var Carbon $currentStart */
2016-11-25 09:52:43 -06:00
$currentStart = clone $currentEnd;
2016-11-26 02:01:00 -06:00
$currentStart->addDay();
2019-05-29 23:23:25 -05:00
$currentStart->startOfDay();
2016-11-12 13:29:16 -06:00
}
2016-12-23 00:20:47 -06:00
// remove all empty entries to prevent cluttering:
2016-12-23 08:52:12 -06:00
$newSet = [];
2016-12-23 00:20:47 -06:00
foreach ($chartData as $key => $entry) {
2017-11-15 05:25:49 -06:00
if (0 === !array_sum($entry['entries'])) {
2019-06-29 01:14:28 -05:00
$newSet[$key] = $chartData[$key]; // @codeCoverageIgnore
2016-12-23 00:20:47 -06:00
}
}
2019-05-29 23:23:25 -05:00
if (0 === count($newSet)) {
2016-12-23 08:52:12 -06:00
$newSet = $chartData;
}
$data = $this->generator->multiSet($newSet);
2016-12-15 06:47:28 -06:00
$cache->store($data);
2016-11-20 04:43:19 -06:00
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-11-12 13:29:16 -06:00
}
}