. */ declare(strict_types=1); namespace FireflyIII\Jobs; use Carbon\Carbon; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Models\Rule; use FireflyIII\TransactionRules\Processor; use FireflyIII\User; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Collection; use Log; /** * Class ExecuteRuleOnExistingTransactions. */ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue { use InteractsWithQueue, SerializesModels; /** @var Collection The accounts */ private $accounts; /** @var Carbon The end date */ private $endDate; /** @var Rule The current rule */ private $rule; /** @var Carbon The start date */ private $startDate; /** @var User The user */ private $user; /** * Create a new job instance. * * @param Rule $rule */ public function __construct(Rule $rule) { $this->rule = $rule; } /** * Get accounts. * * @return Collection */ public function getAccounts(): Collection { return $this->accounts; } /** * Set accounts. * * @param Collection $accounts */ public function setAccounts(Collection $accounts) { $this->accounts = $accounts; } /** * Get end date. * * @return \Carbon\Carbon */ public function getEndDate(): Carbon { return $this->endDate; } /** * Set end date. * * @param Carbon $date */ public function setEndDate(Carbon $date) { $this->endDate = $date; } /** * Get rule. * * @return Rule */ public function getRule(): Rule { return $this->rule; } /** * Get start date. * * @return \Carbon\Carbon */ public function getStartDate(): Carbon { return $this->startDate; } /** * Set start date. * * @param Carbon $date */ public function setStartDate(Carbon $date) { $this->startDate = $date; } /** * Get user. * * @return User */ public function getUser(): User { return $this->user; } /** * Set user. * * @param User $user */ public function setUser(User $user) { $this->user = $user; } /** * Execute the job. * * @throws \FireflyIII\Exceptions\FireflyException * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function handle() { // Lookup all journals that match the parameters specified $transactions = $this->collectJournals(); /** @var Processor $processor */ $processor = app(Processor::class); $processor->make($this->rule, true); $hits = 0; $misses = 0; $total = 0; // Execute the rules for each transaction foreach ($transactions as $transaction) { ++$total; $result = $processor->handleTransaction($transaction); if ($result) { ++$hits; } if (!$result) { ++$misses; } Log::info(sprintf('Current progress: %d Transactions. Hits: %d, misses: %d', $total, $hits, $misses)); } Log::info(sprintf('Total transactions: %d. Hits: %d, misses: %d', $total, $hits, $misses)); } /** * Collect all journals that should be processed. * * @return Collection */ protected function collectJournals(): Collection { /** @var TransactionCollectorInterface $collector */ $collector = app(TransactionCollectorInterface::class); $collector->setUser($this->user); $collector->setAccounts($this->accounts)->setRange($this->startDate, $this->endDate); return $collector->getTransactions(); } }