Example #1
0
 /**
  * @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);
 }