mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-13 16:00:13 +00:00
Merge pull request #7435 from firefly-iii/better-ab-mgt
Remove dangerous eternal loop
This commit is contained in:
@@ -89,6 +89,7 @@ class BudgetLimitHandler
|
|||||||
);
|
);
|
||||||
// have to recalc everything just in case.
|
// have to recalc everything just in case.
|
||||||
$set = $repository->getAllBudgetLimitsByCurrency($availableBudget->transactionCurrency, $availableBudget->start_date, $availableBudget->end_date);
|
$set = $repository->getAllBudgetLimitsByCurrency($availableBudget->transactionCurrency, $availableBudget->start_date, $availableBudget->end_date);
|
||||||
|
Log::debug(sprintf('Found %d interesting budget limit(s).', $set->count()));
|
||||||
/** @var BudgetLimit $budgetLimit */
|
/** @var BudgetLimit $budgetLimit */
|
||||||
foreach ($set as $budgetLimit) {
|
foreach ($set as $budgetLimit) {
|
||||||
Log::debug(
|
Log::debug(
|
||||||
@@ -108,22 +109,17 @@ class BudgetLimitHandler
|
|||||||
);
|
);
|
||||||
// if both equal eachother, amount from this BL must be added to the AB
|
// if both equal eachother, amount from this BL must be added to the AB
|
||||||
if ($limitPeriod->equals($abPeriod)) {
|
if ($limitPeriod->equals($abPeriod)) {
|
||||||
Log::debug('Limit period is the same as AB period.');
|
|
||||||
$newAmount = bcadd($newAmount, $budgetLimit->amount);
|
$newAmount = bcadd($newAmount, $budgetLimit->amount);
|
||||||
}
|
}
|
||||||
// if budget limit period inside AB period, can be added in full.
|
// if budget limit period inside AB period, can be added in full.
|
||||||
|
|
||||||
if (!$limitPeriod->equals($abPeriod) && $abPeriod->contains($limitPeriod)) {
|
if (!$limitPeriod->equals($abPeriod) && $abPeriod->contains($limitPeriod)) {
|
||||||
Log::debug('Limit period falls inside of AB period.');
|
|
||||||
$newAmount = bcadd($newAmount, $budgetLimit->amount);
|
$newAmount = bcadd($newAmount, $budgetLimit->amount);
|
||||||
}
|
}
|
||||||
if (!$limitPeriod->equals($abPeriod) && $abPeriod->overlapsWith($limitPeriod)) {
|
if (!$limitPeriod->equals($abPeriod) && $abPeriod->overlapsWith($limitPeriod)) {
|
||||||
Log::debug('Limit period overlaps AB period.');
|
|
||||||
$overlap = $abPeriod->overlap($limitPeriod);
|
$overlap = $abPeriod->overlap($limitPeriod);
|
||||||
if (null !== $overlap) {
|
if (null !== $overlap) {
|
||||||
$length = $overlap->length();
|
$length = $overlap->length();
|
||||||
$daily = bcmul($this->getDailyAmount($budgetLimit), (string)$length);
|
$daily = bcmul($this->getDailyAmount($budgetLimit), (string)$length);
|
||||||
Log::debug(sprintf('Length of overlap is %d days, so daily amount is %s', $length, $daily));
|
|
||||||
$newAmount = bcadd($newAmount, $daily);
|
$newAmount = bcadd($newAmount, $daily);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,30 +184,31 @@ class BudgetLimitHandler
|
|||||||
$currentEnd->format('Y-m-d')
|
$currentEnd->format('Y-m-d')
|
||||||
)->where('transaction_currency_id', $budgetLimit->transaction_currency_id)->first();
|
)->where('transaction_currency_id', $budgetLimit->transaction_currency_id)->first();
|
||||||
if (null !== $availableBudget) {
|
if (null !== $availableBudget) {
|
||||||
Log::debug('Found AB, will update.');
|
Log::debug('Found 1 AB, will update.');
|
||||||
$this->calculateAmount($availableBudget);
|
$this->calculateAmount($availableBudget);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
// if not exists:
|
if (null === $availableBudget) {
|
||||||
$currentPeriod = Period::make($current, $currentEnd, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());
|
// if not exists:
|
||||||
$daily = $this->getDailyAmount($budgetLimit);
|
$currentPeriod = Period::make($current, $currentEnd, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());
|
||||||
$amount = bcmul($daily, (string)$currentPeriod->length(), 12);
|
$daily = $this->getDailyAmount($budgetLimit);
|
||||||
|
$amount = bcmul($daily, (string)$currentPeriod->length(), 12);
|
||||||
|
|
||||||
// no need to calculate if period is equal.
|
// no need to calculate if period is equal.
|
||||||
if ($currentPeriod->equals($limitPeriod)) {
|
if ($currentPeriod->equals($limitPeriod)) {
|
||||||
$amount = $budgetLimit->amount;
|
$amount = $budgetLimit->amount;
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('Will create AB for period %s to %s', $current->format('Y-m-d'), $currentEnd->format('Y-m-d')));
|
||||||
|
$availableBudget = new AvailableBudget(
|
||||||
|
[
|
||||||
|
'user_id' => $budgetLimit->budget->user->id,
|
||||||
|
'transaction_currency_id' => $budgetLimit->transaction_currency_id,
|
||||||
|
'start_date' => $current,
|
||||||
|
'end_date' => $currentEnd,
|
||||||
|
'amount' => $amount,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$availableBudget->save();
|
||||||
}
|
}
|
||||||
Log::debug(sprintf('Will create AB for period %s to %s', $current->format('Y-m-d'), $currentEnd->format('Y-m-d')));
|
|
||||||
$availableBudget = new AvailableBudget(
|
|
||||||
[
|
|
||||||
'user_id' => $budgetLimit->budget->user->id,
|
|
||||||
'transaction_currency_id' => $budgetLimit->transaction_currency_id,
|
|
||||||
'start_date' => $current,
|
|
||||||
'end_date' => $currentEnd,
|
|
||||||
'amount' => $amount,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
$availableBudget->save();
|
|
||||||
|
|
||||||
// prep for next loop
|
// prep for next loop
|
||||||
$current = app('navigation')->addPeriod($current, $viewRange, 0);
|
$current = app('navigation')->addPeriod($current, $viewRange, 0);
|
||||||
|
Reference in New Issue
Block a user