Merge pull request #7498 from firefly-iii/fix-7478

Fix #7478
This commit is contained in:
James Cole 2023-05-13 06:17:44 +02:00 committed by GitHub
commit 72eab3c0eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 10 deletions

View File

@ -27,6 +27,7 @@ use FireflyIII\Events\Model\BudgetLimit\Created;
use FireflyIII\Events\Model\BudgetLimit\Deleted;
use FireflyIII\Events\Model\BudgetLimit\Updated;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use Illuminate\Support\Facades\Log;
@ -144,7 +145,7 @@ class BudgetLimitHandler
*/
private function getDailyAmount(BudgetLimit $budgetLimit): string
{
if(0 === (int)$budgetLimit->id) {
if (0 === (int)$budgetLimit->id) {
return '0';
}
$limitPeriod = Period::make(
@ -177,13 +178,14 @@ class BudgetLimitHandler
} catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) {
$viewRange = '1M';
}
$start = app('navigation')->startOfPeriod($budgetLimit->start_date, $viewRange);
$end = app('navigation')->startOfPeriod($budgetLimit->end_date, $viewRange);
$end = app('navigation')->endOfPeriod($end, $viewRange);
$user = $budgetLimit?->budget?->user;
$start = app('navigation')->startOfPeriod($budgetLimit->start_date, $viewRange);
$end = app('navigation')->startOfPeriod($budgetLimit->end_date, $viewRange);
$end = app('navigation')->endOfPeriod($end, $viewRange);
$budget = Budget::withTrashed()->find($budgetLimit->budget_id);
$user = $budget->user;
// sanity check. It's rare but this happens.
if(null === $user) {
// sanity check. It happens when the budget has been deleted so the original user is unknown.
if (null === $user) {
Log::warning('User is null, cannot continue.');
$budgetLimit->forceDelete();
return;
@ -217,10 +219,10 @@ class BudgetLimitHandler
if ($currentPeriod->equals($limitPeriod)) {
$amount = 0 === (int)$budgetLimit->id ? '0' : $budgetLimit->amount;
}
if(0 === bccomp($amount, '0')) {
if (0 === bccomp($amount, '0')) {
Log::debug('Amount is zero, will not create AB.');
}
if(0 !== bccomp($amount, '0')) {
if (0 !== bccomp($amount, '0')) {
Log::debug(sprintf('Will create AB for period %s to %s', $current->format('Y-m-d'), $currentEnd->format('Y-m-d')));
$availableBudget = new AvailableBudget(
[

View File

@ -52,6 +52,8 @@ class BudgetDestroyService
DB::table('budget_transaction')->where('budget_id', (int)$budget->id)->delete();
// also delete all budget limits
$budget->budgetlimits()->delete();
foreach($budget->budgetlimits()->get() as $limit) {
$limit->delete();
}
}
}