mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-14 01:13:37 -06:00
158 lines
3.8 KiB
PHP
158 lines
3.8 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace FireflyIII\Jobs;
|
||
|
|
||
|
use Carbon\Carbon;
|
||
|
use FireflyIII\Jobs\Job;
|
||
|
use FireflyIII\User;
|
||
|
use Illuminate\Queue\SerializesModels;
|
||
|
use Illuminate\Queue\InteractsWithQueue;
|
||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
use Log;
|
||
|
|
||
|
class ExecuteRuleGroupOnExistingTransaction extends Job implements ShouldQueue
|
||
|
{
|
||
|
use InteractsWithQueue, SerializesModels;
|
||
|
|
||
|
/** @var RuleGroup */
|
||
|
private $ruleGroup;
|
||
|
/** @var Collection */
|
||
|
private $accounts;
|
||
|
/** @var Carbon */
|
||
|
private $endDate;
|
||
|
/** @var Carbon */
|
||
|
private $startDate;
|
||
|
/** @var User */
|
||
|
private $user;
|
||
|
|
||
|
/**
|
||
|
* Create a new job instance.
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function __construct(RuleGroup $ruleGroup)
|
||
|
{
|
||
|
$this->ruleGroup = $ruleGroup;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Execute the job.
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
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($journals as $journal) {
|
||
|
Log::debug('Processing rulegroup for journal ' . $journal->id . ' (' . $journal->description . ')');
|
||
|
|
||
|
/** @var Processor $processor */
|
||
|
foreach ($processors as $processor) {
|
||
|
$processor->handleTransactionJournal($journal);
|
||
|
|
||
|
// Stop processing this group if the rule specifies 'stop_processing'
|
||
|
if ($processor->getRule()->stop_processing) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Collect all journals that should be processed
|
||
|
* @return Collection
|
||
|
*/
|
||
|
protected function collectJournals()
|
||
|
{
|
||
|
$args = [$this->accounts, $this->user, $this->startDate, $this->endDate];
|
||
|
$journalCollector = app('FireflyIII\Repositories\Journal\JournalCollector', $args);
|
||
|
return $journalCollector->collect();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Collects a list of rule processors, one for each rule within the rule group
|
||
|
* @return array
|
||
|
*/
|
||
|
protected function collectProcessors() {
|
||
|
// 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 ) {
|
||
|
return Processor::make($rule);
|
||
|
}, $rules->all());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return User
|
||
|
*/
|
||
|
public function getUser() {
|
||
|
return $this->user;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param User $user
|
||
|
*/
|
||
|
public function setUser(User $user) {
|
||
|
$this->user = $user;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return Collection
|
||
|
*/
|
||
|
public function getAccounts() {
|
||
|
return $this->accounts;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param Carbon $user
|
||
|
*/
|
||
|
public function setAccounts(Collection $accounts) {
|
||
|
$this->accounts = $accounts;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return \Carbon\Carbon
|
||
|
*/
|
||
|
public function getStartDate() {
|
||
|
return $this->startDate;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param Carbon $date
|
||
|
*/
|
||
|
public function setStartDate(Carbon $date) {
|
||
|
$this->startDate = $date;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return \Carbon\Carbon
|
||
|
*/
|
||
|
public function getEndDate() {
|
||
|
return $this->endDate;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param Carbon $date
|
||
|
*/
|
||
|
public function setEndDate(Carbon $date) {
|
||
|
$this->endDate = $date;
|
||
|
}
|
||
|
|
||
|
}
|