/** * @param Carbon $start * @param Carbon $end * @param Collection $accounts * * @return BudgetCollection */ public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts) : BudgetCollection { $object = new BudgetCollection(); $set = $this->repository->getBudgets(); /** @var Budget $budget */ foreach ($set as $budget) { $repetitions = $budget->limitrepetitions()->before($end)->after($start)->get(); // no repetition(s) for this budget: if ($repetitions->count() == 0) { // spent for budget in time range: $spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end); if ($spent > 0) { $budgetLine = new BudgetLine(); $budgetLine->setBudget($budget)->setOverspent($spent); $object->addOverspent($spent)->addBudgetLine($budgetLine); } continue; } // one or more repetitions for budget: /** @var LimitRepetition $repetition */ foreach ($repetitions as $repetition) { $data = $this->calculateExpenses($budget, $repetition, $accounts); $budgetLine = new BudgetLine(); $budgetLine->setBudget($budget)->setRepetition($repetition)->setLeft($data['left'])->setSpent($data['expenses'])->setOverspent($data['overspent'])->setBudgeted(strval($repetition->amount)); $object->addBudgeted(strval($repetition->amount))->addSpent($data['spent'])->addLeft($data['left'])->addOverspent($data['overspent'])->addBudgetLine($budgetLine); } } // stuff outside of budgets: $noBudget = $this->repository->spentInPeriodWithoutBudget($accounts, $start, $end); $budgetLine = new BudgetLine(); $budgetLine->setOverspent($noBudget)->setSpent($noBudget); $object->addOverspent($noBudget)->addBudgetLine($budgetLine); return $object; }
/** * @param Collection $accounts * @param Carbon $start * @param Carbon $end * * @return BalanceLine */ private function createNoBudgetLine(Collection $accounts, Carbon $start, Carbon $end) : BalanceLine { $empty = new BalanceLine(); foreach ($accounts as $account) { $spent = $this->budgetRepository->spentInPeriodWithoutBudget(new Collection([$account]), $start, $end); // budget $budgetEntry = new BalanceEntry(); $budgetEntry->setAccount($account); $budgetEntry->setSpent($spent); $empty->addBalanceEntry($budgetEntry); } return $empty; }