/** * Shows the amount left in a specific budget limit. * * @param BudgetRepositoryInterface $repository * @param Budget $budget * @param LimitRepetition $repetition * * @return \Symfony\Component\HttpFoundation\Response */ public function budgetLimit(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition) { $start = clone $repetition->startdate; $end = $repetition->enddate; $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('budget-limit'); $cache->addProperty($budget->id); $cache->addProperty($repetition->id); if ($cache->has()) { return Response::json($cache->get()); } $entries = new Collection(); $amount = $repetition->amount; $budgetCollection = new Collection([$budget]); while ($start <= $end) { $spent = $repository->spentInPeriod($budgetCollection, new Collection(), $start, $start); $amount = bcadd($amount, $spent); $entries->push([clone $start, round($amount, 2)]); $start->addDay(); } $data = $this->generator->budgetLimit($entries, 'month_and_day'); $cache->store($data); return Response::json($data); }
/** * Shows the amount left in a specific budget limit. * * @param BudgetRepositoryInterface $repository * @param Budget $budget * @param LimitRepetition $repetition * * @return \Symfony\Component\HttpFoundation\Response */ public function budgetLimit(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition) { $start = clone $repetition->startdate; $end = $repetition->enddate; bcscale(2); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('budget'); $cache->addProperty('limit'); $cache->addProperty($budget->id); $cache->addProperty($repetition->id); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $set = $repository->getExpensesPerDay($budget, $start, $end); $entries = new Collection(); $amount = $repetition->amount; // get sum (har har)! while ($start <= $end) { $formatted = $start->format('Y-m-d'); $filtered = $set->filter(function (Budget $obj) use($formatted) { return $obj->date == $formatted; }); $sum = is_null($filtered->first()) ? '0' : $filtered->first()->dailyAmount; /* * Sum of expenses on this day: */ $amount = round(bcadd($amount, $sum), 2); $entries->push([clone $start, $amount]); $start->addDay(); } $data = $this->generator->budgetLimit($entries); $cache->store($data); return Response::json($data); }