James Cole
2025-11-25 20:12:35 +01:00
parent fa018e80c0
commit d7967a81e3

View File

@@ -29,11 +29,13 @@ use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Support\Facades\Navigation;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Spatie\Period\Boundaries; use Spatie\Period\Boundaries;
use Spatie\Period\Exceptions\InvalidPeriod;
use Spatie\Period\Period; use Spatie\Period\Period;
use Spatie\Period\Precision; use Spatie\Period\Precision;
@@ -171,9 +173,16 @@ trait RecalculatesAvailableBudgetsTrait
} }
$viewRange = (string)$viewRange; $viewRange = (string)$viewRange;
$start = app('navigation')->startOfPeriod($budgetLimit->start_date, $viewRange); $start = Navigation::startOfPeriod($budgetLimit->start_date, $viewRange);
$end = app('navigation')->startOfPeriod($budgetLimit->end_date, $viewRange); $end = Navigation::startOfPeriod($budgetLimit->end_date, $viewRange);
$end = app('navigation')->endOfPeriod($end, $viewRange); $end = Navigation::endOfPeriod($end, $viewRange);
if ($end < $start) {
[$start, $end] = [$end, $start];
$budgetLimit->start_date = $start;
$budgetLimit->end_date = $end;
$budgetLimit->saveQuietly();
}
// limit period in total is: // limit period in total is:
$limitPeriod = Period::make($start, $end, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE()); $limitPeriod = Period::make($start, $end, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());
@@ -182,7 +191,7 @@ trait RecalculatesAvailableBudgetsTrait
// from the start until the end of the budget limit, need to loop! // from the start until the end of the budget limit, need to loop!
$current = clone $start; $current = clone $start;
while ($current <= $end) { while ($current <= $end) {
$currentEnd = app('navigation')->endOfPeriod($current, $viewRange); $currentEnd = Navigation::endOfPeriod($current, $viewRange);
// create or find AB for this particular period, and set the amount accordingly. // create or find AB for this particular period, and set the amount accordingly.
/** @var null|AvailableBudget $availableBudget */ /** @var null|AvailableBudget $availableBudget */
@@ -195,7 +204,13 @@ trait RecalculatesAvailableBudgetsTrait
if (null === $availableBudget) { if (null === $availableBudget) {
Log::debug('No AB found, will create.'); Log::debug('No AB found, will create.');
// if not exists: // 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);
@@ -227,7 +242,7 @@ trait RecalculatesAvailableBudgetsTrait
} }
// prep for next loop // prep for next loop
$current = app('navigation')->addPeriod($current, $viewRange); $current = Navigation::addPeriod($current, $viewRange);
} }
} }
} }