Remove dangerous eternal loop

This commit is contained in:
James Cole
2023-04-26 12:12:23 +02:00
parent 28efc15820
commit 47d5e8d169

View File

@@ -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,10 +184,10 @@ 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 (null === $availableBudget) {
// if not exists: // if not exists:
$currentPeriod = Period::make($current, $currentEnd, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE()); $currentPeriod = Period::make($current, $currentEnd, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());
$daily = $this->getDailyAmount($budgetLimit); $daily = $this->getDailyAmount($budgetLimit);
@@ -212,6 +208,7 @@ class BudgetLimitHandler
] ]
); );
$availableBudget->save(); $availableBudget->save();
}
// prep for next loop // prep for next loop
$current = app('navigation')->addPeriod($current, $viewRange, 0); $current = app('navigation')->addPeriod($current, $viewRange, 0);