firefly-iii/app/Jobs/ExecuteRuleGroupOnExistingTransactions.php

186 lines
4.3 KiB
PHP
Raw Normal View History

<?php
/**
* ExecuteRuleGroupOnExistingTransactions.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2016-05-20 01:57:45 -05:00
declare(strict_types = 1);
namespace FireflyIII\Jobs;
use Carbon\Carbon;
2016-02-23 07:17:41 -06:00
use FireflyIII\Models\RuleGroup;
2016-05-01 02:52:58 -05:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2016-02-23 07:17:41 -06:00
use FireflyIII\Rules\Processor;
2016-02-23 09:02:44 -06:00
use FireflyIII\User;
use Illuminate\Contracts\Queue\ShouldQueue;
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-03-03 01:29:56 -06:00
/**
* Class ExecuteRuleGroupOnExistingTransactions
*
* @package FireflyIII\Jobs
*/
2016-02-23 07:17:41 -06:00
class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/** @var Collection */
private $accounts;
/** @var Carbon */
private $endDate;
2016-02-23 09:02:44 -06:00
/** @var RuleGroup */
private $ruleGroup;
/** @var Carbon */
private $startDate;
/** @var User */
private $user;
2016-02-23 09:02:44 -06:00
/**
* Create a new job instance.
*
2016-03-03 01:29:56 -06:00
* @param RuleGroup $ruleGroup
*/
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;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Lookup all journals that match the parameters specified
$journals = $this->collectJournals();
2016-02-23 09:02:44 -06:00
// Find processors for each rule within the current rule group
2016-02-23 09:02:44 -06:00
$processors = $this->collectProcessors();
// Execute the rules for each transaction
2016-02-23 09:02:44 -06:00
foreach ($journals as $journal) {
/** @var Processor $processor */
foreach ($processors as $processor) {
$processor->handleTransactionJournal($journal);
2016-02-23 09:02:44 -06:00
// Stop processing this group if the rule specifies 'stop_processing'
if ($processor->getRule()->stop_processing) {
break;
}
}
}
}
/**
* Collect all journals that should be processed
2016-02-23 09:02:44 -06:00
*
* @return Collection
*/
protected function collectJournals()
{
2016-05-01 02:52:58 -05:00
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
2016-02-23 09:02:44 -06:00
2016-05-01 02:52:58 -05:00
return $repository->getJournalsInRange($this->accounts, $this->startDate, $this->endDate);
2016-02-23 09:02:44 -06:00
}
/**
* Collects a list of rule processors, one for each rule within the rule group
2016-02-23 09:02:44 -06:00
*
* @return array
*/
2016-02-23 09:02:44 -06:00
protected function collectProcessors()
{
// 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-23 09:02:44 -06:00
// Create a list of processors for these rules
return array_map(
function ($rule) {
return Processor::make($rule);
}, $rules->all()
);
}
}