mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Remove TODO's, add some suppressors for code quality.
This commit is contained in:
@@ -34,7 +34,6 @@ use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\RuleTrigger;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
@@ -42,10 +41,13 @@ use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Navigation;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Class BudgetRepository.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
||||
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
|
||||
*/
|
||||
class BudgetRepository implements BudgetRepositoryInterface
|
||||
{
|
||||
@@ -139,8 +141,8 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
foreach ($budgets as $budget) {
|
||||
$budgetId = $budget->id;
|
||||
$return[$budgetId] = [
|
||||
'spent' => $this->spentInPeriod(new Collection([$budget]), $accounts, $start, $end),
|
||||
'budgeted' => '0',
|
||||
'spent' => $this->spentInPeriod(new Collection([$budget]), $accounts, $start, $end),
|
||||
'budgeted' => '0',
|
||||
];
|
||||
$budgetLimits = $this->getBudgetLimits($budget, $start, $end);
|
||||
$otherLimits = new Collection;
|
||||
@@ -246,7 +248,10 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
* Will cache result.
|
||||
*
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return Carbon
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function firstUseDate(Budget $budget): ?Carbon
|
||||
{
|
||||
@@ -290,6 +295,8 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function getAllBudgetLimits(Carbon $start = null, Carbon $end = null): Collection
|
||||
{
|
||||
@@ -421,6 +428,8 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection
|
||||
{
|
||||
@@ -763,47 +772,12 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
*/
|
||||
public function update(Budget $budget, array $data): Budget
|
||||
{
|
||||
$oldName = $budget->name;
|
||||
// update the budget:
|
||||
$oldName = $budget->name;
|
||||
$budget->name = $data['name'];
|
||||
$budget->active = $data['active'];
|
||||
$budget->save();
|
||||
|
||||
// find any rule triggers related to budgets, with this budget name, and update them accordingly.
|
||||
$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 = $data['name'];
|
||||
$trigger->save();
|
||||
Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value));
|
||||
}
|
||||
|
||||
|
||||
// find any rule actions related to budgets, with this budget name, and update them accordingly.
|
||||
$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 = $data['name'];
|
||||
$action->save();
|
||||
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
|
||||
}
|
||||
|
||||
$this->updateRuleTriggers($oldName, $data['name']);
|
||||
$this->updateRuleActions($oldName, $data['name']);
|
||||
app('preferences')->mark();
|
||||
|
||||
return $budget;
|
||||
@@ -869,6 +843,8 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
* @param string $amount
|
||||
*
|
||||
* @return BudgetLimit|null
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit
|
||||
{
|
||||
@@ -879,6 +855,7 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
||||
->get(['budget_limits.*'])->count();
|
||||
Log::debug(sprintf('Found %d budget limits.', $limits));
|
||||
|
||||
// there might be a budget limit for these dates:
|
||||
/** @var BudgetLimit $limit */
|
||||
$limit = $budget->budgetlimits()
|
||||
@@ -929,4 +906,46 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user