Clean up triggers, add some new ones.

This commit is contained in:
James Cole 2016-01-13 07:16:29 +01:00
parent e722daafd0
commit 98b272383f
8 changed files with 262 additions and 18 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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!');
}
}

View File

@ -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!');
}
}

View File

@ -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();

View File

@ -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',
]

105
app/Rules/Processor.php Normal file
View File

@ -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;
}
}

View File

@ -26,4 +26,12 @@ class Domain
return Config::get('firefly.bindables');
}
/**
* @return array
*/
public static function getRuleTriggers()
{
return Config::get('firefly.rule-triggers');
}
}