Fix issue in rule engine.

This commit is contained in:
James Cole
2021-02-12 20:15:23 +01:00
parent 95966cdcd4
commit 1ecc454f70
7 changed files with 63 additions and 41 deletions

View File

@@ -62,14 +62,12 @@ class StoredGroupEventHandler
Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds)); Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds));
// collect rules: // collect rules:
$ruleRepository = app(RuleRepositoryInterface::class);
$ruleGroupRepository = app(RuleGroupRepositoryInterface::class); $ruleGroupRepository = app(RuleGroupRepositoryInterface::class);
$ruleRepository->setUser($storedGroupEvent->transactionGroup->user);
$ruleGroupRepository->setUser($storedGroupEvent->transactionGroup->user); $ruleGroupRepository->setUser($storedGroupEvent->transactionGroup->user);
// add the groups to the rule engine. // add the groups to the rule engine.
// it should run the rules in the group and cancel the group if necessary. // it should run the rules in the group and cancel the group if necessary.
$groups = $ruleGroupRepository->getRuleGroupsWithRules(); $groups = $ruleGroupRepository->getRuleGroupsWithRules('store-journal');
// create and fire rule engine. // create and fire rule engine.
$newRuleEngine = app(RuleEngineInterface::class); $newRuleEngine = app(RuleEngineInterface::class);

View File

@@ -31,6 +31,7 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Models\Webhook; use FireflyIII\Models\Webhook;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface; use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\TransactionRules\Engine\RuleEngineInterface; use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -103,15 +104,16 @@ class UpdatedGroupEventHandler
Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds)); Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds));
// collect rules: // collect rules:
$ruleRepository = app(RuleRepositoryInterface::class); $ruleGroupRepository = app(RuleGroupRepositoryInterface::class);
$ruleRepository->setUser($updatedGroupEvent->transactionGroup->user); $ruleGroupRepository->setUser($updatedGroupEvent->transactionGroup->user);
$rules = $ruleRepository->getUpdateRules();
$groups = $ruleGroupRepository->getRuleGroupsWithRules('update-journal');
// file rule engine. // file rule engine.
$newRuleEngine = app(RuleEngineInterface::class); $newRuleEngine = app(RuleEngineInterface::class);
$newRuleEngine->setUser($updatedGroupEvent->transactionGroup->user); $newRuleEngine->setUser($updatedGroupEvent->transactionGroup->user);
$newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]); $newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]);
$newRuleEngine->setRules($rules); $newRuleEngine->setRuleGroups($groups);
$newRuleEngine->fire(); $newRuleEngine->fire();
} }

View File

@@ -77,7 +77,7 @@ class IndexController extends Controller
$this->createDefaultRuleGroup(); $this->createDefaultRuleGroup();
$this->createDefaultRule(); $this->createDefaultRule();
$this->ruleGroupRepos->resetRuleGroupOrder(); $this->ruleGroupRepos->resetRuleGroupOrder();
$ruleGroups = $this->ruleGroupRepos->getRuleGroupsWithRules($user); $ruleGroups = $this->ruleGroupRepos->getRuleGroupsWithRules(null);
return prefixView('rules.index', compact('ruleGroups')); return prefixView('rules.index', compact('ruleGroups'));
} }

View File

@@ -47,7 +47,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property int $order * @property int $order
* @property bool $active * @property bool $active
* @property bool $stop_processing * @property bool $stop_processing
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Rule[] $rules * @property \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Rule[] $rules
* @property-read int|null $rules_count * @property-read int|null $rules_count
* @property-read User $user * @property-read User $user
* @method static \Illuminate\Database\Eloquent\Builder|RuleGroup newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|RuleGroup newModelQuery()

View File

@@ -34,8 +34,7 @@ use Log;
*/ */
class RuleGroupRepository implements RuleGroupRepositoryInterface class RuleGroupRepository implements RuleGroupRepositoryInterface
{ {
/** @var User */ private User $user;
private $user;
/** /**
* @return int * @return int
@@ -198,11 +197,13 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
} }
/** /**
* @param string|null $filter
*
* @return Collection * @return Collection
*/ */
public function getRuleGroupsWithRules(): Collection public function getRuleGroupsWithRules(?string $filter): Collection
{ {
return $this->user->ruleGroups() $groups = $this->user->ruleGroups()
->orderBy('order', 'ASC') ->orderBy('order', 'ASC')
->where('active', true) ->where('active', true)
->with( ->with(
@@ -218,6 +219,34 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}, },
] ]
)->get(); )->get();
if (null === $filter) {
return $groups;
}
Log::debug(sprintf('Will filter getRuleGroupsWithRules on "%s".', $filter));
return $groups->map(
function (RuleGroup $group) use ($filter) {
Log::debug(sprintf('Now filtering group #%d', $group->id));
// filter the rules in the rule group:
$group->rules = $group->rules->filter(
function (Rule $rule) use ($filter) {
Log::debug(sprintf('Now filtering rule #%d', $rule->id));
foreach ($rule->ruleTriggers as $trigger) {
if ('user_action' === $trigger->trigger_type && $filter === $trigger->trigger_value) {
Log::debug(sprintf('Rule #%d triggers on %s, include it.', $rule->id, $filter));
return true;
}
}
Log::debug(sprintf('Rule #%d does not trigger on %s, do not include it.', $rule->id, $filter));
return false;
}
);
return $group;
}
);
} }
/** /**

View File

@@ -102,9 +102,11 @@ interface RuleGroupRepositoryInterface
public function getHighestOrderRuleGroup(): int; public function getHighestOrderRuleGroup(): int;
/** /**
* @param string|null $filter
*
* @return Collection * @return Collection
*/ */
public function getRuleGroupsWithRules(): Collection; public function getRuleGroupsWithRules(?string $filter): Collection;
/** /**
* @param RuleGroup $group * @param RuleGroup $group

View File

@@ -29,7 +29,6 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log;
use stdClass; use stdClass;
/** /**
@@ -79,16 +78,8 @@ class Steam
->where('transactions.transaction_currency_id', '!=', $currency->id) ->where('transactions.transaction_currency_id', '!=', $currency->id)
->get(['transactions.foreign_amount'])->toArray(); ->get(['transactions.foreign_amount'])->toArray();
$foreignBalance = $this->sumTransactions($transactions, 'foreign_amount'); $foreignBalance = $this->sumTransactions($transactions, 'foreign_amount');
// check:
Log::debug(sprintf('Steam::balance. Native balance is "%s"', $nativeBalance));
Log::debug(sprintf('Steam::balance. Foreign balance is "%s"', $foreignBalance));
$balance = bcadd($nativeBalance, $foreignBalance); $balance = bcadd($nativeBalance, $foreignBalance);
$virtual = null === $account->virtual_balance ? '0' : (string)$account->virtual_balance; $virtual = null === $account->virtual_balance ? '0' : (string)$account->virtual_balance;
Log::debug(sprintf('Steam::balance. Virtual balance is "%s"', $virtual));
$balance = bcadd($balance, $virtual); $balance = bcadd($balance, $virtual);
$cache->store($balance); $cache->store($balance);