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

153 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* CategoryController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Report;
use Carbon\Carbon;
use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Http\Controllers\Controller;
2016-12-03 13:38:13 -06:00
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
2016-12-03 14:24:55 -06:00
use Log;
2016-12-03 13:38:13 -06:00
use Navigation;
/**
* Class CategoryController
*
* @package FireflyIII\Http\Controllers\Report
*/
class CategoryController extends Controller
{
/**
* @param ReportHelperInterface $helper
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function categoryReport(ReportHelperInterface $helper, Carbon $start, Carbon $end, Collection $accounts)
{
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
return $cache->get();
}
$categories = $helper->getCategoryReport($start, $end, $accounts);
$result = view('reports.partials.categories', compact('categories'))->render();
$cache->store($result);
return $result;
}
2016-12-03 13:38:13 -06:00
/**
2016-12-06 00:48:41 -06:00
* @param Collection $accounts
2016-12-03 13:38:13 -06:00
* @param Carbon $start
* @param Carbon $end
*
2016-12-06 00:48:41 -06:00
* @return mixed|string
2016-12-03 13:38:13 -06:00
*/
2016-12-06 00:48:41 -06:00
public function expenses(Collection $accounts, Carbon $start, Carbon $end)
2016-12-03 13:38:13 -06:00
{
2016-12-03 14:24:55 -06:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category-period-expenses-report');
2016-12-03 14:24:55 -06:00
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
Log::debug('Return report from cache');
return $cache->get();
}
2016-12-03 13:38:13 -06:00
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
$data = $repository->periodExpenses($categories, $accounts, $start, $end);
$data[0] = $repository->periodExpensesNoCategory($accounts, $start, $end);
$report = $this->filterReport($data);
2016-12-03 14:03:20 -06:00
$periods = Navigation::listOfPeriods($start, $end);
$result = view('reports.partials.category-period', compact('report', 'periods'))->render();
2016-12-03 13:38:13 -06:00
2016-12-03 14:24:55 -06:00
$cache->store($result);
2016-12-03 13:38:13 -06:00
return $result;
}
/**
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return string
*/
2016-12-06 00:48:41 -06:00
public function income(Collection $accounts, Carbon $start, Carbon $end)
{
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('category-period-income-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
Log::debug('Return report from cache');
return $cache->get();
}
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
$data = $repository->periodIncome($categories, $accounts, $start, $end);
$data[0] = $repository->periodIncomeNoCategory($accounts, $start, $end);
$report = $this->filterReport($data);
$periods = Navigation::listOfPeriods($start, $end);
$result = view('reports.partials.category-period', compact('report', 'periods'))->render();
$cache->store($result);
return $result;
}
2016-12-03 13:38:13 -06:00
/**
* Filters empty results from category period report
*
* @param array $data
*
* @return array
*/
private function filterReport(array $data): array
2016-12-03 13:38:13 -06:00
{
foreach ($data as $categoryId => $set) {
$sum = '0';
foreach ($set['entries'] as $amount) {
$sum = bcadd($amount, $sum);
}
$data[$categoryId]['sum'] = $sum;
if (bccomp('0', $sum) === 0) {
unset($data[$categoryId]);
2016-12-03 13:38:13 -06:00
}
}
2016-12-03 14:24:55 -06:00
2016-12-03 13:38:13 -06:00
return $data;
}
}