2015-05-16 02:41:14 -05:00
|
|
|
<?php
|
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;
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Models\Category;
|
2016-02-23 02:22:18 -06:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
2015-12-29 15:48:55 -06:00
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI;
|
2016-04-26 02:21:57 -05:00
|
|
|
use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface;
|
2016-04-26 14:40:15 -05:00
|
|
|
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;
|
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-02-12 10:34:42 -06:00
|
|
|
const MAKE_POSITIVE = -1;
|
|
|
|
const KEEP_POSITIVE = 1;
|
|
|
|
|
|
|
|
|
2016-01-28 14:50:20 -06:00
|
|
|
/** @var \FireflyIII\Generator\Chart\Category\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-01-28 14:50:20 -06:00
|
|
|
$this->generator = app('FireflyIII\Generator\Chart\Category\CategoryChartGeneratorInterface');
|
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:
|
2015-07-31 07:20:18 -05:00
|
|
|
$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;
|
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty('all');
|
|
|
|
$cache->addProperty('categories');
|
|
|
|
if ($cache->has()) {
|
2016-04-06 02:27:45 -05:00
|
|
|
return Response::json($cache->get());
|
2015-06-27 04:44:18 -05:00
|
|
|
}
|
2016-04-26 15:30:53 -05:00
|
|
|
$spentArray = $repository->spentPerDay($category, $start, $end, new Collection);
|
|
|
|
$earnedArray = $repository->earnedPerDay($category, $start, $end, new Collection);
|
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-01-15 11:21:59 -06:00
|
|
|
$spent = $this->getSumOfRange($start, $currentEnd, $spentArray);
|
|
|
|
$earned = $this->getSumOfRange($start, $currentEnd, $earnedArray);
|
|
|
|
$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);
|
2015-05-16 02:41:14 -05:00
|
|
|
}
|
|
|
|
|
2016-01-29 00:33:49 -06:00
|
|
|
/**
|
|
|
|
* @param SCRI $repository
|
|
|
|
* @param Category $category
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function currentPeriod(SCRI $repository, Category $category)
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a chart of what has been earned in this period in each category
|
|
|
|
* grouped by month.
|
|
|
|
*
|
|
|
|
* @param CRI $repository
|
|
|
|
* @param $reportType
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2016-02-05 02:25:15 -06:00
|
|
|
public function earnedInPeriod(CRI $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
2016-01-29 00:33:49 -06:00
|
|
|
{
|
|
|
|
$cache = new CacheProperties; // chart properties for cache:
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty($reportType);
|
|
|
|
$cache->addProperty($accounts);
|
|
|
|
$cache->addProperty('category');
|
|
|
|
$cache->addProperty('earned-in-period');
|
|
|
|
if ($cache->has()) {
|
2016-04-06 02:27:45 -05:00
|
|
|
return Response::json($cache->get());
|
2016-01-29 00:33:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
$set = $repository->earnedForAccountsPerMonth($accounts, $start, $end);
|
|
|
|
$categories = $set->unique('id')->sortBy(
|
|
|
|
function (Category $category) {
|
|
|
|
return $category->name;
|
|
|
|
}
|
|
|
|
);
|
2016-02-12 10:34:42 -06:00
|
|
|
$entries = $this->filterCollection($start, $end, $set, $categories);
|
|
|
|
$data = $this->generator->earnedInPeriod($categories, $entries);
|
2016-01-29 00:33:49 -06:00
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
|
|
|
}
|
2015-12-31 00:49:19 -06: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
|
|
|
*
|
2016-03-03 01:29:56 -06:00
|
|
|
* @param ARI $accountRepository
|
|
|
|
*
|
2015-05-16 02:41:14 -05:00
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2016-02-23 02:22:18 -06:00
|
|
|
public function frontpage(CRI $repository, ARI $accountRepository)
|
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-04-06 02:27:45 -05:00
|
|
|
return Response::json($cache->get());
|
2015-06-02 10:44:50 -05:00
|
|
|
}
|
|
|
|
|
2016-01-02 09:32:08 -06:00
|
|
|
// get data for categories (and "no category"):
|
2016-02-23 02:22:18 -06:00
|
|
|
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
|
|
|
|
$set = $repository->spentForAccountsPerMonth($accounts, $start, $end);
|
|
|
|
$outside = $repository->sumSpentNoCategory($accounts, $start, $end);
|
2016-01-02 09:32:08 -06:00
|
|
|
|
|
|
|
// this is a "fake" entry for the "no category" entry.
|
2016-01-15 06:08:25 -06:00
|
|
|
$entry = new stdClass();
|
|
|
|
$entry->name = trans('firefly.no_category');
|
2016-01-02 09:32:08 -06:00
|
|
|
$entry->spent = $outside;
|
|
|
|
$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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-28 13:04:54 -06:00
|
|
|
* @param $reportType
|
2015-12-16 06:08:26 -06: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-02-05 02:25:15 -06:00
|
|
|
public function multiYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
|
2015-12-16 06:08:26 -06:00
|
|
|
{
|
2015-12-30 01:15:04 -06:00
|
|
|
/** @var CRI $repository */
|
2015-12-29 15:44:13 -06:00
|
|
|
$repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
|
|
|
|
|
2015-12-16 06:08:26 -06:00
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties();
|
2015-12-28 13:04:54 -06:00
|
|
|
$cache->addProperty($reportType);
|
2015-12-16 06:08:26 -06:00
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty($accounts);
|
|
|
|
$cache->addProperty($categories);
|
|
|
|
$cache->addProperty('multiYearCategory');
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
2016-04-06 02:27:45 -05:00
|
|
|
return Response::json($cache->get());
|
2015-12-16 06:08:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
$entries = new Collection;
|
2016-01-15 10:53:54 -06:00
|
|
|
$set = $repository->listMultiYear($categories, $accounts, $start, $end);
|
2015-12-30 02:17:29 -06:00
|
|
|
|
2015-12-16 06:08:26 -06:00
|
|
|
/** @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;
|
2015-12-16 06:08:26 -06:00
|
|
|
$currentEnd = clone $currentStart;
|
|
|
|
$currentEnd->endOfYear();
|
|
|
|
|
2015-12-30 02:17:29 -06:00
|
|
|
|
2015-12-16 06:08:26 -06:00
|
|
|
// get data:
|
|
|
|
if (is_null($category->id)) {
|
|
|
|
$name = trans('firefly.noCategory');
|
2015-12-30 01:15:04 -06:00
|
|
|
$spent = $repository->sumSpentNoCategory($accounts, $currentStart, $currentEnd);
|
|
|
|
$earned = $repository->sumEarnedNoCategory($accounts, $currentStart, $currentEnd);
|
2015-12-16 06:08:26 -06:00
|
|
|
} 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();
|
|
|
|
|
2015-12-16 06:08:26 -06:00
|
|
|
$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;
|
2015-12-16 06:08:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2015-06-02 10:44:50 -05:00
|
|
|
return Response::json($data);
|
2015-05-16 02:41:14 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-30 15:30:11 -05:00
|
|
|
/**
|
|
|
|
* @param Category $category
|
|
|
|
* @param string $reportType
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function period(Category $category, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
|
|
|
{
|
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty($reportType);
|
|
|
|
$cache->addProperty($accounts);
|
|
|
|
$cache->addProperty($category->id);
|
|
|
|
$cache->addProperty('category');
|
|
|
|
$cache->addProperty('period');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return Response::json($cache->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var SingleCategoryRepositoryInterface $repository */
|
|
|
|
$repository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface');
|
|
|
|
// loop over period, add by users range:
|
|
|
|
$current = clone $start;
|
|
|
|
$viewRange = Preferences::get('viewRange', '1M')->data;
|
|
|
|
$format = strval(trans('config.month'));
|
|
|
|
$set = new Collection;
|
|
|
|
while ($current < $end) {
|
|
|
|
$currentStart = clone $current;
|
|
|
|
$currentEnd = Navigation::endOfPeriod($currentStart, $viewRange);
|
|
|
|
|
|
|
|
$spent = strval(array_sum($repository->spentPerDay($category, $currentStart, $currentEnd, $accounts)));
|
|
|
|
$earned = strval(array_sum($repository->earnedPerDay($category, $currentStart, $currentEnd, $accounts)));
|
|
|
|
|
|
|
|
$entry = [
|
|
|
|
$category->name,
|
|
|
|
$currentStart->formatLocalized($format),
|
|
|
|
$spent,
|
|
|
|
$earned,
|
|
|
|
|
|
|
|
];
|
|
|
|
$set->push($entry);
|
|
|
|
$currentEnd->addDay();
|
|
|
|
$current = clone $currentEnd;
|
|
|
|
}
|
|
|
|
$data = $this->generator->period($set);
|
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return Response::json($data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-26 00:18:12 -05:00
|
|
|
/**
|
2015-12-29 15:48:55 -06:00
|
|
|
* @param SCRI $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
|
|
|
|
*/
|
2015-12-29 15:48:55 -06:00
|
|
|
public function specificPeriod(SCRI $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:31:54 -06:00
|
|
|
/**
|
2016-01-29 00:33:49 -06:00
|
|
|
* Returns a chart of what has been spent 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
|
|
|
|
*
|
2016-01-15 13:48:06 -06:00
|
|
|
*
|
2015-12-25 00:31:54 -06:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2016-01-29 00:33:49 -06:00
|
|
|
public function spentInPeriod(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');
|
2016-01-29 00:33:49 -06:00
|
|
|
$cache->addProperty('spent-in-period');
|
2015-12-25 00:31:54 -06:00
|
|
|
if ($cache->has()) {
|
2016-04-06 02:27:45 -05:00
|
|
|
return Response::json($cache->get());
|
2015-12-25 00:31:54 -06:00
|
|
|
}
|
|
|
|
|
2016-02-12 10:34:42 -06:00
|
|
|
|
2016-01-29 00:33:49 -06:00
|
|
|
$set = $repository->spentForAccountsPerMonth($accounts, $start, $end);
|
2015-12-29 11:55:30 -06:00
|
|
|
$categories = $set->unique('id')->sortBy(
|
2015-12-25 00:42:00 -06:00
|
|
|
function (Category $category) {
|
|
|
|
return $category->name;
|
|
|
|
}
|
|
|
|
);
|
2016-02-17 15:35:57 -06:00
|
|
|
$entries = $this->filterCollection($start, $end, $set, $categories);
|
|
|
|
$entries = $this->invertSelection($entries);
|
|
|
|
$data = $this->generator->spentInPeriod($categories, $entries);
|
2016-02-12 10:34:42 -06:00
|
|
|
$cache->store($data);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $set
|
|
|
|
* @param Collection $categories
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
private function filterCollection(Carbon $start, Carbon $end, Collection $set, Collection $categories): Collection
|
|
|
|
{
|
|
|
|
$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:
|
2016-01-15 13:48:06 -06:00
|
|
|
$row = [clone $start];
|
2016-02-12 10:34:42 -06:00
|
|
|
$currentSet = $set->filter( // get possibly relevant entries from the big $set
|
2015-12-29 11:55:30 -06:00
|
|
|
function (Category $category) use ($start) {
|
2016-01-27 14:52:21 -06:00
|
|
|
return $category->dateFormatted == $start->format('Y-m');
|
2015-12-25 00:31:54 -06:00
|
|
|
}
|
|
|
|
);
|
|
|
|
/** @var Category $category */
|
2016-02-12 10:34:42 -06:00
|
|
|
foreach ($categories as $category) { // check for each category if its in the current set.
|
|
|
|
$entry = $currentSet->filter( // if its in there, use the value.
|
2015-12-29 11:55:30 -06:00
|
|
|
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)) {
|
2016-02-17 15:35:57 -06:00
|
|
|
$row[] = $entry->earned ? round($entry->earned, 2) : round($entry->spent, 2);
|
2015-12-25 00:31:54 -06:00
|
|
|
} else {
|
|
|
|
$row[] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$entries->push($row);
|
|
|
|
$start->addMonth();
|
|
|
|
}
|
|
|
|
|
2016-02-12 10:34:42 -06:00
|
|
|
return $entries;
|
2015-12-25 00:31:54 -06:00
|
|
|
}
|
|
|
|
|
2016-02-17 15:35:57 -06:00
|
|
|
/**
|
2016-02-17 23:15:01 -06:00
|
|
|
* Not the most elegant solution but it works.
|
|
|
|
*
|
2016-02-17 15:35:57 -06:00
|
|
|
* @param Collection $entries
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
private function invertSelection(Collection $entries): Collection
|
|
|
|
{
|
|
|
|
$result = new Collection;
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$new = [$entry[0]];
|
|
|
|
$count = count($entry);
|
|
|
|
for ($i = 1; $i < $count; $i++) {
|
|
|
|
$new[$i] = ($entry[$i] * -1);
|
|
|
|
}
|
|
|
|
$result->push($new);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-25 00:42:00 -06:00
|
|
|
/**
|
2016-01-29 00:33:49 -06:00
|
|
|
* @param SCRI $repository
|
|
|
|
* @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-01-29 00:33:49 -06:00
|
|
|
private function makePeriodChart(SCRI $repository, Category $category, Carbon $start, Carbon $end)
|
2015-12-25 00:42:00 -06:00
|
|
|
{
|
2016-01-29 00:33:49 -06:00
|
|
|
// chart properties for cache:
|
|
|
|
$cache = new CacheProperties;
|
2015-12-25 00:42:00 -06:00
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2016-01-29 00:33:49 -06:00
|
|
|
$cache->addProperty($category->id);
|
|
|
|
$cache->addProperty('specific-period');
|
2015-12-25 00:42:00 -06:00
|
|
|
if ($cache->has()) {
|
2016-02-04 00:27:03 -06:00
|
|
|
return $cache->get(); //
|
2015-12-25 00:42:00 -06:00
|
|
|
}
|
2016-01-29 00:33:49 -06:00
|
|
|
$entries = new Collection;
|
2015-12-25 00:42:00 -06:00
|
|
|
|
2016-01-29 00:33:49 -06:00
|
|
|
// get amount earned in period, grouped by day.
|
|
|
|
// get amount spent in period, grouped by day.
|
2016-04-26 15:30:53 -05:00
|
|
|
$spentArray = $repository->spentPerDay($category, $start, $end, new Collection);
|
|
|
|
$earnedArray = $repository->earnedPerDay($category, $start, $end, new Collection);
|
2015-12-29 11:55:30 -06:00
|
|
|
|
2016-01-29 00:33:49 -06:00
|
|
|
while ($start <= $end) {
|
|
|
|
$str = $start->format('Y-m-d');
|
2016-02-05 06:09:18 -06:00
|
|
|
$spent = $spentArray[$str] ?? '0';
|
|
|
|
$earned = $earnedArray[$str] ?? '0';
|
2016-01-29 00:33:49 -06:00
|
|
|
$date = Navigation::periodShow($start, '1D');
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2015-05-20 12:56:14 -05:00
|
|
|
}
|