From 1270e5d15ca30a756cc648af68317558ac7f6048 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 13 Jan 2016 14:37:19 +0100 Subject: [PATCH] More valid triggers. --- app/Rules/Triggers/AmountExactly.php | 64 ++++++++++++++++++++ app/Rules/Triggers/AmountLess.php | 64 ++++++++++++++++++++ app/Rules/Triggers/AmountMore.php | 64 ++++++++++++++++++++ app/Rules/Triggers/DescriptionEnds.php | 74 ++++++++++++++++++++++++ app/Rules/Triggers/DescriptionStarts.php | 62 ++++++++++++++++++++ app/Rules/Triggers/ToAccountEnds.php | 2 +- config/firefly.php | 9 ++- 7 files changed, 333 insertions(+), 6 deletions(-) create mode 100644 app/Rules/Triggers/AmountExactly.php create mode 100644 app/Rules/Triggers/AmountLess.php create mode 100644 app/Rules/Triggers/AmountMore.php create mode 100644 app/Rules/Triggers/DescriptionEnds.php create mode 100644 app/Rules/Triggers/DescriptionStarts.php diff --git a/app/Rules/Triggers/AmountExactly.php b/app/Rules/Triggers/AmountExactly.php new file mode 100644 index 0000000000..908ad31a6f --- /dev/null +++ b/app/Rules/Triggers/AmountExactly.php @@ -0,0 +1,64 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $amount = $this->journal->amount_positive; + $compare = $this->trigger->trigger_value; + $result = bccomp($amount, $compare, 4); + if ($result === 0) { + // found something + Log::debug($amount . ' is exactly ' . $compare . '. Return true.'); + + return true; + } + + // found nothing. + Log::debug($amount . ' is not exactly ' . $compare . '. Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/AmountLess.php b/app/Rules/Triggers/AmountLess.php new file mode 100644 index 0000000000..4c659db944 --- /dev/null +++ b/app/Rules/Triggers/AmountLess.php @@ -0,0 +1,64 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $amount = $this->journal->amount_positive; + $compare = $this->trigger->trigger_value; + $result = bccomp($amount, $compare, 4); + if ($result === -1) { + // found something + Log::debug($amount . ' is less than ' . $compare . '. Return true.'); + + return true; + } + + // found nothing. + Log::debug($amount . ' is not less than ' . $compare . '. Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/AmountMore.php b/app/Rules/Triggers/AmountMore.php new file mode 100644 index 0000000000..a3470ffd13 --- /dev/null +++ b/app/Rules/Triggers/AmountMore.php @@ -0,0 +1,64 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $amount = $this->journal->amount_positive; + $compare = $this->trigger->trigger_value; + $result = bccomp($amount, $compare, 4); + if ($result === 1) { + // found something + Log::debug($amount . ' is more than ' . $compare . '. Return true.'); + + return true; + } + + // found nothing. + Log::debug($amount . ' is not more than ' . $compare . '. Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/DescriptionEnds.php b/app/Rules/Triggers/DescriptionEnds.php new file mode 100644 index 0000000000..4d7c0a10a4 --- /dev/null +++ b/app/Rules/Triggers/DescriptionEnds.php @@ -0,0 +1,74 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $description = strtolower($this->journal->description); + $descriptionLength = strlen($description); + $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 > $descriptionLength) { + Log::debug('Search string "' . $search . '" (' . $searchLength . ') is longer than "' . $description . '" (' . $descriptionLength . '). '); + $search = substr($search, ($descriptionLength * -1)); + $searchLength = strlen($search); + Log::debug('Search string is now "' . $search . '" (' . $searchLength . ') instead.'); + } + + + $part = substr($description, $searchLength * -1); + + if ($part == $search) { + Log::debug('"' . $description . '" ends with "' . $search . '". Return true.'); + + return true; + } + Log::debug('"' . $description . '" does not end with "' . $search . '". Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/DescriptionStarts.php b/app/Rules/Triggers/DescriptionStarts.php new file mode 100644 index 0000000000..8475f99fda --- /dev/null +++ b/app/Rules/Triggers/DescriptionStarts.php @@ -0,0 +1,62 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $description = strtolower($this->journal->description); + $search = strtolower($this->trigger->trigger_value); + + $part = substr($description, 0, strlen($search)); + + if ($part == $search) { + Log::debug('"' . $description . '" starts with "' . $search . '". Return true.'); + + return true; + } + Log::debug('"' . $description . '" does not start with "' . $search . '". Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/ToAccountEnds.php b/app/Rules/Triggers/ToAccountEnds.php index 67bb9750f1..7c1984ea2a 100644 --- a/app/Rules/Triggers/ToAccountEnds.php +++ b/app/Rules/Triggers/ToAccountEnds.php @@ -18,7 +18,7 @@ use Log; * * @package FireflyIII\Rules\Triggers */ -class Ends implements TriggerInterface +class ToAccountEnds implements TriggerInterface { /** @var RuleTrigger */ protected $trigger; diff --git a/config/firefly.php b/config/firefly.php index eb78cf5102..4acb99dfd8 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -179,11 +179,10 @@ return [ 'to_account_is' => 'FireflyIII\Rules\Triggers\ToAccountIs', 'to_account_contains' => 'FireflyIII\Rules\Triggers\ToAccountContains', 'transaction_type' => 'FireflyIII\Rules\Triggers\TransactionType', - 'amount_less' => 'FireflyIII\Rules\Triggers', - 'amount_exactly' => 'FireflyIII\Rules\Triggers', - 'amount_exactly_not' => 'FireflyIII\Rules\Triggers', - 'amount_more' => 'FireflyIII\Rules\Triggers', - 'description_starts' => 'FireflyIII\Rules\Triggers', + 'amount_less' => 'FireflyIII\Rules\Triggers\AmountLess', + 'amount_exactly' => 'FireflyIII\Rules\Triggers\AmountExactly', + 'amount_more' => 'FireflyIII\Rules\Triggers\AmountMore', + 'description_starts' => 'FireflyIII\Rules\Triggers\DescriptionStarts', 'description_ends' => 'FireflyIII\Rules\Triggers', 'description_contains' => 'FireflyIII\Rules\Triggers\DescriptionContains', 'description_is' => 'FireflyIII\Rules\Triggers',