/** * Show an overview for a category for all time, per month/week/year. * * @param CRI $repository * @param AccountCrudInterface $crud * @param Category $category * * @return \Symfony\Component\HttpFoundation\Response */ public function all(CRI $repository, AccountCrudInterface $crud, Category $category) { $start = $repository->firstUseDate($category); $range = Preferences::get('viewRange', '1M')->data; $start = Navigation::startOfPeriod($start, $range); $categoryCollection = new Collection([$category]); $end = new Carbon(); $entries = new Collection(); $cache = new CacheProperties(); $accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('all'); $cache->addProperty('categories'); if ($cache->has()) { return Response::json($cache->get()); } while ($start <= $end) { $currentEnd = Navigation::endOfPeriod($start, $range); $spent = $repository->spentInPeriod($categoryCollection, $accounts, $start, $currentEnd); $earned = $repository->earnedInPeriod($categoryCollection, $accounts, $start, $currentEnd); $date = Navigation::periodShow($start, $range); $entries->push([clone $start, $date, $spent, $earned]); $start = Navigation::addPeriod($start, $range, 0); } $entries = $entries->reverse(); $entries = $entries->slice(0, 48); $entries = $entries->reverse(); $data = $this->generator->all($entries); $cache->store($data); return Response::json($data); }
/** * @param CRI $repository * @param AccountCrudInterface $crud * @param Category $category * * @return View */ public function show(CRI $repository, AccountCrudInterface $crud, Category $category) { $range = Preferences::get('viewRange', '1M')->data; /** @var Carbon $start */ $start = session('start', Navigation::startOfPeriod(new Carbon(), $range)); /** @var Carbon $end */ $end = session('end', Navigation::endOfPeriod(new Carbon(), $range)); $hideCategory = true; // used in list. $page = intval(Input::get('page')); $pageSize = Preferences::get('transactionPageSize', 50)->data; $offset = ($page - 1) * $pageSize; $set = $repository->journalsInPeriod(new Collection([$category]), new Collection(), [], $start, $end); $count = $set->count(); $subSet = $set->splice($offset, $pageSize); $subTitle = $category->name; $subTitleIcon = 'fa-bar-chart'; $journals = new LengthAwarePaginator($subSet, $count, $pageSize, $page); $journals->setPath('categories/show/' . $category->id); // oldest transaction in category: $start = $repository->firstUseDate($category); if ($start->year == 1900) { $start = new Carbon(); } $range = Preferences::get('viewRange', '1M')->data; $start = Navigation::startOfPeriod($start, $range); $end = Navigation::endOfX(new Carbon(), $range); $entries = new Collection(); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('category-show'); $cache->addProperty($category->id); if ($cache->has()) { $entries = $cache->get(); return view('categories.show', compact('category', 'journals', 'entries', 'subTitleIcon', 'hideCategory', 'subTitle')); } $categoryCollection = new Collection([$category]); $accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); while ($end >= $start) { $end = Navigation::startOfPeriod($end, $range); $currentEnd = Navigation::endOfPeriod($end, $range); $spent = $repository->spentInPeriod($categoryCollection, $accounts, $end, $currentEnd); $earned = $repository->earnedInPeriod($categoryCollection, $accounts, $end, $currentEnd); $dateStr = $end->format('Y-m-d'); $dateName = Navigation::periodShow($end, $range); $entries->push([$dateStr, $dateName, $spent, $earned]); $end = Navigation::subtractPeriod($end, $range, 1); } $cache->store($entries); return view('categories.show', compact('category', 'journals', 'entries', 'hideCategory', 'subTitle')); }