2016-01-12 14:38:12 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* FireRulesForUpdate.php
|
2016-04-01 09:44:46 -05:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
2016-01-12 14:38:12 -06:00
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
2016-05-20 05:27:31 -05:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2016-01-12 14:38:12 -06:00
|
|
|
namespace FireflyIII\Handlers\Events;
|
2016-01-15 16:12:52 -06:00
|
|
|
|
2016-01-15 23:34:50 -06:00
|
|
|
use Auth;
|
2016-01-12 14:38:12 -06:00
|
|
|
use FireflyIII\Events\TransactionJournalUpdated;
|
2016-01-15 23:34:50 -06:00
|
|
|
use FireflyIII\Models\Rule;
|
|
|
|
use FireflyIII\Models\RuleGroup;
|
|
|
|
use FireflyIII\Rules\Processor;
|
|
|
|
use FireflyIII\User;
|
2016-01-12 14:38:12 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FireRulesForUpdate
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Handlers\Events
|
|
|
|
*/
|
|
|
|
class FireRulesForUpdate
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle the event.
|
|
|
|
*
|
|
|
|
* @param TransactionJournalUpdated $event
|
|
|
|
*
|
2016-02-18 00:21:48 -06:00
|
|
|
* @return bool
|
2016-01-12 14:38:12 -06:00
|
|
|
*/
|
2016-02-18 00:21:48 -06:00
|
|
|
public function handle(TransactionJournalUpdated $event): bool
|
2016-01-12 14:38:12 -06:00
|
|
|
{
|
2016-01-15 23:34:50 -06:00
|
|
|
// get all the user's rule groups, with the rules, order by 'order'.
|
|
|
|
/** @var User $user */
|
|
|
|
$user = Auth::user();
|
|
|
|
$groups = $user->ruleGroups()->where('rule_groups.active', 1)->orderBy('order', 'ASC')->get();
|
|
|
|
//
|
|
|
|
/** @var RuleGroup $group */
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$rules = $group->rules()
|
|
|
|
->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')
|
|
|
|
->where('rule_triggers.trigger_type', 'user_action')
|
|
|
|
->where('rule_triggers.trigger_value', 'update-journal')
|
|
|
|
->where('rules.active', 1)
|
|
|
|
->get(['rules.*']);
|
|
|
|
/** @var Rule $rule */
|
|
|
|
foreach ($rules as $rule) {
|
2016-02-17 11:47:05 -06:00
|
|
|
$processor = Processor::make($rule);
|
|
|
|
$processor->handleTransactionJournal($event->journal);
|
2016-01-15 23:34:50 -06:00
|
|
|
|
|
|
|
if ($rule->stop_processing) {
|
|
|
|
break;
|
|
|
|
}
|
2016-01-12 14:38:12 -06:00
|
|
|
|
2016-01-15 23:34:50 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-14 14:53:56 -05:00
|
|
|
|
2016-02-18 00:21:48 -06:00
|
|
|
return true;
|
2016-01-12 14:38:12 -06:00
|
|
|
}
|
2016-01-28 14:50:20 -06:00
|
|
|
}
|