Files
firefly-iii/app/Jobs/ExecuteRuleGroupOnExistingTransactions.php

215 lines
5.5 KiB
PHP
Raw Normal View History

<?php
/**
* ExecuteRuleGroupOnExistingTransactions.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* 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
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Jobs;
use Carbon\Carbon;
2019-05-31 13:35:33 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2016-02-23 14:17:41 +01:00
use FireflyIII\Models\RuleGroup;
2017-09-13 07:49:58 +02:00
use FireflyIII\TransactionRules\Processor;
2016-02-23 16:02:44 +01:00
use FireflyIII\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
2016-02-23 16:02:44 +01:00
use Illuminate\Queue\SerializesModels;
2016-03-03 08:29:56 +01:00
use Illuminate\Support\Collection;
2016-03-03 08:29:56 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class ExecuteRuleGroupOnExistingTransactions.
2019-05-31 13:35:33 +02:00
* TODO make sure this job honors the "stop_processing" rules.
2016-03-03 08:29:56 +01:00
*/
2016-02-23 14:17:41 +01:00
class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
2018-07-22 15:20:45 +02:00
/** @var Collection Set of accounts */
private $accounts;
2018-07-22 15:20:45 +02:00
/** @var Carbon The end date */
private $endDate;
2018-07-22 15:20:45 +02:00
/** @var RuleGroup The rule group */
2016-02-23 16:02:44 +01:00
private $ruleGroup;
2018-07-22 15:20:45 +02:00
/** @var Carbon The start date */
private $startDate;
2018-07-22 15:20:45 +02:00
/** @var User The user */
private $user;
2016-02-23 16:02:44 +01:00
/**
* Create a new job instance.
*
2016-03-03 08:29:56 +01:00
* @param RuleGroup $ruleGroup
*/
public function __construct(RuleGroup $ruleGroup)
{
$this->ruleGroup = $ruleGroup;
}
2016-02-23 16:02:44 +01:00
/**
2018-07-22 15:20:45 +02:00
* Get accounts.
*
2016-02-23 16:02:44 +01:00
* @return Collection
*/
2016-04-06 09:27:45 +02:00
public function getAccounts(): Collection
2016-02-23 16:02:44 +01:00
{
return $this->accounts;
}
/**
2018-07-22 15:20:45 +02:00
* Set accounts.
*
2016-02-23 16:02:44 +01:00
* @param Collection $accounts
*/
public function setAccounts(Collection $accounts)
{
$this->accounts = $accounts;
}
/**
2018-07-22 15:20:45 +02:00
* Get end date.
*
2016-02-23 16:02:44 +01:00
* @return \Carbon\Carbon
*/
2016-04-06 09:27:45 +02:00
public function getEndDate(): Carbon
2016-02-23 16:02:44 +01:00
{
return $this->endDate;
}
/**
2018-07-22 15:20:45 +02:00
* Set end date.
*
2016-02-23 16:02:44 +01:00
* @param Carbon $date
*/
public function setEndDate(Carbon $date)
{
$this->endDate = $date;
}
/**
2018-07-22 15:20:45 +02:00
* Get start date.
*
2016-02-23 16:02:44 +01:00
* @return \Carbon\Carbon
*/
2016-04-06 09:27:45 +02:00
public function getStartDate(): Carbon
2016-02-23 16:02:44 +01:00
{
return $this->startDate;
}
/**
2018-07-22 15:20:45 +02:00
* Set start date.
*
2016-02-23 16:02:44 +01:00
* @param Carbon $date
*/
public function setStartDate(Carbon $date)
{
$this->startDate = $date;
}
/**
2018-07-22 15:20:45 +02:00
* Get user.
*
2016-02-23 16:02:44 +01:00
* @return User
*/
2016-04-06 09:27:45 +02:00
public function getUser(): User
2016-02-23 16:02:44 +01:00
{
return $this->user;
}
/**
2018-07-22 15:20:45 +02:00
* Set user.
*
2016-02-23 16:02:44 +01:00
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
/**
* Execute the job.
2018-03-29 19:01:47 +02:00
*
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function handle()
{
// Lookup all journals that match the parameters specified
2019-05-31 13:35:33 +02:00
$journals = $this->collectJournals();
2016-02-23 16:02:44 +01:00
// Find processors for each rule within the current rule group
2016-02-23 16:02:44 +01:00
$processors = $this->collectProcessors();
// Execute the rules for each transaction
2018-10-14 16:40:12 +02:00
foreach ($processors as $processor) {
2019-05-31 13:35:33 +02:00
/** @var array $journal */
foreach ($journals as $journal) {
2018-10-14 16:40:12 +02:00
/** @var Processor $processor */
2019-05-31 13:35:33 +02:00
$processor->handleJournalArray($journal);
2018-10-14 16:40:12 +02:00
}
// Stop processing this group if the rule specifies 'stop_processing'
2019-05-31 13:35:33 +02:00
// TODO Fix this.
2018-10-14 16:40:12 +02:00
if ($processor->getRule()->stop_processing) {
break;
}
}
}
/**
2017-11-15 12:25:49 +01:00
* Collect all journals that should be processed.
2016-02-23 16:02:44 +01:00
*
2019-05-31 13:35:33 +02:00
* @return array
*/
2019-05-31 13:35:33 +02:00
protected function collectJournals(): array
{
2019-05-31 13:35:33 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2017-02-15 16:20:16 +01:00
$collector->setUser($this->user);
$collector->setAccounts($this->accounts)->setRange($this->startDate, $this->endDate);
2016-02-23 16:02:44 +01:00
2019-05-31 13:35:33 +02:00
return $collector->getExtractedJournals();
2016-02-23 16:02:44 +01:00
}
/**
2017-11-15 12:25:49 +01:00
* Collects a list of rule processors, one for each rule within the rule group.
2016-02-23 16:02:44 +01:00
*
* @return array
*/
2018-07-22 15:20:45 +02:00
protected function collectProcessors(): array
2016-02-23 16:02:44 +01:00
{
// Find all rules belonging to this rulegroup
$rules = $this->ruleGroup->rules()
2016-02-23 16:02:44 +01:00
->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.*']);
2016-02-23 16:02:44 +01:00
// Create a list of processors for these rules
return array_map(
function ($rule) {
/** @var Processor $processor */
$processor = app(Processor::class);
$processor->make($rule);
2018-10-14 16:40:12 +02:00
return $processor;
2017-11-15 10:52:29 +01:00
},
$rules->all()
2016-02-23 16:02:44 +01:00
);
}
}