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