2015-05-16 02:41:14 -05:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* CategoryController.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-05-16 02:41:14 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Chart;
|
|
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2016-07-02 10:39:58 -05:00
|
|
|
use FireflyIII\Crud\Account\AccountCrudInterface;
|
2016-05-02 13:49:19 -05:00
|
|
|
use FireflyIII\Generator\Chart\Category\CategoryChartGeneratorInterface;
|
2015-05-16 02:41:14 -05:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2016-07-02 10:39:58 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
2015-05-16 02:41:14 -05:00
|
|
|
use FireflyIII\Models\Category;
|
2015-12-29 15:48:55 -06:00
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI;
|
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;
|
2016-01-02 09:32:08 -06:00
|
|
|
use stdClass;
|
2015-05-16 02:41:14 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CategoryController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers\Chart
|
|
|
|
*/
|
|
|
|
class CategoryController extends Controller
|
|
|
|
{
|
2016-05-02 13:49:19 -05:00
|
|
|
/** @var CategoryChartGeneratorInterface */
|
2015-06-27 04:44:18 -05:00
|
|
|
protected $generator;
|
|
|
|
|
|
|
|
/**
|
2016-02-04 00:27:03 -06:00
|
|
|
*
|
2015-06-27 04:44:18 -05:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
// create chart generator:
|
2016-05-02 13:49:19 -05:00
|
|
|
$this->generator = app(CategoryChartGeneratorInterface::class);
|
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
|
|
|
*
|
2016-07-02 10:39:58 -05:00
|
|
|
* @param CRI $repository
|
|
|
|
* @param AccountCrudInterface $crud
|
|
|
|
* @param Category $category
|
2015-05-16 02:41:14 -05:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2016-07-02 10:39:58 -05:00
|
|
|
public function all(CRI $repository, AccountCrudInterface $crud, Category $category)
|
2015-05-16 02:41:14 -05:00
|
|
|
{
|
2016-05-09 13:15:26 -05: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;
|
2016-07-02 10:39:58 -05:00
|
|
|
$accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
2015-06-27 04:44:18 -05:00
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty('all');
|
|
|
|
$cache->addProperty('categories');
|
|
|
|
if ($cache->has()) {
|
2016-05-13 08:53:39 -05:00
|
|
|
return Response::json($cache->get());
|
2015-06-27 04:44:18 -05:00
|
|
|
}
|
2015-12-31 00:49:19 -06: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);
|
2016-07-02 10:39:58 -05:00
|
|
|
$spent = $repository->spentInPeriod($categoryCollection, $accounts, $start, $currentEnd);
|
|
|
|
$earned = $repository->earnedInPeriod($categoryCollection, $accounts, $start, $currentEnd);
|
2016-05-15 11:36:40 -05:00
|
|
|
$date = Navigation::periodShow($start, $range);
|
2015-09-25 12:28:39 -05:00
|
|
|
$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
|
|
|
}
|
2015-09-25 13:13:43 -05:00
|
|
|
$entries = $entries->reverse();
|
2015-09-25 13:15:38 -05:00
|
|
|
$entries = $entries->slice(0, 48);
|
2015-09-25 13:13:43 -05:00
|
|
|
$entries = $entries->reverse();
|
2016-01-15 11:21:59 -06:00
|
|
|
$data = $this->generator->all($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);
|
2016-05-09 13:15:26 -05:00
|
|
|
|
2015-05-16 02:41:14 -05:00
|
|
|
}
|
|
|
|
|
2016-01-29 00:33:49 -06:00
|
|
|
/**
|
2016-05-09 13:15:26 -05:00
|
|
|
* @param CRI $repository
|
2016-01-29 00:33:49 -06:00
|
|
|
* @param Category $category
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2016-05-08 06:45:23 -05:00
|
|
|
public function currentPeriod(CRI $repository, Category $category)
|
2016-01-29 00:33:49 -06:00
|
|
|
{
|
2016-02-04 00:27:03 -06:00
|
|
|
$start = clone session('start', Carbon::now()->startOfMonth());
|
|
|
|
$end = session('end', Carbon::now()->endOfMonth());
|
2016-01-29 00:33:49 -06:00
|
|
|
$data = $this->makePeriodChart($repository, $category, $start, $end);
|
|
|
|
|
|
|
|
return Response::json($data);
|
|
|
|
}
|
|
|
|
|
2015-05-16 02:41:14 -05:00
|
|
|
/**
|
2016-09-15 12:14:22 -05:00
|
|
|
* @param CRI $repository
|
|
|
|
* @param AccountCrudInterface $crud
|
2015-05-16 02:41:14 -05:00
|
|
|
*
|
2016-09-15 12:14:22 -05:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2015-05-16 02:41:14 -05:00
|
|
|
*/
|
2016-09-15 12:14:22 -05:00
|
|
|
public function frontpage(CRI $repository, AccountCrudInterface $crud)
|
2015-05-16 02:41:14 -05:00
|
|
|
{
|
2016-02-04 00:27:03 -06:00
|
|
|
$start = session('start', Carbon::now()->startOfMonth());
|
|
|
|
$end = session('end', Carbon::now()->endOfMonth());
|
2015-06-02 10:44:50 -05:00
|
|
|
// 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()) {
|
2016-05-13 08:53:39 -05:00
|
|
|
return Response::json($cache->get());
|
2016-05-09 13:15:26 -05:00
|
|
|
}
|
|
|
|
$categories = $repository->getCategories();
|
2016-09-15 12:14:22 -05:00
|
|
|
$accounts = $crud->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
|
2016-05-09 13:15:26 -05:00
|
|
|
$set = new Collection;
|
|
|
|
/** @var Category $category */
|
|
|
|
foreach ($categories as $category) {
|
2016-09-15 12:14:22 -05:00
|
|
|
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $end);
|
2016-05-09 13:15:26 -05:00
|
|
|
if (bccomp($spent, '0') === -1) {
|
|
|
|
$category->spent = $spent;
|
|
|
|
$set->push($category);
|
|
|
|
}
|
2015-06-02 10:44:50 -05:00
|
|
|
}
|
2016-01-02 09:32:08 -06:00
|
|
|
// this is a "fake" entry for the "no category" entry.
|
2016-05-09 13:15:26 -05:00
|
|
|
$entry = new stdClass;
|
2016-01-15 06:08:25 -06:00
|
|
|
$entry->name = trans('firefly.no_category');
|
2016-05-09 13:15:26 -05:00
|
|
|
$entry->spent = $repository->spentInPeriodWithoutCategory(new Collection, $start, $end);
|
2016-01-02 09:32:08 -06:00
|
|
|
$set->push($entry);
|
|
|
|
|
2016-01-15 06:08:25 -06:00
|
|
|
$set = $set->sortBy('spent');
|
2015-06-27 04:44:18 -05:00
|
|
|
$data = $this->generator->frontpage($set);
|
2015-12-16 06:08:26 -06:00
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return Response::json($data);
|
2016-05-09 13:15:26 -05:00
|
|
|
|
2015-12-16 06:08:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-11 10:17:43 -05:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Collection $categories
|
2015-12-28 00:55:09 -06:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2015-12-16 06:08:26 -06:00
|
|
|
*/
|
2016-05-11 03:02:27 -05:00
|
|
|
public function multiYear(Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
|
2015-12-16 06:08:26 -06:00
|
|
|
{
|
2016-05-11 03:02:27 -05:00
|
|
|
|
|
|
|
/** @var CRI $repository */
|
|
|
|
$repository = app(CRI::class);
|
|
|
|
|
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty($accounts);
|
|
|
|
$cache->addProperty($categories);
|
|
|
|
$cache->addProperty('multiYearCategory');
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
2016-05-13 08:53:39 -05:00
|
|
|
return Response::json($cache->get());
|
2016-05-11 03:02:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$entries = new Collection;
|
|
|
|
|
|
|
|
/** @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)) {
|
2016-05-20 10:53:03 -05:00
|
|
|
$entry['name'] = trans('firefly.noCategory');
|
|
|
|
$entry['spent'][$year] = ($repository->spentInPeriodWithoutCategory($accounts, $currentStart, $currentEnd) * -1);
|
|
|
|
$entry['earned'][$year] = $repository->earnedInPeriodWithoutCategory($accounts, $currentStart, $currentEnd);
|
2016-05-11 03:02:27 -05:00
|
|
|
|
2016-05-20 10:53:03 -05:00
|
|
|
// jump to next year.
|
|
|
|
$currentStart = clone $currentEnd;
|
|
|
|
$currentStart->addDay();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
// alternative is a normal category:
|
|
|
|
$entry['name'] = $category->name;
|
|
|
|
$entry['spent'][$year] = ($repository->spentInPeriod(new Collection([$category]), $accounts, $currentStart, $currentEnd) * -1);
|
|
|
|
$entry['earned'][$year] = $repository->earnedInPeriod(new Collection([$category]), $accounts, $currentStart, $currentEnd);
|
2016-05-11 03:02:27 -05:00
|
|
|
|
|
|
|
// jump to next year.
|
|
|
|
$currentStart = clone $currentEnd;
|
|
|
|
$currentStart->addDay();
|
|
|
|
}
|
|
|
|
$entries->push($entry);
|
|
|
|
}
|
|
|
|
|
2016-05-20 10:53:03 -05:00
|
|
|
// generate chart with data:
|
2016-05-11 03:02:27 -05:00
|
|
|
$data = $this->generator->multiYear($entries);
|
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return Response::json($data);
|
|
|
|
|
2015-05-16 02:41:14 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-30 15:30:11 -05:00
|
|
|
/**
|
2016-05-09 13:15:26 -05:00
|
|
|
* @param CRI $repository
|
2015-09-26 00:18:12 -05:00
|
|
|
* @param Category $category
|
|
|
|
*
|
2015-12-28 00:55:09 -06:00
|
|
|
* @param $date
|
|
|
|
*
|
2015-09-26 00:18:12 -05:00
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2016-05-08 06:45:23 -05:00
|
|
|
public function specificPeriod(CRI $repository, Category $category, $date)
|
2015-09-26 00:18:12 -05:00
|
|
|
{
|
|
|
|
$carbon = new Carbon($date);
|
|
|
|
$range = Preferences::get('viewRange', '1M')->data;
|
|
|
|
$start = Navigation::startOfPeriod($carbon, $range);
|
|
|
|
$end = Navigation::endOfPeriod($carbon, $range);
|
2016-01-29 00:33:49 -06:00
|
|
|
$data = $this->makePeriodChart($repository, $category, $start, $end);
|
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:42:00 -06:00
|
|
|
/**
|
2016-05-09 13:15:26 -05:00
|
|
|
* @param CRI $repository
|
2016-01-29 00:33:49 -06:00
|
|
|
* @param Category $category
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2016-01-15 12:37:09 -06:00
|
|
|
*
|
2016-01-29 00:33:49 -06:00
|
|
|
* @return array
|
2015-12-25 00:42:00 -06:00
|
|
|
*/
|
2016-05-08 06:45:23 -05:00
|
|
|
private function makePeriodChart(CRI $repository, Category $category, Carbon $start, Carbon $end)
|
2015-12-25 00:42:00 -06:00
|
|
|
{
|
2016-05-09 13:15:26 -05:00
|
|
|
$categoryCollection = new Collection([$category]);
|
|
|
|
$cache = new CacheProperties;
|
2016-07-02 10:42:27 -05:00
|
|
|
/** @var AccountCrudInterface $crud */
|
|
|
|
$crud = app(AccountCrudInterface::class);
|
|
|
|
$accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
|
|
|
|
2015-12-25 00:42:00 -06:00
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2016-07-02 10:42:27 -05:00
|
|
|
$cache->addProperty($accounts);
|
2016-01-29 00:33:49 -06:00
|
|
|
$cache->addProperty($category->id);
|
|
|
|
$cache->addProperty('specific-period');
|
2016-05-09 13:15:26 -05:00
|
|
|
|
2016-07-02 10:42:27 -05:00
|
|
|
|
2015-12-25 00:42:00 -06:00
|
|
|
if ($cache->has()) {
|
2016-05-15 11:36:40 -05:00
|
|
|
return $cache->get();
|
2015-12-25 00:42:00 -06:00
|
|
|
}
|
2016-01-29 00:33:49 -06:00
|
|
|
$entries = new Collection;
|
|
|
|
while ($start <= $end) {
|
2016-07-02 10:42:27 -05:00
|
|
|
$spent = $repository->spentInPeriod($categoryCollection, $accounts, $start, $start);
|
|
|
|
$earned = $repository->earnedInPeriod($categoryCollection, $accounts, $start, $start);
|
2016-05-15 11:36:40 -05:00
|
|
|
$date = Navigation::periodShow($start, '1D');
|
2016-01-29 00:33:49 -06:00
|
|
|
$entries->push([clone $start, $date, $spent, $earned]);
|
|
|
|
$start->addDay();
|
2015-12-25 00:42:00 -06:00
|
|
|
}
|
2016-01-29 00:33:49 -06:00
|
|
|
|
|
|
|
$data = $this->generator->period($entries);
|
2015-12-25 00:42:00 -06:00
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return $data;
|
2016-05-09 13:15:26 -05:00
|
|
|
|
2015-12-25 00:42:00 -06:00
|
|
|
}
|
|
|
|
|
2015-05-20 12:56:14 -05:00
|
|
|
}
|