mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Bug fix in budget search
This commit is contained in:
parent
6e9128d894
commit
695244c928
@ -269,6 +269,10 @@ class TransactionController extends Controller
|
|||||||
{
|
{
|
||||||
$data = $request->getAll();
|
$data = $request->getAll();
|
||||||
$data['user'] = auth()->user()->id;
|
$data['user'] = auth()->user()->id;
|
||||||
|
|
||||||
|
Log::channel('audit')
|
||||||
|
->info('Store new transaction over API.', $data);
|
||||||
|
|
||||||
$transactionGroup = $this->groupRepository->store($data);
|
$transactionGroup = $this->groupRepository->store($data);
|
||||||
|
|
||||||
event(new StoredTransactionGroup($transactionGroup));
|
event(new StoredTransactionGroup($transactionGroup));
|
||||||
|
@ -23,7 +23,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Factory;
|
namespace FireflyIII\Factory;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\TransactionGroup;
|
use FireflyIII\Models\TransactionGroup;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
@ -60,6 +59,7 @@ class TransactionGroupFactory
|
|||||||
$this->journalFactory->setUser($this->user);
|
$this->journalFactory->setUser($this->user);
|
||||||
$collection = $this->journalFactory->create($data);
|
$collection = $this->journalFactory->create($data);
|
||||||
$title = $data['group_title'] ?? null;
|
$title = $data['group_title'] ?? null;
|
||||||
|
$title = $title === '' ? null : $title;
|
||||||
/** @var TransactionJournal $first */
|
/** @var TransactionJournal $first */
|
||||||
$first = $collection->first();
|
$first = $collection->first();
|
||||||
$group = new TransactionGroup;
|
$group = new TransactionGroup;
|
||||||
|
@ -98,40 +98,6 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $avg;
|
return $avg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // 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.
|
||||||
*
|
*
|
||||||
@ -179,6 +145,98 @@ 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 TransactionCollectorInterface $collector */
|
||||||
|
$collector = app(TransactionCollectorInterface::class);
|
||||||
|
$collector->setUser($this->user);
|
||||||
|
$collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation();
|
||||||
|
|
||||||
|
if ($accounts->count() > 0) {
|
||||||
|
$collector->setAccounts($accounts);
|
||||||
|
}
|
||||||
|
if (0 === $accounts->count()) {
|
||||||
|
$collector->setAllAssetAccounts();
|
||||||
|
}
|
||||||
|
|
||||||
|
$set = $collector->getTransactions();
|
||||||
|
|
||||||
|
return (string)$set->sum('transaction_amount');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Budget $budget
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||||
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||||
|
*/
|
||||||
|
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) {
|
||||||
|
$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
|
||||||
*
|
*
|
||||||
@ -230,7 +288,7 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
Log::debug('Now in findBudget()');
|
Log::debug('Now in findBudget()');
|
||||||
Log::debug(sprintf('Searching for budget with ID #%d...', $budgetId));
|
Log::debug(sprintf('Searching for budget with ID #%d...', $budgetId));
|
||||||
$result = $this->findNull((int)$budgetId);
|
$result = $this->findNull((int)$budgetId);
|
||||||
if (null === $result) {
|
if (null === $result && null !== $budgetName && '' !== $budgetName) {
|
||||||
Log::debug(sprintf('Searching for budget with name %s...', $budgetName));
|
Log::debug(sprintf('Searching for budget with name %s...', $budgetName));
|
||||||
$result = $this->findByName((string)$budgetName);
|
$result = $this->findByName((string)$budgetName);
|
||||||
}
|
}
|
||||||
@ -242,6 +300,22 @@ 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.
|
||||||
*
|
*
|
||||||
@ -259,22 +333,6 @@ 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.
|
||||||
@ -414,6 +472,8 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $amount;
|
return $amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
@ -434,8 +494,6 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all available budget objects.
|
* Returns all available budget objects.
|
||||||
*
|
*
|
||||||
@ -476,71 +534,6 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return bcdiv($total, (string)$days);
|
return bcdiv($total, (string)$days);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Budget $budget
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return Collection
|
|
||||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
|
||||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
||||||
*/
|
|
||||||
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) {
|
|
||||||
$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.
|
||||||
@ -715,33 +708,6 @@ 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 TransactionCollectorInterface $collector */
|
|
||||||
$collector = app(TransactionCollectorInterface::class);
|
|
||||||
$collector->setUser($this->user);
|
|
||||||
$collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation();
|
|
||||||
|
|
||||||
if ($accounts->count() > 0) {
|
|
||||||
$collector->setAccounts($accounts);
|
|
||||||
}
|
|
||||||
if (0 === $accounts->count()) {
|
|
||||||
$collector->setAllAssetAccounts();
|
|
||||||
}
|
|
||||||
|
|
||||||
$set = $collector->getTransactions();
|
|
||||||
|
|
||||||
return (string)$set->sum('transaction_amount');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $budgets
|
* @param Collection $budgets
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
@ -885,8 +851,6 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
*
|
||||||
@ -905,6 +869,8 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $newBudget;
|
return $newBudget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
*
|
||||||
@ -949,6 +915,40 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $limit;
|
return $limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // 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
|
||||||
@ -968,6 +968,48 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $budget;
|
return $budget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $oldName
|
||||||
|
* @param string $newName
|
||||||
|
*/
|
||||||
|
private function updateRuleTriggers(string $oldName, string $newName): void
|
||||||
|
{
|
||||||
|
$types = ['budget_is',];
|
||||||
|
$triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id')
|
||||||
|
->where('rules.user_id', $this->user->id)
|
||||||
|
->whereIn('rule_triggers.trigger_type', $types)
|
||||||
|
->where('rule_triggers.trigger_value', $oldName)
|
||||||
|
->get(['rule_triggers.*']);
|
||||||
|
Log::debug(sprintf('Found %d triggers to update.', $triggers->count()));
|
||||||
|
/** @var RuleTrigger $trigger */
|
||||||
|
foreach ($triggers as $trigger) {
|
||||||
|
$trigger->trigger_value = $newName;
|
||||||
|
$trigger->save();
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param AvailableBudget $availableBudget
|
* @param AvailableBudget $availableBudget
|
||||||
* @param array $data
|
* @param array $data
|
||||||
@ -1102,46 +1144,4 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
|
|
||||||
return $limit;
|
return $limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $oldName
|
|
||||||
* @param string $newName
|
|
||||||
*/
|
|
||||||
private function updateRuleTriggers(string $oldName, string $newName): void
|
|
||||||
{
|
|
||||||
$types = ['budget_is',];
|
|
||||||
$triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id')
|
|
||||||
->where('rules.user_id', $this->user->id)
|
|
||||||
->whereIn('rule_triggers.trigger_type', $types)
|
|
||||||
->where('rule_triggers.trigger_value', $oldName)
|
|
||||||
->get(['rule_triggers.*']);
|
|
||||||
Log::debug(sprintf('Found %d triggers to update.', $triggers->count()));
|
|
||||||
/** @var RuleTrigger $trigger */
|
|
||||||
foreach ($triggers as $trigger) {
|
|
||||||
$trigger->trigger_value = $newName;
|
|
||||||
$trigger->save();
|
|
||||||
Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user