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

286 lines
9.0 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;
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 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
*
* @param CategoryRepositoryInterface $repository
2015-05-16 03:13:41 -05:00
* @param Category $category
2015-05-16 02:41:14 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-06-27 04:44:18 -05:00
public function all(CategoryRepositoryInterface $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);
$entries->push([clone $start, $spent, $earned]);
2015-05-16 03:13:41 -05:00
$start = Navigation::addPeriod($start, $range, 0);
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-05-16 02:41:14 -05:00
* @param CategoryRepositoryInterface $repository
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-06-27 04:44:18 -05:00
public function frontpage(CategoryRepositoryInterface $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-06-27 04:44:18 -05:00
$array = $repository->getCategoriesAndExpensesCorrected($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;
}
return ($left['sum'] < $right['sum']) ? 1 : -1;
}
);
2015-06-27 04:44:18 -05:00
$set = new Collection($array);
$data = $this->generator->frontpage($set);
return Response::json($data);
2015-05-16 02:41:14 -05:00
}
/**
* @param CategoryRepositoryInterface $repository
* @param Category $category
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-06-27 04:44:18 -05:00
public function month(CategoryRepositoryInterface $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('month');
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
$entries = new Collection;
2015-05-16 02:41:14 -05:00
while ($start <= $end) {
$spent = $repository->spentOnDaySumCorrected($category, $start);
2015-09-25 09:18:50 -05:00
$earned = $repository->earnedOnDaySumCorrected($category, $start);
if ($spent < 0) {
$earned = $spent * -1;
$spent = 0;
}
2015-06-27 04:44:18 -05:00
$entries->push([clone $start, $spent, $earned]);
2015-05-16 03:13:41 -05:00
$start->addDay();
2015-05-16 02:41:14 -05:00
}
2015-06-27 04:44:18 -05:00
$data = $this->generator->month($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:18:32 -05:00
* This chart will only show expenses.
*
2015-05-16 02:41:14 -05:00
* @param CategoryRepositoryInterface $repository
* @param $year
* @param bool $shared
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function spentInYear(CategoryRepositoryInterface $repository, $year, $shared = false)
2015-05-16 02:41:14 -05:00
{
2015-06-27 04:44:18 -05:00
$start = new Carbon($year . '-01-01');
$end = new Carbon($year . '-12-31');
2015-05-16 02:41:14 -05:00
2015-07-06 15:23:34 -05:00
$cache = new CacheProperties; // chart properties for cache:
2015-06-27 04:44:18 -05:00
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category');
$cache->addProperty('spent-in-year');
2015-06-27 04:44:18 -05:00
if ($cache->has()) {
2015-06-27 15:22:27 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-05-16 02:41:14 -05:00
}
$shared = $shared == 'shared' ? true : false;
$allCategories = $repository->getCategories();
$entries = new Collection;
2015-08-01 00:04:41 -05:00
$categories = $allCategories->filter(
function (Category $category) use ($repository, $start, $end, $shared) {
$spent = $repository->balanceInPeriod($category, $start, $end, $shared);
2015-08-01 00:04:41 -05:00
if ($spent < 0) {
return $category;
}
2015-08-01 00:04:41 -05:00
return null;
}
2015-08-01 00:04:41 -05:00
);
2015-06-27 04:44:18 -05:00
2015-05-16 02:41:14 -05:00
while ($start < $end) {
2015-07-06 15:23:34 -05:00
$month = clone $start; // month is the current end of the period
2015-05-16 02:41:14 -05:00
$month->endOfMonth();
2015-07-06 15:23:34 -05:00
$row = [clone $start]; // make a row:
2015-05-16 02:41:14 -05:00
2015-07-06 15:23:34 -05:00
foreach ($categories as $category) { // each budget, fill the row
$spent = $repository->balanceInPeriod($category, $start, $month, $shared);
if ($spent < 0) {
2015-08-01 00:04:41 -05:00
$row[] = $spent * -1;
} else {
$row[] = 0;
}
2015-05-16 02:41:14 -05:00
}
2015-06-27 04:44:18 -05:00
$entries->push($row);
2015-05-16 02:41:14 -05:00
$start->addMonth();
}
$data = $this->generator->spentInYear($categories, $entries);
$cache->store($data);
return Response::json($data);
}
/**
* This chart will only show income.
*
* @param CategoryRepositoryInterface $repository
* @param $year
* @param bool $shared
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function earnedInYear(CategoryRepositoryInterface $repository, $year, $shared = false)
{
$start = new Carbon($year . '-01-01');
$end = new Carbon($year . '-12-31');
$cache = new CacheProperties; // chart properties for cache:
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category');
$cache->addProperty('earned-in-year');
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
$shared = $shared == 'shared' ? true : false;
$allCategories = $repository->getCategories();
$allEntries = new Collection;
2015-08-01 00:04:41 -05:00
$categories = $allCategories->filter(
function (Category $category) use ($repository, $start, $end, $shared) {
$spent = $repository->balanceInPeriod($category, $start, $end, $shared);
2015-08-01 00:04:41 -05:00
if ($spent > 0) {
return $category;
}
2015-08-01 00:04:41 -05:00
return null;
}
2015-08-01 00:04:41 -05:00
);
while ($start < $end) {
$month = clone $start; // month is the current end of the period
$month->endOfMonth();
$row = [clone $start]; // make a row:
foreach ($categories as $category) { // each budget, fill the row
$spent = $repository->balanceInPeriod($category, $start, $month, $shared);
if ($spent > 0) {
$row[] = $spent;
} else {
$row[] = 0;
}
}
$allEntries->push($row);
$start->addMonth();
}
$data = $this->generator->earnedInYear($categories, $allEntries);
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-05-20 12:56:14 -05:00
}