Some notes for future releases. #2443

This commit is contained in:
James Cole
2019-08-22 18:04:26 +02:00
parent 4b1d66bcf6
commit fdb2abdf92

View File

@@ -96,6 +96,40 @@ class BudgetRepository implements BudgetRepositoryInterface
return $avg; return $avg;
} }
/**
* @return bool
* // it's 5.
*/
public function cleanupBudgets(): bool
{
// delete limits with amount 0:
try {
BudgetLimit::where('amount', 0)->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage()));
}
Budget::where('order', 0)->update(['order' => 100]);
// do the clean up by hand because Sqlite can be tricky with this.
$budgetLimits = BudgetLimit::orderBy('created_at', 'DESC')->get(['id', 'budget_id', 'start_date', 'end_date']);
$count = [];
/** @var BudgetLimit $budgetLimit */
foreach ($budgetLimits as $budgetLimit) {
$key = $budgetLimit->budget_id . '-' . $budgetLimit->start_date->format('Y-m-d') . $budgetLimit->end_date->format('Y-m-d');
if (isset($count[$key])) {
// delete it!
try {
BudgetLimit::find($budgetLimit->id)->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage()));
}
}
$count[$key] = true;
}
return true;
}
/** /**
* This method collects various info on budgets, used on the budget page and on the index. * This method collects various info on budgets, used on the budget page and on the index.
* *
@@ -143,95 +177,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $return; return $return;
} }
/**
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user);
$collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation();
if ($accounts->count() > 0) {
$collector->setAccounts($accounts);
}
return $collector->getSum();
}
/**
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*
*
*/
public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection
{
if (null === $end && null === $start) {
return $budget->budgetlimits()->with(['transactionCurrency'])->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
}
if (null === $end xor null === $start) {
$query = $budget->budgetlimits()->with(['transactionCurrency'])->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) {
$q5->where(
function (Builder $q1) use ($start, $end) {
// budget limit ends within period
$q1->where(
function (Builder $q2) use ($start, $end) {
$q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00'));
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 00:00:00'));
}
)
// budget limit start within period
->orWhere(
function (Builder $q3) use ($start, $end) {
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00'));
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 00:00:00'));
}
);
}
)
->orWhere(
function (Builder $q4) use ($start, $end) {
// or start is before start AND end is after end.
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d 00:00:00'));
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d 00:00:00'));
}
);
}
)->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
return $set;
}
/** /**
* @param Budget $budget * @param Budget $budget
* *
@@ -295,22 +240,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $result; return $result;
} }
/**
* Find a budget or return NULL
*
* @param int $budgetId |null
*
* @return Budget|null
*/
public function findNull(int $budgetId = null): ?Budget
{
if (null === $budgetId) {
return null;
}
return $this->user->budgets()->find($budgetId);
}
/** /**
* Find budget by name. * Find budget by name.
* *
@@ -328,6 +257,22 @@ class BudgetRepository implements BudgetRepositoryInterface
return $this->user->budgets()->where('name', 'LIKE', $query)->first(); return $this->user->budgets()->where('name', 'LIKE', $query)->first();
} }
/**
* Find a budget or return NULL
*
* @param int $budgetId |null
*
* @return Budget|null
*/
public function findNull(int $budgetId = null): ?Budget
{
if (null === $budgetId) {
return null;
}
return $this->user->budgets()->find($budgetId);
}
/** /**
* This method returns the oldest journal or transaction date known to this budget. * This method returns the oldest journal or transaction date known to this budget.
* Will cache result. * Will cache result.
@@ -446,6 +391,22 @@ class BudgetRepository implements BudgetRepositoryInterface
return $set; return $set;
} }
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection
{
return $this->getAllBudgetLimits($start, $end)->filter(
function (BudgetLimit $budgetLimit) use ($currency) {
return $budgetLimit->transaction_currency_id === $currency->id;
}
);
}
/** /**
* @param TransactionCurrency $currency * @param TransactionCurrency $currency
* @param Carbon $start * @param Carbon $start
@@ -468,7 +429,6 @@ class BudgetRepository implements BudgetRepositoryInterface
} }
/** /**
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
@@ -489,6 +449,106 @@ class BudgetRepository implements BudgetRepositoryInterface
return $return; return $return;
} }
/**
* Returns all available budget objects.
*
* @param TransactionCurrency $currency
*
* @return Collection
*/
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection
{
return $this->user->availableBudgets()->where('transaction_currency_id', $currency->id)->get();
}
/**
* Returns all available budget objects.
*
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*
*/
public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection
{
$query = $this->user->availableBudgets();
if (null !== $start) {
$query->where('start_date', '>=', $start->format('Y-m-d H:i:s'));
}
if (null !== $end) {
$query->where('end_date', '<=', $end->format('Y-m-d H:i:s'));
}
return $query->get();
}
/**
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*
*
*/
public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection
{
if (null === $end && null === $start) {
return $budget->budgetlimits()->with(['transactionCurrency'])->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
}
if (null === $end xor null === $start) {
$query = $budget->budgetlimits()->with(['transactionCurrency'])->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) {
$q5->where(
function (Builder $q1) use ($start, $end) {
// budget limit ends within period
$q1->where(
function (Builder $q2) use ($start, $end) {
$q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00'));
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 00:00:00'));
}
)
// budget limit start within period
->orWhere(
function (Builder $q3) use ($start, $end) {
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00'));
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 00:00:00'));
}
);
}
)
->orWhere(
function (Builder $q4) use ($start, $end) {
// or start is before start AND end is after end.
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d 00:00:00'));
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d 00:00:00'));
}
);
}
)->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
return $set;
}
/** /**
* This method is being used to generate the budget overview in the year/multi-year report. Its used * This method is being used to generate the budget overview in the year/multi-year report. Its used
* in both the year/multi-year budget overview AND in the accompanying chart. * in both the year/multi-year budget overview AND in the accompanying chart.
@@ -529,6 +589,7 @@ class BudgetRepository implements BudgetRepositoryInterface
$date = $journal['date']->format($carbonFormat); $date = $journal['date']->format($carbonFormat);
$data[$budgetId]['entries'][$date] = bcadd($data[$budgetId]['entries'][$date] ?? '0', $journal['amount']); $data[$budgetId]['entries'][$date] = bcadd($data[$budgetId]['entries'][$date] ?? '0', $journal['amount']);
} }
return $data; return $data;
} }
@@ -544,8 +605,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $set; return $set;
} }
/** /**
* Get all budgets with these ID's. * Get all budgets with these ID's.
* *
@@ -570,8 +629,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $set; return $set;
} }
/** /**
* @param Collection $accounts * @param Collection $accounts
* @param Carbon $start * @param Carbon $start
@@ -670,6 +727,30 @@ class BudgetRepository implements BudgetRepositoryInterface
$this->user = $user; $this->user = $user;
} }
/**
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user);
$collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation();
if ($accounts->count() > 0) {
$collector->setAccounts($accounts);
}
return $collector->getSum();
}
/** /**
* @param Collection $budgets * @param Collection $budgets
* @param Collection $accounts * @param Collection $accounts
@@ -822,8 +903,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $newBudget; return $newBudget;
} }
/** /**
* @param array $data * @param array $data
* *
@@ -868,40 +947,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $limit; return $limit;
} }
/**
* @return bool
* // it's 5.
*/
public function cleanupBudgets(): bool
{
// delete limits with amount 0:
try {
BudgetLimit::where('amount', 0)->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage()));
}
Budget::where('order', 0)->update(['order' => 100]);
// do the clean up by hand because Sqlite can be tricky with this.
$budgetLimits = BudgetLimit::orderBy('created_at', 'DESC')->get(['id', 'budget_id', 'start_date', 'end_date']);
$count = [];
/** @var BudgetLimit $budgetLimit */
foreach ($budgetLimits as $budgetLimit) {
$key = $budgetLimit->budget_id . '-' . $budgetLimit->start_date->format('Y-m-d') . $budgetLimit->end_date->format('Y-m-d');
if (isset($count[$key])) {
// delete it!
try {
BudgetLimit::find($budgetLimit->id)->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage()));
}
}
$count[$key] = true;
}
return true;
}
/** /**
* @param Budget $budget * @param Budget $budget
* @param array $data * @param array $data
@@ -922,6 +967,8 @@ class BudgetRepository implements BudgetRepositoryInterface
} }
/** /**
* TODO only used in the API.
*
* @param AvailableBudget $availableBudget * @param AvailableBudget $availableBudget
* @param array $data * @param array $data
* *
@@ -1057,52 +1104,24 @@ class BudgetRepository implements BudgetRepositoryInterface
} }
/** /**
* Returns all available budget objects. * @param string $oldName
* * @param string $newName
* @param Carbon|null $start
* @param Carbon|null $end
* @return Collection
*
*/ */
public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection private function updateRuleActions(string $oldName, string $newName): void
{ {
$query = $this->user->availableBudgets(); $types = ['set_budget',];
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
if (null !== $start) { ->where('rules.user_id', $this->user->id)
$query->where('start_date', '>=', $start->format('Y-m-d H:i:s')); ->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*']);
Log::debug(sprintf('Found %d actions to update.', $actions->count()));
/** @var RuleAction $action */
foreach ($actions as $action) {
$action->action_value = $newName;
$action->save();
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
} }
if (null !== $end) {
$query->where('end_date', '<=', $end->format('Y-m-d H:i:s'));
}
return $query->get();
}
/**
* Returns all available budget objects.
*
* @param TransactionCurrency $currency
* @return Collection
*/
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection
{
return $this->user->availableBudgets()->where('transaction_currency_id', $currency->id)->get();
}
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection
{
return $this->getAllBudgetLimits($start, $end)->filter(
function (BudgetLimit $budgetLimit) use ($currency) {
return $budgetLimit->transaction_currency_id === $currency->id;
}
);
} }
/** /**
@@ -1125,25 +1144,4 @@ class BudgetRepository implements BudgetRepositoryInterface
Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value)); Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value));
} }
} }
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleActions(string $oldName, string $newName): void
{
$types = ['set_budget',];
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*']);
Log::debug(sprintf('Found %d actions to update.', $actions->count()));
/** @var RuleAction $action */
foreach ($actions as $action) {
$action->action_value = $newName;
$action->save();
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
}
}
} }