mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-14 00:04:24 +00:00
Expand API with budget limits
This commit is contained in:
@@ -154,6 +154,20 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a budget limit.
|
||||
*
|
||||
* @param BudgetLimit $budgetLimit
|
||||
*/
|
||||
public function deleteBudgetLimit(BudgetLimit $budgetLimit): void
|
||||
{
|
||||
try {
|
||||
$budgetLimit->delete();
|
||||
} catch (Exception $e) {
|
||||
Log::error(sprintf('Could not delete budget limit: %s', $e->getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
*
|
||||
@@ -315,8 +329,35 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAllBudgetLimits(Carbon $start, Carbon $end): Collection
|
||||
public function getAllBudgetLimits(Carbon $start = null, Carbon $end = null): Collection
|
||||
{
|
||||
// both are NULL:
|
||||
if (null === $start && null === $end) {
|
||||
$set = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->with(['budget'])
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
->get(['budget_limits.*']);
|
||||
|
||||
return $set;
|
||||
}
|
||||
// one of the two is NULL.
|
||||
if (null === $start xor null === $end) {
|
||||
$query = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->with(['budget'])
|
||||
->where('budgets.user_id', $this->user->id);
|
||||
if (null !== $end) {
|
||||
// end date must be before $end.
|
||||
$query->where('end_date', '<=', $end->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
if (null !== $start) {
|
||||
// start date must be after $start.
|
||||
$query->where('start_date', '>=', $start->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
$set = $query->get(['budget_limits.*']);
|
||||
|
||||
return $set;
|
||||
}
|
||||
// neither are NULL:
|
||||
$set = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->with(['budget'])
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
@@ -389,8 +430,28 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgetLimits(Budget $budget, Carbon $start, Carbon $end): Collection
|
||||
public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection
|
||||
{
|
||||
if (null === $end && null === $start) {
|
||||
return $budget->budgetlimits()->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
|
||||
}
|
||||
if (null === $end xor null === $start) {
|
||||
$query = $budget->budgetlimits()->orderBy('budget_limits.start_date', 'DESC');
|
||||
// one of the two is null
|
||||
if (null !== $end) {
|
||||
// end date must be before $end.
|
||||
$query->where('end_date', '<=', $end->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
if (null !== $start) {
|
||||
// start date must be after $start.
|
||||
$query->where('start_date', '>=', $start->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
$set = $query->get(['budget_limits.*']);
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
// when both dates are set:
|
||||
$set = $budget->budgetlimits()
|
||||
->where(
|
||||
function (Builder $q5) use ($start, $end) {
|
||||
@@ -663,6 +724,42 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
return $newBudget;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @return BudgetLimit
|
||||
*/
|
||||
public function storeBudgetLimit(array $data): BudgetLimit
|
||||
{
|
||||
$this->cleanupBudgets();
|
||||
/** @var Budget $budget */
|
||||
$budget = $data['budget'];
|
||||
|
||||
// find limit with same date range.
|
||||
// if it exists, throw error.
|
||||
$limits = $budget->budgetlimits()
|
||||
->where('budget_limits.start_date', $data['start_date']->format('Y-m-d 00:00:00'))
|
||||
->where('budget_limits.end_date', $data['end_date']->format('Y-m-d 00:00:00'))
|
||||
->get(['budget_limits.*'])->count();
|
||||
Log::debug(sprintf('Found %d budget limits.', $limits));
|
||||
if ($limits > 0) {
|
||||
throw new FireflyException('A budget limit for this budget, and this date range already exists. You must update the existing one.');
|
||||
}
|
||||
|
||||
Log::debug('No existing budget limit, create a new one');
|
||||
// or create one and return it.
|
||||
$limit = new BudgetLimit;
|
||||
$limit->budget()->associate($budget);
|
||||
$limit->start_date = $data['start_date']->format('Y-m-d 00:00:00');
|
||||
$limit->end_date = $data['end_date']->format('Y-m-d 00:00:00');
|
||||
$limit->amount = $data['amount'];
|
||||
$limit->save();
|
||||
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount']));
|
||||
|
||||
return $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param array $data
|
||||
@@ -708,6 +805,29 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BudgetLimit $budgetLimit
|
||||
* @param array $data
|
||||
*
|
||||
* @return BudgetLimit
|
||||
* @throws Exception
|
||||
*/
|
||||
public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): BudgetLimit
|
||||
{
|
||||
$this->cleanupBudgets();
|
||||
/** @var Budget $budget */
|
||||
$budget = $data['budget'];
|
||||
|
||||
$budgetLimit->budget()->associate($budget);
|
||||
$budgetLimit->start_date = $data['start_date']->format('Y-m-d 00:00:00');
|
||||
$budgetLimit->end_date = $data['end_date']->format('Y-m-d 00:00:00');
|
||||
$budgetLimit->amount = $data['amount'];
|
||||
$budgetLimit->save();
|
||||
Log::debug(sprintf('Updated budget limit with ID #%d and amount %s', $budgetLimit->id, $data['amount']));
|
||||
|
||||
return $budgetLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
|
Reference in New Issue
Block a user