/** * @param Carbon $start * @param Carbon $end * @param Collection $accounts * * @return Balance */ public function getBalanceReport(Carbon $start, Carbon $end, Collection $accounts) { $balance = new Balance(); // build a balance header: $header = new BalanceHeader(); $budgets = $this->budgetRepository->getBudgetsAndLimitsInRange($start, $end); $spentData = $this->budgetRepository->spentPerBudgetPerAccount($budgets, $accounts, $start, $end); foreach ($accounts as $account) { $header->addAccount($account); } /** @var BudgetModel $budget */ foreach ($budgets as $budget) { $balance->addBalanceLine($this->createBalanceLine($budget, $accounts, $spentData)); } $balance->addBalanceLine($this->createEmptyBalanceLine($accounts, $spentData)); $balance->addBalanceLine($this->createTagsBalanceLine($accounts, $start, $end)); $balance->addBalanceLine($this->createDifferenceBalanceLine($accounts, $spentData, $start, $end)); $balance->setBalanceHeader($header); return $balance; }
/** * @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; }
/** * @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 BudgetFormRequest $request * @param BudgetRepositoryInterface $repository * @param Budget $budget * * @return \Illuminate\Http\RedirectResponse */ public function update(BudgetFormRequest $request, BudgetRepositoryInterface $repository, Budget $budget) { $budgetData = ['name' => $request->input('name'), 'active' => intval($request->input('active')) == 1]; $repository->update($budget, $budgetData); Session::flash('success', 'Budget "' . $budget->name . '" updated.'); Preferences::mark(); if (intval(Input::get('return_to_edit')) === 1) { // set value so edit routine will not overwrite URL: Session::put('budgets.edit.fromUpdate', true); return redirect(route('budgets.edit', [$budget->id]))->withInput(['return_to_edit' => 1]); } // redirect to previous URL. return redirect(Session::get('budgets.edit.url')); }
/** * 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 $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 $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); }
/** * @param BudgetRepositoryInterface $repository * @param Carbon $start * @param Carbon $end * * @return array */ private function spentInPeriodWithout(BudgetRepositoryInterface $repository, Carbon $start, Carbon $end) : array { $list = $repository->journalsInPeriodWithoutBudget(new Collection(), $start, $end); $sum = '0'; /** @var TransactionJournal $entry */ foreach ($list as $entry) { $sum = bcadd(TransactionJournal::amount($entry), $sum); } return [trans('firefly.no_budget'), '0', '0', $sum, '0', '0']; }