Grab rules properly.

This commit is contained in:
James Cole 2016-01-13 09:08:13 +01:00
parent 46ee2a0568
commit cf4d7cfeef
2 changed files with 6 additions and 28 deletions

View File

@ -15,7 +15,6 @@ use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Rules\Processor;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Auth;
use Log;
@ -49,17 +48,15 @@ class FireRulesForStore
// get all the user's rule groups, with the rules, order by 'order'.
/** @var User $user */
$user = Auth::user();
$groups = $user->ruleGroups()->with(
[
'rules' => function (HasMany $query) {
$query->hasTrigger('user_action', 'store-journal');
}
]
)->orderBy('order', 'ASC')->get();
$groups = $user->ruleGroups()->orderBy('order', 'ASC')->get();
//
/** @var RuleGroup $group */
foreach ($groups as $group) {
$rules = $group->rules;
$rules = $group->rules()
->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')
->where('rule_triggers.trigger_type', 'user_action')
->where('rule_triggers.trigger_value', 'store-journal')
->get(['rules.*']);
/** @var Rule $rule */
foreach ($rules as $rule) {
Log::debug('Now handling rule #' . $rule->id);

View File

@ -66,23 +66,4 @@ class Rule extends Model
return $this->hasMany('FireflyIII\Models\RuleTrigger');
}
/**
* @param $query
* @param $triggerType
* @param null $triggerValue
*
* @return Builder
*/
public function scopeHasTrigger(Builder $query, $triggerType, $triggerValue = null)
{
$query->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id');
$query->where('rule_triggers.trigger_type', $triggerType);
if (!is_null($triggerValue)) {
$query->where('rule_triggers.trigger_value', $triggerValue);
}
return $query;
}
}