mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-22 12:11:19 +00:00
Code cleanup.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
|
||||
|
||||
/*
|
||||
* BudgetLimitHandler.php
|
||||
* Copyright (c) 2023 james@firefly-iii.org
|
||||
@@ -44,22 +43,26 @@ use Spatie\Period\Precision;
|
||||
*/
|
||||
class BudgetLimitHandler
|
||||
{
|
||||
/**
|
||||
* @param Created $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function created(Created $event): void
|
||||
{
|
||||
app('log')->debug(sprintf('BudgetLimitHandler::created(#%s)', $event->budgetLimit->id));
|
||||
$this->updateAvailableBudget($event->budgetLimit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BudgetLimit $budgetLimit
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Deleted $event): void
|
||||
{
|
||||
app('log')->debug(sprintf('BudgetLimitHandler::deleted(#%s)', $event->budgetLimit->id));
|
||||
$budgetLimit = $event->budgetLimit;
|
||||
$budgetLimit->id = 0;
|
||||
$this->updateAvailableBudget($event->budgetLimit);
|
||||
}
|
||||
|
||||
public function updated(Updated $event): void
|
||||
{
|
||||
app('log')->debug(sprintf('BudgetLimitHandler::updated(#%s)', $event->budgetLimit->id));
|
||||
$this->updateAvailableBudget($event->budgetLimit);
|
||||
}
|
||||
|
||||
private function updateAvailableBudget(BudgetLimit $budgetLimit): void
|
||||
{
|
||||
app('log')->debug(sprintf('Now in updateAvailableBudget(#%d)', $budgetLimit->id));
|
||||
@@ -71,15 +74,18 @@ class BudgetLimitHandler
|
||||
if (null === $budget) {
|
||||
app('log')->warning('Budget is still null, cannot continue, will delete budget limit.');
|
||||
$budgetLimit->forceDelete();
|
||||
|
||||
return;
|
||||
}
|
||||
/** @var User|null $user */
|
||||
|
||||
/** @var null|User $user */
|
||||
$user = $budget->user;
|
||||
|
||||
// sanity check. It happens when the budget has been deleted so the original user is unknown.
|
||||
if (null === $user) {
|
||||
app('log')->warning('User is null, cannot continue.');
|
||||
$budgetLimit->forceDelete();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -88,7 +94,7 @@ class BudgetLimitHandler
|
||||
// all have to be created or updated.
|
||||
try {
|
||||
$viewRange = app('preferences')->getForUser($user, 'viewRange', '1M')->data;
|
||||
} catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) {
|
||||
} catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) {
|
||||
app('log')->error($e->getMessage());
|
||||
$viewRange = '1M';
|
||||
}
|
||||
@@ -112,7 +118,7 @@ class BudgetLimitHandler
|
||||
$currentEnd = app('navigation')->endOfPeriod($current, $viewRange);
|
||||
|
||||
// create or find AB for this particular period, and set the amount accordingly.
|
||||
/** @var AvailableBudget|null $availableBudget */
|
||||
/** @var null|AvailableBudget $availableBudget */
|
||||
$availableBudget = $user->availableBudgets()->where('start_date', $current->format('Y-m-d'))->where(
|
||||
'end_date',
|
||||
$currentEnd->format('Y-m-d')
|
||||
@@ -156,11 +162,6 @@ class BudgetLimitHandler
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AvailableBudget $availableBudget
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function calculateAmount(AvailableBudget $availableBudget): void
|
||||
{
|
||||
$repository = app(BudgetLimitRepositoryInterface::class);
|
||||
@@ -178,6 +179,7 @@ class BudgetLimitHandler
|
||||
// have to recalculate everything just in case.
|
||||
$set = $repository->getAllBudgetLimitsByCurrency($availableBudget->transactionCurrency, $availableBudget->start_date, $availableBudget->end_date);
|
||||
app('log')->debug(sprintf('Found %d interesting budget limit(s).', $set->count()));
|
||||
|
||||
/** @var BudgetLimit $budgetLimit */
|
||||
foreach ($set as $budgetLimit) {
|
||||
app('log')->debug(
|
||||
@@ -218,6 +220,7 @@ class BudgetLimitHandler
|
||||
if (0 === bccomp('0', $newAmount)) {
|
||||
app('log')->debug('New amount is zero, deleting AB.');
|
||||
$availableBudget->delete();
|
||||
|
||||
return;
|
||||
}
|
||||
app('log')->debug(sprintf('Concluded new amount for this AB must be %s', $newAmount));
|
||||
@@ -225,11 +228,6 @@ class BudgetLimitHandler
|
||||
$availableBudget->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BudgetLimit $budgetLimit
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getDailyAmount(BudgetLimit $budgetLimit): string
|
||||
{
|
||||
if (0 === $budgetLimit->id) {
|
||||
@@ -246,31 +244,7 @@ class BudgetLimitHandler
|
||||
app('log')->debug(
|
||||
sprintf('Total amount for budget limit #%d is %s. Nr. of days is %d. Amount per day is %s', $budgetLimit->id, $budgetLimit->amount, $days, $amount)
|
||||
);
|
||||
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Deleted $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Deleted $event): void
|
||||
{
|
||||
app('log')->debug(sprintf('BudgetLimitHandler::deleted(#%s)', $event->budgetLimit->id));
|
||||
$budgetLimit = $event->budgetLimit;
|
||||
$budgetLimit->id = 0;
|
||||
$this->updateAvailableBudget($event->budgetLimit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Updated $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updated(Updated $event): void
|
||||
{
|
||||
app('log')->debug(sprintf('BudgetLimitHandler::updated(#%s)', $event->budgetLimit->id));
|
||||
$this->updateAvailableBudget($event->budgetLimit);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user