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

496 lines
19 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
/**
* CategoryController.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-04-08 02:18:04 -05:00
declare(strict_types=1);
2015-05-16 02:41:14 -05:00
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
2016-12-15 06:47:28 -06:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
2015-05-16 02:41:14 -05:00
use FireflyIII\Http\Controllers\Controller;
2016-07-02 10:39:58 -05:00
use FireflyIII\Models\AccountType;
2015-05-16 02:41:14 -05:00
use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionCurrency;
2016-10-10 00:49:39 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-02-24 22:57:01 -06:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2015-06-03 11:22:47 -05:00
use FireflyIII\Support\CacheProperties;
2018-10-07 02:45:50 -05:00
use FireflyIII\Support\Http\Controllers\DateCalculation;
2018-07-08 05:28:42 -05:00
use Illuminate\Http\JsonResponse;
2015-06-27 04:44:18 -05:00
use Illuminate\Support\Collection;
2018-11-11 13:09:35 -06:00
use Log;
2015-05-16 02:41:14 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class CategoryController.
2015-05-16 02:41:14 -05:00
*/
class CategoryController extends Controller
{
2018-10-07 02:45:50 -05:00
use DateCalculation;
2018-07-21 01:06:24 -05:00
/** @var GeneratorInterface Chart generation methods. */
2015-06-27 04:44:18 -05:00
protected $generator;
/**
2018-07-21 01:06:24 -05:00
* CategoryController constructor.
2015-06-27 04:44:18 -05:00
*/
public function __construct()
{
parent::__construct();
// create chart generator:
2016-12-15 06:47:28 -06:00
$this->generator = app(GeneratorInterface::class);
2015-06-27 04:44:18 -05:00
}
2015-05-16 02:41:14 -05:00
2018-07-08 05:08:53 -05:00
2015-05-16 02:41:14 -05:00
/**
2015-05-16 03:13:41 -05:00
* Show an overview for a category for all time, per month/week/year.
2015-05-16 02:41:14 -05:00
*
* TODO this chart is not multi-currency aware.
*
2017-02-24 22:57:01 -06:00
* @param CategoryRepositoryInterface $repository
* @param AccountRepositoryInterface $accountRepository
* @param Category $category
2015-05-16 02:41:14 -05:00
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2018-07-17 15:21:03 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2015-05-16 02:41:14 -05:00
*/
2018-07-08 05:28:42 -05:00
public function all(CategoryRepositoryInterface $repository, AccountRepositoryInterface $accountRepository, Category $category): JsonResponse
2015-05-16 02:41:14 -05:00
{
2016-12-15 06:47:28 -06:00
$cache = new CacheProperties;
$cache->addProperty('chart.category.all');
$cache->addProperty($category->id);
2015-06-27 04:44:18 -05:00
if ($cache->has()) {
2018-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2015-06-27 04:44:18 -05:00
}
2018-11-11 13:09:35 -06:00
$start = $repository->firstUseDate($category);
$start = $start ?? new Carbon;
$range = app('preferences')->get('viewRange', '1M')->data;
$start = app('navigation')->startOfPeriod($start, $range);
$end = new Carbon;
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
Log::debug(sprintf('Full range is %s to %s', $start->format('Y-m-d'), $end->format('Y-m-d')));
2016-12-15 06:47:28 -06:00
$chartData = [
[
2018-08-13 11:09:47 -05:00
'label' => (string)trans('firefly.spent'),
'entries' => [], 'type' => 'bar',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
2016-12-15 06:47:28 -06:00
],
[
2018-08-13 11:09:47 -05:00
'label' => (string)trans('firefly.earned'),
'entries' => [], 'type' => 'bar',
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
2016-12-15 06:47:28 -06:00
],
2017-04-08 02:18:04 -05:00
[
2018-04-02 08:10:40 -05:00
'label' => (string)trans('firefly.sum'),
2018-07-17 15:21:03 -05:00
'entries' => [], 'type' => 'line', 'fill' => false,
2017-04-08 02:18:04 -05:00
],
2016-12-15 06:47:28 -06:00
];
2018-10-07 02:45:50 -05:00
$step = $this->calculateStep($start, $end);
2018-11-11 13:09:35 -06:00
$current = clone $start;
Log::debug(sprintf('abc Step is %s', $step));
2018-10-07 02:45:50 -05:00
switch ($step) {
case '1D':
while ($current <= $end) {
2018-11-11 13:09:35 -06:00
Log::debug(sprintf('Current day is %s', $current->format('Y-m-d')));
2018-10-07 02:45:50 -05:00
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $current, $current);
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $current, $current);
$sum = bcadd($spent, $earned);
$label = app('navigation')->periodShow($current, $step);
$chartData[0]['entries'][$label] = round(bcmul($spent, '-1'), 12);
$chartData[1]['entries'][$label] = round($earned, 12);
$chartData[2]['entries'][$label] = round($sum, 12);
$current->addDay();
}
break;
case '1W':
case '1M':
case '1Y':
while ($current <= $end) {
2018-11-11 13:09:35 -06:00
$currentEnd = app('navigation')->endOfPeriod($current, $step);
Log::debug(sprintf('abc Range is %s to %s', $current->format('Y-m-d'), $currentEnd->format('Y-m-d')));
2018-10-07 02:45:50 -05:00
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $current, $currentEnd);
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $current, $currentEnd);
$sum = bcadd($spent, $earned);
$label = app('navigation')->periodShow($current, $step);
$chartData[0]['entries'][$label] = round(bcmul($spent, '-1'), 12);
$chartData[1]['entries'][$label] = round($earned, 12);
$chartData[2]['entries'][$label] = round($sum, 12);
2018-11-11 13:09:35 -06:00
$current = app('navigation')->addPeriod($current, $step, 0);
2018-10-07 02:45:50 -05:00
}
break;
2015-05-16 02:41:14 -05:00
}
2016-12-15 06:47:28 -06:00
$data = $this->generator->multiSet($chartData);
2015-06-27 04:44:18 -05:00
$cache->store($data);
2015-05-16 02:41:14 -05:00
2018-03-10 13:30:09 -06:00
return response()->json($data);
2015-05-16 02:41:14 -05:00
}
2018-07-08 05:08:53 -05:00
2015-05-16 02:41:14 -05:00
/**
2018-07-21 01:06:24 -05:00
* Shows the category chart on the front page.
*
2017-02-24 22:57:01 -06:00
* @param CategoryRepositoryInterface $repository
* @param AccountRepositoryInterface $accountRepository
2015-05-16 02:41:14 -05:00
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2015-05-16 02:41:14 -05:00
*/
2018-07-08 05:28:42 -05:00
public function frontpage(CategoryRepositoryInterface $repository, AccountRepositoryInterface $accountRepository): JsonResponse
2015-05-16 02:41:14 -05:00
{
2016-02-04 00:27:03 -06:00
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
// chart properties for cache:
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-15 06:47:28 -06:00
$cache->addProperty('chart.category.frontpage');
2015-06-03 14:25:11 -05:00
if ($cache->has()) {
2018-10-07 02:45:50 -05:00
return response()->json($cache->get()); // @codeCoverageIgnore
2016-05-09 13:15:26 -05:00
}
// currency repos:
/** @var CurrencyRepositoryInterface $currencyRepository */
$currencyRepository = app(CurrencyRepositoryInterface::class);
$currencies = [];
2016-12-15 06:47:28 -06:00
$chartData = [];
$tempData = [];
2016-05-09 13:15:26 -05:00
$categories = $repository->getCategories();
2016-10-10 00:49:39 -05:00
$accounts = $accountRepository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
2016-05-09 13:15:26 -05:00
/** @var Category $category */
foreach ($categories as $category) {
$spentArray = $repository->spentInPeriodPerCurrency(new Collection([$category]), $accounts, $start, $end);
foreach ($spentArray as $currencyId => $spent) {
if (bccomp($spent, '0') === -1) {
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepository->findNull($currencyId);
$tempData[] = [
'name' => $category->name,
'spent' => bcmul($spent, '-1'),
'spent_float' => (float)bcmul($spent, '-1'),
'currency_id' => $currencyId,
];
}
2016-05-09 13:15:26 -05:00
}
}
// no category per currency:
$noCategory = $repository->spentInPeriodPcWoCategory(new Collection, $start, $end);
foreach ($noCategory as $currencyId => $spent) {
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepository->findNull($currencyId);
2018-10-07 02:45:50 -05:00
$tempData[] = [
'name' => trans('firefly.no_category'),
'spent' => bcmul($spent, '-1'),
'spent_float' => (float)bcmul($spent, '-1'),
'currency_id' => $currencyId,
];
}
// sort temp array by amount.
$amounts = array_column($tempData, 'spent_float');
array_multisort($amounts, SORT_DESC, $tempData);
// loop all found currencies and build the data array for the chart.
/**
* @var int $currencyId
* @var TransactionCurrency $currency
*/
foreach ($currencies as $currencyId => $currency) {
$dataSet = [
'label' => (string)trans('firefly.spent'),
'type' => 'bar',
'currency_symbol' => $currency->symbol,
'entries' => $this->expandNames($tempData),
];
$chartData[$currencyId] = $dataSet;
}
// loop temp data and place data in correct array:
foreach ($tempData as $entry) {
$currencyId = $entry['currency_id'];
$name = $entry['name'];
$chartData[$currencyId]['entries'][$name] = $entry['spent'];
}
$data = $this->generator->multiSet($chartData);
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
}
2016-12-03 14:48:40 -06:00
/**
2018-07-21 01:06:24 -05:00
* Chart report.
*
* TODO this chart is not multi-currency aware.
*
2018-07-08 05:08:53 -05:00
* @param Category $category
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-12-06 00:48:41 -06:00
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2018-07-17 15:21:03 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2016-12-03 14:48:40 -06:00
*/
2018-07-08 05:28:42 -05:00
public function reportPeriod(Category $category, Collection $accounts, Carbon $start, Carbon $end): JsonResponse
2016-12-03 14:48:40 -06:00
{
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-15 06:47:28 -06:00
$cache->addProperty('chart.category.period');
2016-12-03 14:48:40 -06:00
$cache->addProperty($accounts->pluck('id')->toArray());
$cache->addProperty($category);
if ($cache->has()) {
2018-07-26 20:09:35 -05:00
return response()->json($cache->get());// @codeCoverageIgnore
2016-12-03 14:48:40 -06:00
}
2018-07-08 05:08:53 -05:00
$repository = app(CategoryRepositoryInterface::class);
$expenses = $repository->periodExpenses(new Collection([$category]), $accounts, $start, $end);
$income = $repository->periodIncome(new Collection([$category]), $accounts, $start, $end);
$periods = app('navigation')->listOfPeriods($start, $end);
$chartData = [
2016-12-15 06:47:28 -06:00
[
2018-08-13 11:09:47 -05:00
'label' => (string)trans('firefly.spent'),
'entries' => [],
'type' => 'bar',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
2016-12-15 06:47:28 -06:00
],
[
2018-08-13 11:09:47 -05:00
'label' => (string)trans('firefly.earned'),
'entries' => [],
'type' => 'bar',
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
2016-12-15 06:47:28 -06:00
],
2017-04-08 02:18:04 -05:00
[
2018-04-02 08:10:40 -05:00
'label' => (string)trans('firefly.sum'),
2017-04-08 02:18:04 -05:00
'entries' => [],
'type' => 'line',
'fill' => false,
],
2016-12-15 06:47:28 -06:00
];
2016-12-03 14:48:40 -06:00
foreach (array_keys($periods) as $period) {
2016-12-15 06:47:28 -06:00
$label = $periods[$period];
$spent = $expenses[$category->id]['entries'][$period] ?? '0';
2017-04-08 02:18:04 -05:00
$earned = $income[$category->id]['entries'][$period] ?? '0';
$sum = bcadd($spent, $earned);
2017-04-23 02:33:33 -05:00
$chartData[0]['entries'][$label] = round(bcmul($spent, '-1'), 12);
$chartData[1]['entries'][$label] = round($earned, 12);
$chartData[2]['entries'][$label] = round($sum, 12);
2016-12-06 00:48:41 -06:00
}
2016-12-15 06:47:28 -06:00
$data = $this->generator->multiSet($chartData);
$cache->store($data);
2016-12-06 00:48:41 -06:00
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-12-06 00:48:41 -06:00
}
2018-07-08 05:08:53 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
2016-12-06 00:48:41 -06:00
/**
2018-07-21 01:06:24 -05:00
* Chart for period for transactions without a category.
*
* TODO this chart is not multi-currency aware.
*
2018-07-08 05:08:53 -05:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-12-06 00:48:41 -06:00
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2018-07-17 15:21:03 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2016-12-06 00:48:41 -06:00
*/
2018-07-08 05:28:42 -05:00
public function reportPeriodNoCategory(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
2016-12-06 00:48:41 -06:00
{
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
2016-12-15 06:47:28 -06:00
$cache->addProperty('chart.category.period.no-cat');
2016-12-06 00:48:41 -06:00
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
2018-07-26 20:09:35 -05:00
return response()->json($cache->get()); // @codeCoverageIgnore
2016-12-06 00:48:41 -06:00
}
2018-07-08 05:08:53 -05:00
$repository = app(CategoryRepositoryInterface::class);
$expenses = $repository->periodExpensesNoCategory($accounts, $start, $end);
$income = $repository->periodIncomeNoCategory($accounts, $start, $end);
$periods = app('navigation')->listOfPeriods($start, $end);
$chartData = [
2016-12-15 06:47:28 -06:00
[
2018-08-13 11:09:47 -05:00
'label' => (string)trans('firefly.spent'),
'entries' => [],
'type' => 'bar',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
2016-12-15 06:47:28 -06:00
],
[
2018-08-13 11:09:47 -05:00
'label' => (string)trans('firefly.earned'),
'entries' => [],
'type' => 'bar',
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
2016-12-15 06:47:28 -06:00
],
2017-04-08 02:18:04 -05:00
[
2018-04-02 08:10:40 -05:00
'label' => (string)trans('firefly.sum'),
2017-04-08 02:18:04 -05:00
'entries' => [],
'type' => 'line',
'fill' => false,
],
2016-12-15 06:47:28 -06:00
];
2016-12-06 00:48:41 -06:00
foreach (array_keys($periods) as $period) {
2016-12-15 06:47:28 -06:00
$label = $periods[$period];
$spent = $expenses['entries'][$period] ?? '0';
2017-04-08 02:18:04 -05:00
$earned = $income['entries'][$period] ?? '0';
$sum = bcadd($spent, $earned);
2016-12-15 06:47:28 -06:00
$chartData[0]['entries'][$label] = bcmul($spent, '-1');
2017-04-08 02:18:04 -05:00
$chartData[1]['entries'][$label] = $earned;
$chartData[2]['entries'][$label] = $sum;
2016-12-03 14:48:40 -06:00
}
2016-12-15 06:47:28 -06:00
$data = $this->generator->multiSet($chartData);
$cache->store($data);
2016-12-03 14:48:40 -06:00
2018-03-10 13:30:09 -06:00
return response()->json($data);
2016-12-03 14:48:40 -06:00
}
2016-04-30 15:30:11 -05:00
/**
2018-07-21 01:06:24 -05:00
* Chart for a specific period.
*
* TODO this chart is not multi-currency aware.
*
* @param Category $category
* @param $date
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
*/
2018-07-08 05:28:42 -05:00
public function specificPeriod(Category $category, Carbon $date): JsonResponse
{
$range = app('preferences')->get('viewRange', '1M')->data;
$start = app('navigation')->startOfPeriod($date, $range);
$end = app('navigation')->endOfPeriod($date, $range);
2018-07-08 05:08:53 -05:00
$data = $this->makePeriodChart($category, $start, $end);
2015-05-16 02:41:14 -05:00
2018-03-10 13:30:09 -06:00
return response()->json($data);
2015-05-16 02:41:14 -05:00
}
/**
2018-07-21 01:06:24 -05:00
* Chart for a specific period (start and end).
*
*
2018-07-08 05:08:53 -05:00
* @param Category $category
* @param Carbon $start
* @param Carbon $end
2016-01-15 12:37:09 -06:00
*
2016-01-29 00:33:49 -06:00
* @return array
2018-07-17 15:21:03 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function makePeriodChart(Category $category, Carbon $start, Carbon $end): array // chart helper method.
{
2016-12-15 06:47:28 -06:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
2016-01-29 00:33:49 -06:00
$cache->addProperty($category->id);
2016-12-15 06:47:28 -06:00
$cache->addProperty('chart.category.period-chart');
2016-05-09 13:15:26 -05:00
2016-07-02 10:42:27 -05:00
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
}
2016-12-15 06:47:28 -06:00
2018-07-08 05:08:53 -05:00
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$repository = app(CategoryRepositoryInterface::class);
2016-12-15 06:47:28 -06:00
// chart data
$chartData = [
[
2018-08-13 11:09:47 -05:00
'label' => (string)trans('firefly.spent'),
'entries' => [],
'type' => 'bar',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
2016-12-15 06:47:28 -06:00
],
[
2018-08-13 11:09:47 -05:00
'label' => (string)trans('firefly.earned'),
'entries' => [],
'type' => 'bar',
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
2016-12-15 06:47:28 -06:00
],
2017-04-08 02:18:04 -05:00
[
2018-04-02 08:10:40 -05:00
'label' => (string)trans('firefly.sum'),
2017-04-08 02:18:04 -05:00
'entries' => [],
'type' => 'line',
'fill' => false,
],
2016-12-15 06:47:28 -06:00
];
2018-11-11 13:09:35 -06:00
$step = $this->calculateStep($start, $end);
2016-12-15 06:47:28 -06:00
2018-11-11 13:09:35 -06:00
while ($start <= $end) {
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $start);
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $start, $start);
$sum = bcadd($spent, $earned);
$label = trim(app('navigation')->periodShow($start, $step));
2017-06-05 04:12:50 -05:00
$chartData[0]['entries'][$label] = round(bcmul($spent, '-1'), 12);
$chartData[1]['entries'][$label] = round($earned, 12);
$chartData[2]['entries'][$label] = round($sum, 12);
2016-12-15 06:47:28 -06:00
2018-11-11 13:09:35 -06:00
switch ($step) {
default:
case '1D':
$start->addDay();
break;
case '1W':
$start->addDays(7);
break;
case '1M':
$start->addMonth();
break;
case '1Y':
$start->addYear();
break;
}
}
2016-01-29 00:33:49 -06:00
2016-12-15 06:47:28 -06:00
$data = $this->generator->multiSet($chartData);
$cache->store($data);
return $data;
}
/**
* Small helper function for the revenue and expense account charts.
*
* @param array $names
*
* @return array
*/
private function expandNames(array $names): array
{
$result = [];
foreach ($names as $entry) {
$result[$entry['name']] = 0;
}
return $result;
}
2015-05-20 12:56:14 -05:00
}