Attempted fix with sanity check for #11284

This commit is contained in:
James Cole
2025-11-26 18:35:56 +01:00
parent 3c904c9017
commit 7abd30f4dd
2 changed files with 90 additions and 66 deletions

View File

@@ -248,14 +248,28 @@ class Navigation
Log::debug(sprintf('Diff in days is %d', $diffInDays)); Log::debug(sprintf('Diff in days is %d', $diffInDays));
$currentEnd->addDays($diffInDays); $currentEnd->addDays($diffInDays);
// add sanity check.
if ($currentEnd->lt($end)) {
throw new FireflyException(sprintf('[a] endOfPeriod(%s, %s) failed, because it resulted in %s.', $end->toW3cString(), $repeatFreq, $currentEnd->toW3cString()));
}
return $currentEnd; return $currentEnd;
} }
if ('MTD' === $repeatFreq) { if ('MTD' === $repeatFreq) {
$today = today(); $today = today();
if ($today->isSameMonth($end)) { if ($today->isSameMonth($end)) {
return $today->endOfDay()->milli(0); $res = $today->endOfDay()->milli(0);
// add sanity check.
if($res->lt($end)) {
throw new FireflyException(sprintf('[b] endOfPeriod(%s, %s) failed, because it resulted in %s.', $end->toW3cString(), $repeatFreq, $res->toW3cString()));
}
return $res;
} }
// add sanity check.
if ($currentEnd->lt($end)) {
throw new FireflyException(sprintf('[c] endOfPeriod(%s, %s) failed, because it resulted in %s.', $end->toW3cString(), $repeatFreq, $currentEnd->toW3cString()));
}
return $end->endOfMonth(); return $end->endOfMonth();
} }
@@ -270,6 +284,11 @@ class Navigation
default => null, default => null,
}; };
if (null !== $result) { if (null !== $result) {
// add sanity check.
if ($currentEnd->lt($end)) {
throw new FireflyException(sprintf('[d] endOfPeriod(%s, %s) failed, because it resulted in %s.', $end->toW3cString(), $repeatFreq, $currentEnd->toW3cString()));
}
return $result; return $result;
} }
unset($result); unset($result);
@@ -277,6 +296,11 @@ class Navigation
if (!array_key_exists($repeatFreq, $functionMap)) { if (!array_key_exists($repeatFreq, $functionMap)) {
Log::error(sprintf('Cannot do endOfPeriod for $repeat_freq "%s"', $repeatFreq)); Log::error(sprintf('Cannot do endOfPeriod for $repeat_freq "%s"', $repeatFreq));
// add sanity check.
if ($currentEnd->lt($end)) {
throw new FireflyException(sprintf('[e] endOfPeriod(%s, %s) failed, because it resulted in %s.', $end->toW3cString(), $repeatFreq, $currentEnd->toW3cString()));
}
return $end; return $end;
} }
$function = $functionMap[$repeatFreq]; $function = $functionMap[$repeatFreq];
@@ -288,6 +312,11 @@ class Navigation
} }
$currentEnd->endOfDay()->milli(0); $currentEnd->endOfDay()->milli(0);
// add sanity check.
if ($currentEnd->lt($end)) {
throw new FireflyException(sprintf('[f] endOfPeriod(%s, %s) failed, because it resulted in %s.', $end->toW3cString(), $repeatFreq, $currentEnd->toW3cString()));
}
return $currentEnd; return $currentEnd;
} }
$currentEnd->{$function}(); // @phpstan-ignore-line $currentEnd->{$function}(); // @phpstan-ignore-line
@@ -297,6 +326,11 @@ class Navigation
} }
// Log::debug(sprintf('Final result: %s', $currentEnd->toIso8601String())); // Log::debug(sprintf('Final result: %s', $currentEnd->toIso8601String()));
// add sanity check.
if ($currentEnd->lt($end)) {
throw new FireflyException(sprintf('[g] endOfPeriod(%s, %s) failed, because it resulted in %s.', $end->toW3cString(), $repeatFreq, $currentEnd->toW3cString()));
}
return $currentEnd; return $currentEnd;
} }

View File

@@ -201,18 +201,8 @@ trait RecalculatesAvailableBudgetsTrait
Log::debug('Found 1 AB, will update.'); Log::debug('Found 1 AB, will update.');
$this->calculateAmount($availableBudget); $this->calculateAmount($availableBudget);
} }
if (null === $availableBudget) { if (null === $availableBudget && $currentEnd->gte($current)) {
Log::debug('No AB found, will create.');
// if not exists:
try {
$currentPeriod = Period::make($current, $currentEnd, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE()); $currentPeriod = Period::make($current, $currentEnd, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());
} catch (InvalidPeriod $e) {
Log::error('Tried to make invalid period.');
Log::error($e->getMessage());
continue;
}
$daily = $this->getDailyAmount($budgetLimit); $daily = $this->getDailyAmount($budgetLimit);
$amount = bcmul((string)$daily, (string)$currentPeriod->length(), 12); $amount = bcmul((string)$daily, (string)$currentPeriod->length(), 12);