firefly-iii/app/Http/Controllers/Budget/IndexController.php

254 lines
11 KiB
PHP
Raw Normal View History

2018-07-14 08:22:21 -05:00
<?php
/**
* IndexController.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-14 08:22:21 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-14 08:22:21 -05:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-07-14 08:22:21 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-07-14 08:22:21 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-07-14 08:22:21 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-14 08:22:21 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Budget;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionCurrency;
2019-08-30 01:00:52 -05:00
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
2018-07-14 08:22:21 -05:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Controllers\DateCalculation;
use Illuminate\Contracts\View\Factory;
2018-10-17 08:18:09 -05:00
use Illuminate\Http\JsonResponse;
2018-07-14 08:22:21 -05:00
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\View\View;
use Log;
2018-07-14 08:22:21 -05:00
/**
*
* Class IndexController
*/
class IndexController extends Controller
{
use DateCalculation;
/** @var AvailableBudgetRepositoryInterface */
private $abRepository;
/** @var BudgetLimitRepositoryInterface */
private $blRepository;
/** @var CurrencyRepositoryInterface */
private $currencyRepository;
/** @var OperationsRepositoryInterface */
private $opsRepository;
2018-07-21 01:06:24 -05:00
/** @var BudgetRepositoryInterface The budget repository */
2018-07-14 08:22:21 -05:00
private $repository;
/**
2018-07-21 01:06:24 -05:00
* IndexController constructor.
*
* @codeCoverageIgnore
2018-07-14 08:22:21 -05:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string) trans('firefly.budgets'));
app('view')->share('mainTitleIcon', 'fa-pie-chart');
$this->repository = app(BudgetRepositoryInterface::class);
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->abRepository = app(AvailableBudgetRepositoryInterface::class);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
$this->blRepository = app(BudgetLimitRepositoryInterface::class);
2018-08-07 10:34:43 -05:00
$this->repository->cleanupBudgets();
2018-07-14 08:22:21 -05:00
return $next($request);
}
);
}
/**
2018-07-21 01:06:24 -05:00
* Show all budgets.
*
2018-07-14 08:22:21 -05:00
* @param Request $request
*
* @param Carbon|null $start
* @param Carbon|null $end
2018-07-14 08:22:21 -05:00
*
* @return Factory|View
2018-07-14 08:22:21 -05:00
*/
public function index(Request $request, Carbon $start = null, Carbon $end = null)
2018-07-14 08:22:21 -05:00
{
2020-05-22 06:52:33 -05:00
Log::debug('Start of IndexController::index()');
2018-08-07 10:34:43 -05:00
// collect some basic vars:
$range = app('preferences')->get('viewRange', '1M')->data;
$start = $start ?? session('start', Carbon::now()->startOfMonth());
$end = $end ?? app('navigation')->endOfPeriod($start, $range);
2018-08-07 10:34:43 -05:00
$defaultCurrency = app('amount')->getDefaultCurrency();
$budgeted = '0';
$spent = '0';
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('1) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
// new period stuff:
$periodTitle = app('navigation')->periodShow($start, $range);
$prevLoop = $this->getPreviousPeriods($start, $range);
$nextLoop = $this->getNextPeriods($start, $range);
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('2) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
// get all available budgets.
$ab = $this->abRepository->get($start, $end);
$availableBudgets = [];
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('3) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
// 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;
// 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';
// budgeted in period:
2019-11-15 09:37:23 -06:00
$budgeted = $this->blRepository->budgeted($entry->start_date, $entry->end_date, $entry->transactionCurrency, );
$array['budgeted'] = $budgeted;
$availableBudgets[] = $array;
unset($spentArr);
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('4) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
}
2018-08-07 10:34:43 -05:00
if (0 === count($availableBudgets)) {
// get budgeted for default currency:
2019-11-15 09:37:23 -06:00
$budgeted = $this->blRepository->budgeted($start, $end, $defaultCurrency, );
$spentArr = $this->opsRepository->sumExpenses($start, $end, null, null, $defaultCurrency);
$spent = $spentArr[$defaultCurrency->id]['sum'] ?? '0';
unset($spentArr);
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('5) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
}
// count the number of enabled currencies. This determines if we display a "+" button.
$currencies = $this->currencyRepository->getEnabled();
$enableAddButton = $currencies->count() > count($availableBudgets);
2018-07-14 08:22:21 -05:00
2018-08-07 10:34:43 -05:00
// number of days for consistent budgeting.
$activeDaysPassed = $this->activeDaysPassed($start, $end); // see method description.
$activeDaysLeft = $this->activeDaysLeft($start, $end); // see method description.
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('6) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
2018-08-07 10:34:43 -05:00
// get all budgets, and paginate them into $budgets.
$collection = $this->repository->getActiveBudgets();
$budgets = [];
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('7) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
// complement budget with budget limits in range, and expenses in currency X in range.
/** @var Budget $current */
foreach ($collection as $current) {
$array = $current->toArray();
$array['spent'] = [];
$array['budgeted'] = [];
2020-03-19 12:28:02 -05:00
$array['attachments'] = $this->repository->getAttachments($current);
2020-03-14 02:03:43 -05:00
$array['auto_budget'] = $this->repository->getAutoBudget($current);
$budgetLimits = $this->blRepository->getBudgetLimits($current, $start, $end);
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('8) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
/** @var BudgetLimit $limit */
foreach ($budgetLimits as $limit) {
$currency = $limit->transactionCurrency ?? $defaultCurrency;
$array['budgeted'][] = [
'id' => $limit->id,
2019-09-20 09:16:15 -05:00
'amount' => round($limit->amount, $currency->decimal_places),
2020-05-22 06:52:33 -05:00
'start_date' => $limit->start_date->formatLocalized($this->monthAndDayFormat),
'end_date' => $limit->end_date->formatLocalized($this->monthAndDayFormat),
'in_range' => $limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end),
2019-09-05 23:02:22 -05:00
'currency_id' => $currency->id,
'currency_symbol' => $currency->symbol,
'currency_name' => $currency->name,
'currency_decimal_places' => $currency->decimal_places,
];
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('9) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
}
/** @var TransactionCurrency $currency */
foreach ($currencies as $currency) {
$spentArr = $this->opsRepository->sumExpenses($start, $end, null, new Collection([$current]), $currency);
if (isset($spentArr[$currency->id]['sum'])) {
$array['spent'][$currency->id]['spent'] = $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;
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('10) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
}
}
$budgets[] = $array;
}
2018-08-07 10:34:43 -05:00
// get all inactive budgets, and simply list them:
$inactive = $this->repository->getInactiveBudgets();
2020-05-22 06:52:33 -05:00
Log::debug(sprintf('11) Start is "%s", end is "%s"', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
2018-08-07 10:34:43 -05:00
2018-07-14 08:22:21 -05:00
return view(
'budgets.index',
compact(
'availableBudgets',
'budgeted',
'spent',
'prevLoop',
'nextLoop',
'budgets',
'currencies',
'enableAddButton',
'periodTitle',
'defaultCurrency',
'activeDaysPassed',
'activeDaysLeft',
'inactive',
'budgets',
'start',
'end'
)
2018-07-14 08:22:21 -05:00
);
}
2018-10-17 08:18:09 -05:00
/**
2019-02-13 10:38:41 -06:00
* @param Request $request
*
* @param BudgetRepositoryInterface $repository
2018-10-17 08:18:09 -05:00
*
* @return JsonResponse
*/
public function reorder(Request $request, BudgetRepositoryInterface $repository): JsonResponse
{
$budgetIds = $request->get('budgetIds');
foreach ($budgetIds as $index => $budgetId) {
$budgetId = (int) $budgetId;
2018-10-17 08:18:09 -05:00
$budget = $repository->findNull($budgetId);
if (null !== $budget) {
Log::debug(sprintf('Set budget #%d ("%s") to position %d', $budget->id, $budget->name, $index + 1));
$repository->setBudgetOrder($budget, $index + 1);
2018-10-17 08:18:09 -05:00
}
}
2020-03-31 00:03:37 -05:00
app('preferences')->mark();
2018-10-17 08:18:09 -05:00
return response()->json(['OK']);
}
}