Skip to content

Firefly III: a personal finances manager

Code for #608

James Colef0dab5bdb94c
diff --git a/app/Rules/Triggers/HasAttachment.php b/app/Rules/Triggers/HasAttachment.php
new file mode 100644
index 0000000000..4fc3bd720f
--- /dev/null
+++ b/app/Rules/Triggers/HasAttachment.php
@@ -0,0 +1,61 @@
+<?php
+/**
+ * HasAttachment.php
+ * Copyright (c) 2017 [email protected]
+ * This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * See the LICENSE file for details.
+ */
+
+declare(strict_types=1);
+
+namespace FireflyIII\Rules\Triggers;
+
+
+use FireflyIII\Models\TransactionJournal;
+
+class HasAttachment extends AbstractTrigger implements TriggerInterface
+{
+
+ /**
+ * A trigger is said to "match anything", or match any given transaction,
+ * when the trigger value is very vague or has no restrictions. Easy examples
+ * are the "AmountMore"-trigger combined with an amount of 0: any given transaction
+ * has an amount of more than zero! Other examples are all the "Description"-triggers
+ * which have hard time handling empty trigger values such as "" or "*" (wild cards).
+ *
+ * If the user tries to create such a trigger, this method MUST return true so Firefly III
+ * can stop the storing / updating the trigger. If the trigger is in any way restrictive
+ * (even if it will still include 99.9% of the users transactions), this method MUST return
+ * false.
+ *
+ * @param null $value
+ *
+ * @return bool
+ */
+ public static function willMatchEverything($value = null)
+ {
+ $value = intval($value);
+ if ($value < 0) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * @param TransactionJournal $journal
+ *
+ * @return bool
+ */
+ public function triggered(TransactionJournal $journal): bool
+ {
+ $minimum = intval($this->triggerValue);
+ $attachments = $journal->attachments()->count();
+ if ($attachments >= $minimum) {
+ return true;
+ }
+
+ return false;
+ }
+}
\ No newline at end of file
diff --git a/config/firefly.php b/config/firefly.php
index 33c0eee07e..bcdbe94c02 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -176,6 +176,7 @@ return [
'category_is' => 'FireflyIII\Rules\Triggers\CategoryIs',
'budget_is' => 'FireflyIII\Rules\Triggers\BudgetIs',
'tag_is' => 'FireflyIII\Rules\Triggers\TagIs',
+ 'has_attachments' => 'FireflyIII\Rules\Triggers\HasAttachment',
],
'rule-actions' => [
'set_category' => 'FireflyIII\Rules\Actions\SetCategory',
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 8ea6c261f0..2190dcdc92 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -287,6 +287,8 @@ return [
'rule_trigger_category_is_choice' => 'Category is..',
'rule_trigger_budget_is_choice' => 'Budget is..',
'rule_trigger_tag_is_choice' => '(A) tag is..',
+ 'rule_trigger_has_attachments_choice' => 'Has at least this many attachments',
+ 'rule_trigger_has_attachments' => 'Has at least :trigger_value attachment(s)',
'rule_trigger_store_journal' => 'When a transaction is created',
'rule_trigger_update_journal' => 'When a transaction is updated',
'rule_action_set_category' => 'Set category to ":action_value"',