From a7a00ecf409f77cf0eff1ca4e7806b2dc39ceec1 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 13 Jan 2016 14:02:22 +0100 Subject: [PATCH] More rule possibilities --- app/Handlers/Events/FireRulesForStore.php | 6 +- .../Controllers/TransactionController.php | 2 + app/Models/TransactionJournal.php | 2 +- app/Rules/Processor.php | 7 +- app/Rules/Triggers/FromAccountEnds.php | 74 +++++++++++++++++++ app/Rules/Triggers/FromAccountStarts.php | 62 ++++++++++++++++ config/firefly.php | 4 +- 7 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 app/Rules/Triggers/FromAccountEnds.php create mode 100644 app/Rules/Triggers/FromAccountStarts.php diff --git a/app/Handlers/Events/FireRulesForStore.php b/app/Handlers/Events/FireRulesForStore.php index 7c1723004c..37506ca9dd 100644 --- a/app/Handlers/Events/FireRulesForStore.php +++ b/app/Handlers/Events/FireRulesForStore.php @@ -45,6 +45,7 @@ class FireRulesForStore */ public function handle(TransactionJournalStored $event) { + Log::debug('Before event (in handle). From account name is: ' . $event->journal->source_account->name); // get all the user's rule groups, with the rules, order by 'order'. /** @var User $user */ $user = Auth::user(); @@ -52,6 +53,7 @@ class FireRulesForStore // /** @var RuleGroup $group */ foreach ($groups as $group) { + Log::debug('Now processing group "' . $group->title . '".'); $rules = $group->rules() ->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id') ->where('rule_triggers.trigger_type', 'user_action') @@ -59,7 +61,7 @@ class FireRulesForStore ->get(['rules.*']); /** @var Rule $rule */ foreach ($rules as $rule) { - Log::debug('Now handling rule #' . $rule->id); + Log::debug('Now handling rule #' . $rule->id . ' (' . $rule->title. ')'); $processor = new Processor($rule, $event->journal); // get some return out of this? @@ -67,5 +69,7 @@ class FireRulesForStore } } +// echo 'Done processing rules. See log.'; +// exit; } } \ No newline at end of file diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index d760b9dbe6..a5009e0cca 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -18,6 +18,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use Illuminate\Support\Collection; use Input; +use Log; use Preferences; use Response; use Session; @@ -315,6 +316,7 @@ class TransactionController extends Controller if (count($att->getMessages()->get('attachments')) > 0) { Session::flash('info', $att->getMessages()->get('attachments')); } + Log::debug('Before event. From account name is: ' . $journal->source_account->name); event(new TransactionJournalStored($journal, intval($request->get('piggy_bank_id')))); diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 8ec64eceb1..d060021302 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -291,7 +291,7 @@ class TransactionJournal extends Model public function getDestinationAccountAttribute() { $account = $this->transactions()->where('amount', '>', 0)->first()->account; - + return $account; } diff --git a/app/Rules/Processor.php b/app/Rules/Processor.php index 310c73d898..9ef84fa551 100644 --- a/app/Rules/Processor.php +++ b/app/Rules/Processor.php @@ -71,7 +71,12 @@ class Processor /** @var RuleTrigger $trigger */ foreach ($this->rule->ruleTriggers()->orderBy('order', 'ASC')->get() as $index => $trigger) { $foundTriggers++; - $type = $trigger->trigger_type; + $type = $trigger->trigger_type; + + if (!isset($this->triggerTypes[$type])) { + abort(500, 'No such trigger exists ("' . $type . '").'); + } + $class = $this->triggerTypes[$type]; Log::debug('Trigger #' . $trigger->id . ' for rule #' . $trigger->rule_id . ' (' . $type . ')'); if (!class_exists($class)) { diff --git a/app/Rules/Triggers/FromAccountEnds.php b/app/Rules/Triggers/FromAccountEnds.php new file mode 100644 index 0000000000..b207ef6ddd --- /dev/null +++ b/app/Rules/Triggers/FromAccountEnds.php @@ -0,0 +1,74 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $fromAccountName = strtolower($this->journal->source_account->name); + $fromAccountNameLength = strlen($fromAccountName); + $search = strtolower($this->trigger->trigger_value); + $searchLength = strlen($search); + + // if the string to search for is longer than the account name, + // shorten the search string. + if ($searchLength > $fromAccountNameLength) { + Log::debug('Search string "' . $search . '" (' . $searchLength . ') is longer than "' . $fromAccountName . '" (' . $fromAccountNameLength . '). '); + $search = substr($search, ($fromAccountNameLength * -1)); + $searchLength = strlen($search); + Log::debug('Search string is now "' . $search . '" (' . $searchLength . ') instead.'); + } + + + $part = substr($fromAccountName, $searchLength * -1); + + if ($part == $search) { + Log::debug('"' . $fromAccountName . '" ends with "' . $search . '". Return true.'); + + return true; + } + Log::debug('"' . $fromAccountName . '" does not end with "' . $search . '". Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/FromAccountStarts.php b/app/Rules/Triggers/FromAccountStarts.php new file mode 100644 index 0000000000..a5206026ea --- /dev/null +++ b/app/Rules/Triggers/FromAccountStarts.php @@ -0,0 +1,62 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $fromAccountName = strtolower($this->journal->source_account->name); + $search = strtolower($this->trigger->trigger_value); + + $part = substr($fromAccountName, 0, strlen($search)); + + if ($part == $search) { + Log::debug('"' . $fromAccountName . '" starts with "' . $search . '". Return true.'); + + return true; + } + Log::debug('"' . $fromAccountName . '" does not start with "' . $search . '". Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/config/firefly.php b/config/firefly.php index 469a6203b7..bd323ebfc0 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -170,8 +170,8 @@ return [ 'rule-triggers' => [ 'user_action' => 'FireflyIII\Rules\Triggers\UserAction', - 'from_account_starts' => 'FireflyIII\Rules\Triggers', - 'from_account_ends' => 'FireflyIII\Rules\Triggers', + 'from_account_starts' => 'FireflyIII\Rules\Triggers\FromAccountStarts', + 'from_account_ends' => 'FireflyIII\Rules\Triggers\FromAccountEnds', 'from_account_is' => 'FireflyIII\Rules\Triggers\FromAccountIs', 'from_account_contains' => 'FireflyIII\Rules\Triggers', 'to_account_starts' => 'FireflyIII\Rules\Triggers',