/** * @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; }
/** * Show a yearly overview for a budget. * * @param BudgetRepositoryInterface $repository * @param $year * @param bool $shared * * @return \Symfony\Component\HttpFoundation\Response */ public function year(BudgetRepositoryInterface $repository, $year, $shared = false) { $start = new Carbon($year . '-01-01'); $end = new Carbon($year . '-12-31'); $shared = $shared == 'shared' ? true : false; $budgets = $repository->getBudgets(); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('budget'); $cache->addProperty('year'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $entries = new Collection(); while ($start < $end) { // month is the current end of the period: $month = clone $start; $month->endOfMonth(); $row = [clone $start]; // each budget, fill the row: foreach ($budgets as $budget) { $spent = $repository->spentInPeriodCorrected($budget, $start, $month, $shared); $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 $report_type * @param Carbon $start * @param Carbon $end * @param Collection $accounts * * @return \Illuminate\Http\JsonResponse */ public function year(BudgetRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts) { $allBudgets = $repository->getBudgets(); $budgets = new Collection(); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty($report_type); $cache->addProperty($accounts); $cache->addProperty('budget'); $cache->addProperty('year'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } // filter empty budgets: foreach ($allBudgets as $budget) { $spent = $repository->balanceInPeriod($budget, $start, $end, $accounts); if ($spent != 0) { $budgets->push($budget); } } $entries = new Collection(); while ($start < $end) { // month is the current end of the period: $month = clone $start; $month->endOfMonth(); $row = [clone $start]; // each budget, fill the row: foreach ($budgets as $budget) { $spent = $repository->balanceInPeriod($budget, $start, $month, $accounts); $row[] = $spent * -1; } $entries->push($row); $start->endOfMonth()->addDay(); } $data = $this->generator->year($budgets, $entries); $cache->store($data); return Response::json($data); }