firefly-iii/app/Handlers/Events/FireRulesForStore.php

69 lines
2.0 KiB
PHP
Raw Normal View History

2016-01-12 14:38:12 -06:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2016-01-12 14:38:12 -06:00
/**
* FireRulesForStore.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;
2016-01-13 00:16:29 -06:00
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Rules\Processor;
use FireflyIII\User;
use Illuminate\Support\Facades\Auth;
2016-01-12 22:37:25 -06:00
use Log;
2016-01-12 14:38:12 -06:00
/**
* Class FireRulesForStore
*
* @package FireflyIII\Handlers\Events
*/
class FireRulesForStore
{
/**
* Connect a new transaction journal to any related piggy banks.
*
* @param TransactionJournalStored $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(TransactionJournalStored $event): bool
2016-01-12 14:38:12 -06:00
{
// get all the user's rule groups, with the rules, order by 'order'.
2016-01-13 00:16:29 -06:00
/** @var User $user */
$user = Auth::user();
2016-01-15 02:25:32 -06:00
$groups = $user->ruleGroups()->where('rule_groups.active', 1)->orderBy('order', 'ASC')->get();
2016-01-13 00:16:29 -06:00
//
/** @var RuleGroup $group */
foreach ($groups as $group) {
2016-01-13 07:02:22 -06:00
Log::debug('Now processing group "' . $group->title . '".');
2016-01-13 02:08:13 -06:00
$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')
2016-01-15 02:25:32 -06:00
->where('rules.active', 1)
2016-01-13 02:08:13 -06:00
->get(['rules.*']);
2016-01-13 00:16:29 -06:00
/** @var Rule $rule */
foreach ($rules as $rule) {
Log::debug('Now handling rule #' . $rule->id . ' (' . $rule->title . ')');
$processor = Processor::make($rule);
$processor->handleTransactionJournal($event->journal);
2016-01-13 00:16:29 -06:00
2016-01-15 02:25:32 -06:00
if ($rule->stop_processing) {
2016-02-18 03:04:26 -06:00
return true;
2016-01-14 23:43:00 -06:00
}
2016-01-13 00:16:29 -06:00
}
}
2016-02-18 03:04:26 -06:00
return true;
2016-01-12 14:38:12 -06:00
}
}