Ejemplo n.º 1
0
 /**
  *
  * @param BudgetRepositoryInterface $repository
  * @param                           $reportType
  * @param Carbon                    $start
  * @param Carbon                    $end
  * @param Collection                $accounts
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function year(BudgetRepositoryInterface $repository, $reportType, Carbon $start, Carbon $end, Collection $accounts)
 {
     // chart properties for cache:
     $cache = new CacheProperties();
     $cache->addProperty($start);
     $cache->addProperty($end);
     $cache->addProperty($reportType);
     $cache->addProperty($accounts);
     $cache->addProperty('budget');
     $cache->addProperty('year');
     if ($cache->has()) {
         return Response::json($cache->get());
         // @codeCoverageIgnore
     }
     $budgetInformation = $repository->getBudgetsAndExpensesPerMonth($accounts, $start, $end);
     $budgets = new Collection();
     $entries = new Collection();
     /** @var array $row */
     foreach ($budgetInformation as $row) {
         $budgets->push($row['budget']);
     }
     while ($start < $end) {
         // month is the current end of the period:
         $month = clone $start;
         $month->endOfMonth();
         $row = [clone $start];
         $dateFormatted = $start->format('Y-m');
         // each budget, check if there is an entry for this month:
         /** @var array $row */
         foreach ($budgetInformation as $budgetRow) {
             $spent = 0;
             // nothing spent.
             if (isset($budgetRow['entries'][$dateFormatted])) {
                 $spent = $budgetRow['entries'][$dateFormatted] * -1;
                 // to fit array
             }
             $row[] = $spent;
         }
         $entries->push($row);
         $start->endOfMonth()->addDay();
     }
     $data = $this->generator->year($budgets, $entries);
     $cache->store($data);
     return Response::json($data);
 }
 /**
  * @param BudgetRepositoryInterface $repository
  * @param Budget                    $budget
  * @param Carbon                    $start
  * @param Carbon                    $end
  * @param Collection                $accounts
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function period(BudgetRepositoryInterface $repository, Budget $budget, Carbon $start, Carbon $end, Collection $accounts)
 {
     // chart properties for cache:
     $cache = new CacheProperties();
     $cache->addProperty($start);
     $cache->addProperty($end);
     $cache->addProperty($accounts);
     $cache->addProperty($budget->id);
     $cache->addProperty('budget');
     $cache->addProperty('period');
     if ($cache->has()) {
         return Response::json($cache->get());
     }
     // loop over period, add by users range:
     $current = clone $start;
     $viewRange = Preferences::get('viewRange', '1M')->data;
     $set = new Collection();
     $repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
     while ($current < $end) {
         $currentStart = clone $current;
         $currentEnd = Navigation::endOfPeriod($currentStart, $viewRange);
         $reps = $repetitions->filter(function (LimitRepetition $repetition) use($budget, $currentStart) {
             if ($repetition->budget_id === $budget->id && $repetition->startdate == $currentStart) {
                 return true;
             }
             return false;
         });
         $budgeted = $reps->sum('amount');
         $spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $currentStart, $currentEnd);
         $entry = ['date' => clone $currentStart, 'budgeted' => $budgeted, 'spent' => $spent];
         $set->push($entry);
         $currentEnd->addDay();
         $current = clone $currentEnd;
     }
     $data = $this->generator->period($set, $viewRange);
     $cache->store($data);
     return Response::json($data);
 }