mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
feat: a budget type that will rollover but also incorporate overspending from the previous period (if any)
This commit is contained in:
@@ -81,6 +81,70 @@ class CreateAutoBudgetLimits implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AutoBudget $autoBudget
|
||||
* @return void
|
||||
*/
|
||||
private function createAdjustedLimit(AutoBudget $autoBudget): void
|
||||
{
|
||||
Log::debug(sprintf('Will now manage rollover for auto budget #%d', $autoBudget->id));
|
||||
// current period:
|
||||
$start = app('navigation')->startOfPeriod($this->date, $autoBudget->period);
|
||||
$end = app('navigation')->endOfPeriod($start, $autoBudget->period);
|
||||
|
||||
// which means previous period:
|
||||
$previousStart = app('navigation')->subtractPeriod($start, $autoBudget->period);
|
||||
$previousEnd = app('navigation')->endOfPeriod($previousStart, $autoBudget->period);
|
||||
|
||||
Log::debug(
|
||||
sprintf(
|
||||
'Current period is %s-%s, so previous period is %s-%s',
|
||||
$start->format('Y-m-d'),
|
||||
$end->format('Y-m-d'),
|
||||
$previousStart->format('Y-m-d'),
|
||||
$previousEnd->format('Y-m-d')
|
||||
)
|
||||
);
|
||||
|
||||
// has budget limit in previous period?
|
||||
$budgetLimit = $this->findBudgetLimit($autoBudget->budget, $previousStart, $previousEnd);
|
||||
|
||||
if (null === $budgetLimit) {
|
||||
Log::debug('No budget limit exists in previous period, so create one.');
|
||||
// if not, create standard amount and we're done.
|
||||
$this->createBudgetLimit($autoBudget, $start, $end);
|
||||
return;
|
||||
}
|
||||
Log::debug('Budget limit exists for previous period.');
|
||||
|
||||
// if has one, calculate expenses and use that as a base.
|
||||
$repository = app(OperationsRepositoryInterface::class);
|
||||
$repository->setUser($autoBudget->budget->user);
|
||||
$spent = $repository->sumExpenses($previousStart, $previousEnd, null, new Collection([$autoBudget->budget]), $autoBudget->transactionCurrency);
|
||||
$currencyId = (int)$autoBudget->transaction_currency_id;
|
||||
$spentAmount = $spent[$currencyId]['sum'] ?? '0';
|
||||
Log::debug(sprintf('Spent in previous budget period (%s-%s) is %s', $previousStart->format('Y-m-d'), $previousEnd->format('Y-m-d'), $spentAmount));
|
||||
|
||||
// what you spent in previous period PLUS the amount for the current period,
|
||||
// if that is more than zero, that's the amount that will be set.
|
||||
|
||||
$budgetAvailable = bcadd(bcadd($budgetLimit->amount, $autoBudget->amount), $spentAmount);
|
||||
$totalAmount = $autoBudget->amount;
|
||||
Log::debug(sprintf('Total amount available for current budget period is %s', $budgetAvailable));
|
||||
|
||||
if (-1 !== bccomp( $budgetAvailable, $totalAmount)) {
|
||||
Log::info(sprintf('There is no overspending, no need to adjust. Budget limit amount will be %s.', $totalAmount));
|
||||
// create budget limit:
|
||||
$this->createBudgetLimit($autoBudget, $start, $end, $totalAmount);
|
||||
}
|
||||
if (1 !== bccomp($budgetAvailable, $totalAmount)) {
|
||||
Log::info(sprintf('There was overspending, so the new amount will be %s.', $budgetAvailable));
|
||||
// create budget limit:
|
||||
$this->createBudgetLimit($autoBudget, $start, $end, $budgetAvailable);
|
||||
}
|
||||
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AutoBudget $autoBudget
|
||||
*
|
||||
@@ -148,6 +212,13 @@ class CreateAutoBudgetLimits implements ShouldQueue
|
||||
|
||||
return;
|
||||
}
|
||||
if (null === $budgetLimit && AutoBudget::AUTO_BUDGET_ADJUSTED === (int)$autoBudget->auto_budget_type) {
|
||||
// budget limit exists already,
|
||||
$this->createAdjustedLimit($autoBudget);
|
||||
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user