mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 18:54:58 +00:00 
			
		
		
		
	Fix issue in rule engine.
This commit is contained in:
		| @@ -62,14 +62,12 @@ class StoredGroupEventHandler | ||||
|         Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds)); | ||||
|  | ||||
|         // collect rules: | ||||
|         $ruleRepository = app(RuleRepositoryInterface::class); | ||||
|         $ruleGroupRepository = app(RuleGroupRepositoryInterface::class); | ||||
|         $ruleRepository->setUser($storedGroupEvent->transactionGroup->user); | ||||
|         $ruleGroupRepository->setUser($storedGroupEvent->transactionGroup->user); | ||||
|  | ||||
|         // add the groups to the rule engine. | ||||
|         // 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. | ||||
|         $newRuleEngine = app(RuleEngineInterface::class); | ||||
|   | ||||
| @@ -31,6 +31,7 @@ use FireflyIII\Models\TransactionJournal; | ||||
| use FireflyIII\Models\TransactionType; | ||||
| use FireflyIII\Models\Webhook; | ||||
| use FireflyIII\Repositories\Rule\RuleRepositoryInterface; | ||||
| use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; | ||||
| use FireflyIII\TransactionRules\Engine\RuleEngineInterface; | ||||
| use Illuminate\Support\Collection; | ||||
| use Log; | ||||
| @@ -103,15 +104,16 @@ class UpdatedGroupEventHandler | ||||
|         Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds)); | ||||
|  | ||||
|         // collect rules: | ||||
|         $ruleRepository = app(RuleRepositoryInterface::class); | ||||
|         $ruleRepository->setUser($updatedGroupEvent->transactionGroup->user); | ||||
|         $rules = $ruleRepository->getUpdateRules(); | ||||
|         $ruleGroupRepository = app(RuleGroupRepositoryInterface::class); | ||||
|         $ruleGroupRepository->setUser($updatedGroupEvent->transactionGroup->user); | ||||
|  | ||||
|         $groups = $ruleGroupRepository->getRuleGroupsWithRules('update-journal'); | ||||
|  | ||||
|         // file rule engine. | ||||
|         $newRuleEngine = app(RuleEngineInterface::class); | ||||
|         $newRuleEngine->setUser($updatedGroupEvent->transactionGroup->user); | ||||
|         $newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]); | ||||
|         $newRuleEngine->setRules($rules); | ||||
|         $newRuleEngine->setRuleGroups($groups); | ||||
|         $newRuleEngine->fire(); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -77,7 +77,7 @@ class IndexController extends Controller | ||||
|         $this->createDefaultRuleGroup(); | ||||
|         $this->createDefaultRule(); | ||||
|         $this->ruleGroupRepos->resetRuleGroupOrder(); | ||||
|         $ruleGroups = $this->ruleGroupRepos->getRuleGroupsWithRules($user); | ||||
|         $ruleGroups = $this->ruleGroupRepos->getRuleGroupsWithRules(null); | ||||
|  | ||||
|         return prefixView('rules.index', compact('ruleGroups')); | ||||
|     } | ||||
|   | ||||
| @@ -47,7 +47,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||||
|  * @property int $order | ||||
|  * @property bool $active | ||||
|  * @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 User $user | ||||
|  * @method static \Illuminate\Database\Eloquent\Builder|RuleGroup newModelQuery() | ||||
|   | ||||
| @@ -34,8 +34,7 @@ use Log; | ||||
|  */ | ||||
| class RuleGroupRepository implements RuleGroupRepositoryInterface | ||||
| { | ||||
|     /** @var User */ | ||||
|     private $user; | ||||
|     private User $user; | ||||
|  | ||||
|     /** | ||||
|      * @return int | ||||
| @@ -198,26 +197,56 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param string|null $filter | ||||
|      * | ||||
|      * @return Collection | ||||
|      */ | ||||
|     public function getRuleGroupsWithRules(): Collection | ||||
|     public function getRuleGroupsWithRules(?string $filter): Collection | ||||
|     { | ||||
|         return $this->user->ruleGroups() | ||||
|                     ->orderBy('order', 'ASC') | ||||
|                     ->where('active', true) | ||||
|                     ->with( | ||||
|                         [ | ||||
|                             'rules'              => static function (HasMany $query) { | ||||
|                                 $query->orderBy('order', 'ASC'); | ||||
|                             }, | ||||
|                             'rules.ruleTriggers' => static function (HasMany $query) { | ||||
|                                 $query->orderBy('order', 'ASC'); | ||||
|                             }, | ||||
|                             'rules.ruleActions'  => static function (HasMany $query) { | ||||
|                                 $query->orderBy('order', 'ASC'); | ||||
|                             }, | ||||
|                         ] | ||||
|                     )->get(); | ||||
|         $groups = $this->user->ruleGroups() | ||||
|                              ->orderBy('order', 'ASC') | ||||
|                              ->where('active', true) | ||||
|                              ->with( | ||||
|                                  [ | ||||
|                                      'rules'              => static function (HasMany $query) { | ||||
|                                          $query->orderBy('order', 'ASC'); | ||||
|                                      }, | ||||
|                                      'rules.ruleTriggers' => static function (HasMany $query) { | ||||
|                                          $query->orderBy('order', 'ASC'); | ||||
|                                      }, | ||||
|                                      'rules.ruleActions'  => static function (HasMany $query) { | ||||
|                                          $query->orderBy('order', 'ASC'); | ||||
|                                      }, | ||||
|                                  ] | ||||
|                              )->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; | ||||
|             } | ||||
|         ); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|   | ||||
| @@ -102,9 +102,11 @@ interface RuleGroupRepositoryInterface | ||||
|     public function getHighestOrderRuleGroup(): int; | ||||
|  | ||||
|     /** | ||||
|      * @param string|null $filter | ||||
|      * | ||||
|      * @return Collection | ||||
|      */ | ||||
|     public function getRuleGroupsWithRules(): Collection; | ||||
|     public function getRuleGroupsWithRules(?string $filter): Collection; | ||||
|  | ||||
|     /** | ||||
|      * @param RuleGroup $group | ||||
|   | ||||
| @@ -29,7 +29,6 @@ use FireflyIII\Models\Transaction; | ||||
| use FireflyIII\Models\TransactionCurrency; | ||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||
| use Illuminate\Support\Collection; | ||||
| use Log; | ||||
| use stdClass; | ||||
|  | ||||
| /** | ||||
| @@ -79,17 +78,9 @@ class Steam | ||||
|                                   ->where('transactions.transaction_currency_id', '!=', $currency->id) | ||||
|                                   ->get(['transactions.foreign_amount'])->toArray(); | ||||
|         $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); | ||||
|         $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($nativeBalance, $foreignBalance); | ||||
|         $virtual        = null === $account->virtual_balance ? '0' : (string)$account->virtual_balance; | ||||
|         $balance        = bcadd($balance, $virtual); | ||||
|  | ||||
|         $cache->store($balance); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user