mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
328 lines
12 KiB
PHP
328 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* CategoryController.php
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
|
*
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
*
|
|
* 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.
|
|
*
|
|
* This program 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 Affero General Public License for more details.
|
|
*
|
|
* 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/>.
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace FireflyIII\Http\Controllers\Chart;
|
|
|
|
use Carbon\Carbon;
|
|
use Exception;
|
|
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
use FireflyIII\Models\Category;
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
|
use FireflyIII\Repositories\Category\NoCategoryRepositoryInterface;
|
|
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
|
|
use FireflyIII\Support\CacheProperties;
|
|
use FireflyIII\Support\Chart\Category\FrontpageChartGenerator;
|
|
use FireflyIII\Support\Chart\Category\WholePeriodChartGenerator;
|
|
use FireflyIII\Support\Http\Controllers\AugumentData;
|
|
use FireflyIII\Support\Http\Controllers\ChartGeneration;
|
|
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* Class CategoryController.
|
|
*/
|
|
class CategoryController extends Controller
|
|
{
|
|
use DateCalculation, AugumentData, ChartGeneration;
|
|
|
|
/** @var GeneratorInterface Chart generation methods. */
|
|
protected $generator;
|
|
|
|
/**
|
|
* CategoryController constructor.
|
|
*
|
|
* @codeCoverageIgnore
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
// create chart generator:
|
|
$this->generator = app(GeneratorInterface::class);
|
|
}
|
|
|
|
|
|
/**
|
|
* Show an overview for a category for all time, per month/week/year.
|
|
* TODO test method, for category refactor.
|
|
*
|
|
* @param Category $category
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
public function all(Category $category): JsonResponse
|
|
{
|
|
// cache results:
|
|
$cache = new CacheProperties;
|
|
$cache->addProperty('chart.category.all');
|
|
$cache->addProperty($category->id);
|
|
if ($cache->has()) {
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
|
}
|
|
/** @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();
|
|
|
|
/** @var WholePeriodChartGenerator $chartGenerator */
|
|
$chartGenerator = app(WholePeriodChartGenerator::class);
|
|
$chartData = $chartGenerator->generate($category, $start, $end);
|
|
$data = $this->generator->multiSet($chartData);
|
|
$cache->store($data);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
|
|
/**
|
|
* Shows the category chart on the front page.
|
|
* TODO test method, for category refactor.
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
public function frontPage(): JsonResponse
|
|
{
|
|
$start = session('start', Carbon::now()->startOfMonth());
|
|
$end = session('end', Carbon::now()->endOfMonth());
|
|
// chart properties for cache:
|
|
$cache = new CacheProperties;
|
|
$cache->addProperty($start);
|
|
$cache->addProperty($end);
|
|
$cache->addProperty('chart.category.frontpage');
|
|
if ($cache->has()) {
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
|
}
|
|
|
|
$frontPageGenerator = new FrontpageChartGenerator($start, $end);
|
|
|
|
|
|
$chartData = $frontPageGenerator->generate();
|
|
$data = $this->generator->multiSet($chartData);
|
|
$cache->store($data);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
/**
|
|
* Chart report.
|
|
* TODO test method, for category refactor.
|
|
*
|
|
* @param Category $category
|
|
* @param Collection $accounts
|
|
* @param Carbon $start
|
|
* @param Carbon $end
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
public function reportPeriod(Category $category, Collection $accounts, Carbon $start, Carbon $end): JsonResponse
|
|
{
|
|
$cache = new CacheProperties;
|
|
$cache->addProperty($start);
|
|
$cache->addProperty($end);
|
|
$cache->addProperty('chart.category.period');
|
|
$cache->addProperty($accounts->pluck('id')->toArray());
|
|
$cache->addProperty($category);
|
|
if ($cache->has()) {
|
|
return response()->json($cache->get());// @codeCoverageIgnore
|
|
}
|
|
$data = $this->reportPeriodChart($accounts, $start, $end, $category);
|
|
|
|
$cache->store($data);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
|
|
/**
|
|
* Chart for period for transactions without a category.
|
|
* TODO test me.
|
|
*
|
|
* @param Collection $accounts
|
|
* @param Carbon $start
|
|
* @param Carbon $end
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
public function reportPeriodNoCategory(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
|
|
{
|
|
$cache = new CacheProperties;
|
|
$cache->addProperty($start);
|
|
$cache->addProperty($end);
|
|
$cache->addProperty('chart.category.period.no-cat');
|
|
$cache->addProperty($accounts->pluck('id')->toArray());
|
|
if ($cache->has()) {
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
|
}
|
|
$data = $this->reportPeriodChart($accounts, $start, $end, null);
|
|
|
|
$cache->store($data);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
/**
|
|
* Chart for a specific period.
|
|
* TODO test method, for category refactor.
|
|
*
|
|
* @param Category $category
|
|
* @param $date
|
|
*
|
|
* @return JsonResponse
|
|
*/
|
|
public function specificPeriod(Category $category, Carbon $date): JsonResponse
|
|
{
|
|
$range = app('preferences')->get('viewRange', '1M')->data;
|
|
$start = app('navigation')->startOfPeriod($date, $range);
|
|
$end = session()->get('end');
|
|
if ($end < $start) {
|
|
[$end, $start] = [$start, $end]; // @codeCoverageIgnore
|
|
}
|
|
|
|
$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 WholePeriodChartGenerator $chartGenerator */
|
|
$chartGenerator = app(WholePeriodChartGenerator::class);
|
|
$chartData = $chartGenerator->generate($category, $start, $end);
|
|
$data = $this->generator->multiSet($chartData);
|
|
|
|
$cache->store($data);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
/**
|
|
* @return Carbon
|
|
*/
|
|
private function getDate(): Carbon
|
|
{
|
|
$carbon = null;
|
|
try {
|
|
$carbon = today(config('app.timezone'));
|
|
} catch (Exception $e) {
|
|
$e->getMessage();
|
|
}
|
|
|
|
return $carbon;
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
$categoryId = (int) $category->id;
|
|
// 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]
|
|
= [
|
|
'label' => sprintf('%s (%s)', (string) trans('firefly.spent'), $currencyInfo['currency_name']),
|
|
'entries' => [],
|
|
'type' => 'bar',
|
|
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
|
|
];
|
|
|
|
$chartData[$inKey]
|
|
= [
|
|
'label' => sprintf('%s (%s)', (string) trans('firefly.earned'), $currencyInfo['currency_name']),
|
|
'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);
|
|
}
|
|
}
|