mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Allow user to set multi-currency available budget. WIP
This commit is contained in:
209
app/Http/Controllers/Budget/AvailableBudgetController.php
Normal file
209
app/Http/Controllers/Budget/AvailableBudgetController.php
Normal file
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
/**
|
||||
* AvailableBudgetController.php
|
||||
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Http\Controllers\Budget;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Carbon\Exceptions\InvalidDateException;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\AvailableBudget;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class AvailableBudgetController
|
||||
*/
|
||||
class AvailableBudgetController extends Controller
|
||||
{
|
||||
|
||||
/** @var AvailableBudgetRepositoryInterface */
|
||||
private $abRepository;
|
||||
/** @var BudgetLimitRepositoryInterface */
|
||||
private $blRepository;
|
||||
/** @var CurrencyRepositoryInterface */
|
||||
private $currencyRepos;
|
||||
/** @var OperationsRepositoryInterface */
|
||||
private $opsRepository;
|
||||
/** @var BudgetRepositoryInterface The budget repository */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* AmountController constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
app('view')->share('hideBudgets', true);
|
||||
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
app('view')->share('title', (string)trans('firefly.budgets'));
|
||||
app('view')->share('mainTitleIcon', 'fa-tasks');
|
||||
$this->repository = app(BudgetRepositoryInterface::class);
|
||||
$this->opsRepository = app(OperationsRepositoryInterface::class);
|
||||
$this->abRepository = app(AvailableBudgetRepositoryInterface::class);
|
||||
$this->blRepository = app(BudgetLimitRepositoryInterface::class);
|
||||
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create will always assume the user's default currency, if it's not set.
|
||||
*
|
||||
* This method will check if there is no AB, and refuse to continue if it exists.
|
||||
*/
|
||||
public function create(Request $request, Carbon $start, Carbon $end, ?TransactionCurrency $currency = null)
|
||||
{
|
||||
$currency = $currency ?? app('amount')->getDefaultCurrency();
|
||||
$collection = $this->abRepository->get($start, $end);
|
||||
$filtered = $collection->filter(
|
||||
static function (AvailableBudget $budget) use ($currency) {
|
||||
return $currency->id === $budget->transaction_currency_id;
|
||||
}
|
||||
);
|
||||
if ($filtered->count() > 0) {
|
||||
/** @var AvailableBudget $first */
|
||||
$first = $filtered->first();
|
||||
|
||||
return redirect(route('available-budgets.edit', [$first->id]));
|
||||
}
|
||||
$page = (int)($request->get('page') ?? 1);
|
||||
|
||||
return view('budgets.available-budgets.create', compact('start', 'end', 'page', 'currency'));
|
||||
}
|
||||
|
||||
/**
|
||||
* createAlternative will show a list of enabled currencies so the user can pick one.
|
||||
*/
|
||||
public function createAlternative(Request $request, Carbon $start, Carbon $end)
|
||||
{
|
||||
$currencies = $this->currencyRepos->getEnabled();
|
||||
$availableBudgets = $this->abRepository->get($start, $end);
|
||||
|
||||
// remove already budgeted currencies:
|
||||
$currencies = $currencies->filter(
|
||||
static function (TransactionCurrency $currency) use ($availableBudgets) {
|
||||
/** @var AvailableBudget $budget */
|
||||
foreach ($availableBudgets as $budget) {
|
||||
if ($budget->transaction_currency_id === $currency->id) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
$page = (int)($request->get('page') ?? 1);
|
||||
return view('budgets.available-budgets.create-alternative', compact('start', 'end', 'page', 'currencies'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AvailableBudget $availableBudget
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function delete(AvailableBudget $availableBudget)
|
||||
{
|
||||
$this->abRepository->destroyAvailableBudget($availableBudget);
|
||||
session()->flash('success', trans('firefly.deleted_ab'));
|
||||
|
||||
return redirect(route('budgets.index'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AvailableBudget $availableBudget
|
||||
*/
|
||||
public function edit(AvailableBudget $availableBudget)
|
||||
{
|
||||
return view('budgets.available-budgets.edit', compact('availableBudget'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
// make dates.
|
||||
try {
|
||||
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
|
||||
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
|
||||
} catch (InvalidDateException $e) {
|
||||
$start = session()->get('start');
|
||||
$end = session()->get('end');
|
||||
Log::info($e->getMessage());
|
||||
}
|
||||
// find currency
|
||||
$currency = $this->currencyRepos->find((int)$request->get('currency_id'));
|
||||
if (null === $currency) {
|
||||
session()->flash('error', trans('firefly.invalid_currency'));
|
||||
|
||||
return redirect(route('budgets.index'));
|
||||
}
|
||||
|
||||
// find existing AB
|
||||
$existing = $this->abRepository->find($currency, $start, $end);
|
||||
if (null === $existing) {
|
||||
$this->abRepository->store(
|
||||
[
|
||||
'amount' => $request->get('amount'),
|
||||
'currency' => $currency,
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
]
|
||||
);
|
||||
}
|
||||
if (null !== $existing) {
|
||||
// update amount:
|
||||
$this->abRepository->update($existing, ['amount' => $request->get('amount')]);
|
||||
}
|
||||
session()->flash('success', trans('firefly.set_ab'));
|
||||
|
||||
return redirect(route('budgets.index'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param AvailableBudget $availableBudget
|
||||
*/
|
||||
public function update(Request $request, AvailableBudget $availableBudget)
|
||||
{
|
||||
$this->abRepository->update($availableBudget, ['amount' => $request->get('amount')]);
|
||||
session()->flash('success', trans('firefly.updated_ab'));
|
||||
|
||||
return redirect(route('budgets.index'));
|
||||
}
|
||||
|
||||
}
|
@@ -26,13 +26,18 @@ namespace FireflyIII\Http\Controllers\Budget;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\AvailableBudget;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -42,12 +47,16 @@ class IndexController extends Controller
|
||||
{
|
||||
|
||||
use DateCalculation;
|
||||
/** @var AvailableBudgetRepositoryInterface */
|
||||
private $abRepository;
|
||||
/** @var BudgetLimitRepositoryInterface */
|
||||
private $blRepository;
|
||||
/** @var CurrencyRepositoryInterface */
|
||||
private $currencyRepository;
|
||||
/** @var OperationsRepositoryInterface */
|
||||
private $opsRepository;
|
||||
/** @var BudgetRepositoryInterface The budget repository */
|
||||
private $repository;
|
||||
/** @var AvailableBudgetRepositoryInterface */
|
||||
private $abRepository;
|
||||
|
||||
/**
|
||||
* IndexController constructor.
|
||||
@@ -64,9 +73,11 @@ class IndexController extends Controller
|
||||
function ($request, $next) {
|
||||
app('view')->share('title', (string)trans('firefly.budgets'));
|
||||
app('view')->share('mainTitleIcon', 'fa-tasks');
|
||||
$this->repository = app(BudgetRepositoryInterface::class);
|
||||
$this->opsRepository = app(OperationsRepositoryInterface::class);
|
||||
$this->abRepository = app(AvailableBudgetRepositoryInterface::class);
|
||||
$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);
|
||||
$this->repository->cleanupBudgets();
|
||||
|
||||
return $next($request);
|
||||
@@ -93,18 +104,47 @@ class IndexController extends Controller
|
||||
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
|
||||
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
|
||||
$defaultCurrency = app('amount')->getDefaultCurrency();
|
||||
$budgeted = '0';
|
||||
$spent = '0';
|
||||
// new period stuff:
|
||||
$periodTitle = app('navigation')->periodShow($start, $range);
|
||||
|
||||
// make the next and previous period, and calculate the periods used for period navigation
|
||||
$next = clone $end;
|
||||
$next->addDay();
|
||||
$prev = clone $start;
|
||||
$prev->subDay();
|
||||
$prev = app('navigation')->startOfPeriod($prev, $range);
|
||||
$previousLoop = $this->getPreviousPeriods($start, $range);
|
||||
$nextLoop = $this->getNextPeriods($start, $range);
|
||||
$currentMonth = app('navigation')->periodShow($start, $range);
|
||||
$nextText = app('navigation')->periodShow($next, $range);
|
||||
$prevText = app('navigation')->periodShow($prev, $range);
|
||||
// loop of previous periods:
|
||||
$prevLoop = $this->getPreviousPeriods($start, $range);
|
||||
$nextLoop = $this->getNextPeriods($start, $range);
|
||||
|
||||
// get all available budgets.
|
||||
$ab = $this->abRepository->get($start, $end);
|
||||
$availableBudgets = [];
|
||||
// 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:
|
||||
$budgeted = $this->blRepository->budgeted($entry->start_date, $entry->end_date, $entry->transactionCurrency,);
|
||||
$array['budgeted'] = $budgeted;
|
||||
$availableBudgets[] = $array;
|
||||
unset($spentArr);
|
||||
}
|
||||
|
||||
if (0 === count($availableBudgets)) {
|
||||
// get budgeted for default currency:
|
||||
$budgeted = $this->blRepository->budgeted($start, $end, $defaultCurrency,);
|
||||
$spentArr = $this->opsRepository->sumExpenses($start, $end, null, null, $defaultCurrency);
|
||||
$spent = $spentArr[$defaultCurrency->id]['sum'] ?? '0';
|
||||
unset($spentArr);
|
||||
}
|
||||
|
||||
// count the number of enabled currencies. This determines if we display a "+" button.
|
||||
$currencies = $this->currencyRepository->getEnabled();
|
||||
$enableAddButton = $currencies->count() > count($availableBudgets);
|
||||
|
||||
// number of days for consistent budgeting.
|
||||
$activeDaysPassed = $this->activeDaysPassed($start, $end); // see method description.
|
||||
@@ -118,14 +158,6 @@ class IndexController extends Controller
|
||||
// get all inactive budgets, and simply list them:
|
||||
$inactive = $this->repository->getInactiveBudgets();
|
||||
|
||||
// collect budget info to fill bars and so on.
|
||||
$budgetInformation = $this->opsRepository->collectBudgetInformation($collection, $start, $end);
|
||||
|
||||
// to display available budget:
|
||||
$available = $this->abRepository->getAvailableBudget($defaultCurrency, $start, $end);
|
||||
$spent = array_sum(array_column($budgetInformation, 'spent'));
|
||||
$budgeted = array_sum(array_column($budgetInformation, 'budgeted'));
|
||||
|
||||
|
||||
// paginate budgets
|
||||
$paginator = new LengthAwarePaginator($budgets, $total, $pageSize, $page);
|
||||
@@ -133,11 +165,18 @@ class IndexController extends Controller
|
||||
|
||||
return view(
|
||||
'budgets.index', compact(
|
||||
'available', 'currentMonth', 'next', 'nextText', 'prev', 'paginator',
|
||||
'prevText',
|
||||
'availableBudgets',
|
||||
//'available',
|
||||
//'currentMonth', 'next', 'nextText', 'prev',
|
||||
//'prevText', 'previousLoop', 'nextLoop',
|
||||
'budgeted', 'spent',
|
||||
'prevLoop', 'nextLoop',
|
||||
'paginator',
|
||||
'enableAddButton',
|
||||
'periodTitle',
|
||||
'defaultCurrency',
|
||||
'page', 'activeDaysPassed', 'activeDaysLeft',
|
||||
'budgetInformation',
|
||||
'inactive', 'budgets', 'spent', 'budgeted', 'previousLoop', 'nextLoop', 'start', 'end'
|
||||
'inactive', 'budgets', 'start', 'end'
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -160,6 +199,7 @@ class IndexController extends Controller
|
||||
$budgetId = (int)$budgetId;
|
||||
$budget = $repository->findNull($budgetId);
|
||||
if (null !== $budget) {
|
||||
Log::debug(sprintf('Set budget #%d ("%s") to position %d', $budget->id, $budget->name, $currentOrder));
|
||||
$repository->setBudgetOrder($budget, $currentOrder);
|
||||
}
|
||||
$currentOrder++;
|
||||
|
Reference in New Issue
Block a user