mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Code improvements for budgets.
This commit is contained in:
@@ -408,14 +408,16 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param string $repeatFreq
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return LimitRepetition
|
||||
*/
|
||||
public function getCurrentRepetition(Budget $budget, Carbon $start, Carbon $end): LimitRepetition
|
||||
public function getCurrentRepetition(Budget $budget, string $repeatFreq, Carbon $start, Carbon $end): LimitRepetition
|
||||
{
|
||||
$data = $budget->limitrepetitions()
|
||||
->where('budget_limits.repeat_freq', $repeatFreq)
|
||||
->where('limit_repetitions.startdate', $start->format('Y-m-d 00:00:00'))
|
||||
->where('limit_repetitions.enddate', $end->format('Y-m-d 00:00:00'))
|
||||
->first(['limit_repetitions.*']);
|
||||
@@ -844,4 +846,44 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
|
||||
return $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of budget limits that are valid in the current given range.
|
||||
* $ignore is optional. Send an empty limit rep.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param LimitRepetition $ignore
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getValidRepetitions(Budget $budget, Carbon $start, Carbon $end, LimitRepetition $ignore) : Collection
|
||||
{
|
||||
$query = $budget->limitrepetitions()
|
||||
->where( // valid when either of these are true:
|
||||
function ($q) use ($start, $end) {
|
||||
$q->where(
|
||||
function ($query) use ($start, $end) {
|
||||
// starts before start time, and the end also after start time.
|
||||
$query->where('limit_repetitions.startdate', '<=', $start->format('Y-m-d 00:00:00'));
|
||||
$query->where('limit_repetitions.enddate', '>=', $start->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
);
|
||||
$q->orWhere(
|
||||
function ($query) use ($start, $end) {
|
||||
// end after end time, and start is before end time
|
||||
$query->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'));
|
||||
$query->where('limit_repetitions.enddate', '>=', $end->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
if (!is_null($ignore->id)) {
|
||||
$query->where('limit_repetitions.id', '!=', $ignore->id);
|
||||
}
|
||||
$data = $query->get(['limit_repetitions.*']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user