diff --git a/app/Handlers/Events/FireRulesForStore.php b/app/Handlers/Events/FireRulesForStore.php index 55b343aae4..dcad94713c 100644 --- a/app/Handlers/Events/FireRulesForStore.php +++ b/app/Handlers/Events/FireRulesForStore.php @@ -13,7 +13,7 @@ namespace FireflyIII\Handlers\Events; use FireflyIII\Events\TransactionJournalStored; use FireflyIII\Models\Rule; use FireflyIII\Models\RuleGroup; -use FireflyIII\Rules\TriggerProcessor; +use FireflyIII\Rules\Processor; use FireflyIII\User; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Facades\Auth; @@ -63,15 +63,12 @@ class FireRulesForStore /** @var Rule $rule */ foreach ($rules as $rule) { Log::debug('Now handling rule #' . $rule->id); - $processor = new TriggerProcessor($rule, $event->journal); + $processor = new Processor($rule, $event->journal); // get some return out of this? $processor->handle(); } } - Log::debug('FireRulesForStore!'); - echo 'done handling rules.'; - exit; } } \ No newline at end of file diff --git a/app/Models/Category.php b/app/Models/Category.php index cca73fe416..b331b5960a 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -42,7 +42,6 @@ class Category extends Model unset($search['name']); foreach ($search as $name => $value) { $query->where($name, $value); - } $set = $query->get(['categories.*']); /** @var Category $category */ diff --git a/app/Rules/Actions/ActionInterface.php b/app/Rules/Actions/ActionInterface.php new file mode 100644 index 0000000000..16a597b7e1 --- /dev/null +++ b/app/Rules/Actions/ActionInterface.php @@ -0,0 +1,33 @@ +action = $action; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function act() + { + /** @var BudgetRepositoryInterface $repository */ + $repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $search = $this->action->action_value; + $budgets = $repository->getActiveBudgets(); + $budget = $budgets->filter( + function (Budget $current) use ($search) { + return $current->name == $search; + } + )->first(); + if (!is_null($budget)) { + Log::debug('Will set budget "' . $search . '" (#' . $budget->id . ') on journal #' . $this->journal->id . '.'); + $this->journal->budgets()->save($budget); + } else { + Log::debug('Could not find budget "'.$search.'". Failed.'); + } + + return true; + } +} \ No newline at end of file diff --git a/app/Rules/Actions/SetCategory.php b/app/Rules/Actions/SetCategory.php new file mode 100644 index 0000000000..764bfdc4d6 --- /dev/null +++ b/app/Rules/Actions/SetCategory.php @@ -0,0 +1,54 @@ +action = $action; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function act() + { + $name = $this->action->action_value; + $category = Category::firstOrCreateEncrypted(['name' => $name, 'user_id' => Auth::user()->id]); + Log::debug('Will set category "' . $name . '" (#' . $category->id . ') on journal #' . $this->journal->id . '.'); + $this->journal->categories()->save($category); + + return true; + } +} \ No newline at end of file diff --git a/app/Rules/TriggerProcessor.php b/app/Rules/Processor.php similarity index 70% rename from app/Rules/TriggerProcessor.php rename to app/Rules/Processor.php index 216f05b178..310c73d898 100644 --- a/app/Rules/TriggerProcessor.php +++ b/app/Rules/Processor.php @@ -1,6 +1,6 @@ rule = $rule; $this->journal = $journal; $this->triggerTypes = Domain::getRuleTriggers(); + $this->actionTypes = Domain::getRuleActions(); } public function handle() @@ -47,6 +54,7 @@ class TriggerProcessor $triggered = $this->triggered(); if ($triggered) { Log::debug('Rule #' . $this->rule->id . ' was triggered. Now process each action.'); + $this->actions(); } } @@ -82,6 +90,31 @@ class TriggerProcessor } + /** + * @return bool + */ + protected function actions() + { + /** + * @var int $index + * @var RuleAction $action + */ + foreach ($this->rule->ruleActions()->orderBy('order', 'ASC')->get() as $index => $action) { + $type = $action->action_type; + $class = $this->actionTypes[$type]; + Log::debug('Action #' . $action->id . ' for rule #' . $action->rule_id . ' (' . $type . ')'); + if (!class_exists($class)) { + abort(500, 'Could not instantiate class for rule action type "' . $type . '" (' . $class . ').'); + } + /** @var ActionInterface $actionClass */ + $actionClass = new $class($action, $this->journal); + $actionClass->act(); + + } + + return true; + } + /** * @return Rule */ diff --git a/app/Support/Domain.php b/app/Support/Domain.php index bf4616f6f3..d1f8be1fed 100644 --- a/app/Support/Domain.php +++ b/app/Support/Domain.php @@ -34,4 +34,12 @@ class Domain { return Config::get('firefly.rule-triggers'); } + + /** + * @return array + */ + public static function getRuleActions() + { + return Config::get('firefly.rule-actions'); + } } \ No newline at end of file diff --git a/config/firefly.php b/config/firefly.php index e1b535fd30..469a6203b7 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -189,9 +189,9 @@ return [ 'description_is' => 'FireflyIII\Rules\Triggers', ], 'rule-actions' => [ - 'set_category' => 'FireflyIII\Rules\Actions', + 'set_category' => 'FireflyIII\Rules\Actions\SetCategory', 'clear_category' => 'FireflyIII\Rules\Actions', - 'set_budget' => 'FireflyIII\Rules\Actions', + 'set_budget' => 'FireflyIII\Rules\Actions\SetBudget', 'clear_budget' => 'FireflyIII\Rules\Actions', 'add_tag' => 'FireflyIII\Rules\Actions', 'remove_tag' => 'FireflyIII\Rules\Actions',