2015-05-16 02:41:14 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Chart;
|
|
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2015-05-16 02:57:31 -05:00
|
|
|
use Crypt;
|
2015-05-16 02:41:14 -05:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Models\Category;
|
|
|
|
use FireflyIII\Models\LimitRepetition;
|
|
|
|
use FireflyIII\Models\Preference;
|
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
|
|
|
use Grumpydictator\Gchart\GChart;
|
|
|
|
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->spentInPeriod($category, $start, $currentEnd);
|
|
|
|
$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());
|
|
|
|
$set = $repository->getCategoriesAndExpenses($start, $end);
|
|
|
|
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
$isEncrypted = intval($entry->encrypted) == 1 ? true : false;
|
|
|
|
$name = strlen($entry->name) == 0 ? trans('firefly.noCategory') : $entry->name;
|
|
|
|
$name = $isEncrypted ? Crypt::decrypt($name) : $name;
|
|
|
|
$chart->addRow($name, floatval($entry->sum));
|
2015-05-16 02:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$chart->generate();
|
|
|
|
|
|
|
|
return Response::json($chart->getData());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
2015-05-16 03:13:41 -05:00
|
|
|
$spent = $repository->spentOnDaySum($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->spentInPeriod($category, $start, $month, $shared);
|
|
|
|
$row[] = $spent;
|
|
|
|
}
|
|
|
|
$chart->addRowArray($row);
|
|
|
|
|
|
|
|
$start->addMonth();
|
|
|
|
}
|
|
|
|
|
|
|
|
$chart->generate();
|
|
|
|
|
|
|
|
return Response::json($chart->getData());
|
|
|
|
}
|
|
|
|
}
|