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

197 lines
5.7 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
namespace FireflyIII\Http\Controllers\Chart;
use Cache;
2015-05-16 02:41:14 -05:00
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-05-16 02:41:14 -05:00
use Grumpydictator\Gchart\GChart;
use Log;
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-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 GChart $chart
* @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-05-16 03:13:41 -05:00
public function all(GChart $chart, 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;
// jump to start of week / month / year / etc
$start = Navigation::startOfPeriod($start, $range);
$chart->addColumn(trans('firefly.period'), 'date');
2015-05-16 02:41:14 -05:00
$chart->addColumn(trans('firefly.spent'), 'number');
2015-05-16 03:13:41 -05:00
$end = new Carbon;
while ($start <= $end) {
$currentEnd = Navigation::endOfPeriod($start, $range);
$spent = $repository->spentInPeriodCorrected($category, $start, $currentEnd);
2015-05-16 03:13:41 -05:00
$chart->addRow(clone $start, $spent);
$start = Navigation::addPeriod($start, $range, 0);
2015-05-16 02:41:14 -05:00
}
$chart->generate();
return Response::json($chart->getData());
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 GChart $chart
* @param CategoryRepositoryInterface $repository
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-05-16 03:13:41 -05:00
public function frontpage(GChart $chart, CategoryRepositoryInterface $repository)
2015-05-16 02:41:14 -05:00
{
2015-05-16 03:13:41 -05:00
$chart->addColumn(trans('firefly.category'), 'string');
2015-05-16 02:41:14 -05:00
$chart->addColumn(trans('firefly.spent'), 'number');
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 11:22:47 -05:00
$chartProperties = new CacheProperties;
$chartProperties->addProperty($start);
$chartProperties->addProperty($end);
$chartProperties->addProperty('category');
$chartProperties->addProperty('frontpage');
2015-06-03 14:15:52 -05:00
if ($chartProperties->has()) {
return Response::json($chartProperties->get());
}
2015-06-03 14:15:52 -05:00
$md5 = $chartProperties->getMd5();
$set = $repository->getCategoriesAndExpensesCorrected($start, $end);
2015-05-16 03:13:41 -05:00
2015-05-21 11:57:14 -05:00
// sort by callback:
uasort(
$set,
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-05-16 03:13:41 -05:00
foreach ($set as $entry) {
2015-05-20 11:09:44 -05:00
$sum = floatval($entry['sum']);
2015-05-20 12:55:53 -05:00
if ($sum != 0) {
$chart->addRow($entry['name'], $sum);
2015-05-20 11:09:44 -05:00
}
2015-05-16 02:41:14 -05:00
}
$chart->generate();
$data = $chart->getData();
Cache::forever($md5, $data);
return Response::json($data);
2015-05-16 02:41:14 -05:00
}
/**
* @param GChart $chart
* @param CategoryRepositoryInterface $repository
* @param Category $category
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-05-16 03:13:41 -05:00
public function month(GChart $chart, 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-05-16 02:41:14 -05:00
$chart->addColumn(trans('firefly.period'), 'date');
$chart->addColumn(trans('firefly.spent'), 'number');
while ($start <= $end) {
$spent = $repository->spentOnDaySumCorrected($category, $start);
2015-05-16 02:41:14 -05:00
$chart->addRow(clone $start, $spent);
2015-05-16 03:13:41 -05:00
$start->addDay();
2015-05-16 02:41:14 -05:00
}
$chart->generate();
return Response::json($chart->getData());
}
/**
2015-05-16 03:18:32 -05:00
* This chart will only show expenses.
*
2015-05-16 02:41:14 -05:00
* @param GChart $chart
* @param CategoryRepositoryInterface $repository
* @param $year
* @param bool $shared
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function year(GChart $chart, CategoryRepositoryInterface $repository, $year, $shared = false)
{
$start = new Carbon($year . '-01-01');
$end = new Carbon($year . '-12-31');
$shared = $shared == 'shared' ? true : false;
$categories = $repository->getCategories();
// add columns:
$chart->addColumn(trans('firefly.month'), 'date');
foreach ($categories as $category) {
$chart->addColumn($category->name, 'number');
}
while ($start < $end) {
// month is the current end of the period:
$month = clone $start;
$month->endOfMonth();
// make a row:
$row = [clone $start];
// each budget, fill the row:
foreach ($categories as $category) {
$spent = $repository->spentInPeriodCorrected($category, $start, $month, $shared);
2015-05-16 02:41:14 -05:00
$row[] = $spent;
}
$chart->addRowArray($row);
$start->addMonth();
}
$chart->generate();
return Response::json($chart->getData());
}
2015-05-20 12:56:14 -05:00
}