mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-20 16:40:10 +00:00
Clean up repositories and cron code.
This commit is contained in:
@@ -56,10 +56,13 @@ use FireflyIII\Models\RecurrenceMeta;
|
||||
use FireflyIII\Models\RecurrenceRepetition;
|
||||
use FireflyIII\Models\RecurrenceTransaction;
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
|
||||
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
|
||||
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Engine\RuleEngine;
|
||||
use FireflyIII\TransactionRules\Processor;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Bus\Queueable;
|
||||
@@ -83,10 +86,14 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
private $date;
|
||||
/** @var JournalRepositoryInterface Journal repository */
|
||||
private $journalRepository;
|
||||
/** @var TransactionGroupRepositoryInterface */
|
||||
private $groupRepository;
|
||||
/** @var RecurringRepositoryInterface Recurring transactions repository. */
|
||||
private $repository;
|
||||
/** @var array The users rules. */
|
||||
private $rules = [];
|
||||
/** @var bool Force the transaction to be created no matter what. */
|
||||
private $force;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
@@ -99,13 +106,23 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
$this->date = $date;
|
||||
$this->repository = app(RecurringRepositoryInterface::class);
|
||||
$this->journalRepository = app(JournalRepositoryInterface::class);
|
||||
$this->groupRepository = app(TransactionGroupRepositoryInterface::class);
|
||||
$this->force = false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $force
|
||||
*/
|
||||
public function setForce(bool $force): void
|
||||
{
|
||||
$this->force = $force;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
@@ -118,7 +135,6 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
$filtered = $recurrences->filter(
|
||||
function (Recurrence $recurrence) {
|
||||
return $this->validRecurrence($recurrence);
|
||||
|
||||
}
|
||||
);
|
||||
Log::debug(sprintf('Left after filtering is %d', $filtered->count()));
|
||||
@@ -129,13 +145,11 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
}
|
||||
$this->repository->setUser($recurrence->user);
|
||||
$this->journalRepository->setUser($recurrence->user);
|
||||
$this->groupRepository->setUser($recurrence->user);
|
||||
Log::debug(sprintf('Now at recurrence #%d', $recurrence->id));
|
||||
$created = $this->handleRepetitions($recurrence);
|
||||
Log::debug(sprintf('Done with recurrence #%d', $recurrence->id));
|
||||
$result[$recurrence->user_id] = $result[$recurrence->user_id]->merge($created);
|
||||
|
||||
// apply rules:
|
||||
$this->applyRules($recurrence->user, $created);
|
||||
}
|
||||
|
||||
Log::debug('Now running report thing.');
|
||||
@@ -165,33 +179,29 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
/**
|
||||
* Apply the users rules to newly created journals.
|
||||
*
|
||||
* @param User $user
|
||||
* @param Collection $journals
|
||||
* @param User $user
|
||||
* @param Collection $groups
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
private function applyRules(User $user, Collection $journals): void
|
||||
private function applyRules(User $user, Collection $groups): void
|
||||
{
|
||||
$userId = $user->id;
|
||||
if (!isset($this->rules[$userId])) {
|
||||
$this->rules[$userId] = $this->getRules($user);
|
||||
}
|
||||
|
||||
/** @var RuleEngine $ruleEngine */
|
||||
$ruleEngine = app(RuleEngine::class);
|
||||
$ruleEngine->setUser($user);
|
||||
$ruleEngine->setAllRules(true);
|
||||
|
||||
// run the rules:
|
||||
if ($this->rules[$userId]->count() > 0) {
|
||||
/** @var TransactionGroup $group */
|
||||
foreach ($groups as $group) {
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
$this->rules[$userId]->each(
|
||||
function (Rule $rule) use ($journal) {
|
||||
Log::debug(sprintf('Going to apply rule #%d to journal %d.', $rule->id, $journal->id));
|
||||
/** @var Processor $processor */
|
||||
$processor = app(Processor::class);
|
||||
$processor->make($rule);
|
||||
$processor->handleTransactionJournal($journal);
|
||||
if ($rule->stop_processing) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
foreach ($group->transactionJournals as $journal) {
|
||||
//$ruleEngine->processTransactionJournal($journal);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -232,7 +242,7 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the users rules.
|
||||
* Get the users rule groups.
|
||||
*
|
||||
* @param User $user
|
||||
*
|
||||
@@ -271,19 +281,23 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
* Get transaction information from a recurring transaction.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getTransactionData(Recurrence $recurrence): array
|
||||
private function getTransactionData(Recurrence $recurrence, Carbon $date): array
|
||||
{
|
||||
$transactions = $recurrence->recurrenceTransactions()->get();
|
||||
$return = [];
|
||||
/** @var RecurrenceTransaction $transaction */
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
$single = [
|
||||
'type' => strtolower($recurrence->transactionType->type),
|
||||
'date' => $date,
|
||||
'user' => $recurrence->user_id,
|
||||
'currency_id' => (int)$transaction->transaction_currency_id,
|
||||
'currency_code' => null,
|
||||
'description' => null,
|
||||
'description' => $recurrence->recurrenceTransactions()->first()->description,
|
||||
'amount' => $transaction->amount,
|
||||
'budget_id' => $this->repository->getBudget($transaction),
|
||||
'budget_name' => null,
|
||||
@@ -298,6 +312,14 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
'foreign_amount' => $transaction->foreign_amount,
|
||||
'reconciled' => false,
|
||||
'identifier' => $index,
|
||||
'recurrence_id' => (int)$recurrence->id,
|
||||
'order' => $index,
|
||||
'notes' => (string)trans('firefly.created_from_recurrence', ['id' => $recurrence->id, 'title' => $recurrence->title]),
|
||||
'tags' => $this->repository->getTags($recurrence),
|
||||
'piggy_bank_id' => null,
|
||||
'piggy_bank_name' => null,
|
||||
'bill_id' => null,
|
||||
'bill_name' => null,
|
||||
];
|
||||
$return[] = $single;
|
||||
}
|
||||
@@ -309,7 +331,7 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
* Check if the occurences should be executed.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $occurrences
|
||||
* @param array $occurrences
|
||||
*
|
||||
* @return Collection
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
@@ -318,7 +340,6 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
*/
|
||||
private function handleOccurrences(Recurrence $recurrence, array $occurrences): Collection
|
||||
{
|
||||
throw new FireflyException('Needs refactor');
|
||||
$collection = new Collection;
|
||||
/** @var Carbon $date */
|
||||
foreach ($occurrences as $date) {
|
||||
@@ -332,55 +353,58 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
|
||||
// count created journals on THIS day.
|
||||
$journalCount = $this->repository->getJournalCount($recurrence, $date, $date);
|
||||
if ($journalCount > 0) {
|
||||
if ($journalCount > 0 && false === $this->force) {
|
||||
Log::info(sprintf('Already created %d journal(s) for date %s', $journalCount, $date->format('Y-m-d')));
|
||||
continue;
|
||||
}
|
||||
if ($journalCount > 0 && true === $this->force) {
|
||||
Log::warning(sprintf('Already created %d groups for date %s but FORCED to continue.', $journalCount, $date->format('Y-m-d')));
|
||||
}
|
||||
|
||||
// create transaction array and send to factory.
|
||||
$array = [
|
||||
'type' => $recurrence->transactionType->type,
|
||||
'date' => $date,
|
||||
'tags' => $this->repository->getTags($recurrence),
|
||||
'user' => $recurrence->user_id,
|
||||
'notes' => (string)trans('firefly.created_from_recurrence', ['id' => $recurrence->id, 'title' => $recurrence->title]),
|
||||
// journal data:
|
||||
'description' => $recurrence->recurrenceTransactions()->first()->description,
|
||||
'piggy_bank_id' => null,
|
||||
'piggy_bank_name' => null,
|
||||
'bill_id' => null,
|
||||
'bill_name' => null,
|
||||
'recurrence_id' => (int)$recurrence->id,
|
||||
// transaction data:
|
||||
'transactions' => $this->getTransactionData($recurrence),
|
||||
$groupTitle = null;
|
||||
if ($recurrence->recurrenceTransactions->count() > 0) {
|
||||
/** @var RecurrenceTransaction $first */
|
||||
$first = $recurrence->recurrenceTransactions()->first();
|
||||
$groupTitle = $first->description;
|
||||
}
|
||||
$array = [
|
||||
'user' => $recurrence->user_id,
|
||||
'group_title' => $groupTitle,
|
||||
'transactions' => $this->getTransactionData($recurrence, $date),
|
||||
];
|
||||
$journal = $this->journalRepository->store($array);
|
||||
Log::info(sprintf('Created new journal #%d', $journal->id));
|
||||
/** @var TransactionGroup $group */
|
||||
$group = $this->groupRepository->store($array);
|
||||
Log::info(sprintf('Created new transaction group #%d', $group->id));
|
||||
|
||||
// get piggy bank ID from meta data:
|
||||
$piggyBankId = $this->getPiggyId($recurrence);
|
||||
Log::debug(sprintf('Piggy bank ID for recurrence #%d is #%d', $recurrence->id, $piggyBankId));
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($group->transactionJournals as $journal) {
|
||||
// get piggy bank ID from meta data:
|
||||
$piggyBankId = $this->getPiggyId($recurrence);
|
||||
Log::debug(sprintf('Piggy bank ID for recurrence #%d is #%d', $recurrence->id, $piggyBankId));
|
||||
|
||||
// trigger event:
|
||||
event(new StoredTransactionGroup($journal));
|
||||
// link to piggy bank:
|
||||
/** @var PiggyBankFactory $factory */
|
||||
$factory = app(PiggyBankFactory::class);
|
||||
$factory->setUser($recurrence->user);
|
||||
|
||||
// link to piggy bank:
|
||||
/** @var PiggyBankFactory $factory */
|
||||
$factory = app(PiggyBankFactory::class);
|
||||
$factory->setUser($recurrence->user);
|
||||
$piggyBank = $factory->find($piggyBankId, null);
|
||||
if (null !== $piggyBank) {
|
||||
/** @var PiggyBankEventFactory $factory */
|
||||
$factory = app(PiggyBankEventFactory::class);
|
||||
$factory->create($journal, $piggyBank);
|
||||
}
|
||||
|
||||
$piggyBank = $factory->find($piggyBankId, null);
|
||||
if (null !== $piggyBank) {
|
||||
/** @var PiggyBankEventFactory $factory */
|
||||
$factory = app(PiggyBankEventFactory::class);
|
||||
$factory->create($journal, $piggyBank);
|
||||
}
|
||||
|
||||
|
||||
$collection->push($journal);
|
||||
// trigger event:
|
||||
event(new StoredTransactionGroup($group, $recurrence->apply_rules));
|
||||
|
||||
// update recurring thing:
|
||||
$recurrence->latest_date = $date;
|
||||
$recurrence->save();
|
||||
$collection->push($group);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
@@ -489,12 +513,13 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
|
||||
// has repeated X times.
|
||||
$journalCount = $this->repository->getJournalCount($recurrence);
|
||||
if (0 !== $recurrence->repetitions && $journalCount >= $recurrence->repetitions) {
|
||||
if (0 !== $recurrence->repetitions && $journalCount >= $recurrence->repetitions && false === $this->force) {
|
||||
Log::info(sprintf('Recurrence #%d has run %d times, so will run no longer.', $recurrence->id, $recurrence->repetitions));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// is no longer running
|
||||
if ($this->repeatUntilHasPassed($recurrence)) {
|
||||
Log::info(
|
||||
@@ -524,7 +549,7 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
}
|
||||
|
||||
// already fired today (with success):
|
||||
if ($this->hasFiredToday($recurrence)) {
|
||||
if ($this->hasFiredToday($recurrence) && false === $this->force) {
|
||||
Log::info(sprintf('Recurrence #%d has already fired today. Skipped.', $recurrence->id));
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user