Clean up repositories and cron code.

This commit is contained in:
James Cole
2019-06-07 17:57:46 +02:00
parent a845cb9af9
commit e4a9abc315
19 changed files with 547 additions and 1188 deletions

View File

@@ -1,7 +1,6 @@
<?php
/**
* ApplyRules.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
@@ -36,6 +35,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\TransactionRules\Engine\RuleEngine;
use FireflyIII\TransactionRules\Processor;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
@@ -138,17 +138,25 @@ class ApplyRules extends Command
$this->allRules = $this->option('all_rules');
// always get all the rules of the user.
$this->grabAllRules();
// loop all groups and rules and indicate if they're included:
$count = 0;
$count = 0;
$rulesToApply = [];
/** @var RuleGroup $group */
foreach ($this->groups as $group) {
/** @var Rule $rule */
foreach ($group->rules as $rule) {
// if in rule selection, or group in selection or all rules, it's included.
if ($this->includeRule($rule, $group)) {
$test = $this->includeRule($rule, $group);
if (true === $test) {
Log::debug(sprintf('Will include rule #%d "%s"', $rule->id, $rule->title));
$count++;
$rulesToApply[] = $rule->id;
}
if (false === $test) {
Log::debug(sprintf('Will not include rule #%d "%s"', $rule->id, $rule->title));
}
}
}
@@ -160,7 +168,6 @@ class ApplyRules extends Command
$this->warn(' --all_rules');
}
// get transactions from asset accounts.
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
@@ -173,40 +180,17 @@ class ApplyRules extends Command
$this->line(sprintf('Will apply %d rules to %d transactions.', $count, count($journals)));
// start looping.
/** @var RuleEngine $ruleEngine */
$ruleEngine = app(RuleEngine::class);
$ruleEngine->setUser($this->getUser());
$ruleEngine->setRulesToApply($rulesToApply);
$bar = $this->output->createProgressBar(count($journals));
Log::debug(sprintf('Now looping %d transactions.', count($journals)));
/** @var array $journal */
foreach ($journals as $journal) {
Log::debug('Start of new journal.');
foreach ($this->groups as $group) {
$groupTriggered = false;
/** @var Rule $rule */
foreach ($group->rules as $rule) {
$ruleTriggered = false;
// if in rule selection, or group in selection or all rules, it's included.
if ($this->includeRule($rule, $group)) {
/** @var Processor $processor */
$processor = app(Processor::class);
$processor->make($rule, true);
$ruleTriggered = $processor->handleJournalArray($journal);
if ($ruleTriggered) {
$groupTriggered = true;
}
}
// if the rule is triggered and stop processing is true, cancel the entire group.
if ($ruleTriggered && $rule->stop_processing) {
Log::info('Break out group because rule was triggered.');
break;
}
}
// if group is triggered and stop processing is true, cancel the whole thing.
if ($groupTriggered && $group->stop_processing) {
Log::info('Break out ALL because group was triggered.');
break;
}
}
$ruleEngine->processJournalArray($journal);
Log::debug('Done with all rules for this group + done with journal.');
$bar->advance();
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* Cron.php
* Copyright (c) 2019 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\Console\Commands\Tools;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Support\Cronjobs\RecurringCronjob;
use Illuminate\Console\Command;
/**
* Class Cron
*
* @codeCoverageIgnore
*/
class Cron extends Command
{
/**
* The console command description.
*
* @var string
*/
protected $description = 'Runs all Firefly III cron-job related commands. Configure a cron job according to the official Firefly III documentation.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:cron
{--F|force : Force the cron job(s) to execute.}
{--date= : Set the date in YYYY-MM-DD to make Firefly III think that\'s the current date.}
';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle(): int
{
$recurring = new RecurringCronjob;
$recurring->setForce($this->option('force'));
try {
$result = $recurring->fire();
} catch (FireflyException $e) {
$this->error($e->getMessage());
return 0;
}
if (false === $result) {
$this->line('The recurring transaction cron job did not fire.');
}
if (true === $result) {
$this->line('The recurring transaction cron job fired successfully.');
}
$this->info('More feedback on the cron jobs can be found in the log files.');
return 0;
}
}