Skip to content

Firefly III: a personal finances manager

Can now trigger.

James Cole1087b9f5df3d
diff --git a/app/Handlers/Events/FireRulesForStore.php b/app/Handlers/Events/FireRulesForStore.php
index 782e244bec..39435cd1a8 100644
--- a/app/Handlers/Events/FireRulesForStore.php
+++ b/app/Handlers/Events/FireRulesForStore.php
@@ -62,6 +62,7 @@ class FireRulesForStore
$rules = $group->rules;
/** @var Rule $rule */
foreach ($rules as $rule) {
+ Log::debug('Now handling rule #' . $rule->id);
$processor = new Processor($rule, $event->journal);
// get some return out of this?
diff --git a/app/Rules/Processor.php b/app/Rules/Processor.php
index ef44b53af0..0f58d9015f 100644
--- a/app/Rules/Processor.php
+++ b/app/Rules/Processor.php
@@ -9,11 +9,12 @@
namespace FireflyIII\Rules;
-use Exception;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Rules\Triggers\TriggerInterface;
use FireflyIII\Support\Domain;
+use Log;
/**
* Class Processor
@@ -45,27 +46,39 @@ class Processor
// get all triggers:
$triggered = $this->triggered();
if ($triggered) {
-
+ Log::debug('Rule #' . $this->rule->id . ' was triggered. Now process each action.');
}
}
/**
+ * TODO stop when stop_processing is present.
+ *
* @return bool
*/
protected function triggered()
{
+ $foundTriggers = 0;
+ $hitTriggers = 0;
/** @var RuleTrigger $trigger */
- foreach ($this->rule->ruleTriggers as $trigger) {
+ foreach ($this->rule->ruleTriggers()->orderBy('order', 'ASC')->get() as $index => $trigger) {
+ $foundTriggers++;
$type = $trigger->trigger_type;
$class = $this->triggerTypes[$type];
+ Log::debug('Trigger #' . $trigger->id . ' for rule #' . $trigger->rule_id . ' (' . $type . ')');
if (!class_exists($class)) {
- abort(500, 'Could not instantiate class for rule trigger type "' . $type . '" ('.$class.').');
+ abort(500, 'Could not instantiate class for rule trigger type "' . $type . '" (' . $class . ').');
+ }
+ /** @var TriggerInterface $triggerClass */
+ $triggerClass = new $class($trigger, $this->journal);
+ if ($triggerClass->triggered()) {
+ $hitTriggers++;
}
- }
+ }
+ Log::debug('Total: ' . $foundTriggers . ' found triggers. ' . $hitTriggers . ' triggers were hit.');
- return true;
+ return ($hitTriggers == $foundTriggers);
}
diff --git a/app/Rules/Triggers/DescriptionContains.php b/app/Rules/Triggers/DescriptionContains.php
new file mode 100644
index 0000000000..e55b850d15
--- /dev/null
+++ b/app/Rules/Triggers/DescriptionContains.php
@@ -0,0 +1,65 @@
+<?php
+/**
+ * DescriptionContains.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\Triggers;
+
+
+use FireflyIII\Models\RuleTrigger;
+use FireflyIII\Models\TransactionJournal;
+use Log;
+
+/**
+ * Class DescriptionContains
+ *
+ * @package FireflyIII\Rules\Triggers
+ */
+class DescriptionContains implements TriggerInterface
+{
+ /** @var RuleTrigger */
+ protected $trigger;
+
+ /** @var TransactionJournal */
+ protected $journal;
+
+
+ /**
+ * TriggerInterface constructor.
+ *
+ * @param RuleTrigger $trigger
+ * @param TransactionJournal $journal
+ */
+ public function __construct(RuleTrigger $trigger, TransactionJournal $journal)
+ {
+ $this->trigger = $trigger;
+ $this->journal = $journal;
+ }
+
+ /**
+ * @return bool
+ */
+ public function triggered()
+ {
+ $search = strtolower($this->trigger->trigger_value);
+ $source = strtolower($this->journal->description);
+
+ $strpos = strpos($source, $search);
+ if (!($strpos === false)) {
+ // found something
+ Log::debug('"' . $source . '" contains the text "' . $search . '". Return true.');
+
+ return true;
+ }
+
+ // found nothing.
+ Log::debug('"' . $source . '" does not contain the text "' . $search . '". Return false.');
+
+ return false;
+
+ }
+}
\ No newline at end of file
diff --git a/app/Rules/Triggers/FromAccountIs.php b/app/Rules/Triggers/FromAccountIs.php
new file mode 100644
index 0000000000..27de26653d
--- /dev/null
+++ b/app/Rules/Triggers/FromAccountIs.php
@@ -0,0 +1,60 @@
+<?php
+/**
+ * FromAccountIs.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\Triggers;
+
+use FireflyIII\Models\RuleTrigger;
+use FireflyIII\Models\TransactionJournal;
+use Log;
+
+/**
+ * Class FromAccountIs
+ *
+ * @package FireflyIII\Rules\Triggers
+ */
+class FromAccountIs implements TriggerInterface
+{
+ /** @var RuleTrigger */
+ protected $trigger;
+
+ /** @var TransactionJournal */
+ protected $journal;
+
+
+ /**
+ * TriggerInterface constructor.
+ *
+ * @param RuleTrigger $trigger
+ * @param TransactionJournal $journal
+ */
+ public function __construct(RuleTrigger $trigger, TransactionJournal $journal)
+ {
+ $this->trigger = $trigger;
+ $this->journal = $journal;
+ }
+
+ /**
+ * @return bool
+ */
+ public function triggered()
+ {
+ $fromAccountName = strtolower($this->journal->source_account->name);
+ $search = strtolower($this->trigger->trigger_value);
+
+ if ($fromAccountName == $search) {
+ Log::debug('"' . $fromAccountName . '" equals "' . $search . '" exactly. Return true.');
+
+ return true;
+ }
+ Log::debug('"' . $fromAccountName . '" does not equal "' . $search . '". Return false.');
+
+ return false;
+
+ }
+}
\ No newline at end of file
diff --git a/app/Rules/Triggers/TriggerInterface.php b/app/Rules/Triggers/TriggerInterface.php
new file mode 100644
index 0000000000..094ee0f234
--- /dev/null
+++ b/app/Rules/Triggers/TriggerInterface.php
@@ -0,0 +1,34 @@
+<?php
+/**
+ * TriggerInterface.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\Triggers;
+
+use FireflyIII\Models\RuleTrigger;
+use FireflyIII\Models\TransactionJournal;
+
+/**
+ * Interface TriggerInterface
+ *
+ * @package FireflyIII\Rules\Triggers
+ */
+interface TriggerInterface
+{
+ /**
+ * TriggerInterface constructor.
+ *
+ * @param RuleTrigger $trigger
+ * @param TransactionJournal $journal
+ */
+ public function __construct(RuleTrigger $trigger, TransactionJournal $journal);
+
+ /**
+ * @return bool
+ */
+ public function triggered();
+}
\ No newline at end of file
diff --git a/app/Rules/Triggers/UserAction.php b/app/Rules/Triggers/UserAction.php
new file mode 100644
index 0000000000..48c9932a33
--- /dev/null
+++ b/app/Rules/Triggers/UserAction.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * UserAction.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\Triggers;
+
+use FireflyIII\Models\RuleTrigger;
+use FireflyIII\Models\TransactionJournal;
+use Log;
+
+/**
+ * Class UserAction
+ *
+ * @package FireflyIII\Rules\Triggers
+ */
+class UserAction implements TriggerInterface
+{
+ /** @var RuleTrigger */
+ protected $trigger;
+
+ /** @var TransactionJournal */
+ protected $journal;
+
+
+ /**
+ * TriggerInterface constructor.
+ *
+ * @param RuleTrigger $trigger
+ * @param TransactionJournal $journal
+ */
+ public function __construct(RuleTrigger $trigger, TransactionJournal $journal)
+ {
+ $this->trigger = $trigger;
+ $this->journal = $journal;
+
+ }
+
+ /**
+ * This trigger is always triggered, because the rule that it is a part of has been pre-selected on this condition.
+ *
+ * @return bool
+ */
+ public function triggered()
+ {
+ Log::debug('user_action always returns true.');
+ return true;
+ }
+
+}
\ No newline at end of file
diff --git a/config/firefly.php b/config/firefly.php
index 53580cc889..e1b535fd30 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -169,10 +169,10 @@ return [
],
'rule-triggers' => [
- 'user_action' => 'FireflyIII\Rules\Triggers',
+ 'user_action' => 'FireflyIII\Rules\Triggers\UserAction',
'from_account_starts' => 'FireflyIII\Rules\Triggers',
'from_account_ends' => 'FireflyIII\Rules\Triggers',
- 'from_account_is' => 'FireflyIII\Rules\Triggers',
+ 'from_account_is' => 'FireflyIII\Rules\Triggers\FromAccountIs',
'from_account_contains' => 'FireflyIII\Rules\Triggers',
'to_account_starts' => 'FireflyIII\Rules\Triggers',
'to_account_ends' => 'FireflyIII\Rules\Triggers',
@@ -185,7 +185,7 @@ return [
'amount_more' => 'FireflyIII\Rules\Triggers',
'description_starts' => 'FireflyIII\Rules\Triggers',
'description_ends' => 'FireflyIII\Rules\Triggers',
- 'description_contains' => 'FireflyIII\Rules\Triggers',
+ 'description_contains' => 'FireflyIII\Rules\Triggers\DescriptionContains',
'description_is' => 'FireflyIII\Rules\Triggers',
],
'rule-actions' => [
diff --git a/database/seeds/TestDataSeeder.php b/database/seeds/TestDataSeeder.php
index c25c3391fd..bb6ce4c74e 100644
--- a/database/seeds/TestDataSeeder.php
+++ b/database/seeds/TestDataSeeder.php
@@ -72,7 +72,7 @@ class TestDataSeeder extends Seeder
$ruleTrigger->active = 1;
$ruleTrigger->stop_processing = 0;
$ruleTrigger->trigger_type = 'user_action';
- $ruleTrigger->trigger_value = 'create-journal';
+ $ruleTrigger->trigger_value = 'store-journal';
$ruleTrigger->save();
unset($ruleTrigger);