/** * @param BudgetRepositoryInterface $repository * @param Budget $budget * * @return \Symfony\Component\HttpFoundation\Response */ public function budget(BudgetRepositoryInterface $repository, Budget $budget) { // dates and times $first = $repository->getFirstBudgetLimitDate($budget); $range = Preferences::get('viewRange', '1M')->data; $last = Session::get('end', new Carbon()); $final = clone $last; $final->addYears(2); $last = Navigation::endOfX($last, $range, $final); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($first); $cache->addProperty($last); $cache->addProperty('budget'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $entries = new Collection(); while ($first < $last) { $end = Navigation::addPeriod($first, $range, 0); $end->subDay(); $chartDate = clone $end; $chartDate->startOfMonth(); $spent = $repository->spentInPeriodCorrected($budget, $first, $end); $entries->push([$chartDate, $spent]); $first = Navigation::addPeriod($first, $range, 0); } $data = $this->generator->budget($entries); $cache->store($data); return Response::json($data); }
/** * @param BudgetRepositoryInterface $repository * @param Budget $budget * * @return \Symfony\Component\HttpFoundation\Response */ public function budget(BudgetRepositoryInterface $repository, Budget $budget) { // dates and times $first = $repository->getFirstBudgetLimitDate($budget); $range = Preferences::get('viewRange', '1M')->data; $last = Session::get('end', new Carbon()); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($first); $cache->addProperty($last); $cache->addProperty('budget'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $final = clone $last; $final->addYears(2); $last = Navigation::endOfX($last, $range, $final); $entries = new Collection(); // get all expenses: $set = $repository->getExpensesPerMonth($budget, $first, $last); while ($first < $last) { $monthFormatted = $first->format('Y-m'); $filtered = $set->filter(function (Budget $obj) use($monthFormatted) { return $obj->dateFormatted == $monthFormatted; }); $spent = is_null($filtered->first()) ? '0' : $filtered->first()->monthlyAmount; $entries->push([$first, round($spent * -1, 2)]); $first = Navigation::addPeriod($first, $range, 0); } $data = $this->generator->budget($entries); $cache->store($data); return Response::json($data); }