/** * Shows a budget list with spent/left/overspent. * * @param BudgetRepositoryInterface $repository * * @return \Symfony\Component\HttpFoundation\Response */ public function frontpage(BudgetRepositoryInterface $repository) { $budgets = $repository->getBudgets(); $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); $allEntries = new Collection(); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('budget'); $cache->addProperty('all'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } bcscale(2); /** @var Budget $budget */ foreach ($budgets as $budget) { $repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end); if ($repetitions->count() == 0) { $expenses = $repository->spentInPeriodCorrected($budget, $start, $end, true); $allEntries->push([$budget->name, 0, 0, $expenses, 0, 0]); continue; } /** @var LimitRepetition $repetition */ foreach ($repetitions as $repetition) { $expenses = $repository->spentInPeriodCorrected($budget, $repetition->startdate, $repetition->enddate, true); // $left can be less than zero. // $overspent can be more than zero ( = overspending) $left = max(bcsub($repetition->amount, $expenses), 0); // limited at zero. $overspent = max(bcsub($expenses, $repetition->amount), 0); // limited at zero. $name = $budget->name; // $spent is maxed to the repetition amount: $spent = $expenses > $repetition->amount ? $repetition->amount : $expenses; $allEntries->push([$name, $left, $spent, $overspent, $repetition->amount, $expenses]); } } $noBudgetExpenses = $repository->getWithoutBudgetSum($start, $end) * -1; $allEntries->push([trans('firefly.noBudget'), 0, 0, $noBudgetExpenses, 0, 0]); $data = $this->generator->frontpage($allEntries); $cache->store($data); return Response::json($data); }
/** * Shows a budget list with spent/left/overspent. * * @param BudgetRepositoryInterface $repository * * @param ARI $accountRepository * * @return \Symfony\Component\HttpFoundation\Response */ public function frontpage(BudgetRepositoryInterface $repository, ARI $accountRepository) { $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('budget'); $cache->addProperty('all'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $budgets = $repository->getBudgetsAndLimitsInRange($start, $end); $allEntries = new Collection(); $accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']); bcscale(2); /** @var Budget $budget */ foreach ($budgets as $budget) { // we already have amount, startdate and enddate. // if this "is" a limit repetition (as opposed to a budget without one entirely) // depends on whether startdate and enddate are null. $name = $budget->name; if (is_null($budget->startdate) && is_null($budget->enddate)) { $currentStart = clone $start; $currentEnd = clone $end; $expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts); $amount = 0; $left = 0; $spent = $expenses; $overspent = 0; } else { $currentStart = clone $budget->startdate; $currentEnd = clone $budget->enddate; $expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts); $amount = $budget->amount; // smaller than 1 means spent MORE than budget allows. $left = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? 0 : bcadd($budget->amount, $expenses); $spent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? $amount * -1 : $expenses; $overspent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? bcadd($budget->amount, $expenses) : 0; } $allEntries->push([$name, $left, $spent, $overspent, $amount, $expenses]); } $noBudgetExpenses = $repository->getWithoutBudgetSum($start, $end); $allEntries->push([trans('firefly.noBudget'), 0, 0, $noBudgetExpenses, 0, 0]); $data = $this->generator->frontpage($allEntries); $cache->store($data); return Response::json($data); }