/**
  * @param Carbon     $current
  * @param Carbon     $end
  * @param Budget     $budget
  * @param Collection $accounts
  *
  * @return array
  */
 private function getBudgetSpentData(Carbon $current, Carbon $end, Budget $budget, Collection $accounts) : array
 {
     $sum = '0';
     $spent = [];
     while ($current < $end) {
         $currentEnd = clone $current;
         $currentEnd->endOfMonth();
         $format = $current->format('m-Y');
         $budgetSpent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $current, $currentEnd);
         $spent[$format] = $budgetSpent;
         $sum = bcadd($sum, $budgetSpent);
         $current->addMonth();
     }
     return ['spent' => $spent, 'sum' => $sum];
 }
 /**
  * @param Budget          $budget
  * @param LimitRepetition $repetition
  * @param Collection      $accounts
  *
  * @return BalanceLine
  */
 private function createBalanceLine(Budget $budget, LimitRepetition $repetition, Collection $accounts) : BalanceLine
 {
     $line = new BalanceLine();
     $budget->amount = $repetition->amount;
     $line->setBudget($budget);
     $line->setStartDate($repetition->startdate);
     $line->setEndDate($repetition->enddate);
     // loop accounts:
     foreach ($accounts as $account) {
         $balanceEntry = new BalanceEntry();
         $balanceEntry->setAccount($account);
         $spent = $this->budgetRepository->spentInPeriod(new Collection([$budget]), new Collection([$account]), $repetition->startdate, $repetition->enddate);
         $balanceEntry->setSpent($spent);
         $line->addBalanceEntry($balanceEntry);
     }
     return $line;
 }
 /**
  * @param BudgetRepositoryInterface $repository
  * @param Budget                    $budget
  * @param LimitRepetition           $repetition
  *
  * @return View
  * @throws FireflyException
  */
 public function showWithRepetition(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition)
 {
     if ($repetition->budgetLimit->budget->id != $budget->id) {
         throw new FireflyException('This budget limit is not part of this budget.');
     }
     $start = $repetition->startdate;
     $end = $repetition->enddate;
     $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
     $pageSize = Preferences::get('transactionPageSize', 50)->data;
     $offset = ($page - 1) * $pageSize;
     $journals = $repository->journalsInPeriod(new Collection([$budget]), new Collection(), $start, $end);
     $count = $journals->count();
     $journals = $journals->slice($offset, $pageSize);
     $journals = new LengthAwarePaginator($journals, $count, $pageSize);
     $subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]);
     $journals->setPath('/budgets/show/' . $budget->id . '/' . $repetition->id);
     $repetition->spent = $repository->spentInPeriod(new Collection([$budget]), new Collection(), $repetition->startdate, $repetition->enddate);
     $limits = new Collection([$repetition]);
     return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle'));
 }
 /**
  * @param BudgetRepositoryInterface $repository
  * @param Budget                    $budget
  * @param Carbon                    $start
  * @param Carbon                    $end
  *
  * @return Collection
  */
 private function spentInPeriodSingle(BudgetRepositoryInterface $repository, Budget $budget, Carbon $start, Carbon $end) : Collection
 {
     $collection = new Collection();
     $amount = '0';
     $left = '0';
     $spent = $repository->spentInPeriod(new Collection([$budget]), new Collection(), $start, $end);
     $overspent = '0';
     $array = [$budget->name, $left, $spent, $overspent, $amount, $spent];
     $collection->push($array);
     return $collection;
 }