mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-01 02:21:45 +00:00
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
@@ -161,7 +163,7 @@ trait RecalculatesAvailableBudgetsTrait
|
|||||||
// all have to be created or updated.
|
// all have to be created or updated.
|
||||||
try {
|
try {
|
||||||
$viewRange = app('preferences')->getForUser($user, 'viewRange', '1M')->data;
|
$viewRange = app('preferences')->getForUser($user, 'viewRange', '1M')->data;
|
||||||
} catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) {
|
} catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) {
|
||||||
Log::error($e->getMessage());
|
Log::error($e->getMessage());
|
||||||
$viewRange = '1M';
|
$viewRange = '1M';
|
||||||
}
|
}
|
||||||
@@ -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,9 +204,15 @@ 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);
|
||||||
|
|
||||||
// no need to calculate if period is equal.
|
// no need to calculate if period is equal.
|
||||||
if ($currentPeriod->equals($limitPeriod)) {
|
if ($currentPeriod->equals($limitPeriod)) {
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user