mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Code cleanup
This commit is contained in:
@@ -129,35 +129,6 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
app('preferences')->mark();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*/
|
||||
public function setDate(Carbon $date): void
|
||||
{
|
||||
$date->startOfDay();
|
||||
$this->date = $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $force
|
||||
*/
|
||||
public function setForce(bool $force): void
|
||||
{
|
||||
$this->force = $force;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return recurring transaction is active.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function active(Recurrence $recurrence): bool
|
||||
{
|
||||
return $recurrence->active;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $recurrences
|
||||
*
|
||||
@@ -172,238 +143,6 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the start date of a recurrence.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
private function getStartDate(Recurrence $recurrence): Carbon
|
||||
{
|
||||
$startDate = clone $recurrence->first_date;
|
||||
if (null !== $recurrence->latest_date && $recurrence->latest_date->gte($startDate)) {
|
||||
$startDate = clone $recurrence->latest_date;
|
||||
}
|
||||
|
||||
return $startDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transaction information from a recurring transaction.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
*/
|
||||
private function getTransactionData(Recurrence $recurrence, RecurrenceRepetition $repetition, Carbon $date): array
|
||||
{
|
||||
// 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' => $recurrence->recurrenceTransactions()->first()->description,
|
||||
'amount' => $transaction->amount,
|
||||
'budget_id' => $this->repository->getBudget($transaction),
|
||||
'budget_name' => null,
|
||||
'category_id' => null,
|
||||
'category_name' => $this->repository->getCategory($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' => null,
|
||||
'bill_name' => null,
|
||||
'recurrence_total' => $total,
|
||||
'recurrence_count' => $count,
|
||||
];
|
||||
$return[] = $single;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return TransactionGroup|null
|
||||
*/
|
||||
private function handleOccurrence(Recurrence $recurrence, RecurrenceRepetition $repetition, Carbon $date): ?TransactionGroup
|
||||
{
|
||||
$date->startOfDay();
|
||||
if ($date->ne($this->date)) {
|
||||
|
||||
return null;
|
||||
}
|
||||
Log::debug(sprintf('%s IS today (%s)', $date->format('Y-m-d'), $this->date->format('Y-m-d')));
|
||||
|
||||
// count created journals on THIS day.
|
||||
$journalCount = $this->repository->getJournalCount($recurrence, $date, $date);
|
||||
if ($journalCount > 0 && false === $this->force) {
|
||||
Log::info(sprintf('Already created %d journal(s) for date %s', $journalCount, $date->format('Y-m-d')));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
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.
|
||||
$groupTitle = null;
|
||||
if ($recurrence->recurrenceTransactions->count() > 1) {
|
||||
/** @var RecurrenceTransaction $first */
|
||||
// @codeCoverageIgnoreStart
|
||||
$first = $recurrence->recurrenceTransactions()->first();
|
||||
$groupTitle = $first->description;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
$array = [
|
||||
'user' => $recurrence->user_id,
|
||||
'group_title' => $groupTitle,
|
||||
'transactions' => $this->getTransactionData($recurrence, $repetition, $date),
|
||||
];
|
||||
/** @var TransactionGroup $group */
|
||||
$group = $this->groupRepository->store($array);
|
||||
$this->created++;
|
||||
Log::info(sprintf('Created new transaction group #%d', $group->id));
|
||||
|
||||
// trigger event:
|
||||
event(new StoredTransactionGroup($group, $recurrence->apply_rules));
|
||||
|
||||
// update recurring thing:
|
||||
$recurrence->latest_date = $date;
|
||||
$recurrence->save();
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the occurences should be executed.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param array $occurrences
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Separate method that will loop all repetitions and do something with it. Will return
|
||||
* all created transaction journals.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the recurrence fired today.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasFiredToday(Recurrence $recurrence): bool
|
||||
{
|
||||
return null !== $recurrence->latest_date && $recurrence->latest_date->eq($this->date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the reuccrence started yet.
|
||||
*
|
||||
* @param $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasNotStartedYet(Recurrence $recurrence): bool
|
||||
{
|
||||
$startDate = $this->getStartDate($recurrence);
|
||||
|
||||
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?
|
||||
*
|
||||
@@ -467,4 +206,264 @@ class CreateRecurringTransactions implements ShouldQueue
|
||||
|
||||
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 reuccrence started yet.
|
||||
*
|
||||
* @param $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasNotStartedYet(Recurrence $recurrence): bool
|
||||
{
|
||||
$startDate = $this->getStartDate($recurrence);
|
||||
|
||||
return $startDate->gt($this->date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the start date of a recurrence.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
private function getStartDate(Recurrence $recurrence): Carbon
|
||||
{
|
||||
$startDate = clone $recurrence->first_date;
|
||||
if (null !== $recurrence->latest_date && $recurrence->latest_date->gte($startDate)) {
|
||||
$startDate = clone $recurrence->latest_date;
|
||||
}
|
||||
|
||||
return $startDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the recurrence fired today.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasFiredToday(Recurrence $recurrence): bool
|
||||
{
|
||||
return null !== $recurrence->latest_date && $recurrence->latest_date->eq($this->date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Separate method that will loop all repetitions and do something with it. Will return
|
||||
* all created transaction journals.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
private function handleOccurrence(Recurrence $recurrence, RecurrenceRepetition $repetition, Carbon $date): ?TransactionGroup
|
||||
{
|
||||
$date->startOfDay();
|
||||
if ($date->ne($this->date)) {
|
||||
|
||||
return null;
|
||||
}
|
||||
Log::debug(sprintf('%s IS today (%s)', $date->format('Y-m-d'), $this->date->format('Y-m-d')));
|
||||
|
||||
// count created journals on THIS day.
|
||||
$journalCount = $this->repository->getJournalCount($recurrence, $date, $date);
|
||||
if ($journalCount > 0 && false === $this->force) {
|
||||
Log::info(sprintf('Already created %d journal(s) for date %s', $journalCount, $date->format('Y-m-d')));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
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.
|
||||
$groupTitle = null;
|
||||
if ($recurrence->recurrenceTransactions->count() > 1) {
|
||||
/** @var RecurrenceTransaction $first */
|
||||
// @codeCoverageIgnoreStart
|
||||
$first = $recurrence->recurrenceTransactions()->first();
|
||||
$groupTitle = $first->description;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
$array = [
|
||||
'user' => $recurrence->user_id,
|
||||
'group_title' => $groupTitle,
|
||||
'transactions' => $this->getTransactionData($recurrence, $repetition, $date),
|
||||
];
|
||||
/** @var TransactionGroup $group */
|
||||
$group = $this->groupRepository->store($array);
|
||||
$this->created++;
|
||||
Log::info(sprintf('Created new transaction group #%d', $group->id));
|
||||
|
||||
// trigger event:
|
||||
event(new StoredTransactionGroup($group, $recurrence->apply_rules));
|
||||
|
||||
// update recurring thing:
|
||||
$recurrence->latest_date = $date;
|
||||
$recurrence->save();
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transaction information from a recurring transaction.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
*/
|
||||
private function getTransactionData(Recurrence $recurrence, RecurrenceRepetition $repetition, Carbon $date): array
|
||||
{
|
||||
// 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' => $recurrence->recurrenceTransactions()->first()->description,
|
||||
'amount' => $transaction->amount,
|
||||
'budget_id' => $this->repository->getBudget($transaction),
|
||||
'budget_name' => null,
|
||||
'category_id' => null,
|
||||
'category_name' => $this->repository->getCategory($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' => null,
|
||||
'bill_name' => null,
|
||||
'recurrence_total' => $total,
|
||||
'recurrence_count' => $count,
|
||||
];
|
||||
$return[] = $single;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*/
|
||||
public function setDate(Carbon $date): void
|
||||
{
|
||||
$date->startOfDay();
|
||||
$this->date = $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $force
|
||||
*/
|
||||
public function setForce(bool $force): void
|
||||
{
|
||||
$this->force = $force;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user