/** * 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); }