Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Foce structured budgets page
James Cole111f28dd002c
diff --git a/app/Http/Controllers/Budget/IndexController.php b/app/Http/Controllers/Budget/IndexController.phpindex d40d4432a3..734bca1cfb 100644--- a/app/Http/Controllers/Budget/IndexController.php+++ b/app/Http/Controllers/Budget/IndexController.php@@ -59,10 +59,10 @@ final class IndexController extends Controlleruse DateCalculation;private AvailableBudgetRepositoryInterface $abRepository;- private BudgetLimitRepositoryInterface $blRepository;- private CurrencyRepositoryInterface $currencyRepository;- private OperationsRepositoryInterface $opsRepository;- private BudgetRepositoryInterface $repository;+ private BudgetLimitRepositoryInterface $blRepository;+ private CurrencyRepositoryInterface $currencyRepository;+ private OperationsRepositoryInterface $opsRepository;+ private BudgetRepositoryInterface $repository;/*** IndexController constructor.@@ -72,7 +72,7 @@ final class IndexController extends Controllerparent::__construct();$this->middleware(function ($request, $next) {- app('view')->share('title', (string) trans('firefly.budgets'));+ app('view')->share('title', (string)trans('firefly.budgets'));app('view')->share('mainTitleIcon', 'bi-pie-chart');$this->repository = app(BudgetRepositoryInterface::class);$this->opsRepository = app(OperationsRepositoryInterface::class);@@ -92,46 +92,34 @@ final class IndexController extends Controller** @throws FireflyException* */- public function index(?Carbon $start = null, ?Carbon $end = null): Factory|\Illuminate\Contracts\View\View+ public function index(?Carbon $start = null, ?Carbon $end = null): Factory | \Illuminate\Contracts\View\View{$this->abRepository->cleanup();Log::debug(sprintf('Start of IndexController::index("%s", "%s")', $start?->format('Y-m-d'), $end?->format('Y-m-d')));// collect some basic vars:- $range = Navigation::getViewRange(true);- $isCustomRange = session('is_custom_range', false);- if (false === $isCustomRange) {- $start ??= session('start', today(config('app.timezone'))->startOfMonth());- $end ??= Navigation::endOfPeriod($start, $range);- }-- // overrule start and end if necessary:- if (true === $isCustomRange) {- $start ??= session('start', today(config('app.timezone'))->startOfMonth());- $end ??= session('end', today(config('app.timezone'))->endOfMonth());- }-- $currencies = $this->currencyRepository->get();- $budgeted = '0';- $spent = '0';+ $range = Navigation::getViewRange(true);+ $isCustomRange = session('is_custom_range', false);+ $start ??= Navigation::startOfPeriod(session('start', today(config('app.timezone'))), $range);+ $end ??= Navigation::endOfPeriod($start, $range);+ $currencies = $this->currencyRepository->get();+ $budgeted = '0';+ $spent = '0';// new period stuff:- $periodTitle = Navigation::periodShow($start, $range);- $prevLoop = $this->getPreviousPeriods($start, $range);- $nextLoop = $this->getNextPeriods($start, $range);+ $periodTitle = Navigation::periodShow($start, $range);+ $prevLoop = $this->getPreviousPeriods($start, $range);+ $nextLoop = $this->getNextPeriods($start, $range);// number of days for consistent budgeting.$activeDaysPassed = $this->activeDaysPassed($start, $end); // see method description.- $activeDaysLeft = $this->activeDaysLeft($start, $end); // see method description.+ $activeDaysLeft = $this->activeDaysLeft($start, $end); // see method description.// get all available budgets:$availableBudgets = $this->getAllAvailableBudgets($start, $end);// get all active budgets:- $budgets = $this->getAllBudgets($start, $end, $currencies, $this->primaryCurrency);-- // echo '<pre>';- // var_dump($budgets[0]);exit;- $sums = $this->getSums($budgets);+ $budgets = $this->getAllBudgets($start, $end, $currencies, $this->primaryCurrency);+ $sums = $this->getSums($budgets);// get budgeted for default currency:if (0 === count($availableBudgets)) {@@ -142,7 +130,7 @@ final class IndexController extends Controller}// get all inactive budgets, and simply list them:- $inactive = $this->repository->getInactiveBudgets();+ $inactive = $this->repository->getInactiveBudgets();return view('budgets.index', ['availableBudgets' => $availableBudgets,@@ -168,7 +156,7 @@ final class IndexController extends Controller$budgetIds = $request->input('budgetIds');foreach ($budgetIds as $index => $budgetId) {- $budgetId = (int) $budgetId;+ $budgetId = (int)$budgetId;$budget = $repository->find($budgetId);if ($budget instanceof Budget) {Log::debug(sprintf('Set budget #%d ("%s") to position %d', $budget->id, $budget->name, $index + 1));@@ -183,7 +171,7 @@ final class IndexController extends Controllerprivate function getAllAvailableBudgets(Carbon $start, Carbon $end): array{Log::debug(sprintf('Start of getAllAvailableBudgets("%s", "%s")', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));- $converter = new ExchangeRateConverter();+ $converter = new ExchangeRateConverter();// get all available budgets.$ab = $this->abRepository->get($start, $end);$availableBudgets = [];@@ -191,14 +179,14 @@ final class IndexController extends Controller// for each, complement with spent amount:/** @var AvailableBudget $entry */foreach ($ab as $entry) {- $array = $entry->toArray();- $array['start_date'] = $entry->start_date;- $array['end_date'] = $entry->end_date;+ $array = $entry->toArray();+ $array['start_date'] = $entry->start_date;+ $array['end_date'] = $entry->end_date;// spent in period:- $spentArr = $this->opsRepository->sumExpenses($entry->start_date, $entry->end_date, null, null, $entry->transactionCurrency);- $array['spent'] = $spentArr[$entry->transaction_currency_id]['sum'] ?? '0';- $array['pc_spent'] = $this->convertToPrimary && $entry->transaction_currency_id !== $this->primaryCurrency->id+ $spentArr = $this->opsRepository->sumExpenses($entry->start_date, $entry->end_date, null, null, $entry->transactionCurrency);+ $array['spent'] = $spentArr[$entry->transaction_currency_id]['sum'] ?? '0';+ $array['pc_spent'] = $this->convertToPrimary && $entry->transaction_currency_id !== $this->primaryCurrency->id? $converter->convert($entry->transactionCurrency, $this->primaryCurrency, $entry->start_date, $array['spent']): null;// budgeted in period:@@ -209,7 +197,7 @@ final class IndexController extends Controller: null;// this time, because of complex sums, use the currency converter.- $availableBudgets[] = $array;+ $availableBudgets[] = $array;unset($spentArr);}@@ -239,13 +227,13 @@ final class IndexController extends Controllerforeach ($budgetLimits as $limit) {Log::debug(sprintf('Working on budget limit #%d', $limit->id));// number of days for consistent budgeting.- $activeDaysPassed = $this->activeDaysPassed($limit->start_date, $limit->end_date); // see method description.- $activeDaysLeft = $this->activeDaysLeft($limit->start_date, $limit->end_date); // see method description.- $limitPeriod = Period::make($limit->start_date, $limit->end_date, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());- $inPast = $limitPeriod->startsBefore(now()) && $limitPeriod->endsBefore(now());- $currency = $limit->transactionCurrency ?? $primaryCurrency;- $amount = Steam::bcround($limit->amount, $currency->decimal_places);- $spent = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection()->push($budget), $currency);+ $activeDaysPassed = $this->activeDaysPassed($limit->start_date, $limit->end_date); // see method description.+ $activeDaysLeft = $this->activeDaysLeft($limit->start_date, $limit->end_date); // see method description.+ $limitPeriod = Period::make($limit->start_date, $limit->end_date, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());+ $inPast = $limitPeriod->startsBefore(now()) && $limitPeriod->endsBefore(now());+ $currency = $limit->transactionCurrency ?? $primaryCurrency;+ $amount = Steam::bcround($limit->amount, $currency->decimal_places);+ $spent = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection()->push($budget), $currency);$spentAmount = $spent[$currency->id]['sum'] ?? '0';$array['budgeted'][] = [@@ -285,15 +273,15 @@ final class IndexController extends Controllerif (array_key_exists($currency->id, $spentArr) && array_key_exists('sum', $spentArr[$currency->id])) {$array['spent'][$currency->id]['spent'] = $spentArr[$currency->id]['sum'];$array['spent'][$currency->id]['spent_outside'] = Steam::negative(bcsub(- $spentInLimits[$currency->id],- $spentArr[$currency->id]['sum']- ));+ $spentInLimits[$currency->id],+ $spentArr[$currency->id]['sum']+ ));$array['spent'][$currency->id]['currency_id'] = $currency->id;$array['spent'][$currency->id]['currency_symbol'] = $currency->symbol;$array['spent'][$currency->id]['currency_decimal_places'] = $currency->decimal_places;}}- $budgets[] = $array;+ $budgets[] = $array;}return $budgets;@@ -310,28 +298,28 @@ final class IndexController extends Controller/** @var array $spent */foreach ($budget['spent'] as $spent) {$currencyId = $spent['currency_id'];- $sums['spent'][$currencyId] ??= [+ $sums['spent'][$currencyId] ??= ['amount' => '0','currency_id' => $spent['currency_id'],'currency_symbol' => $spent['currency_symbol'],'currency_decimal_places' => $spent['currency_decimal_places'],];- $sums['spent'][$currencyId]['amount'] = bcadd($sums['spent'][$currencyId]['amount'], (string) $spent['spent']);+ $sums['spent'][$currencyId]['amount'] = bcadd($sums['spent'][$currencyId]['amount'], (string)$spent['spent']);}/** @var array $budgeted */foreach ($budget['budgeted'] as $budgeted) {$currencyId = $budgeted['currency_id'];- $sums['budgeted'][$currencyId] ??= [+ $sums['budgeted'][$currencyId] ??= ['amount' => '0','currency_id' => $budgeted['currency_id'],'currency_symbol' => $budgeted['currency_symbol'],'currency_decimal_places' => $budgeted['currency_decimal_places'],];- $sums['budgeted'][$currencyId]['amount'] = bcadd($sums['budgeted'][$currencyId]['amount'], (string) $budgeted['amount']);+ $sums['budgeted'][$currencyId]['amount'] = bcadd($sums['budgeted'][$currencyId]['amount'], (string)$budgeted['amount']);// also calculate how much left from budgeted:- $sums['left'][$currencyId] ??= [+ $sums['left'][$currencyId] ??= ['amount' => '0','currency_id' => $budgeted['currency_id'],'currency_symbol' => $budgeted['currency_symbol'],