/**
  * Connect a new transaction journal to any related piggy banks.
  *
  * @param  TransactionJournalStored $event
  *
  * @return bool
  */
 public function handle(TransactionJournalStored $event) : bool
 {
     // 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', 'store-journal')->where('rules.active', 1)->get(['rules.*']);
         /** @var Rule $rule */
         foreach ($rules as $rule) {
             $processor = Processor::make($rule);
             $processor->handleTransactionJournal($event->journal);
             if ($rule->stop_processing) {
                 return true;
             }
         }
     }
     return true;
 }
 /**
  * Collects a list of rule processors, one for each rule within the rule group
  *
  * @return array
  */
 protected function collectProcessors()
 {
     // Find all rules belonging to this rulegroup
     $rules = $this->ruleGroup->rules()->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')->where('rule_triggers.trigger_type', 'user_action')->where('rule_triggers.trigger_value', 'store-journal')->where('rules.active', 1)->get(['rules.*']);
     // Create a list of processors for these rules
     return array_map(function ($rule) {
         return Processor::make($rule);
     }, $rules->all());
 }
Ejemplo n.º 3
0
 /**
  * @param TransactionJournal $journal
  *
  * @return bool
  */
 private function applyRules(TransactionJournal $journal) : bool
 {
     if ($this->rules->count() > 0) {
         /** @var Rule $rule */
         foreach ($this->rules as $rule) {
             Log::debug(sprintf('Going to apply rule #%d to journal %d.', $rule->id, $journal->id));
             $processor = Processor::make($rule);
             $processor->handleTransactionJournal($journal);
             if ($rule->stop_processing) {
                 return true;
             }
         }
     }
     return true;
 }