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

200 lines
7.6 KiB
PHP
Raw Normal View History

2017-12-12 11:22:29 -06:00
<?php
/**
* ExpenseReportController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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-12-12 11:22:29 -06:00
*/
2018-07-08 05:08:53 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
2017-12-12 11:22:29 -06:00
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
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;
2017-12-12 11:22:29 -06:00
use Illuminate\Support\Collection;
/**
* Separate controller because many helper functions are shared.
*
* Class ExpenseReportController
*/
class ExpenseReportController extends Controller
{
use AugumentData, TransactionCalculation;
/** @var AccountRepositoryInterface The account repository */
2017-12-12 11:22:29 -06:00
protected $accountRepository;
2018-07-21 01:06:24 -05:00
/** @var GeneratorInterface Chart generation methods. */
2017-12-12 11:22:29 -06:00
protected $generator;
/**
2018-07-22 01:10:16 -05:00
* ExpenseReportController constructor.
2017-12-12 11:22:29 -06:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->generator = app(GeneratorInterface::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
return $next($request);
}
);
}
2018-07-17 15:21:03 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
2017-12-12 11:22:29 -06:00
/**
2018-07-21 01:06:24 -05:00
* Main chart that shows income and expense for a combination of expense/revenue accounts.
*
* TODO this chart is not multi-currency aware.
*
2017-12-12 11:22:29 -06:00
* @param Collection $accounts
* @param Collection $expense
2019-05-31 06:35:33 -05:00
* @param Carbon $start
* @param Carbon $end
2017-12-12 11:22:29 -06:00
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2018-07-17 15:21:03 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
2017-12-12 11:22:29 -06:00
*/
2018-07-08 05:28:42 -05:00
public function mainChart(Collection $accounts, Collection $expense, Carbon $start, Carbon $end): JsonResponse
2017-12-12 11:22:29 -06:00
{
$cache = new CacheProperties;
$cache->addProperty('chart.expense.report.main');
$cache->addProperty($accounts);
$cache->addProperty($expense);
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
2019-05-31 06:35:33 -05:00
// return response()->json($cache->get()); // @codeCoverageIgnore
2017-12-12 11:22:29 -06:00
}
$format = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
$function = app('navigation')->preferredEndOfPeriod($start, $end);
2017-12-12 11:22:29 -06:00
$chartData = [];
$currentStart = clone $start;
$combined = $this->combineAccounts($expense);
// make "all" set:
$all = new Collection;
2019-05-31 06:35:33 -05:00
foreach ($combined as $name => $combination) {
$all = $all->merge($combination);
2017-12-12 11:22:29 -06:00
}
// prep chart data:
/**
2019-05-31 06:35:33 -05:00
* @var string $name
* @var Collection $combination
*/
2019-05-31 06:35:33 -05:00
foreach ($combined as $name => $combination) {
2017-12-12 11:22:29 -06:00
// first is always expense account:
/** @var Account $exp */
2019-05-31 06:35:33 -05:00
$exp = $combination->first();
2017-12-12 11:22:29 -06:00
$chartData[$exp->id . '-in'] = [
2019-05-31 06:35:33 -05:00
'label' => sprintf('%s (%s)', $name, strtolower((string)trans('firefly.income'))),
2017-12-12 11:22:29 -06:00
'type' => 'bar',
'yAxisID' => 'y-axis-0',
'entries' => [],
];
$chartData[$exp->id . '-out'] = [
2019-05-31 06:35:33 -05:00
'label' => sprintf('%s (%s)', $name, strtolower((string)trans('firefly.expenses'))),
2017-12-12 11:22:29 -06:00
'type' => 'bar',
'yAxisID' => 'y-axis-0',
'entries' => [],
];
// total in, total out:
$chartData[$exp->id . '-total-in'] = [
2019-05-31 06:35:33 -05:00
'label' => sprintf('%s (%s)', $name, strtolower((string)trans('firefly.sum_of_income'))),
2017-12-12 11:22:29 -06:00
'type' => 'line',
'fill' => false,
'yAxisID' => 'y-axis-1',
'entries' => [],
];
$chartData[$exp->id . '-total-out'] = [
2019-05-31 06:35:33 -05:00
'label' => sprintf('%s (%s)', $name, strtolower((string)trans('firefly.sum_of_expenses'))),
2017-12-12 11:22:29 -06:00
'type' => 'line',
'fill' => false,
'yAxisID' => 'y-axis-1',
'entries' => [],
];
}
$sumOfIncome = [];
$sumOfExpense = [];
while ($currentStart < $end) {
$currentEnd = clone $currentStart;
$currentEnd = $currentEnd->$function();
// get expenses grouped by opposing name:
$expenses = $this->groupByName($this->getExpensesForOpposing($accounts, $all, $currentStart, $currentEnd));
$income = $this->groupByName($this->getIncomeForOpposing($accounts, $all, $currentStart, $currentEnd));
2017-12-12 11:22:29 -06:00
$label = $currentStart->formatLocalized($format);
2019-05-31 06:35:33 -05:00
foreach ($combined as $name => $combination) {
2017-12-12 11:22:29 -06:00
// first is always expense account:
/** @var Account $exp */
2019-05-31 06:35:33 -05:00
$exp = $combination->first();
2017-12-12 11:22:29 -06:00
$labelIn = $exp->id . '-in';
$labelOut = $exp->id . '-out';
$labelSumIn = $exp->id . '-total-in';
$labelSumOut = $exp->id . '-total-out';
2019-05-31 06:35:33 -05:00
$currentIncome = bcmul($income[$name] ?? '0', '-1');
2017-12-12 11:22:29 -06:00
$currentExpense = $expenses[$name] ?? '0';
// add to sum:
$sumOfIncome[$exp->id] = $sumOfIncome[$exp->id] ?? '0';
$sumOfExpense[$exp->id] = $sumOfExpense[$exp->id] ?? '0';
$sumOfIncome[$exp->id] = bcadd($sumOfIncome[$exp->id], $currentIncome);
$sumOfExpense[$exp->id] = bcadd($sumOfExpense[$exp->id], $currentExpense);
// add to chart:
$chartData[$labelIn]['entries'][$label] = $currentIncome;
$chartData[$labelOut]['entries'][$label] = $currentExpense;
$chartData[$labelSumIn]['entries'][$label] = $sumOfIncome[$exp->id];
$chartData[$labelSumOut]['entries'][$label] = $sumOfExpense[$exp->id];
}
2018-07-09 12:24:08 -05:00
/** @var Carbon $currentStart */
2017-12-12 11:22:29 -06:00
$currentStart = clone $currentEnd;
$currentStart->addDay();
2019-05-31 06:35:33 -05:00
$currentStart->startOfDay();
2017-12-12 11:22:29 -06:00
}
// remove all empty entries to prevent cluttering:
$newSet = [];
foreach ($chartData as $key => $entry) {
if (0 === !array_sum($entry['entries'])) {
$newSet[$key] = $chartData[$key];
}
}
if (0 === count($newSet)) {
2017-12-12 11:22:29 -06:00
$newSet = $chartData; // @codeCoverageIgnore
}
$data = $this->generator->multiSet($newSet);
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
2017-12-12 11:22:29 -06:00
}
}