mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 10:53:37 +00:00
chore: reformat code.
This commit is contained in:
@@ -70,7 +70,7 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
* Create a new job instance.
|
||||
*
|
||||
*
|
||||
* @param Carbon|null $date
|
||||
* @param Carbon|null $date
|
||||
*/
|
||||
public function __construct(?Carbon $date)
|
||||
{
|
||||
@@ -160,45 +160,7 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*/
|
||||
public function setDate(Carbon $date): void
|
||||
{
|
||||
$newDate = clone $date;
|
||||
$newDate->startOfDay();
|
||||
$this->date = $newDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $force
|
||||
*/
|
||||
public function setForce(bool $force): void
|
||||
{
|
||||
$this->force = $force;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $recurrences
|
||||
*/
|
||||
public function setRecurrences(Collection $recurrences): void
|
||||
{
|
||||
$this->recurrences = $recurrences;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return recurring transaction is active.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function active(Recurrence $recurrence): bool
|
||||
{
|
||||
return $recurrence->active;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $recurrences
|
||||
* @param Collection $recurrences
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
@@ -211,10 +173,115 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the info in the recurrence valid?
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
*/
|
||||
private function validRecurrence(Recurrence $recurrence): bool
|
||||
{
|
||||
Log::debug(sprintf('Now filtering recurrence #%d, owned by user #%d', $recurrence->id, $recurrence->user_id));
|
||||
// is not active.
|
||||
if (!$this->active($recurrence)) {
|
||||
Log::info(sprintf('Recurrence #%d is not active. Skipped.', $recurrence->id));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// has repeated X times.
|
||||
$journalCount = $this->repository->getJournalCount($recurrence);
|
||||
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(
|
||||
sprintf(
|
||||
'Recurrence #%d was set to run until %s, and today\'s date is %s. Skipped.',
|
||||
$recurrence->id,
|
||||
$recurrence->repeat_until->format('Y-m-d'),
|
||||
$this->date->format('Y-m-d')
|
||||
)
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// first_date is in the future
|
||||
if ($this->hasNotStartedYet($recurrence)) {
|
||||
Log::info(
|
||||
sprintf(
|
||||
'Recurrence #%d is set to run on %s, and today\'s date is %s. Skipped.',
|
||||
$recurrence->id,
|
||||
$recurrence->first_date->format('Y-m-d'),
|
||||
$this->date->format('Y-m-d')
|
||||
)
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// already fired today (with success):
|
||||
if (false === $this->force && $this->hasFiredToday($recurrence)) {
|
||||
Log::info(sprintf('Recurrence #%d has already fired today. Skipped.', $recurrence->id));
|
||||
|
||||
return false;
|
||||
}
|
||||
Log::debug('Will be included.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return recurring transaction is active.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function active(Recurrence $recurrence): bool
|
||||
{
|
||||
return $recurrence->active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the $repeat_until date is in the past.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function repeatUntilHasPassed(Recurrence $recurrence): bool
|
||||
{
|
||||
// date has passed
|
||||
return null !== $recurrence->repeat_until && $recurrence->repeat_until->lt($this->date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the recurrence started yet?
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasNotStartedYet(Recurrence $recurrence): bool
|
||||
{
|
||||
$startDate = $this->getStartDate($recurrence);
|
||||
Log::debug(sprintf('Start date is %s', $startDate->format('Y-m-d')));
|
||||
|
||||
return $startDate->gt($this->date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the start date of a recurrence.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
@@ -229,67 +296,85 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transaction information from a recurring transaction.
|
||||
* Has the recurrence fired today.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return array
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function getTransactionData(Recurrence $recurrence, RecurrenceRepetition $repetition, Carbon $date): array
|
||||
private function hasFiredToday(Recurrence $recurrence): bool
|
||||
{
|
||||
// total transactions expected for this recurrence:
|
||||
$total = $this->repository->totalTransactions($recurrence, $repetition);
|
||||
$count = $this->repository->getJournalCount($recurrence) + 1;
|
||||
$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' => $transactions->first()->description,
|
||||
'amount' => $transaction->amount,
|
||||
'budget_id' => $this->repository->getBudget($transaction),
|
||||
'budget_name' => null,
|
||||
'category_id' => $this->repository->getCategoryId($transaction),
|
||||
'category_name' => $this->repository->getCategoryName($transaction),
|
||||
'source_id' => $transaction->source_id,
|
||||
'source_name' => null,
|
||||
'destination_id' => $transaction->destination_id,
|
||||
'destination_name' => null,
|
||||
'foreign_currency_id' => $transaction->foreign_currency_id,
|
||||
'foreign_currency_code' => null,
|
||||
'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($transaction),
|
||||
'piggy_bank_id' => $this->repository->getPiggyBank($transaction),
|
||||
'piggy_bank_name' => null,
|
||||
'bill_id' => $this->repository->getBillId($transaction),
|
||||
'bill_name' => null,
|
||||
'recurrence_total' => $total,
|
||||
'recurrence_count' => $count,
|
||||
'recurrence_date' => $date,
|
||||
];
|
||||
$return[] = $single;
|
||||
}
|
||||
|
||||
return $return;
|
||||
return null !== $recurrence->latest_date && $recurrence->latest_date->eq($this->date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param Carbon $date
|
||||
* Separate method that will loop all repetitions and do something with it. Will return
|
||||
* all created transaction journals.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return Collection
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function handleRepetitions(Recurrence $recurrence): Collection
|
||||
{
|
||||
$collection = new Collection();
|
||||
/** @var RecurrenceRepetition $repetition */
|
||||
foreach ($recurrence->recurrenceRepetitions as $repetition) {
|
||||
Log::debug(
|
||||
sprintf(
|
||||
'Now repeating %s with value "%s", skips every %d time(s)',
|
||||
$repetition->repetition_type,
|
||||
$repetition->repetition_moment,
|
||||
$repetition->repetition_skip
|
||||
)
|
||||
);
|
||||
|
||||
// start looping from $startDate to today perhaps we have a hit?
|
||||
// add two days to $this->date, so we always include the weekend.
|
||||
$includeWeekend = clone $this->date;
|
||||
$includeWeekend->addDays(2);
|
||||
$occurrences = $this->repository->getOccurrencesInRange($repetition, $recurrence->first_date, $includeWeekend);
|
||||
|
||||
unset($includeWeekend);
|
||||
|
||||
$result = $this->handleOccurrences($recurrence, $repetition, $occurrences);
|
||||
$collection = $collection->merge($result);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the occurences should be executed.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param array $occurrences
|
||||
*
|
||||
* @return Collection
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function handleOccurrences(Recurrence $recurrence, RecurrenceRepetition $repetition, array $occurrences): Collection
|
||||
{
|
||||
$collection = new Collection();
|
||||
/** @var Carbon $date */
|
||||
foreach ($occurrences as $date) {
|
||||
$result = $this->handleOccurrence($recurrence, $repetition, $date);
|
||||
if (null !== $result) {
|
||||
$collection->push($result);
|
||||
}
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return TransactionGroup|null
|
||||
* @throws DuplicateTransactionException
|
||||
@@ -360,171 +445,86 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the occurences should be executed.
|
||||
* Get transaction information from a recurring transaction.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param array $occurrences
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @return Collection
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function handleOccurrences(Recurrence $recurrence, RecurrenceRepetition $repetition, array $occurrences): Collection
|
||||
private function getTransactionData(Recurrence $recurrence, RecurrenceRepetition $repetition, Carbon $date): array
|
||||
{
|
||||
$collection = new Collection();
|
||||
/** @var Carbon $date */
|
||||
foreach ($occurrences as $date) {
|
||||
$result = $this->handleOccurrence($recurrence, $repetition, $date);
|
||||
if (null !== $result) {
|
||||
$collection->push($result);
|
||||
}
|
||||
// total transactions expected for this recurrence:
|
||||
$total = $this->repository->totalTransactions($recurrence, $repetition);
|
||||
$count = $this->repository->getJournalCount($recurrence) + 1;
|
||||
$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' => $transactions->first()->description,
|
||||
'amount' => $transaction->amount,
|
||||
'budget_id' => $this->repository->getBudget($transaction),
|
||||
'budget_name' => null,
|
||||
'category_id' => $this->repository->getCategoryId($transaction),
|
||||
'category_name' => $this->repository->getCategoryName($transaction),
|
||||
'source_id' => $transaction->source_id,
|
||||
'source_name' => null,
|
||||
'destination_id' => $transaction->destination_id,
|
||||
'destination_name' => null,
|
||||
'foreign_currency_id' => $transaction->foreign_currency_id,
|
||||
'foreign_currency_code' => null,
|
||||
'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($transaction),
|
||||
'piggy_bank_id' => $this->repository->getPiggyBank($transaction),
|
||||
'piggy_bank_name' => null,
|
||||
'bill_id' => $this->repository->getBillId($transaction),
|
||||
'bill_name' => null,
|
||||
'recurrence_total' => $total,
|
||||
'recurrence_count' => $count,
|
||||
'recurrence_date' => $date,
|
||||
];
|
||||
$return[] = $single;
|
||||
}
|
||||
|
||||
return $collection;
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Separate method that will loop all repetitions and do something with it. Will return
|
||||
* all created transaction journals.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return Collection
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws FireflyException
|
||||
* @param Carbon $date
|
||||
*/
|
||||
private function handleRepetitions(Recurrence $recurrence): Collection
|
||||
public function setDate(Carbon $date): void
|
||||
{
|
||||
$collection = new Collection();
|
||||
/** @var RecurrenceRepetition $repetition */
|
||||
foreach ($recurrence->recurrenceRepetitions as $repetition) {
|
||||
Log::debug(
|
||||
sprintf(
|
||||
'Now repeating %s with value "%s", skips every %d time(s)',
|
||||
$repetition->repetition_type,
|
||||
$repetition->repetition_moment,
|
||||
$repetition->repetition_skip
|
||||
)
|
||||
);
|
||||
|
||||
// start looping from $startDate to today perhaps we have a hit?
|
||||
// add two days to $this->date, so we always include the weekend.
|
||||
$includeWeekend = clone $this->date;
|
||||
$includeWeekend->addDays(2);
|
||||
$occurrences = $this->repository->getOccurrencesInRange($repetition, $recurrence->first_date, $includeWeekend);
|
||||
|
||||
unset($includeWeekend);
|
||||
|
||||
$result = $this->handleOccurrences($recurrence, $repetition, $occurrences);
|
||||
$collection = $collection->merge($result);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
$newDate = clone $date;
|
||||
$newDate->startOfDay();
|
||||
$this->date = $newDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the recurrence fired today.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
* @param bool $force
|
||||
*/
|
||||
private function hasFiredToday(Recurrence $recurrence): bool
|
||||
public function setForce(bool $force): void
|
||||
{
|
||||
return null !== $recurrence->latest_date && $recurrence->latest_date->eq($this->date);
|
||||
$this->force = $force;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the recurrence started yet?
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
* @param Collection $recurrences
|
||||
*/
|
||||
private function hasNotStartedYet(Recurrence $recurrence): bool
|
||||
public function setRecurrences(Collection $recurrences): void
|
||||
{
|
||||
$startDate = $this->getStartDate($recurrence);
|
||||
Log::debug(sprintf('Start date is %s', $startDate->format('Y-m-d')));
|
||||
|
||||
return $startDate->gt($this->date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the $repeat_until date is in the past.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function repeatUntilHasPassed(Recurrence $recurrence): bool
|
||||
{
|
||||
// date has passed
|
||||
return null !== $recurrence->repeat_until && $recurrence->repeat_until->lt($this->date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the info in the recurrence valid?
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
*/
|
||||
private function validRecurrence(Recurrence $recurrence): bool
|
||||
{
|
||||
Log::debug(sprintf('Now filtering recurrence #%d, owned by user #%d', $recurrence->id, $recurrence->user_id));
|
||||
// is not active.
|
||||
if (!$this->active($recurrence)) {
|
||||
Log::info(sprintf('Recurrence #%d is not active. Skipped.', $recurrence->id));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// has repeated X times.
|
||||
$journalCount = $this->repository->getJournalCount($recurrence);
|
||||
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(
|
||||
sprintf(
|
||||
'Recurrence #%d was set to run until %s, and today\'s date is %s. Skipped.',
|
||||
$recurrence->id,
|
||||
$recurrence->repeat_until->format('Y-m-d'),
|
||||
$this->date->format('Y-m-d')
|
||||
)
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// first_date is in the future
|
||||
if ($this->hasNotStartedYet($recurrence)) {
|
||||
Log::info(
|
||||
sprintf(
|
||||
'Recurrence #%d is set to run on %s, and today\'s date is %s. Skipped.',
|
||||
$recurrence->id,
|
||||
$recurrence->first_date->format('Y-m-d'),
|
||||
$this->date->format('Y-m-d')
|
||||
)
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// already fired today (with success):
|
||||
if (false === $this->force && $this->hasFiredToday($recurrence)) {
|
||||
Log::info(sprintf('Recurrence #%d has already fired today. Skipped.', $recurrence->id));
|
||||
|
||||
return false;
|
||||
}
|
||||
Log::debug('Will be included.');
|
||||
|
||||
return true;
|
||||
$this->recurrences = $recurrences;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user