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

520 lines
18 KiB
PHP
Raw Normal View History

2015-05-16 09:41:14 +02:00
<?php
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2015-05-16 09:41:14 +02:00
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
2016-05-02 20:49:19 +02:00
use FireflyIII\Generator\Chart\Category\CategoryChartGeneratorInterface;
2015-05-16 09:41:14 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Category;
2015-12-29 22:48:55 +01:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI;
2016-05-11 09:17:47 +02:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2015-06-03 18:22:47 +02:00
use FireflyIII\Support\CacheProperties;
2015-06-27 11:44:18 +02:00
use Illuminate\Support\Collection;
2016-05-09 20:15:26 +02:00
use Log;
2015-05-16 09:41:14 +02:00
use Navigation;
use Preferences;
use Response;
2016-01-02 16:32:08 +01:00
use stdClass;
2015-05-16 09:41:14 +02:00
/**
* Class CategoryController
*
* @package FireflyIII\Http\Controllers\Chart
*/
class CategoryController extends Controller
{
2016-02-12 17:34:42 +01:00
const MAKE_POSITIVE = -1;
const KEEP_POSITIVE = 1;
2016-05-02 20:49:19 +02:00
/** @var CategoryChartGeneratorInterface */
2015-06-27 11:44:18 +02:00
protected $generator;
/**
2016-02-04 07:27:03 +01:00
*
2015-06-27 11:44:18 +02:00
*/
public function __construct()
{
parent::__construct();
// create chart generator:
2016-05-02 20:49:19 +02:00
$this->generator = app(CategoryChartGeneratorInterface::class);
2015-06-27 11:44:18 +02:00
}
2015-05-16 09:41:14 +02:00
/**
2015-05-16 10:13:41 +02:00
* Show an overview for a category for all time, per month/week/year.
2015-05-16 09:41:14 +02:00
*
2016-05-09 20:15:26 +02:00
* @param CRI $repository
2015-12-29 22:48:55 +01:00
* @param Category $category
2015-05-16 09:41:14 +02:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function all(CRI $repository, Category $category)
2015-05-16 09:41:14 +02:00
{
2016-05-09 20:15:26 +02:00
$start = $repository->firstUseDate($category, new Collection);
$range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod($start, $range);
$categoryCollection = new Collection([$category]);
$end = new Carbon;
$entries = new Collection;
$cache = new CacheProperties;
2015-06-27 11:44:18 +02:00
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('all');
$cache->addProperty('categories');
if ($cache->has()) {
2016-05-09 20:15:26 +02:00
//return Response::json($cache->get());
2015-06-27 11:44:18 +02:00
}
2015-12-31 07:49:19 +01:00
2015-06-27 11:44:18 +02:00
while ($start <= $end) {
2015-05-16 10:13:41 +02:00
$currentEnd = Navigation::endOfPeriod($start, $range);
2016-05-09 20:15:26 +02:00
Log::debug('Searching for expenses from ' . $start . ' to ' . $currentEnd);
$spent = $repository->spentInPeriod($categoryCollection, new Collection, $start, $currentEnd);
$earned = $repository->earnedInPeriod($categoryCollection, new Collection, $start, $currentEnd);
$date = Navigation::periodShow($start, $range);
$entries->push([clone $start, $date, $spent, $earned]);
2015-05-16 10:13:41 +02:00
$start = Navigation::addPeriod($start, $range, 0);
2015-05-16 09:41:14 +02:00
}
$entries = $entries->reverse();
2015-09-25 20:15:38 +02:00
$entries = $entries->slice(0, 48);
$entries = $entries->reverse();
$data = $this->generator->all($entries);
2015-06-27 11:44:18 +02:00
$cache->store($data);
2015-05-16 09:41:14 +02:00
2015-06-27 11:44:18 +02:00
return Response::json($data);
2016-05-09 20:15:26 +02:00
2015-05-16 09:41:14 +02:00
}
2016-01-29 07:33:49 +01:00
/**
2016-05-09 20:15:26 +02:00
* @param CRI $repository
2016-01-29 07:33:49 +01:00
* @param Category $category
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function currentPeriod(CRI $repository, Category $category)
2016-01-29 07:33:49 +01:00
{
2016-02-04 07:27:03 +01:00
$start = clone session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
2016-01-29 07:33:49 +01:00
$data = $this->makePeriodChart($repository, $category, $start, $end);
return Response::json($data);
}
/**
* Returns a chart of what has been earned in this period in each category
* grouped by month.
*
* @param CRI $repository
* @param $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse
*/
2016-02-05 09:25:15 +01:00
public function earnedInPeriod(CRI $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
2016-01-29 07:33:49 +01:00
{
/**
2016-05-09 20:15:26 +02:00
* $cache = new CacheProperties; // chart properties for cache:
* $cache->addProperty($start);
* $cache->addProperty($end);
* $cache->addProperty($reportType);
* $cache->addProperty($accounts);
* $cache->addProperty('category');
* $cache->addProperty('earned-in-period');
* if ($cache->has()) {
* return Response::json($cache->get());
* }
*
* $set = $repository->earnedForAccountsPerMonth($accounts, $start, $end);
* $categories = $set->unique('id')->sortBy(
* function (Category $category) {
* return $category->name;
* }
* );
* $entries = $this->filterCollection($start, $end, $set, $categories);
* $data = $this->generator->earnedInPeriod($categories, $entries);
* $cache->store($data);
*
* return $data;
**/
2016-01-29 07:33:49 +01:00
}
2015-12-31 07:49:19 +01:00
2015-05-16 09:41:14 +02:00
/**
2015-05-16 10:13:41 +02:00
* Show this month's category overview.
*
2015-12-29 22:48:55 +01:00
* @param CRI $repository
2015-05-16 09:41:14 +02:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2016-05-09 20:15:26 +02:00
public function frontpage(CRI $repository)
2015-05-16 09:41:14 +02:00
{
2016-02-04 07:27:03 +01:00
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
// chart properties for cache:
2015-06-03 21:25:11 +02:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category');
$cache->addProperty('frontpage');
if ($cache->has()) {
2016-05-09 20:15:26 +02:00
//return Response::json($cache->get());
}
$categories = $repository->getCategories();
$set = new Collection;
/** @var Category $category */
foreach ($categories as $category) {
$spent = $repository->spentInPeriod(new Collection([$category]), new Collection, $start, $end);
Log::debug('Spent for ' . $category->name . ' is ' . $spent . ' (' . bccomp($spent, '0') . ')');
if (bccomp($spent, '0') === -1) {
$category->spent = $spent;
$set->push($category);
}
}
2016-01-02 16:32:08 +01:00
// this is a "fake" entry for the "no category" entry.
2016-05-09 20:15:26 +02:00
$entry = new stdClass;
2016-01-15 13:08:25 +01:00
$entry->name = trans('firefly.no_category');
2016-05-09 20:15:26 +02:00
$entry->spent = $repository->spentInPeriodWithoutCategory(new Collection, $start, $end);
2016-01-02 16:32:08 +01:00
$set->push($entry);
2016-01-15 13:08:25 +01:00
$set = $set->sortBy('spent');
2015-06-27 11:44:18 +02:00
$data = $this->generator->frontpage($set);
$cache->store($data);
return Response::json($data);
2016-05-09 20:15:26 +02:00
}
/**
2015-12-28 20:04:54 +01:00
* @param $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Collection $categories
*
* @return \Illuminate\Http\JsonResponse
*/
2016-02-05 09:25:15 +01:00
public function multiYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
{
/**
2016-05-09 20:15:26 +02:00
* // /** @var CRI $repository
* // $repository = app(CRI::class);
*
* // chart properties for cache:
* $cache = new CacheProperties();
* $cache->addProperty($reportType);
* $cache->addProperty($start);
* $cache->addProperty($end);
* $cache->addProperty($accounts);
* $cache->addProperty($categories);
* $cache->addProperty('multiYearCategory');
*
* if ($cache->has()) {
* return Response::json($cache->get());
* }
*
* $entries = new Collection;
* $set = $repository->listMultiYear($categories, $accounts, $start, $end);
*
* /** @var Category $category
* foreach ($categories as $category) {
* $entry = ['name' => '', 'spent' => [], 'earned' => []];
*
* $currentStart = clone $start;
* while ($currentStart < $end) {
* // fix the date:
* $year = $currentStart->year;
* $currentEnd = clone $currentStart;
* $currentEnd->endOfYear();
*
*
* // get data:
* if (is_null($category->id)) {
* $name = trans('firefly.noCategory');
* $spent = $repository->sumSpentNoCategory($accounts, $currentStart, $currentEnd);
* $earned = $repository->sumEarnedNoCategory($accounts, $currentStart, $currentEnd);
* } else {
* // get from set:
* $entrySpent = $set->filter(
* function (Category $cat) use ($year, $category) {
* return ($cat->type == 'Withdrawal' && $cat->dateFormatted == $year && $cat->id == $category->id);
* }
* )->first();
* $entryEarned = $set->filter(
* function (Category $cat) use ($year, $category) {
* return ($cat->type == 'Deposit' && $cat->dateFormatted == $year && $cat->id == $category->id);
* }
* )->first();
*
* $name = $category->name;
* $spent = !is_null($entrySpent) ? $entrySpent->sum : 0;
* $earned = !is_null($entryEarned) ? $entryEarned->sum : 0;
* }
*
* // save to array:
* $entry['name'] = $name;
* $entry['spent'][$year] = ($spent * -1);
* $entry['earned'][$year] = $earned;
*
* // jump to next year.
* $currentStart = clone $currentEnd;
* $currentStart->addDay();
* }
* $entries->push($entry);
* }
* // generate chart with data:
2016-05-11 09:17:47 +02:00
*
2016-05-09 20:15:26 +02:00
* $data = $this->generator->multiYear($entries);
* $cache->store($data);
*
* return Response::json($data);
*
*/
2015-05-16 09:41:14 +02:00
}
2016-04-30 22:30:11 +02:00
/**
* @param Category $category
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse
*/
2016-05-11 09:17:47 +02:00
public function period(Category $category, Carbon $start, Carbon $end, Collection $accounts)
2016-04-30 22:30:11 +02:00
{
2016-05-11 09:17:47 +02:00
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($accounts);
$cache->addProperty($category->id);
$cache->addProperty('category');
$cache->addProperty('period');
if ($cache->has()) {
// return Response::json($cache->get());
}
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categoryCollection = new Collection([$category]);
// loop over period, add by users range:
$current = clone $start;
$viewRange = Preferences::get('viewRange', '1M')->data;
$format = strval(trans('config.month'));
$set = new Collection;
while ($current < $end) {
$currentStart = clone $current;
$currentEnd = Navigation::endOfPeriod($currentStart, $viewRange);
$spent = $repository->spentInPeriod($categoryCollection, $accounts, $currentStart, $currentEnd);
$earned = $repository->earnedInPeriod($categoryCollection, $accounts, $currentStart, $currentEnd);
$entry = [
$category->name,
$currentStart->formatLocalized($format),
$spent,
$earned,
2016-04-30 22:30:11 +02:00
2016-05-11 09:17:47 +02:00
];
$set->push($entry);
$currentEnd->addDay();
$current = clone $currentEnd;
}
$data = $this->generator->period($set);
$cache->store($data);
return Response::json($data);
2016-04-30 22:30:11 +02:00
}
/**
2016-05-09 20:15:26 +02:00
* @param CRI $repository
* @param Category $category
*
* @param $date
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function specificPeriod(CRI $repository, Category $category, $date)
{
$carbon = new Carbon($date);
$range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod($carbon, $range);
$end = Navigation::endOfPeriod($carbon, $range);
2016-01-29 07:33:49 +01:00
$data = $this->makePeriodChart($repository, $category, $start, $end);
2015-05-16 09:41:14 +02:00
2015-06-27 11:44:18 +02:00
return Response::json($data);
2015-05-16 09:41:14 +02:00
}
2015-12-25 07:31:54 +01:00
/**
2016-01-29 07:33:49 +01:00
* Returns a chart of what has been spent in this period in each category
2015-12-25 07:31:54 +01:00
* grouped by month.
*
2015-12-29 22:48:55 +01:00
* @param CRI $repository
2015-12-28 20:04:54 +01:00
* @param $reportType
2015-12-25 07:31:54 +01:00
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
2016-01-15 20:48:06 +01:00
*
2015-12-25 07:31:54 +01:00
* @return \Illuminate\Http\JsonResponse
*/
2016-01-29 07:33:49 +01:00
public function spentInPeriod(CRI $repository, $reportType, Carbon $start, Carbon $end, Collection $accounts)
2015-12-25 07:31:54 +01:00
{
/**
2016-05-09 20:15:26 +02:00
* $cache = new CacheProperties; // chart properties for cache:
* $cache->addProperty($start);
* $cache->addProperty($end);
* $cache->addProperty($reportType);
* $cache->addProperty($accounts);
* $cache->addProperty('category');
* $cache->addProperty('spent-in-period');
* if ($cache->has()) {
* return Response::json($cache->get());
* }
*
*
* $set = $repository->spentForAccountsPerMonth($accounts, $start, $end);
* $categories = $set->unique('id')->sortBy(
* function (Category $category) {
* return $category->name;
* }
* );
* $entries = $this->filterCollection($start, $end, $set, $categories);
* $entries = $this->invertSelection($entries);
* $data = $this->generator->spentInPeriod($categories, $entries);
* $cache->store($data);
*
* return $data;
* */
2016-02-12 17:34:42 +01:00
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $set
* @param Collection $categories
*
* @return Collection
*/
private function filterCollection(Carbon $start, Carbon $end, Collection $set, Collection $categories): Collection
{
/**
2016-05-09 20:15:26 +02:00
* $entries = new Collection;
*
* while ($start < $end) { // filter the set:
* $row = [clone $start];
* $currentSet = $set->filter( // get possibly relevant entries from the big $set
* function (Category $category) use ($start) {
* return $category->dateFormatted == $start->format('Y-m');
* }
* );
* /** @var Category $category
* foreach ($categories as $category) { // check for each category if its in the current set.
* $entry = $currentSet->filter( // if its in there, use the value.
* function (Category $cat) use ($category) {
* return ($cat->id == $category->id);
* }
* )->first();
* if (!is_null($entry)) {
* $row[] = $entry->earned ? round($entry->earned, 2) : round($entry->spent, 2);
* } else {
* $row[] = 0;
* }
* }
* $entries->push($row);
* $start->addMonth();
* }
*
* return $entries;
* */
2015-12-25 07:31:54 +01:00
}
2016-02-17 22:35:57 +01:00
/**
2016-02-18 06:15:01 +01:00
* Not the most elegant solution but it works.
*
2016-02-17 22:35:57 +01:00
* @param Collection $entries
*
* @return Collection
*/
private function invertSelection(Collection $entries): Collection
{
/**
2016-05-09 20:15:26 +02:00
* $result = new Collection;
* foreach ($entries as $entry) {
* $new = [$entry[0]];
* $count = count($entry);
* for ($i = 1; $i < $count; $i++) {
* $new[$i] = ($entry[$i] * -1);
* }
* $result->push($new);
* }
*
* return $result;
* **/
2016-02-17 22:35:57 +01:00
}
/**
2016-05-09 20:15:26 +02:00
* @param CRI $repository
2016-01-29 07:33:49 +01:00
* @param Category $category
* @param Carbon $start
* @param Carbon $end
2016-01-15 19:37:09 +01:00
*
2016-01-29 07:33:49 +01:00
* @return array
*/
private function makePeriodChart(CRI $repository, Category $category, Carbon $start, Carbon $end)
{
2016-05-09 20:15:26 +02:00
$categoryCollection = new Collection([$category]);
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
2016-01-29 07:33:49 +01:00
$cache->addProperty($category->id);
$cache->addProperty('specific-period');
2016-05-09 20:15:26 +02:00
if ($cache->has()) {
2016-05-09 20:15:26 +02:00
// return $cache->get();
}
2016-01-29 07:33:49 +01:00
$entries = new Collection;
2016-05-09 20:15:26 +02:00
Log::debug('Start is ' . $start . ' en end is ' . $end);
2016-01-29 07:33:49 +01:00
while ($start <= $end) {
2016-05-09 20:15:26 +02:00
Log::debug('Now at ' . $start);
$spent = $repository->spentInPeriod($categoryCollection, new Collection, $start, $start);
Log::debug('spent: ' . $spent);
$earned = $repository->earnedInPeriod($categoryCollection, new Collection, $start, $start);
Log::debug('earned: ' . $earned);
$date = Navigation::periodShow($start, '1D');
2016-01-29 07:33:49 +01:00
$entries->push([clone $start, $date, $spent, $earned]);
$start->addDay();
}
2016-01-29 07:33:49 +01:00
$data = $this->generator->period($entries);
$cache->store($data);
return $data;
2016-05-09 20:15:26 +02:00
/**
* // get amount earned in period, grouped by day.
* // get amount spent in period, grouped by day.
* $spentArray = $repository->spentPerDay($category, $start, $end, new Collection);
* $earnedArray = $repository->earnedPerDay($category, $start, $end, new Collection);
*
* while ($start <= $end) {
* $str = $start->format('Y-m-d');
* $spent = $spentArray[$str] ?? '0';
* $earned = $earnedArray[$str] ?? '0';
* $date = Navigation::periodShow($start, '1D');
* $entries->push([clone $start, $date, $spent, $earned]);
* $start->addDay();
* }
*
* $data = $this->generator->period($entries);
* $cache->store($data);
*
* return $data;
*/
}
2015-05-20 19:56:14 +02:00
}