Code improvements for budgets.

This commit is contained in:
James Cole
2016-04-25 13:20:42 +02:00
parent b4f18dbe77
commit 607d0115f0
5 changed files with 114 additions and 38 deletions

View File

@@ -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;
}
}