2015-05-16 02:41:14 -05:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* CategoryController.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
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;
|
2019-08-12 11:18:51 -05:00
|
|
|
use Exception;
|
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;
|
|
|
|
use FireflyIII\Models\Category;
|
2017-02-24 22:57:01 -06:00
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
2019-08-27 03:52:07 -05:00
|
|
|
use FireflyIII\Repositories\Category\NoCategoryRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
|
2015-06-03 11:22:47 -05:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2020-03-20 23:26:39 -05:00
|
|
|
use FireflyIII\Support\Chart\Category\FrontpageChartGenerator;
|
2019-08-12 11:18:51 -05:00
|
|
|
use FireflyIII\Support\Chart\Category\WholePeriodChartGenerator;
|
2018-12-31 00:58:13 -06:00
|
|
|
use FireflyIII\Support\Http\Controllers\AugumentData;
|
2018-12-31 01:11:57 -06:00
|
|
|
use FireflyIII\Support\Http\Controllers\ChartGeneration;
|
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;
|
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-12-31 01:11:57 -06:00
|
|
|
use DateCalculation, AugumentData, ChartGeneration;
|
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.
|
2019-08-27 03:52:07 -05:00
|
|
|
*
|
2019-06-29 01:14:28 -05:00
|
|
|
* @codeCoverageIgnore
|
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.
|
2019-08-28 10:01:48 -05:00
|
|
|
* TODO test method, for category refactor.
|
2015-05-16 02:41:14 -05:00
|
|
|
*
|
2019-08-28 10:01:48 -05:00
|
|
|
* @param Category $category
|
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
|
|
|
*/
|
2019-08-28 10:01:48 -05:00
|
|
|
public function all(Category $category): JsonResponse
|
2015-05-16 02:41:14 -05:00
|
|
|
{
|
2019-08-12 11:18:51 -05:00
|
|
|
// cache results:
|
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()) {
|
2019-08-28 10:01:48 -05:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2015-06-27 04:44:18 -05:00
|
|
|
}
|
2019-08-28 10:01:48 -05:00
|
|
|
/** @var CategoryRepositoryInterface $repository */
|
|
|
|
$repository = app(CategoryRepositoryInterface::class);
|
|
|
|
$start = $repository->firstUseDate($category) ?? $this->getDate();
|
|
|
|
$range = app('preferences')->get('viewRange', '1M')->data;
|
|
|
|
$start = app('navigation')->startOfPeriod($start, $range);
|
|
|
|
$end = $this->getDate();
|
2018-11-11 13:09:35 -06:00
|
|
|
|
2019-08-12 11:18:51 -05:00
|
|
|
/** @var WholePeriodChartGenerator $generator */
|
|
|
|
$generator = app(WholePeriodChartGenerator::class);
|
|
|
|
$chartData = $generator->generate($category, $start, $end);
|
|
|
|
$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.
|
2019-08-28 10:01:48 -05:00
|
|
|
* TODO test method, for category refactor.
|
2018-07-21 01:06:24 -05:00
|
|
|
*
|
2018-07-08 05:28:42 -05:00
|
|
|
* @return JsonResponse
|
2015-05-16 02:41:14 -05:00
|
|
|
*/
|
2019-08-27 03:52:07 -05:00
|
|
|
public function frontPage(): 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());
|
2015-06-02 10:44:50 -05:00
|
|
|
// 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()) {
|
2020-07-02 22:42:57 -05:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2016-05-09 13:15:26 -05:00
|
|
|
}
|
2018-08-27 11:59:30 -05:00
|
|
|
|
2020-03-20 23:26:39 -05:00
|
|
|
$frontPageGenerator = new FrontpageChartGenerator($start, $end);
|
2019-08-27 03:52:07 -05:00
|
|
|
|
2020-02-23 05:42:28 -06:00
|
|
|
|
2020-03-20 23:26:39 -05:00
|
|
|
$chartData = $frontPageGenerator->generate();
|
|
|
|
$data = $this->generator->multiSet($chartData);
|
2015-12-16 06:08:26 -06:00
|
|
|
$cache->store($data);
|
|
|
|
|
2018-03-10 13:30:09 -06:00
|
|
|
return response()->json($data);
|
2015-12-16 06:08:26 -06:00
|
|
|
}
|
|
|
|
|
2016-12-03 14:48:40 -06:00
|
|
|
/**
|
2018-07-21 01:06:24 -05:00
|
|
|
* Chart report.
|
2019-08-28 10:01:48 -05:00
|
|
|
* TODO test method, for category refactor.
|
2018-07-21 01:06:24 -05:00
|
|
|
*
|
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
|
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()) {
|
2019-08-28 10:01:48 -05:00
|
|
|
return response()->json($cache->get());// @codeCoverageIgnore
|
2016-12-03 14:48:40 -06:00
|
|
|
}
|
2020-02-01 08:54:26 -06:00
|
|
|
$data = $this->reportPeriodChart($accounts, $start, $end, $category);
|
2019-08-27 03:52:07 -05:00
|
|
|
|
2016-12-15 06:47:28 -06:00
|
|
|
$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
|
|
|
|
2016-12-06 00:48:41 -06:00
|
|
|
/**
|
2018-07-21 01:06:24 -05:00
|
|
|
* Chart for period for transactions without a category.
|
2019-08-28 10:01:48 -05:00
|
|
|
* TODO test me.
|
2018-07-21 01:06:24 -05:00
|
|
|
*
|
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
|
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()) {
|
2019-09-01 11:41:57 -05:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2016-12-06 00:48:41 -06:00
|
|
|
}
|
2020-02-01 08:54:26 -06:00
|
|
|
$data = $this->reportPeriodChart($accounts, $start, $end, null);
|
2019-08-27 03:52:07 -05:00
|
|
|
|
2019-08-27 07:45:14 -05:00
|
|
|
$cache->store($data);
|
|
|
|
|
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.
|
2019-08-28 10:01:48 -05:00
|
|
|
* TODO test method, for category refactor.
|
2018-07-21 01:06:24 -05:00
|
|
|
*
|
2015-09-26 00:18:12 -05:00
|
|
|
* @param Category $category
|
2015-12-28 00:55:09 -06:00
|
|
|
* @param $date
|
|
|
|
*
|
2018-07-08 05:28:42 -05:00
|
|
|
* @return JsonResponse
|
2015-09-26 00:18:12 -05:00
|
|
|
*/
|
2018-07-08 05:28:42 -05:00
|
|
|
public function specificPeriod(Category $category, Carbon $date): JsonResponse
|
2015-09-26 00:18:12 -05:00
|
|
|
{
|
2018-07-14 09:08:34 -05:00
|
|
|
$range = app('preferences')->get('viewRange', '1M')->data;
|
2017-12-17 07:06:14 -06:00
|
|
|
$start = app('navigation')->startOfPeriod($date, $range);
|
2018-11-12 12:31:00 -06:00
|
|
|
$end = session()->get('end');
|
|
|
|
if ($end < $start) {
|
2019-06-29 01:14:28 -05:00
|
|
|
[$end, $start] = [$start, $end]; // @codeCoverageIgnore
|
2018-11-12 12:31:00 -06:00
|
|
|
}
|
|
|
|
|
2019-08-12 11:18:51 -05:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty($category->id);
|
|
|
|
$cache->addProperty('chart.category.period-chart');
|
|
|
|
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var GeneratorInterface $generator */
|
|
|
|
$generator = app(GeneratorInterface::class);
|
|
|
|
|
|
|
|
/** @var WholePeriodChartGenerator $chartGenerator */
|
|
|
|
$chartGenerator = app(WholePeriodChartGenerator::class);
|
|
|
|
$chartData = $chartGenerator->generate($category, $start, $end);
|
|
|
|
$data = $generator->multiSet($chartData);
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
2019-08-12 11:18:51 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
private function getDate(): Carbon
|
|
|
|
{
|
|
|
|
$carbon = null;
|
|
|
|
try {
|
|
|
|
$carbon = new Carbon;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $carbon;
|
|
|
|
}
|
2020-02-01 08:54:26 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate report chart for either with or without category.
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Category $category
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function reportPeriodChart(Collection $accounts, Carbon $start, Carbon $end, ?Category $category): array
|
|
|
|
{
|
|
|
|
$income = [];
|
|
|
|
$expenses = [];
|
|
|
|
$categoryId = 0;
|
|
|
|
if (null === $category) {
|
|
|
|
/** @var NoCategoryRepositoryInterface $noCatRepository */
|
|
|
|
$noCatRepository = app(NoCategoryRepositoryInterface::class);
|
|
|
|
|
|
|
|
// this gives us all currencies
|
|
|
|
$expenses = $noCatRepository->listExpenses($start, $end, $accounts);
|
|
|
|
$income = $noCatRepository->listIncome($start, $end, $accounts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $category) {
|
|
|
|
/** @var OperationsRepositoryInterface $opsRepository */
|
|
|
|
$opsRepository = app(OperationsRepositoryInterface::class);
|
2020-03-17 09:01:00 -05:00
|
|
|
$categoryId = (int) $category->id;
|
2020-02-01 08:54:26 -06:00
|
|
|
// this gives us all currencies
|
|
|
|
$collection = new Collection([$category]);
|
|
|
|
$expenses = $opsRepository->listExpenses($start, $end, null, $collection);
|
|
|
|
$income = $opsRepository->listIncome($start, $end, null, $collection);
|
|
|
|
|
|
|
|
}
|
|
|
|
$currencies = array_unique(array_merge(array_keys($income), array_keys($expenses)));
|
|
|
|
$periods = app('navigation')->listOfPeriods($start, $end);
|
|
|
|
$format = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
|
|
|
|
$chartData = [];
|
|
|
|
// make empty data array:
|
|
|
|
// double foreach (bad) to make empty array:
|
|
|
|
foreach ($currencies as $currencyId) {
|
|
|
|
$currencyInfo = $expenses[$currencyId] ?? $income[$currencyId];
|
|
|
|
$outKey = sprintf('%d-out', $currencyId);
|
|
|
|
$inKey = sprintf('%d-in', $currencyId);
|
|
|
|
$chartData[$outKey]
|
|
|
|
= [
|
2020-03-17 09:01:00 -05:00
|
|
|
'label' => sprintf('%s (%s)', (string) trans('firefly.spent'), $currencyInfo['currency_name']),
|
2020-02-01 08:54:26 -06:00
|
|
|
'entries' => [],
|
|
|
|
'type' => 'bar',
|
|
|
|
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
|
|
|
|
];
|
|
|
|
|
|
|
|
$chartData[$inKey]
|
|
|
|
= [
|
2020-03-17 09:01:00 -05:00
|
|
|
'label' => sprintf('%s (%s)', (string) trans('firefly.earned'), $currencyInfo['currency_name']),
|
2020-02-01 08:54:26 -06:00
|
|
|
'entries' => [],
|
|
|
|
'type' => 'bar',
|
|
|
|
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
// loop empty periods:
|
|
|
|
foreach (array_keys($periods) as $period) {
|
|
|
|
$label = $periods[$period];
|
|
|
|
$chartData[$outKey]['entries'][$label] = '0';
|
|
|
|
$chartData[$inKey]['entries'][$label] = '0';
|
|
|
|
}
|
|
|
|
// loop income and expenses for this category.:
|
|
|
|
$outSet = $expenses[$currencyId]['categories'][$categoryId] ?? ['transaction_journals' => []];
|
|
|
|
foreach ($outSet['transaction_journals'] as $journal) {
|
|
|
|
$amount = app('steam')->positive($journal['amount']);
|
|
|
|
$date = $journal['date']->formatLocalized($format);
|
|
|
|
$chartData[$outKey]['entries'][$date] = $chartData[$outKey]['entries'][$date] ?? '0';
|
|
|
|
|
|
|
|
$chartData[$outKey]['entries'][$date] = bcadd($amount, $chartData[$outKey]['entries'][$date]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$inSet = $income[$currencyId]['categories'][$categoryId] ?? ['transaction_journals' => []];
|
|
|
|
foreach ($inSet['transaction_journals'] as $journal) {
|
|
|
|
$amount = app('steam')->positive($journal['amount']);
|
|
|
|
$date = $journal['date']->formatLocalized($format);
|
|
|
|
$chartData[$inKey]['entries'][$date] = $chartData[$inKey]['entries'][$date] ?? '0';
|
|
|
|
$chartData[$inKey]['entries'][$date] = bcadd($amount, $chartData[$inKey]['entries'][$date]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->generator->multiSet($chartData);
|
|
|
|
}
|
2015-05-20 12:56:14 -05:00
|
|
|
}
|