mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-07 06:31:22 +00:00
Code cleanup
This commit is contained in:
@@ -78,122 +78,6 @@ class CreateAutoBudgetLimits implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*/
|
||||
public function setDate(Carbon $date): void
|
||||
{
|
||||
$date->startOfDay();
|
||||
$this->date = $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AutoBudget $autoBudget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param string|null $amount
|
||||
*/
|
||||
private function createBudgetLimit(AutoBudget $autoBudget, Carbon $start, Carbon $end, ?string $amount = null)
|
||||
{
|
||||
Log::debug(sprintf('No budget limit exist. Must create one for auto-budget #%d', $autoBudget->id));
|
||||
if (null !== $amount) {
|
||||
Log::debug(sprintf('Amount is overruled and will be set to %s', $amount));
|
||||
}
|
||||
$budgetLimit = new BudgetLimit;
|
||||
$budgetLimit->budget()->associate($autoBudget->budget);
|
||||
$budgetLimit->transactionCurrency()->associate($autoBudget->transactionCurrency);
|
||||
$budgetLimit->start_date = $start;
|
||||
$budgetLimit->end_date = $end;
|
||||
$budgetLimit->amount = $amount ?? $autoBudget->amount;
|
||||
$budgetLimit->period = $autoBudget->period;
|
||||
$budgetLimit->generated = true;
|
||||
$budgetLimit->save();
|
||||
|
||||
Log::debug(sprintf('Created budget limit #%d.', $budgetLimit->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AutoBudget $autoBudget
|
||||
*/
|
||||
private function createRollover(AutoBudget $autoBudget): void
|
||||
{
|
||||
Log::debug(sprintf('Will now manage rollover for auto budget #%d', $autoBudget->id));
|
||||
// current period:
|
||||
$start = app('navigation')->startOfPeriod($this->date, $autoBudget->period);
|
||||
$end = app('navigation')->endOfPeriod($start, $autoBudget->period);
|
||||
|
||||
// which means previous period:
|
||||
$previousStart = app('navigation')->subtractPeriod($start, $autoBudget->period);
|
||||
$previousEnd = app('navigation')->endOfPeriod($previousStart, $autoBudget->period);
|
||||
|
||||
Log::debug(
|
||||
sprintf(
|
||||
'Current period is %s-%s, so previous period is %s-%s',
|
||||
$start->format('Y-m-d'),
|
||||
$end->format('Y-m-d'),
|
||||
$previousStart->format('Y-m-d'),
|
||||
$previousEnd->format('Y-m-d')
|
||||
)
|
||||
);
|
||||
|
||||
// has budget limit in previous period?
|
||||
$budgetLimit = $this->findBudgetLimit($autoBudget->budget, $previousStart, $previousEnd);
|
||||
|
||||
if (null === $budgetLimit) {
|
||||
Log::debug('No budget limit exists in previous period, so create one.');
|
||||
// if not, create it and we're done.
|
||||
$this->createBudgetLimit($autoBudget, $start, $end);
|
||||
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug('Budget limit exists for previous period.');
|
||||
// if has one, calculate expenses and use that as a base.
|
||||
$repository = app(OperationsRepositoryInterface::class);
|
||||
$repository->setUser($autoBudget->budget->user);
|
||||
$spent = $repository->sumExpenses($previousStart, $previousEnd, null, new Collection([$autoBudget->budget]), $autoBudget->transactionCurrency);
|
||||
$currencyId = (int)$autoBudget->transaction_currency_id;
|
||||
$spentAmount = $spent[$currencyId]['sum'] ?? '0';
|
||||
Log::debug(sprintf('Spent in previous budget period (%s-%s) is %s', $previousStart->format('Y-m-d'), $previousEnd->format('Y-m-d'), $spentAmount));
|
||||
|
||||
// previous budget limit + this period + spent
|
||||
$totalAmount = bcadd(bcadd($budgetLimit->amount, $autoBudget->amount), $spentAmount);
|
||||
Log::debug(sprintf('Total amount for current budget period will be %s', $totalAmount));
|
||||
|
||||
if (1 !== bccomp($totalAmount, '0')) {
|
||||
Log::info(sprintf('The total amount is negative, so it will be reset to %s.', $totalAmount));
|
||||
$totalAmount = $autoBudget->amount;
|
||||
}
|
||||
|
||||
// create budget limit:
|
||||
$this->createBudgetLimit($autoBudget, $start, $end, $totalAmount);
|
||||
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return BudgetLimit|null
|
||||
*/
|
||||
private function findBudgetLimit(Budget $budget, Carbon $start, Carbon $end): ?BudgetLimit
|
||||
{
|
||||
Log::debug(
|
||||
sprintf(
|
||||
'Going to find a budget limit for budget #%d ("%s") between %s and %s',
|
||||
$budget->id,
|
||||
$budget->name,
|
||||
$start->format('Y-m-d'),
|
||||
$end->format('Y-m-d')
|
||||
)
|
||||
);
|
||||
|
||||
return $budget->budgetlimits()
|
||||
->where('start_date', $start->format('Y-m-d'))
|
||||
->where('end_date', $end->format('Y-m-d'))->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AutoBudget $autoBudget
|
||||
*
|
||||
@@ -296,4 +180,120 @@ class CreateAutoBudgetLimits implements ShouldQueue
|
||||
return '01-01' === $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return BudgetLimit|null
|
||||
*/
|
||||
private function findBudgetLimit(Budget $budget, Carbon $start, Carbon $end): ?BudgetLimit
|
||||
{
|
||||
Log::debug(
|
||||
sprintf(
|
||||
'Going to find a budget limit for budget #%d ("%s") between %s and %s',
|
||||
$budget->id,
|
||||
$budget->name,
|
||||
$start->format('Y-m-d'),
|
||||
$end->format('Y-m-d')
|
||||
)
|
||||
);
|
||||
|
||||
return $budget->budgetlimits()
|
||||
->where('start_date', $start->format('Y-m-d'))
|
||||
->where('end_date', $end->format('Y-m-d'))->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AutoBudget $autoBudget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param string|null $amount
|
||||
*/
|
||||
private function createBudgetLimit(AutoBudget $autoBudget, Carbon $start, Carbon $end, ?string $amount = null)
|
||||
{
|
||||
Log::debug(sprintf('No budget limit exist. Must create one for auto-budget #%d', $autoBudget->id));
|
||||
if (null !== $amount) {
|
||||
Log::debug(sprintf('Amount is overruled and will be set to %s', $amount));
|
||||
}
|
||||
$budgetLimit = new BudgetLimit;
|
||||
$budgetLimit->budget()->associate($autoBudget->budget);
|
||||
$budgetLimit->transactionCurrency()->associate($autoBudget->transactionCurrency);
|
||||
$budgetLimit->start_date = $start;
|
||||
$budgetLimit->end_date = $end;
|
||||
$budgetLimit->amount = $amount ?? $autoBudget->amount;
|
||||
$budgetLimit->period = $autoBudget->period;
|
||||
$budgetLimit->generated = true;
|
||||
$budgetLimit->save();
|
||||
|
||||
Log::debug(sprintf('Created budget limit #%d.', $budgetLimit->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AutoBudget $autoBudget
|
||||
*/
|
||||
private function createRollover(AutoBudget $autoBudget): void
|
||||
{
|
||||
Log::debug(sprintf('Will now manage rollover for auto budget #%d', $autoBudget->id));
|
||||
// current period:
|
||||
$start = app('navigation')->startOfPeriod($this->date, $autoBudget->period);
|
||||
$end = app('navigation')->endOfPeriod($start, $autoBudget->period);
|
||||
|
||||
// which means previous period:
|
||||
$previousStart = app('navigation')->subtractPeriod($start, $autoBudget->period);
|
||||
$previousEnd = app('navigation')->endOfPeriod($previousStart, $autoBudget->period);
|
||||
|
||||
Log::debug(
|
||||
sprintf(
|
||||
'Current period is %s-%s, so previous period is %s-%s',
|
||||
$start->format('Y-m-d'),
|
||||
$end->format('Y-m-d'),
|
||||
$previousStart->format('Y-m-d'),
|
||||
$previousEnd->format('Y-m-d')
|
||||
)
|
||||
);
|
||||
|
||||
// has budget limit in previous period?
|
||||
$budgetLimit = $this->findBudgetLimit($autoBudget->budget, $previousStart, $previousEnd);
|
||||
|
||||
if (null === $budgetLimit) {
|
||||
Log::debug('No budget limit exists in previous period, so create one.');
|
||||
// if not, create it and we're done.
|
||||
$this->createBudgetLimit($autoBudget, $start, $end);
|
||||
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug('Budget limit exists for previous period.');
|
||||
// if has one, calculate expenses and use that as a base.
|
||||
$repository = app(OperationsRepositoryInterface::class);
|
||||
$repository->setUser($autoBudget->budget->user);
|
||||
$spent = $repository->sumExpenses($previousStart, $previousEnd, null, new Collection([$autoBudget->budget]), $autoBudget->transactionCurrency);
|
||||
$currencyId = (int)$autoBudget->transaction_currency_id;
|
||||
$spentAmount = $spent[$currencyId]['sum'] ?? '0';
|
||||
Log::debug(sprintf('Spent in previous budget period (%s-%s) is %s', $previousStart->format('Y-m-d'), $previousEnd->format('Y-m-d'), $spentAmount));
|
||||
|
||||
// previous budget limit + this period + spent
|
||||
$totalAmount = bcadd(bcadd($budgetLimit->amount, $autoBudget->amount), $spentAmount);
|
||||
Log::debug(sprintf('Total amount for current budget period will be %s', $totalAmount));
|
||||
|
||||
if (1 !== bccomp($totalAmount, '0')) {
|
||||
Log::info(sprintf('The total amount is negative, so it will be reset to %s.', $totalAmount));
|
||||
$totalAmount = $autoBudget->amount;
|
||||
}
|
||||
|
||||
// create budget limit:
|
||||
$this->createBudgetLimit($autoBudget, $start, $end, $totalAmount);
|
||||
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*/
|
||||
public function setDate(Carbon $date): void
|
||||
{
|
||||
$date->startOfDay();
|
||||
$this->date = $date;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ class SendWebhookMessage implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
private WebhookMessage $message;
|
||||
private WebhookMessage $message;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace FireflyIII\Jobs;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Telemetry;
|
||||
use GuzzleHttp\Client;
|
||||
@@ -38,7 +39,6 @@ use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Collection;
|
||||
use JsonException;
|
||||
use Log;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class SubmitTelemetryData
|
||||
@@ -102,13 +102,13 @@ class SubmitTelemetryData implements ShouldQueue
|
||||
];
|
||||
try {
|
||||
$result = $client->post($url, $options);
|
||||
} catch (GuzzleException|Exception $e) {
|
||||
} catch (GuzzleException | Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
Log::error('Could not submit telemetry.');
|
||||
throw new FireflyException(sprintf('Could not submit telemetry: %s', $e->getMessage()));
|
||||
}
|
||||
$body = (string) $result->getBody();
|
||||
$body = (string)$result->getBody();
|
||||
$statusCode = $result->getStatusCode();
|
||||
Log::info(sprintf('Result of submission [%d]: %s', $statusCode, $body));
|
||||
if (200 === $statusCode) {
|
||||
@@ -117,22 +117,6 @@ class SubmitTelemetryData implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*/
|
||||
public function setDate(Carbon $date): void
|
||||
{
|
||||
$this->date = $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $force
|
||||
*/
|
||||
public function setForce(bool $force): void
|
||||
{
|
||||
$this->force = $force;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
@@ -144,19 +128,6 @@ class SubmitTelemetryData implements ShouldQueue
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $telemetry
|
||||
*/
|
||||
private function markAsSubmitted(Collection $telemetry): void
|
||||
{
|
||||
$telemetry->each(
|
||||
static function (Telemetry $entry) {
|
||||
$entry->submitted = today(config('app.timezone'));
|
||||
$entry->save();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $telemetry
|
||||
*
|
||||
@@ -179,4 +150,33 @@ class SubmitTelemetryData implements ShouldQueue
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $telemetry
|
||||
*/
|
||||
private function markAsSubmitted(Collection $telemetry): void
|
||||
{
|
||||
$telemetry->each(
|
||||
static function (Telemetry $entry) {
|
||||
$entry->submitted = today(config('app.timezone'));
|
||||
$entry->save();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*/
|
||||
public function setDate(Carbon $date): void
|
||||
{
|
||||
$this->date = $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $force
|
||||
*/
|
||||
public function setForce(bool $force): void
|
||||
{
|
||||
$this->force = $force;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user