Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -43,7 +43,6 @@ use Illuminate\Support\Collection;
use Illuminate\View\View;
/**
*
* Class BudgetLimitController
*/
class BudgetLimitController extends Controller
@@ -76,10 +75,6 @@ class BudgetLimitController extends Controller
}
/**
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return Factory|View
*/
public function create(Budget $budget, Carbon $start, Carbon $end)
@@ -106,9 +101,7 @@ class BudgetLimitController extends Controller
}
/**
* @param BudgetLimit $budgetLimit
*
* @return RedirectResponse|Redirector
* @return Redirector|RedirectResponse
*/
public function delete(BudgetLimit $budgetLimit)
{
@@ -121,12 +114,9 @@ class BudgetLimitController extends Controller
/**
* TODO why redirect AND json response?
*
* @param Request $request
*
* @return RedirectResponse|JsonResponse
* @throws FireflyException
*/
public function store(Request $request): RedirectResponse | JsonResponse
public function store(Request $request): JsonResponse|RedirectResponse
{
app('log')->debug('Going to store new budget-limit.', $request->all());
// first search for existing one and update it if necessary.
@@ -159,6 +149,7 @@ class BudgetLimitController extends Controller
if (null !== $limit) {
$this->blRepository->destroyBudgetLimit($limit);
}
// return empty=ish array:
return response()->json([]);
}
@@ -205,12 +196,6 @@ class BudgetLimitController extends Controller
return redirect(route('budgets.index'));
}
/**
* @param Request $request
* @param BudgetLimit $budgetLimit
*
* @return JsonResponse
*/
public function update(Request $request, BudgetLimit $budgetLimit): JsonResponse
{
$amount = (string)$request->get('amount');
@@ -229,6 +214,7 @@ class BudgetLimitController extends Controller
'left_per_day_formatted' => app('amount')->formatAnything($currency, '0'),
'transaction_currency_id' => $currency->id,
];
return response()->json($array);
}
if ((int)$amount > 268435456) { // 268 million, intentional integer

View File

@@ -44,8 +44,6 @@ class CreateController extends Controller
/**
* CreateController constructor.
*
*/
public function __construct()
{
@@ -65,8 +63,6 @@ class CreateController extends Controller
/**
* Form to create a budget.
*
* @param Request $request
*
* @return Factory|View
*/
public function create(Request $request)
@@ -110,9 +106,6 @@ class CreateController extends Controller
/**
* Stores a budget.
*
* @param BudgetFormStoreRequest $request
*
* @return RedirectResponse
* @throws FireflyException
*/
public function store(BudgetFormStoreRequest $request): RedirectResponse
@@ -125,7 +118,7 @@ class CreateController extends Controller
app('preferences')->mark();
// store attachment(s):
/** @var array|null $files */
/** @var null|array $files */
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
if (null !== $files && !auth()->user()->hasRole('demo')) {
$this->attachments->saveAttachmentsForModel($budget, $files);

View File

@@ -33,7 +33,6 @@ use Illuminate\Routing\Redirector;
use Illuminate\View\View;
/**
*
* Class DeleteController
*/
class DeleteController extends Controller
@@ -43,8 +42,6 @@ class DeleteController extends Controller
/**
* DeleteController constructor.
*
*/
public function __construct()
{
@@ -64,8 +61,6 @@ class DeleteController extends Controller
/**
* Deletes a budget.
*
* @param Budget $budget
*
* @return Factory|View
*/
public function delete(Budget $budget)
@@ -81,10 +76,7 @@ class DeleteController extends Controller
/**
* Destroys a budget.
*
* @param Request $request
* @param Budget $budget
*
* @return RedirectResponse|Redirector
* @return Redirector|RedirectResponse
*/
public function destroy(Request $request, Budget $budget)
{

View File

@@ -35,7 +35,6 @@ use Illuminate\Http\Request;
use Illuminate\View\View;
/**
*
* Class EditController
*/
class EditController extends Controller
@@ -45,8 +44,6 @@ class EditController extends Controller
/**
* EditController constructor.
*
*/
public function __construct()
{
@@ -67,9 +64,6 @@ class EditController extends Controller
/**
* Budget edit form.
*
* @param Request $request
* @param Budget $budget
*
* @return Factory|View
*/
public function edit(Request $request, Budget $budget)
@@ -121,11 +115,6 @@ class EditController extends Controller
/**
* Budget update routine.
*
* @param BudgetFormUpdateRequest $request
* @param Budget $budget
*
* @return RedirectResponse
*/
public function update(BudgetFormUpdateRequest $request, Budget $budget): RedirectResponse
{
@@ -139,7 +128,7 @@ class EditController extends Controller
$redirect = redirect($this->getPreviousUrl('budgets.edit.url'));
// store new attachment(s):
/** @var array|null $files */
/** @var null|array $files */
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
if (null !== $files && !auth()->user()->hasRole('demo')) {
$this->attachments->saveAttachmentsForModel($budget, $files);

View File

@@ -41,12 +41,10 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\View\View;
use JsonException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
*
* Class IndexController
*/
class IndexController extends Controller
@@ -61,8 +59,6 @@ class IndexController extends Controller
/**
* IndexController constructor.
*
*/
public function __construct()
{
@@ -87,12 +83,10 @@ class IndexController extends Controller
/**
* Show all budgets.
*
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Factory|View
*
* @throws FireflyException
* @throws JsonException
* @throws \JsonException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
@@ -115,7 +109,6 @@ class IndexController extends Controller
$end ??= session('end', today(config('app.timezone'))->endOfMonth());
}
$defaultCurrency = app('amount')->getDefaultCurrency();
$currencies = $this->currencyRepository->get();
$budgeted = '0';
@@ -135,7 +128,7 @@ class IndexController extends Controller
// get budgeted for default currency:
if (0 === count($availableBudgets)) {
$budgeted = $this->blRepository->budgeted($start, $end, $defaultCurrency, );
$budgeted = $this->blRepository->budgeted($start, $end, $defaultCurrency);
$spentArr = $this->opsRepository->sumExpenses($start, $end, null, null, $defaultCurrency);
$spent = $spentArr[$defaultCurrency->id]['sum'] ?? '0';
unset($spentArr);
@@ -171,17 +164,30 @@ class IndexController extends Controller
);
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function reorder(Request $request, BudgetRepositoryInterface $repository): JsonResponse
{
$this->abRepository->cleanup();
$budgetIds = $request->get('budgetIds');
foreach ($budgetIds as $index => $budgetId) {
$budgetId = (int)$budgetId;
$budget = $repository->find($budgetId);
if (null !== $budget) {
app('log')->debug(sprintf('Set budget #%d ("%s") to position %d', $budget->id, $budget->name, $index + 1));
$repository->setBudgetOrder($budget, $index + 1);
}
}
app('preferences')->mark();
return response()->json(['OK']);
}
private function getAllAvailableBudgets(Carbon $start, Carbon $end): array
{
// get all available budgets.
$ab = $this->abRepository->get($start, $end);
$availableBudgets = [];
// for each, complement with spent amount:
/** @var AvailableBudget $entry */
foreach ($ab as $entry) {
@@ -194,7 +200,7 @@ class IndexController extends Controller
$array['spent'] = $spentArr[$entry->transaction_currency_id]['sum'] ?? '0';
// budgeted in period:
$budgeted = $this->blRepository->budgeted($entry->start_date, $entry->end_date, $entry->transactionCurrency, );
$budgeted = $this->blRepository->budgeted($entry->start_date, $entry->end_date, $entry->transactionCurrency);
$array['budgeted'] = $budgeted;
$availableBudgets[] = $array;
unset($spentArr);
@@ -203,14 +209,6 @@ class IndexController extends Controller
return $availableBudgets;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $currencies
* @param TransactionCurrency $defaultCurrency
*
* @return array
*/
private function getAllBudgets(Carbon $start, Carbon $end, Collection $currencies, TransactionCurrency $defaultCurrency): array
{
// get all budgets, and paginate them into $budgets.
@@ -229,6 +227,7 @@ class IndexController extends Controller
$array['attachments'] = $this->repository->getAttachments($current);
$array['auto_budget'] = $this->repository->getAutoBudget($current);
$budgetLimits = $this->blRepository->getBudgetLimits($current, $start, $end);
/** @var BudgetLimit $limit */
foreach ($budgetLimits as $limit) {
app('log')->debug(sprintf('Working on budget limit #%d', $limit->id));
@@ -264,11 +263,6 @@ class IndexController extends Controller
return $budgets;
}
/**
* @param array $budgets
*
* @return array
*/
private function getSums(array $budgets): array
{
$sums = [
@@ -284,11 +278,11 @@ class IndexController extends Controller
$currencyId = $spent['currency_id'];
$sums['spent'][$currencyId]
??= [
'amount' => '0',
'currency_id' => $spent['currency_id'],
'currency_symbol' => $spent['currency_symbol'],
'currency_decimal_places' => $spent['currency_decimal_places'],
];
'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'], $spent['spent']);
}
@@ -297,23 +291,24 @@ class IndexController extends Controller
$currencyId = $budgeted['currency_id'];
$sums['budgeted'][$currencyId]
??= [
'amount' => '0',
'currency_id' => $budgeted['currency_id'],
'currency_symbol' => $budgeted['currency_symbol'],
'currency_decimal_places' => $budgeted['currency_decimal_places'],
];
'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'], $budgeted['amount']);
// also calculate how much left from budgeted:
$sums['left'][$currencyId]
??= [
'amount' => '0',
'currency_id' => $budgeted['currency_id'],
'currency_symbol' => $budgeted['currency_symbol'],
'currency_decimal_places' => $budgeted['currency_decimal_places'],
];
'amount' => '0',
'currency_id' => $budgeted['currency_id'],
'currency_symbol' => $budgeted['currency_symbol'],
'currency_decimal_places' => $budgeted['currency_decimal_places'],
];
}
}
// final calculation for 'left':
/**
* @var int $currencyId
@@ -326,28 +321,4 @@ class IndexController extends Controller
return $sums;
}
/**
* @param Request $request
* @param BudgetRepositoryInterface $repository
*
* @return JsonResponse
*/
public function reorder(Request $request, BudgetRepositoryInterface $repository): JsonResponse
{
$this->abRepository->cleanup();
$budgetIds = $request->get('budgetIds');
foreach ($budgetIds as $index => $budgetId) {
$budgetId = (int)$budgetId;
$budget = $repository->find($budgetId);
if (null !== $budget) {
app('log')->debug(sprintf('Set budget #%d ("%s") to position %d', $budget->id, $budget->name, $index + 1));
$repository->setBudgetOrder($budget, $index + 1);
}
}
app('preferences')->mark();
return response()->json(['OK']);
}
}

View File

@@ -41,7 +41,6 @@ use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
*
* Class ShowController
*/
class ShowController extends Controller
@@ -54,8 +53,6 @@ class ShowController extends Controller
/**
* ShowController constructor.
*
*/
public function __construct()
{
@@ -76,20 +73,17 @@ class ShowController extends Controller
/**
* Show transactions without a budget.
*
* @param Request $request
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Factory|View
*
* @throws FireflyException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function noBudget(Request $request, Carbon $start = null, Carbon $end = null)
{
/** @var Carbon $start */
// @var Carbon $start
$start ??= session('start');
/** @var Carbon $end */
// @var Carbon $end
$end ??= session('end');
$subTitle = trans(
'firefly.without_budget_between',
@@ -106,7 +100,8 @@ class ShowController extends Controller
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setLimit($pageSize)->setPage($page)
->withoutBudget()->withAccountInformation()->withCategoryInformation();
->withoutBudget()->withAccountInformation()->withCategoryInformation()
;
$groups = $collector->getPaginatedGroups();
$groups->setPath(route('budgets.no-budget'));
@@ -116,9 +111,8 @@ class ShowController extends Controller
/**
* Shows ALL transactions without a budget.
*
* @param Request $request
*
* @return Factory|View
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
@@ -134,7 +128,8 @@ class ShowController extends Controller
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setLimit($pageSize)->setPage($page)
->withoutBudget()->withAccountInformation()->withCategoryInformation();
->withoutBudget()->withAccountInformation()->withCategoryInformation()
;
$groups = $collector->getPaginatedGroups();
$groups->setPath(route('budgets.no-budget-all'));
@@ -144,10 +139,8 @@ class ShowController extends Controller
/**
* Show a single budget.
*
* @param Request $request
* @param Budget $budget
*
* @return Factory|View
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
@@ -166,8 +159,9 @@ class ShowController extends Controller
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setRange($allStart, $allEnd)->setBudget($budget)
->withAccountInformation()
->setLimit($pageSize)->setPage($page)->withBudgetInformation()->withCategoryInformation();
->withAccountInformation()
->setLimit($pageSize)->setPage($page)->withBudgetInformation()->withCategoryInformation()
;
$groups = $collector->getPaginatedGroups();
$groups->setPath(route('budgets.show', [$budget->id]));
@@ -179,11 +173,8 @@ class ShowController extends Controller
/**
* Show a single budget by a budget limit.
*
* @param Request $request
* @param Budget $budget
* @param BudgetLimit $budgetLimit
*
* @return Factory|View
*
* @throws FireflyException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
@@ -211,9 +202,11 @@ class ShowController extends Controller
$collector = app(GroupCollectorInterface::class);
$collector->setRange($budgetLimit->start_date, $budgetLimit->end_date)->withAccountInformation()
->setBudget($budget)->setLimit($pageSize)->setPage($page)->withBudgetInformation()->withCategoryInformation();
->setBudget($budget)->setLimit($pageSize)->setPage($page)->withBudgetInformation()->withCategoryInformation()
;
$groups = $collector->getPaginatedGroups();
$groups->setPath(route('budgets.show.limit', [$budget->id, $budgetLimit->id]));
/** @var Carbon $start */
$start = session('first', today(config('app.timezone'))->startOfYear());
$end = today(config('app.timezone'));