Update rule handling and views.

This commit is contained in:
James Cole
2019-07-27 15:01:13 +02:00
parent 67c0ef6ec6
commit 5524941c90
14 changed files with 112 additions and 478 deletions

View File

@@ -1,214 +0,0 @@
<?php
/**
* ExecuteRuleGroupOnExistingTransactions.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Jobs;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\RuleGroup;
use FireflyIII\TransactionRules\Processor;
use FireflyIII\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
/**
* Class ExecuteRuleGroupOnExistingTransactions.
* TODO make sure this job honors the "stop_processing" rules.
*/
class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/** @var Collection Set of accounts */
private $accounts;
/** @var Carbon The end date */
private $endDate;
/** @var RuleGroup The rule group */
private $ruleGroup;
/** @var Carbon The start date */
private $startDate;
/** @var User The user */
private $user;
/**
* Create a new job instance.
*
* @param RuleGroup $ruleGroup
*/
public function __construct(RuleGroup $ruleGroup)
{
$this->ruleGroup = $ruleGroup;
}
/**
* Get accounts.
*
* @return Collection
*/
public function getAccounts(): Collection
{
return $this->accounts;
}
/**
* Set accounts.
*
* @param Collection $accounts
*/
public function setAccounts(Collection $accounts)
{
$this->accounts = $accounts;
}
/**
* Get end date.
*
* @return \Carbon\Carbon
*/
public function getEndDate(): Carbon
{
return $this->endDate;
}
/**
* Set end date.
*
* @param Carbon $date
*/
public function setEndDate(Carbon $date)
{
$this->endDate = $date;
}
/**
* Get start date.
*
* @return \Carbon\Carbon
*/
public function getStartDate(): Carbon
{
return $this->startDate;
}
/**
* Set start date.
*
* @param Carbon $date
*/
public function setStartDate(Carbon $date)
{
$this->startDate = $date;
}
/**
* Get user.
*
* @return User
*/
public function getUser(): User
{
return $this->user;
}
/**
* Set user.
*
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
/**
* Execute the job.
*
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function handle()
{
// Lookup all journals that match the parameters specified
$journals = $this->collectJournals();
// Find processors for each rule within the current rule group
$processors = $this->collectProcessors();
// Execute the rules for each transaction
foreach ($processors as $processor) {
/** @var array $journal */
foreach ($journals as $journal) {
/** @var Processor $processor */
$processor->handleJournalArray($journal);
}
// Stop processing this group if the rule specifies 'stop_processing'
// TODO Fix this.
if ($processor->getRule()->stop_processing) {
break;
}
}
}
/**
* Collect all journals that should be processed.
*
* @return array
*/
protected function collectJournals(): array
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user);
$collector->setAccounts($this->accounts)->setRange($this->startDate, $this->endDate);
return $collector->getExtractedJournals();
}
/**
* Collects a list of rule processors, one for each rule within the rule group.
*
* @return array
*/
protected function collectProcessors(): array
{
// 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) {
/** @var Processor $processor */
$processor = app(Processor::class);
$processor->make($rule);
return $processor;
},
$rules->all()
);
}
}

View File

@@ -1,202 +0,0 @@
<?php
/**
* ExecuteRuleOnExistingTransactions.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Jobs;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Rule;
use FireflyIII\TransactionRules\Processor;
use FireflyIII\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Log;
/**
* Class ExecuteRuleOnExistingTransactions.
*/
class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/** @var Collection The accounts */
private $accounts;
/** @var Carbon The end date */
private $endDate;
/** @var Rule The current rule */
private $rule;
/** @var Carbon The start date */
private $startDate;
/** @var User The user */
private $user;
/**
* Create a new job instance.
*
* @param Rule $rule
*/
public function __construct(Rule $rule)
{
$this->rule = $rule;
}
/**
* Get accounts.
*
* @return Collection
*/
public function getAccounts(): Collection
{
return $this->accounts;
}
/**
* Set accounts.
*
* @param Collection $accounts
*/
public function setAccounts(Collection $accounts)
{
$this->accounts = $accounts;
}
/**
* Get end date.
*
* @return \Carbon\Carbon
*/
public function getEndDate(): Carbon
{
return $this->endDate;
}
/**
* Set end date.
*
* @param Carbon $date
*/
public function setEndDate(Carbon $date)
{
$this->endDate = $date;
}
/**
* Get rule.
*
* @return Rule
*/
public function getRule(): Rule
{
return $this->rule;
}
/**
* Get start date.
*
* @return \Carbon\Carbon
*/
public function getStartDate(): Carbon
{
return $this->startDate;
}
/**
* Set start date.
*
* @param Carbon $date
*/
public function setStartDate(Carbon $date)
{
$this->startDate = $date;
}
/**
* Get user.
*
* @return User
*/
public function getUser(): User
{
return $this->user;
}
/**
* Set user.
*
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
/**
* Execute the job.
*
* @throws \FireflyIII\Exceptions\FireflyException
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function handle()
{
// Lookup all journals that match the parameters specified
$journals = $this->collectJournals();
/** @var Processor $processor */
$processor = app(Processor::class);
$processor->make($this->rule, true);
$hits = 0;
$misses = 0;
$total = 0;
// Execute the rules for each transaction
/** @var array $journal */
foreach ($journals as $journal) {
++$total;
$result = $processor->handleJournalArray($journal);
if ($result) {
++$hits;
}
if (!$result) {
++$misses;
}
Log::info(sprintf('Current progress: %d Transactions. Hits: %d, misses: %d', $total, $hits, $misses));
}
Log::info(sprintf('Total transactions: %d. Hits: %d, misses: %d', $total, $hits, $misses));
}
/**
* Collect all journals that should be processed.
*
* @return array
*/
protected function collectJournals(): array
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user);
$collector->setAccounts($this->accounts)->setRange($this->startDate, $this->endDate);
return $collector->getExtractedJournals();
}
}