From 57cf7f6f0d3cc40263d8cc5557e449d00a470d89 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 28 Jun 2018 06:31:31 +0200 Subject: [PATCH] Update some things for recurring transactions. --- app/Console/Kernel.php | 3 --- app/Events/RequestedReportOnJournals.php | 3 ++- app/Jobs/CreateRecurringTransactions.php | 16 ++++++++++------ app/Mail/ReportNewJournalsMail.php | 8 +++++++- .../Recurring/RecurringRepository.php | 19 +++++++++++++++++-- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index a9d5b137c3..bda9202d93 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -25,9 +25,7 @@ declare(strict_types=1); namespace FireflyIII\Console; use Carbon\Carbon; -use FireflyIII\Events\AdminRequestedTestMessage; use FireflyIII\Jobs\CreateRecurringTransactions; -use FireflyIII\User; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -62,7 +60,6 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule): void { - // create recurring transactions. $schedule->job(new CreateRecurringTransactions(new Carbon))->daily(); } } diff --git a/app/Events/RequestedReportOnJournals.php b/app/Events/RequestedReportOnJournals.php index 9956ee62fd..d4bdf0dbb7 100644 --- a/app/Events/RequestedReportOnJournals.php +++ b/app/Events/RequestedReportOnJournals.php @@ -24,7 +24,8 @@ class RequestedReportOnJournals /** * Create a new event instance. * - * @return void + * @param int $userId + * @param Collection $journals */ public function __construct(int $userId, Collection $journals) { diff --git a/app/Jobs/CreateRecurringTransactions.php b/app/Jobs/CreateRecurringTransactions.php index 37319dc958..05b036b5ed 100644 --- a/app/Jobs/CreateRecurringTransactions.php +++ b/app/Jobs/CreateRecurringTransactions.php @@ -51,7 +51,7 @@ class CreateRecurringTransactions implements ShouldQueue */ public function handle(): void { - Log::debug('Now at start of CreateRecurringTransactions() job.'); + Log::debug(sprintf('Now at start of CreateRecurringTransactions() job for %s.', $this->date->format('D d M Y'))); $recurrences = $this->repository->getAll(); Log::debug(sprintf('Count of collection is %d', $recurrences->count())); @@ -125,9 +125,6 @@ class CreateRecurringTransactions implements ShouldQueue $startDate = clone $recurrence->first_date; if (null !== $recurrence->latest_date && $recurrence->latest_date->gte($startDate)) { $startDate = clone $recurrence->latest_date; - // jump to a day later. - $startDate->addDay(); - } return $startDate; @@ -250,12 +247,19 @@ class CreateRecurringTransactions implements ShouldQueue ); // start looping from $startDate to today perhaps we have a hit? - $occurrences = $this->repository->getOccurrencesInRange($repetition, $recurrence->first_date, $this->date); + // 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); Log::debug( sprintf( - 'Calculated %d occurrences between %s and %s', \count($occurrences), $recurrence->first_date->format('Y-m-d'), $this->date->format('Y-m-d') + 'Calculated %d occurrences between %s and %s', + \count($occurrences), + $recurrence->first_date->format('Y-m-d'), + $includeWeekend->format('Y-m-d') ), $this->debugArray($occurrences) ); + unset($includeWeekend); $result = $this->handleOccurrences($recurrence, $occurrences); $collection = $collection->merge($result); diff --git a/app/Mail/ReportNewJournalsMail.php b/app/Mail/ReportNewJournalsMail.php index d1f1d2445e..a483625c1a 100644 --- a/app/Mail/ReportNewJournalsMail.php +++ b/app/Mail/ReportNewJournalsMail.php @@ -65,7 +65,13 @@ class ReportNewJournalsMail extends Mailable */ public function build(): self { + $subject = $this->journals->count() === 1 + ? 'Firefly III has created a new transaction' + : sprintf( + 'Firefly III has created new %d transactions', $this->journals->count() + ); + return $this->view('emails.report-new-journals-html')->text('emails.report-new-journals-text') - ->subject('Firefly III has created new transactions'); + ->subject($subject); } } diff --git a/app/Repositories/Recurring/RecurringRepository.php b/app/Repositories/Recurring/RecurringRepository.php index 8776c8d2de..1046fb41f3 100644 --- a/app/Repositories/Recurring/RecurringRepository.php +++ b/app/Repositories/Recurring/RecurringRepository.php @@ -547,6 +547,8 @@ class RecurringRepository implements RecurringRepositoryInterface protected function filterWeekends(RecurrenceRepetition $repetition, array $dates): array { if ($repetition->weekend === RecurrenceRepetition::WEEKEND_DO_NOTHING) { + Log::debug('Repetition will not be filtered on weekend days.'); + return $dates; } $return = []; @@ -555,29 +557,42 @@ class RecurringRepository implements RecurringRepositoryInterface $isWeekend = $date->isWeekend(); if (!$isWeekend) { $return[] = clone $date; + Log::debug(sprintf('Date is %s, not a weekend date.', $date->format('D d M Y'))); continue; } // is weekend and must set back to Friday? - if ($isWeekend && $repetition->weekend === RecurrenceRepetition::WEEKEND_TO_FRIDAY) { + if ($repetition->weekend === RecurrenceRepetition::WEEKEND_TO_FRIDAY) { $clone = clone $date; $clone->addDays(5 - $date->dayOfWeekIso); + Log::debug( + sprintf('Date is %s, and this is in the weekend, so corrected to %s (Friday).', $date->format('D d M Y'), $clone->format('D d M Y')) + ); $return[] = clone $clone; + continue; } // postpone to Monday? - if ($isWeekend && $repetition->weekend === RecurrenceRepetition::WEEKEND_TO_MONDAY) { + if ($repetition->weekend === RecurrenceRepetition::WEEKEND_TO_MONDAY) { $clone = clone $date; $clone->addDays(8 - $date->dayOfWeekIso); + Log::debug( + sprintf('Date is %s, and this is in the weekend, so corrected to %s (Monday).', $date->format('D d M Y'), $clone->format('D d M Y')) + ); $return[] = $clone; + continue; } + Log::debug(sprintf('Date is %s, removed from final result', $date->format('D d M Y'))); } // filter unique dates + Log::debug(sprintf('Count before filtering: %d', \count($dates))); $collection = new Collection($return); $filtered = $collection->unique(); $return = $filtered->toArray(); + Log::debug(sprintf('Count after filtering: %d', \count($return))); + return $return; } } \ No newline at end of file