addProperty($start); $cache->addProperty($end); $cache->addProperty('category-period-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(); $report = $repository->getCategoryPeriodReport($categories, $accounts, $start, $end, true); $report = $this->filterCategoryPeriodReport($report); $periods = Navigation::listOfPeriods($start, $end); $result = view('reports.partials.category-period', compact('categories', 'periods', 'report'))->render(); $cache->store($result); return $result; } /** * @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; } /** * Filters empty results from category period report * * @param array $data * * @return array */ private function filterCategoryPeriodReport(array $data): array { /** * @var string $type * @var array $report */ foreach ($data as $type => $report) { foreach ($report as $categoryId => $set) { $sum = '0'; foreach ($set['entries'] as $amount) { $sum = bcadd($amount, $sum); } $data[$type][$categoryId]['sum'] = $sum; if (bccomp('0', $sum) === 0) { unset($data[$type][$categoryId]); } } } return $data; } /** * @param int $categoryId * @param Collection $categories * * @return string */ private function getCategoryName(int $categoryId, Collection $categories): string { $first = $categories->filter( function (Category $category) use ($categoryId) { return $categoryId === $category->id; } ); if (!is_null($first->first())) { return $first->first()->name; } return '(unknown)'; } }