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

352 lines
13 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;
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;
2015-06-03 11:22:47 -05:00
use FireflyIII\Support\CacheProperties;
2015-06-27 04:44:18 -05:00
use Illuminate\Support\Collection;
2015-05-16 02:41:14 -05:00
use Preferences;
/**
2017-11-15 05:25:49 -06:00
* Class CategoryController.
2015-05-16 02:41:14 -05:00
*/
class CategoryController extends Controller
{
2017-11-15 05:25:49 -06:00
/** @var GeneratorInterface */
2015-06-27 04:44:18 -05:00
protected $generator;
/**
2016-02-04 00:27:03 -06:00
*
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
/**
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
*
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
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2017-02-24 22:57:01 -06:00
public function all(CategoryRepositoryInterface $repository, AccountRepositoryInterface $accountRepository, Category $category)
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
}
2015-12-31 00:49:19 -06:00
2017-01-02 13:42:29 -06:00
$start = $repository->firstUseDate($category);
2017-11-15 05:25:49 -06:00
if (null === $start) {
2017-12-17 11:23:10 -06:00
$start = new Carbon; // @codeCoverageIgnore
2017-01-02 13:42:29 -06:00
}
2016-12-15 06:47:28 -06:00
$range = Preferences::get('viewRange', '1M')->data;
$start = app('navigation')->startOfPeriod($start, $range);
2016-12-15 06:47:28 -06:00
$end = new Carbon;
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$chartData = [
[
'label' => strval(trans('firefly.spent')),
'entries' => [],
'type' => 'bar',
],
[
'label' => strval(trans('firefly.earned')),
'entries' => [],
'type' => 'bar',
],
2017-04-08 02:18:04 -05:00
[
'label' => strval(trans('firefly.sum')),
'entries' => [],
'type' => 'line',
'fill' => false,
],
2016-12-15 06:47:28 -06:00
];
2015-06-27 04:44:18 -05:00
while ($start <= $end) {
$currentEnd = app('navigation')->endOfPeriod($start, $range);
2017-01-05 01:47:45 -06:00
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $currentEnd);
2017-01-05 01:47:09 -06:00
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $start, $currentEnd);
2017-04-08 02:18:04 -05:00
$sum = bcadd($spent, $earned);
$label = app('navigation')->periodShow($start, $range);
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);
$start = app('navigation')->addPeriod($start, $range, 0);
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
}
/**
2017-02-24 22:57:01 -06:00
* @param CategoryRepositoryInterface $repository
* @param AccountRepositoryInterface $accountRepository
2015-05-16 02:41:14 -05:00
*
* @return \Illuminate\Http\JsonResponse
2015-05-16 02:41:14 -05:00
*/
2017-02-24 22:57:01 -06:00
public function frontpage(CategoryRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
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-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2016-05-09 13:15:26 -05:00
}
2016-12-15 06:47:28 -06:00
$chartData = [];
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) {
2017-01-05 01:47:45 -06:00
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $end);
2016-05-09 13:15:26 -05:00
if (bccomp($spent, '0') === -1) {
2016-12-15 06:47:28 -06:00
$chartData[$category->name] = bcmul($spent, '-1');
2016-05-09 13:15:26 -05:00
}
}
2017-01-05 01:48:16 -06:00
$chartData[strval(trans('firefly.no_category'))] = bcmul($repository->spentInPeriodWithoutCategory(new Collection, $start, $end), '-1');
2016-12-15 06:47:28 -06:00
// sort
arsort($chartData);
$data = $this->generator->singleSet(strval(trans('firefly.spent')), $chartData);
$cache->store($data);
2018-03-10 13:30:09 -06:00
return response()->json($data);
}
2016-12-03 14:48:40 -06:00
/**
2017-02-24 22:57:01 -06:00
* @param CategoryRepositoryInterface $repository
* @param Category $category
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-12-06 00:48:41 -06:00
*
* @return \Illuminate\Http\JsonResponse|mixed
2016-12-03 14:48:40 -06:00
*/
2017-02-24 22:57:01 -06:00
public function reportPeriod(CategoryRepositoryInterface $repository, Category $category, Collection $accounts, Carbon $start, Carbon $end)
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()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2016-12-03 14:48:40 -06:00
}
2016-12-15 06:47:28 -06:00
$expenses = $repository->periodExpenses(new Collection([$category]), $accounts, $start, $end);
$income = $repository->periodIncome(new Collection([$category]), $accounts, $start, $end);
$periods = app('navigation')->listOfPeriods($start, $end);
2016-12-15 06:47:28 -06:00
$chartData = [
[
'label' => strval(trans('firefly.spent')),
'entries' => [],
'type' => 'bar',
],
[
'label' => strval(trans('firefly.earned')),
'entries' => [],
'type' => 'bar',
],
2017-04-08 02:18:04 -05:00
[
'label' => strval(trans('firefly.sum')),
'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
}
/**
2017-02-24 22:57:01 -06:00
* @param CategoryRepositoryInterface $repository
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-12-06 00:48:41 -06:00
*
* @return \Illuminate\Http\JsonResponse|mixed
*/
2017-02-24 22:57:01 -06:00
public function reportPeriodNoCategory(CategoryRepositoryInterface $repository, Collection $accounts, Carbon $start, Carbon $end)
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()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2016-12-06 00:48:41 -06:00
}
2016-12-15 06:47:28 -06:00
$expenses = $repository->periodExpensesNoCategory($accounts, $start, $end);
$income = $repository->periodIncomeNoCategory($accounts, $start, $end);
$periods = app('navigation')->listOfPeriods($start, $end);
2016-12-15 06:47:28 -06:00
$chartData = [
[
'label' => strval(trans('firefly.spent')),
'entries' => [],
'type' => 'bar',
],
[
'label' => strval(trans('firefly.earned')),
'entries' => [],
'type' => 'bar',
],
2017-04-08 02:18:04 -05:00
[
'label' => strval(trans('firefly.sum')),
'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
/**
2017-02-24 22:57:01 -06:00
* @param CategoryRepositoryInterface $repository
* @param Category $category
* @param $date
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2017-04-23 02:33:33 -05:00
public function specificPeriod(CategoryRepositoryInterface $repository, Category $category, Carbon $date)
{
2017-06-05 04:12:50 -05:00
$range = Preferences::get('viewRange', '1M')->data;
$start = app('navigation')->startOfPeriod($date, $range);
$end = app('navigation')->endOfPeriod($date, $range);
2017-06-05 04:12:50 -05:00
$data = $this->makePeriodChart($repository, $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
}
/**
2017-02-24 22:57:01 -06:00
* @param CategoryRepositoryInterface $repository
* @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
*/
2017-02-24 22:57:01 -06:00
private function makePeriodChart(CategoryRepositoryInterface $repository, Category $category, Carbon $start, Carbon $end)
{
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-12-15 06:47:28 -06:00
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
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
// chart data
$chartData = [
[
'label' => strval(trans('firefly.spent')),
'entries' => [],
'type' => 'bar',
],
[
'label' => strval(trans('firefly.earned')),
'entries' => [],
'type' => 'bar',
],
2017-04-08 02:18:04 -05:00
[
'label' => strval(trans('firefly.sum')),
'entries' => [],
'type' => 'line',
'fill' => false,
],
2016-12-15 06:47:28 -06:00
];
2016-01-29 00:33:49 -06:00
while ($start <= $end) {
2017-01-05 01:47:45 -06:00
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $start);
2017-01-05 01:47:09 -06:00
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $start, $start);
2017-04-08 02:18:04 -05:00
$sum = bcadd($spent, $earned);
$label = trim(app('navigation')->periodShow($start, '1D'));
2016-12-15 06:47:28 -06:00
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
2016-01-29 00:33:49 -06:00
$start->addDay();
}
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;
}
2015-05-20 12:56:14 -05:00
}