mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-01-26 16:26:35 -06:00
Can now automatically handle some rules. No user interface, yet.
This commit is contained in:
parent
ce250c85fc
commit
46ee2a0568
@ -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;
|
||||
}
|
||||
}
|
@ -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 */
|
||||
|
33
app/Rules/Actions/ActionInterface.php
Normal file
33
app/Rules/Actions/ActionInterface.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* ActionInterface.php
|
||||
* Copyright (C) 2016 Sander Dorigo
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Rules\Actions;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
|
||||
/**
|
||||
* Interface ActionInterface
|
||||
*
|
||||
* @package FireflyIII\Rules\Action
|
||||
*/
|
||||
interface ActionInterface
|
||||
{
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
* @param TransactionJournal $journal
|
||||
*/
|
||||
public function __construct(RuleAction $action, TransactionJournal $journal);
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function act();
|
||||
}
|
64
app/Rules/Actions/SetBudget.php
Normal file
64
app/Rules/Actions/SetBudget.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* SetBudget.php
|
||||
* Copyright (C) 2016 Sander Dorigo
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Rules\Actions;
|
||||
|
||||
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use Log;
|
||||
/**
|
||||
* Class SetBudget
|
||||
*
|
||||
* @package FireflyIII\Rules\Action
|
||||
*/
|
||||
class SetBudget implements ActionInterface
|
||||
{
|
||||
|
||||
private $action;
|
||||
private $journal;
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
* @param TransactionJournal $journal
|
||||
*/
|
||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
54
app/Rules/Actions/SetCategory.php
Normal file
54
app/Rules/Actions/SetCategory.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* SetCategory.php
|
||||
* Copyright (C) 2016 Sander Dorigo
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Rules\Actions;
|
||||
|
||||
|
||||
use Auth;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class SetCategory
|
||||
*
|
||||
* @package FireflyIII\Rules\Action
|
||||
*/
|
||||
class SetCategory implements ActionInterface
|
||||
{
|
||||
|
||||
private $action;
|
||||
private $journal;
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
* @param TransactionJournal $journal
|
||||
*/
|
||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* TriggerProcessor.php
|
||||
* Processor.php
|
||||
* Copyright (C) 2016 Sander Dorigo
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
@ -10,18 +10,20 @@
|
||||
namespace FireflyIII\Rules;
|
||||
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\RuleTrigger;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Rules\Actions\ActionInterface;
|
||||
use FireflyIII\Rules\Triggers\TriggerInterface;
|
||||
use FireflyIII\Support\Domain;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class TriggerProcessor
|
||||
* Class Processor
|
||||
*
|
||||
* @package FireflyIII\Rules
|
||||
*/
|
||||
class TriggerProcessor
|
||||
class Processor
|
||||
{
|
||||
/** @var Rule */
|
||||
protected $rule;
|
||||
@ -29,16 +31,21 @@ class TriggerProcessor
|
||||
/** @var TransactionJournal */
|
||||
protected $journal;
|
||||
|
||||
/** @var array */
|
||||
private $triggerTypes = [];
|
||||
|
||||
/** @var array */
|
||||
private $actionTypes = [];
|
||||
|
||||
/**
|
||||
* TriggerProcessor constructor.
|
||||
* Processor constructor.
|
||||
*/
|
||||
public function __construct(Rule $rule, TransactionJournal $journal)
|
||||
{
|
||||
$this->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
|
||||
*/
|
@ -34,4 +34,12 @@ class Domain
|
||||
{
|
||||
return Config::get('firefly.rule-triggers');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getRuleActions()
|
||||
{
|
||||
return Config::get('firefly.rule-actions');
|
||||
}
|
||||
}
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user