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

457 lines
16 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Category;
2015-12-29 15:48:55 -06:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI;
use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface as SCRI;
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 Navigation;
use Preferences;
use Response;
use Session;
/**
* Class CategoryController
*
* @package FireflyIII\Http\Controllers\Chart
*/
class CategoryController extends Controller
{
2015-06-27 04:44:18 -05:00
/** @var \FireflyIII\Generator\Chart\Category\CategoryChartGenerator */
protected $generator;
/**
2015-06-28 03:03:34 -05:00
* @codeCoverageIgnore
2015-06-27 04:44:18 -05:00
*/
public function __construct()
{
parent::__construct();
// create chart generator:
2015-07-07 12:09:45 -05:00
$this->generator = app('FireflyIII\Generator\Chart\Category\CategoryChartGenerator');
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
*
2015-12-29 15:48:55 -06:00
* @param SCRI $repository
* @param Category $category
2015-05-16 02:41:14 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-12-29 15:48:55 -06:00
public function all(SCRI $repository, Category $category)
2015-05-16 02:41:14 -05:00
{
2015-05-16 03:13:41 -05:00
// oldest transaction in category:
$start = $repository->getFirstActivityDate($category);
$range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod($start, $range);
$end = new Carbon;
2015-06-27 04:44:18 -05:00
$entries = new Collection;
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('all');
$cache->addProperty('categories');
if ($cache->has()) {
2015-06-27 15:22:27 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-27 04:44:18 -05:00
}
2015-05-16 03:13:41 -05:00
2015-06-27 04:44:18 -05:00
while ($start <= $end) {
2015-05-16 03:13:41 -05:00
$currentEnd = Navigation::endOfPeriod($start, $range);
2015-09-13 00:40:37 -05:00
$spent = $repository->spentInPeriod($category, $start, $currentEnd);
$earned = $repository->earnedInPeriod($category, $start, $currentEnd);
$date = Navigation::periodShow($start, $range);
$entries->push([clone $start, $date, $spent, $earned]);
2015-05-16 03:13:41 -05:00
$start = Navigation::addPeriod($start, $range, 0);
2015-05-16 02:41:14 -05:00
}
// limit the set to the last 40:
$entries = $entries->reverse();
2015-09-25 13:15:38 -05:00
$entries = $entries->slice(0, 48);
$entries = $entries->reverse();
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
$data = $this->generator->all($entries);
$cache->store($data);
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
return Response::json($data);
2015-05-16 02:41:14 -05:00
2015-05-16 03:13:41 -05:00
2015-05-16 02:41:14 -05:00
}
/**
2015-05-16 03:13:41 -05:00
* Show this month's category overview.
*
2015-12-29 15:48:55 -06:00
* @param CRI $repository
2015-05-16 02:41:14 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-12-29 15:48:55 -06:00
public function frontpage(CRI $repository)
2015-05-16 02:41:14 -05:00
{
2015-05-16 03:13:41 -05:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('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);
$cache->addProperty('category');
$cache->addProperty('frontpage');
if ($cache->has()) {
2015-06-27 15:22:27 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
}
2015-12-13 03:05:13 -06:00
$array = $repository->getCategoriesAndExpenses($start, $end);
2015-05-21 11:57:14 -05:00
// sort by callback:
uasort(
2015-06-27 04:44:18 -05:00
$array,
2015-06-06 16:09:12 -05:00
function ($left, $right) {
2015-05-21 11:57:14 -05:00
if ($left['sum'] == $right['sum']) {
return 0;
}
2015-09-28 07:43:08 -05:00
return ($left['sum'] < $right['sum']) ? -1 : 1;
2015-05-21 11:57:14 -05:00
}
);
2015-06-27 04:44:18 -05:00
$set = new Collection($array);
$data = $this->generator->frontpage($set);
$cache->store($data);
return Response::json($data);
}
/**
2015-12-28 13:04:54 -06:00
* @param $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Collection $categories
*
* @return \Illuminate\Http\JsonResponse
*/
public function multiYear($reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
{
/** @var CRI $repository */
$repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
// chart properties for cache:
$cache = new CacheProperties();
2015-12-28 13:04:54 -06:00
$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()); // @codeCoverageIgnore
}
/**
* category
* year:
* spent: x
* earned: x
* year
* spent: x
* earned: x
*/
$entries = new Collection;
2015-12-30 02:17:29 -06:00
// go by category, not by year.
// given a set of categories and accounts, it should not be difficult to get
// the exact array of data we need.
// then get the data for "no category".
$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:
2015-12-30 02:17:29 -06:00
$year = $currentStart->year;
$currentEnd = clone $currentStart;
$currentEnd->endOfYear();
2015-12-30 02:17:29 -06:00
// get data:
if (is_null($category->id)) {
$name = trans('firefly.noCategory');
$spent = $repository->sumSpentNoCategory($accounts, $currentStart, $currentEnd);
$earned = $repository->sumEarnedNoCategory($accounts, $currentStart, $currentEnd);
} else {
2015-12-30 02:17:29 -06:00
// 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;
2015-12-30 02:17:29 -06:00
$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:
$data = $this->generator->multiYear($entries);
$cache->store($data);
return Response::json($data);
2015-05-16 02:41:14 -05:00
}
/**
2015-12-29 15:48:55 -06:00
* @param SCRI $repository
* @param Category $category
2015-05-16 02:41:14 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-12-29 15:48:55 -06:00
public function currentPeriod(SCRI $repository, Category $category)
2015-05-16 02:41:14 -05:00
{
2015-05-16 03:13:41 -05:00
$start = clone Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
2015-05-16 02:57:31 -05:00
2015-06-27 04:44:18 -05:00
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($category->id);
$cache->addProperty('category');
$cache->addProperty('current-period');
2015-06-27 04:44:18 -05:00
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
$entries = new Collection;
// get amount earned in period, grouped by day.
$spentArray = $repository->spentPerDay($category, $start, $end);
$earnedArray = $repository->earnedPerDay($category, $start, $end);
// get amount spent in period, grouped by day.
2015-05-16 02:41:14 -05:00
while ($start <= $end) {
$str = $start->format('Y-m-d');
$spent = isset($spentArray[$str]) ? $spentArray[$str] : 0;
$earned = isset($earnedArray[$str]) ? $earnedArray[$str] : 0;
$date = Navigation::periodShow($start, '1D');
$entries->push([clone $start, $date, $spent, $earned]);
2015-05-16 03:13:41 -05:00
$start->addDay();
2015-05-16 02:41:14 -05:00
}
$data = $this->generator->period($entries);
$cache->store($data);
return Response::json($data);
}
/**
2015-12-29 15:48:55 -06:00
* @param SCRI $repository
* @param Category $category
*
* @param $date
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-12-29 15:48:55 -06:00
public function specificPeriod(SCRI $repository, Category $category, $date)
{
$carbon = new Carbon($date);
$range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod($carbon, $range);
$end = Navigation::endOfPeriod($carbon, $range);
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($category->id);
$cache->addProperty('category');
$cache->addProperty('specificPeriod');
$cache->addProperty($date);
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
$entries = new Collection;
// get amount earned in period, grouped by day.
$spentArray = $repository->spentPerDay($category, $start, $end);
$earnedArray = $repository->earnedPerDay($category, $start, $end);
// get amount spent in period, grouped by day.
while ($start <= $end) {
$str = $start->format('Y-m-d');
$spent = isset($spentArray[$str]) ? $spentArray[$str] : 0;
$earned = isset($earnedArray[$str]) ? $earnedArray[$str] : 0;
$date = Navigation::periodShow($start, '1D');
$entries->push([clone $start, $date, $spent, $earned]);
$start->addDay();
}
$data = $this->generator->period($entries);
2015-06-27 04:44:18 -05:00
$cache->store($data);
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
return Response::json($data);
2015-05-16 02:41:14 -05:00
}
2015-12-25 00:31:54 -06:00
/**
* Returns a chart of what has been earned in this period in each category
2015-12-25 00:31:54 -06:00
* grouped by month.
*
2015-12-29 15:48:55 -06:00
* @param CRI $repository
2015-12-28 13:04:54 -06:00
* @param $reportType
2015-12-25 00:31:54 -06:00
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse
*/
2015-12-29 15:48:55 -06:00
public function earnedInPeriod(CRI $repository, $reportType, Carbon $start, Carbon $end, Collection $accounts)
2015-12-25 00:31:54 -06:00
{
2015-12-29 11:55:30 -06:00
$cache = new CacheProperties; // chart properties for cache:
2015-12-25 00:31:54 -06:00
$cache->addProperty($start);
$cache->addProperty($end);
2015-12-28 13:04:54 -06:00
$cache->addProperty($reportType);
2015-12-25 00:31:54 -06:00
$cache->addProperty($accounts);
$cache->addProperty('category');
$cache->addProperty('earned-in-period');
if ($cache->has()) {
2015-12-25 00:43:34 -06:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-12-25 00:31:54 -06:00
}
2015-12-29 11:55:30 -06:00
$set = $repository->earnedForAccountsPerMonth($accounts, $start, $end);
$categories = $set->unique('id')->sortBy(
function (Category $category) {
return $category->name;
}
);
2015-12-29 11:55:30 -06:00
$entries = new Collection;
2015-12-25 00:31:54 -06:00
2015-12-29 11:55:30 -06:00
while ($start < $end) { // filter the set:
$row = [clone $start];
// get possibly relevant entries from the big $set
$currentSet = $set->filter(
function (Category $category) use ($start) {
return $category->dateFormatted == $start->format("Y-m");
2015-12-25 00:31:54 -06:00
}
);
2015-12-29 11:55:30 -06:00
// check for each category if its in the current set.
2015-12-25 00:31:54 -06:00
/** @var Category $category */
foreach ($categories as $category) {
2015-12-29 11:55:30 -06:00
// if its in there, use the value.
$entry = $currentSet->filter(
function (Category $cat) use ($category) {
return ($cat->id == $category->id);
2015-12-25 00:31:54 -06:00
}
2015-12-29 11:55:30 -06:00
)->first();
2015-12-25 00:31:54 -06:00
if (!is_null($entry)) {
2015-12-29 11:55:30 -06:00
$row[] = round($entry->earned, 2);
2015-12-25 00:31:54 -06:00
} else {
$row[] = 0;
}
}
2015-12-29 11:55:30 -06:00
2015-12-25 00:31:54 -06:00
$entries->push($row);
$start->addMonth();
}
$data = $this->generator->earnedInPeriod($categories, $entries);
$cache->store($data);
return $data;
}
/**
* Returns a chart of what has been spent in this period in each category
* grouped by month.
*
2015-12-29 15:48:55 -06:00
* @param CRI $repository
2015-12-28 13:04:54 -06:00
* @param $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse
*/
2015-12-29 15:48:55 -06:00
public function spentInPeriod(CRI $repository, $reportType, Carbon $start, Carbon $end, Collection $accounts)
{
2015-12-29 11:55:30 -06:00
$cache = new CacheProperties; // chart properties for cache:
$cache->addProperty($start);
$cache->addProperty($end);
2015-12-28 13:04:54 -06:00
$cache->addProperty($reportType);
$cache->addProperty($accounts);
$cache->addProperty('category');
$cache->addProperty('spent-in-period');
if ($cache->has()) {
2015-12-25 00:43:34 -06:00
return Response::json($cache->get()); // @codeCoverageIgnore
}
2015-12-29 11:55:30 -06:00
$set = $repository->spentForAccountsPerMonth($accounts, $start, $end);
$categories = $set->unique('id')->sortBy(
function (Category $category) {
return $category->name;
}
);
2015-12-29 11:55:30 -06:00
$entries = new Collection;
2015-12-29 11:55:30 -06:00
while ($start < $end) { // filter the set:
$row = [clone $start];
// get possibly relevant entries from the big $set
$currentSet = $set->filter(
function (Category $category) use ($start) {
return $category->dateFormatted == $start->format("Y-m");
}
);
2015-12-29 11:55:30 -06:00
// check for each category if its in the current set.
/** @var Category $category */
foreach ($categories as $category) {
2015-12-29 11:55:30 -06:00
// if its in there, use the value.
$entry = $currentSet->filter(
function (Category $cat) use ($category) {
return ($cat->id == $category->id);
}
2015-12-29 11:55:30 -06:00
)->first();
if (!is_null($entry)) {
2015-12-29 11:55:30 -06:00
$row[] = round(($entry->spent * -1), 2);
} else {
$row[] = 0;
}
}
2015-12-29 11:55:30 -06:00
$entries->push($row);
$start->addMonth();
}
$data = $this->generator->spentInPeriod($categories, $entries);
$cache->store($data);
return $data;
}
2015-05-20 12:56:14 -05:00
}