mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-18 02:03:40 +00:00
Refactor references to static facades. Improve budget controller code.
This commit is contained in:
@@ -32,13 +32,12 @@ use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Preferences;
|
||||
use View;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -46,6 +45,7 @@ use View;
|
||||
*/
|
||||
class AmountController extends Controller
|
||||
{
|
||||
use DateCalculation;
|
||||
/** @var BudgetRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
@@ -56,7 +56,7 @@ class AmountController extends Controller
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
View::share('hideBudgets', true);
|
||||
app('view')->share('hideBudgets', true);
|
||||
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
@@ -79,53 +79,36 @@ class AmountController extends Controller
|
||||
*/
|
||||
public function amount(Request $request, BudgetRepositoryInterface $repository, Budget $budget): JsonResponse
|
||||
{
|
||||
$amount = (string)$request->get('amount');
|
||||
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
|
||||
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
|
||||
$budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount);
|
||||
$largeDiff = false;
|
||||
$warnText = '';
|
||||
$days = 0;
|
||||
$daysInMonth = 0;
|
||||
if (0 === bccomp($amount, '0')) {
|
||||
$budgetLimit = null;
|
||||
}
|
||||
$amount = (string)$request->get('amount');
|
||||
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
|
||||
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
|
||||
$budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount);
|
||||
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
|
||||
$currency = app('amount')->getDefaultCurrency();
|
||||
$left = app('amount')->formatAnything($currency, bcadd($amount, $spent), true);
|
||||
$largeDiff = false;
|
||||
$warnText = '';
|
||||
$leftPerDay = null;
|
||||
$periodLength = $start->diffInDays($end);
|
||||
$dayDifference = $this->getDayDifference($start, $end);
|
||||
|
||||
// if today is between start and end, use the diff in days between end and today (days left)
|
||||
// otherwise, use diff between start and end.
|
||||
$today = new Carbon;
|
||||
Log::debug(sprintf('Start is %s, end is %s, today is %s', $start->format('Y-m-d'), $end->format('Y-m-d'), $today->format('Y-m-d')));
|
||||
if ($today->gte($start) && $today->lte($end)) {
|
||||
$days = $end->diffInDays($today);
|
||||
$daysInMonth = $start->diffInDays($today);
|
||||
}
|
||||
if ($today->lte($start) || $today->gte($end)) {
|
||||
$days = $start->diffInDays($end);
|
||||
$daysInMonth = $start->diffInDays($end);
|
||||
}
|
||||
$days = 0 === $days ? 1 : $days;
|
||||
$daysInMonth = 0 === $daysInMonth ? 1 : $daysInMonth;
|
||||
|
||||
// calculate left in budget:
|
||||
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
|
||||
$currency = app('amount')->getDefaultCurrency();
|
||||
$left = app('amount')->formatAnything($currency, bcadd($amount, $spent), true);
|
||||
$leftPerDay = 'none';
|
||||
|
||||
// is user has money left, calculate.
|
||||
/*
|
||||
* If the user budgets ANY amount per day for this budget (anything but zero)
|
||||
* Firefly III calculates how much he could spend per day.
|
||||
*/
|
||||
if (1 === bccomp(bcadd($amount, $spent), '0')) {
|
||||
$leftPerDay = app('amount')->formatAnything($currency, bcdiv(bcadd($amount, $spent), (string)$days), true);
|
||||
$leftPerDay = app('amount')->formatAnything($currency, bcdiv(bcadd($amount, $spent), (string)$dayDifference), true);
|
||||
}
|
||||
|
||||
|
||||
// over or under budgeting, compared to previous budgets?
|
||||
/*
|
||||
* Get the average amount of money the user budgets for this budget.
|
||||
* And calculate the same for the current amount.
|
||||
*
|
||||
* If the difference is very large, give the user a notification.
|
||||
*/
|
||||
$average = $this->repository->budgetedPerDay($budget);
|
||||
// current average per day:
|
||||
$diff = $start->diffInDays($end);
|
||||
$current = $amount;
|
||||
if ($diff > 0) {
|
||||
$current = bcdiv($amount, (string)$diff);
|
||||
}
|
||||
$current = bcdiv($amount, (string)$periodLength);
|
||||
if (bccomp(bcmul('1.1', $average), $current) === -1) {
|
||||
$largeDiff = true;
|
||||
$warnText = (string)trans(
|
||||
@@ -150,8 +133,6 @@ class AmountController extends Controller
|
||||
'large_diff' => $largeDiff,
|
||||
'left_per_day' => $leftPerDay,
|
||||
'warn_text' => $warnText,
|
||||
'daysInMonth' => $daysInMonth,
|
||||
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -187,7 +168,7 @@ class AmountController extends Controller
|
||||
'suggested' => '0',
|
||||
];
|
||||
$currency = app('amount')->getDefaultCurrency();
|
||||
$range = Preferences::get('viewRange', '1M')->data;
|
||||
$range = app('preferences')->get('viewRange', '1M')->data;
|
||||
/** @var Carbon $begin */
|
||||
$begin = app('navigation')->subtractPeriod($start, $range, 3);
|
||||
|
||||
@@ -272,5 +253,4 @@ class AmountController extends Controller
|
||||
|
||||
return view('budgets.income', compact('available', 'start', 'end', 'page'));
|
||||
}
|
||||
|
||||
}
|
@@ -29,7 +29,6 @@ use FireflyIII\Http\Requests\BudgetFormRequest;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use View;
|
||||
|
||||
/**
|
||||
* Class CreateController
|
||||
@@ -46,7 +45,7 @@ class CreateController extends Controller
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
View::share('hideBudgets', true);
|
||||
app('view')->share('hideBudgets', true);
|
||||
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
|
@@ -28,7 +28,6 @@ use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use View;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -46,7 +45,7 @@ class DeleteController extends Controller
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
View::share('hideBudgets', true);
|
||||
app('view')->share('hideBudgets', true);
|
||||
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
|
@@ -30,7 +30,6 @@ use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use View;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -48,7 +47,7 @@ class EditController extends Controller
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
View::share('hideBudgets', true);
|
||||
app('view')->share('hideBudgets', true);
|
||||
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
|
@@ -28,11 +28,10 @@ use Carbon\Carbon;
|
||||
use Exception;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Log;
|
||||
use Preferences;
|
||||
use View;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -41,6 +40,7 @@ use View;
|
||||
class IndexController extends Controller
|
||||
{
|
||||
|
||||
use DateCalculation;
|
||||
/** @var BudgetRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
@@ -51,7 +51,7 @@ class IndexController extends Controller
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
View::share('hideBudgets', true);
|
||||
app('view')->share('hideBudgets', true);
|
||||
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
@@ -73,13 +73,13 @@ class IndexController extends Controller
|
||||
*/
|
||||
public function index(Request $request, string $moment = null)
|
||||
{
|
||||
$range = Preferences::get('viewRange', '1M')->data;
|
||||
$start = session('start', new Carbon);
|
||||
$end = session('end', new Carbon);
|
||||
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
|
||||
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
|
||||
$days = 0;
|
||||
$daysInMonth = 0;
|
||||
$range = app('preferences')->get('viewRange', '1M')->data;
|
||||
$start = session('start', new Carbon);
|
||||
$end = session('end', new Carbon);
|
||||
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
|
||||
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
|
||||
$days = 0;
|
||||
|
||||
|
||||
// make date if present:
|
||||
if (null !== $moment || '' !== (string)$moment) {
|
||||
@@ -96,15 +96,12 @@ class IndexController extends Controller
|
||||
// otherwise, use diff between start and end.
|
||||
$today = new Carbon;
|
||||
if ($today->gte($start) && $today->lte($end)) {
|
||||
$days = $end->diffInDays($today);
|
||||
$daysInMonth = $start->diffInDays($today);
|
||||
$days = $end->diffInDays($today);
|
||||
}
|
||||
if ($today->lte($start) || $today->gte($end)) {
|
||||
$days = $start->diffInDays($end);
|
||||
$daysInMonth = $start->diffInDays($end);
|
||||
$days = $start->diffInDays($end);
|
||||
}
|
||||
$days = 0 === $days ? 1 : $days;
|
||||
$daysInMonth = 0 === $daysInMonth ? 1 : $daysInMonth;
|
||||
$days = 0 === $days ? 1 : $days;
|
||||
|
||||
|
||||
$next = clone $end;
|
||||
@@ -113,6 +110,7 @@ class IndexController extends Controller
|
||||
$prev->subDay();
|
||||
$prev = app('navigation')->startOfPeriod($prev, $range);
|
||||
$this->repository->cleanupBudgets();
|
||||
$daysPassed = $this->getDaysPassedInPeriod($start, $end);
|
||||
$allBudgets = $this->repository->getActiveBudgets();
|
||||
$total = $allBudgets->count();
|
||||
$budgets = $allBudgets->slice(($page - 1) * $pageSize, $pageSize);
|
||||
@@ -165,10 +163,14 @@ class IndexController extends Controller
|
||||
return view(
|
||||
'budgets.index', compact(
|
||||
'available', 'currentMonth', 'next', 'nextText', 'prev', 'allBudgets', 'prevText', 'periodStart', 'periodEnd', 'days', 'page',
|
||||
'budgetInformation', 'daysInMonth',
|
||||
'budgetInformation', 'daysPassed',
|
||||
'inactive', 'budgets', 'spent', 'budgeted', 'previousLoop', 'nextLoop', 'start', 'end'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@@ -36,8 +36,6 @@ use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Preferences;
|
||||
use View;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -56,7 +54,7 @@ class ShowController extends Controller
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
View::share('hideBudgets', true);
|
||||
app('view')->share('hideBudgets', true);
|
||||
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
@@ -80,7 +78,7 @@ class ShowController extends Controller
|
||||
{
|
||||
// default values:
|
||||
$moment = $moment ?? '';
|
||||
$range = Preferences::get('viewRange', '1M')->data;
|
||||
$range = app('preferences')->get('viewRange', '1M')->data;
|
||||
$start = null;
|
||||
$end = null;
|
||||
$periods = new Collection;
|
||||
@@ -117,7 +115,7 @@ class ShowController extends Controller
|
||||
}
|
||||
|
||||
$page = (int)$request->get('page');
|
||||
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
|
||||
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
|
||||
|
||||
/** @var JournalCollectorInterface $collector */
|
||||
$collector = app(JournalCollectorInterface::class);
|
||||
@@ -141,7 +139,7 @@ class ShowController extends Controller
|
||||
$start = session('first', Carbon::create()->startOfYear());
|
||||
$end = new Carbon;
|
||||
$page = (int)$request->get('page');
|
||||
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
|
||||
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
|
||||
$limits = $this->getLimits($budget, $start, $end);
|
||||
$repetition = null;
|
||||
|
||||
@@ -172,7 +170,7 @@ class ShowController extends Controller
|
||||
}
|
||||
|
||||
$page = (int)$request->get('page');
|
||||
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
|
||||
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
|
||||
$subTitle = trans(
|
||||
'firefly.budget_in_period',
|
||||
[
|
||||
@@ -239,7 +237,7 @@ class ShowController extends Controller
|
||||
$repository = app(JournalRepositoryInterface::class);
|
||||
$first = $repository->firstNull();
|
||||
$start = null === $first ? new Carbon : $first->date;
|
||||
$range = Preferences::get('viewRange', '1M')->data;
|
||||
$range = app('preferences')->get('viewRange', '1M')->data;
|
||||
$start = app('navigation')->startOfPeriod($start, $range);
|
||||
$end = app('navigation')->endOfX(new Carbon, $range, null);
|
||||
$entries = new Collection;
|
||||
|
Reference in New Issue
Block a user