More rule possibilities

This commit is contained in:
James Cole 2016-01-13 14:02:22 +01:00
parent 88bbafd3e8
commit a7a00ecf40
7 changed files with 152 additions and 5 deletions

View File

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

View File

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

View File

@ -72,6 +72,11 @@ class Processor
foreach ($this->rule->ruleTriggers()->orderBy('order', 'ASC')->get() as $index => $trigger) {
$foundTriggers++;
$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)) {

View File

@ -0,0 +1,74 @@
<?php
/**
* FromAccountEnds.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\Triggers;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class FromAccountEnds
*
* @package FireflyIII\Rules\Triggers
*/
class FromAccountEnds implements TriggerInterface
{
/** @var RuleTrigger */
protected $trigger;
/** @var TransactionJournal */
protected $journal;
/**
* TriggerInterface constructor.
*
* @param RuleTrigger $trigger
* @param TransactionJournal $journal
*/
public function __construct(RuleTrigger $trigger, TransactionJournal $journal)
{
$this->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;
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* FromAccountStarts.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\Triggers;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class FromAccountStarts
*
* @package FireflyIII\Rules\Triggers
*/
class FromAccountStarts implements TriggerInterface
{
/** @var RuleTrigger */
protected $trigger;
/** @var TransactionJournal */
protected $journal;
/**
* TriggerInterface constructor.
*
* @param RuleTrigger $trigger
* @param TransactionJournal $journal
*/
public function __construct(RuleTrigger $trigger, TransactionJournal $journal)
{
$this->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;
}
}

View File

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