Skip to content

Firefly III: a personal finances manager

Clean up triggers, add some new ones.

James Cole98b272383f3e
diff --git a/app/Handlers/Events/ConnectJournalToPiggyBank.php b/app/Handlers/Events/ConnectJournalToPiggyBank.php
index 495d112cae..0b6c96da8a 100644
--- a/app/Handlers/Events/ConnectJournalToPiggyBank.php
+++ b/app/Handlers/Events/ConnectJournalToPiggyBank.php
@@ -42,12 +42,12 @@ class ConnectJournalToPiggyBank
$piggyBank = Auth::user()->piggybanks()->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']);
if (is_null($piggyBank)) {
- return false;
+ return true;
}
// update piggy bank rep for date of transaction journal.
$repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first();
if (is_null($repetition)) {
- return false;
+ return true;
}
bcscale(2);
diff --git a/app/Handlers/Events/FireRulesForStore.php b/app/Handlers/Events/FireRulesForStore.php
index b68c61157b..1220f4ed10 100644
--- a/app/Handlers/Events/FireRulesForStore.php
+++ b/app/Handlers/Events/FireRulesForStore.php
@@ -11,7 +11,9 @@ namespace FireflyIII\Handlers\Events;
use FireflyIII\Events\TransactionJournalStored;
+use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
+use FireflyIII\Rules\Processor;
use FireflyIII\User;
use Illuminate\Support\Facades\Auth;
use Log;
@@ -44,15 +46,24 @@ class FireRulesForStore
public function handle(TransactionJournalStored $event)
{
// get all the user's rule groups, with the rules, order by 'order'.
-// /** @var User $user */
-// $user = Auth::user();
-//
-// $groups = $user->ruleGroups()->with('rules')->orderBy('order','ASC')->get();
-//
-// /** @var RuleGroup $group */
-// foreach($groups as $group) {
-//
-// }
+ /** @var User $user */
+ $user = Auth::user();
+ $groups = $user->ruleGroups()->with('rules')->hasTrigger('user_action','store-journal')->orderBy('order', 'ASC')->get();
+ //
+ /** @var RuleGroup $group */
+ foreach ($groups as $group) {
+ $rules = $group->rules;
+ /** @var Rule $rule */
+ foreach ($rules as $rule) {
+ $processor = new Processor($rule, $event->journal);
+
+ // get some return out of this?
+ $processor->handle();
+
+ }
+ }
Log::debug('FireRulesForStore!');
+ echo 'handle';
+ exit;
}
}
\ No newline at end of file
diff --git a/app/Handlers/Events/ScanForBillsAfterStore.php b/app/Handlers/Events/ScanForBillsAfterStore.php
new file mode 100644
index 0000000000..26f807fac1
--- /dev/null
+++ b/app/Handlers/Events/ScanForBillsAfterStore.php
@@ -0,0 +1,61 @@
+<?php
+/**
+ * RescanJournalAfterStore.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\Handlers\Events;
+
+use FireflyIII\Events\TransactionJournalStored;
+use Log;
+
+/**
+ * Class RescanJournal
+ *
+ * @codeCoverageIgnore
+ * @package FireflyIII\Handlers\Events
+ */
+class ScanForBillsAfterStore
+{
+
+ /**
+ * Create the event handler.
+ *
+ */
+ public function __construct()
+ {
+ //
+ }
+
+ /**
+ * Scan a transaction journal for possible links to bills, right after storing.
+ *
+ * @param TransactionJournalStored $event
+ *
+ * @return void
+ */
+ public function handle(TransactionJournalStored $event)
+ {
+ $journal = $event->journal;
+
+ Log::debug('Triggered saved event for journal #' . $journal->id . ' (' . $journal->description . ')');
+
+ /** @var \FireflyIII\Repositories\Bill\BillRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
+ $list = $journal->user->bills()->where('active', 1)->where('automatch', 1)->get();
+
+ Log::debug('Found ' . $list->count() . ' bills to check.');
+
+ /** @var \FireflyIII\Models\Bill $bill */
+ foreach ($list as $bill) {
+ Log::debug('Now calling bill #' . $bill->id . ' (' . $bill->name . ')');
+ $repository->scan($bill, $journal);
+ }
+
+ Log::debug('Done!');
+ }
+
+}
diff --git a/app/Handlers/Events/ScanForBillsAfterUpdate.php b/app/Handlers/Events/ScanForBillsAfterUpdate.php
new file mode 100644
index 0000000000..424e1c0e84
--- /dev/null
+++ b/app/Handlers/Events/ScanForBillsAfterUpdate.php
@@ -0,0 +1,61 @@
+<?php
+/**
+ * ScanForBillsAfterUpdate.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\Handlers\Events;
+
+use FireflyIII\Events\TransactionJournalUpdated;
+use Log;
+
+/**
+ * Class RescanJournal
+ *
+ * @codeCoverageIgnore
+ * @package FireflyIII\Handlers\Events
+ */
+class ScanForBillsAfterUpdate
+{
+
+ /**
+ * Create the event handler.
+ *
+ */
+ public function __construct()
+ {
+ //
+ }
+
+ /**
+ * Scan a transaction journal for possibly related bills after it has been updated.
+ *
+ * @param TransactionJournalUpdated $event
+ *
+ * @return void
+ */
+ public function handle(TransactionJournalUpdated $event)
+ {
+ $journal = $event->journal;
+
+ Log::debug('Triggered saved event for journal #' . $journal->id . ' (' . $journal->description . ')');
+
+ /** @var \FireflyIII\Repositories\Bill\BillRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
+ $list = $journal->user->bills()->where('active', 1)->where('automatch', 1)->get();
+
+ Log::debug('Found ' . $list->count() . ' bills to check.');
+
+ /** @var \FireflyIII\Models\Bill $bill */
+ foreach ($list as $bill) {
+ Log::debug('Now calling bill #' . $bill->id . ' (' . $bill->name . ')');
+ $repository->scan($bill, $journal);
+ }
+
+ Log::debug('Done!');
+ }
+
+}
diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php
index e0f9bf0bf5..d760b9dbe6 100644
--- a/app/Http/Controllers/TransactionController.php
+++ b/app/Http/Controllers/TransactionController.php
@@ -316,12 +316,7 @@ class TransactionController extends Controller
Session::flash('info', $att->getMessages()->get('attachments'));
}
- // rescan journal, UpdateJournalConnection
- event(new TransactionJournalUpdated($journal));
-
- if ($journal->isTransfer() && intval($request->get('piggy_bank_id')) > 0) {
- event(new TransactionJournalStored($journal, intval($request->get('piggy_bank_id'))));
- }
+ event(new TransactionJournalStored($journal, intval($request->get('piggy_bank_id'))));
Session::flash('success', 'New transaction "' . $journal->description . '" stored!');
Preferences::mark();
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 9f623b00fd..7b86a54d04 100755
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -30,12 +30,15 @@ class EventServiceProvider extends ServiceProvider
protected $listen
= [
'FireflyIII\Events\TransactionJournalUpdated' => [
- 'FireflyIII\Handlers\Events\RescanJournal',
+ 'FireflyIII\Handlers\Events\ScanForBillsAfterUpdate',
+
'FireflyIII\Handlers\Events\UpdateJournalConnection',
'FireflyIII\Handlers\Events\FireRulesForUpdate',
],
'FireflyIII\Events\TransactionJournalStored' => [
+ 'FireflyIII\Handlers\Events\ScanForBillsAfterStore',
+
'FireflyIII\Handlers\Events\ConnectJournalToPiggyBank',
'FireflyIII\Handlers\Events\FireRulesForStore',
]
diff --git a/app/Rules/Processor.php b/app/Rules/Processor.php
new file mode 100644
index 0000000000..870fc1ecd6
--- /dev/null
+++ b/app/Rules/Processor.php
@@ -0,0 +1,105 @@
+<?php
+/**
+ * Processor.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;
+
+use Exception;
+use FireflyIII\Models\Rule;
+use FireflyIII\Models\RuleTrigger;
+use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Support\Domain;
+
+/**
+ * Class Processor
+ *
+ * @package FireflyIII\Rules
+ */
+class Processor
+{
+ /** @var Rule */
+ protected $rule;
+
+ /** @var TransactionJournal */
+ protected $journal;
+
+ private $triggerTypes = [];
+
+ /**
+ * Processor constructor.
+ */
+ public function __construct(Rule $rule, TransactionJournal $journal)
+ {
+ $this->rule = $rule;
+ $this->journal = $journal;
+ $this->triggerTypes = Domain::getRuleTriggers();
+ }
+
+ public function handle()
+ {
+ // get all triggers:
+ $triggered = $this->triggered();
+ if ($triggered) {
+
+ }
+
+ }
+
+ /**
+ * @return bool
+ */
+ protected function triggered()
+ {
+ /** @var RuleTrigger $trigger */
+ foreach ($this->rule->ruleTriggers as $trigger) {
+ $type = $trigger->trigger_type;
+ $class = $this->triggerTypes[$type];
+ if (!class_exists($class)) {
+ throw new Exception('Could not instantiate class for rule trigger type "' . $type . '".');
+ }
+ }
+
+
+ return true;
+
+ }
+
+ /**
+ * @return Rule
+ */
+ public function getRule()
+ {
+ return $this->rule;
+ }
+
+ /**
+ * @param Rule $rule
+ */
+ public function setRule($rule)
+ {
+ $this->rule = $rule;
+ }
+
+ /**
+ * @return TransactionJournal
+ */
+ public function getJournal()
+ {
+ return $this->journal;
+ }
+
+ /**
+ * @param TransactionJournal $journal
+ */
+ public function setJournal($journal)
+ {
+ $this->journal = $journal;
+ }
+
+
+}
\ No newline at end of file
diff --git a/app/Support/Domain.php b/app/Support/Domain.php
index 53b9012298..bf4616f6f3 100644
--- a/app/Support/Domain.php
+++ b/app/Support/Domain.php
@@ -26,4 +26,12 @@ class Domain
return Config::get('firefly.bindables');
}
+
+ /**
+ * @return array
+ */
+ public static function getRuleTriggers()
+ {
+ return Config::get('firefly.rule-triggers');
+ }
}
\ No newline at end of file