diff --git a/app/Console/Commands/RecurringCheck.php b/app/Console/Commands/RecurringCheck.php index f29cf3ce0..297e5d4ee 100644 --- a/app/Console/Commands/RecurringCheck.php +++ b/app/Console/Commands/RecurringCheck.php @@ -112,11 +112,6 @@ class RecurringCheck extends Command foreach ($schedules as $schedule) { $schedule_date = Date::parse($schedule->getStart()->format('Y-m-d')); - // Don't recur the future - if ($schedule_date->greaterThan($today)) { - continue; - } - $this->recur($model, $recur->recurable_type, $schedule_date); } } diff --git a/app/Listeners/Document/SendDocumentRecurringNotification.php b/app/Listeners/Document/SendDocumentRecurringNotification.php index 5b13e2dac..7ac73522a 100644 --- a/app/Listeners/Document/SendDocumentRecurringNotification.php +++ b/app/Listeners/Document/SendDocumentRecurringNotification.php @@ -35,7 +35,7 @@ class SendDocumentRecurringNotification // Notify all users assigned to this company foreach ($document->company->users as $user) { - if (!$user->can('read-notifications')) { + if ($user->cannot('read-notifications')) { continue; } diff --git a/app/Traits/Recurring.php b/app/Traits/Recurring.php index 4aa267723..54ebcd8ea 100644 --- a/app/Traits/Recurring.php +++ b/app/Traits/Recurring.php @@ -87,15 +87,15 @@ trait Recurring ->setFreq($this->getRecurringRuleFrequency()) ->setInterval($this->getRecurringRuleInterval()); - if ($set_until_date) { - $rule->setUntil($this->getRecurringRuleUntilDate()); - } - // 0 means infinite if ($this->count != 0) { $rule->setCount($this->getRecurringRuleCount()); } + if ($set_until_date) { + $rule->setUntil($this->getRecurringRuleUntilDate()); + } + return $rule; } diff --git a/database/factories/Document.php b/database/factories/Document.php index 31c0b4abc..470fd60c4 100644 --- a/database/factories/Document.php +++ b/database/factories/Document.php @@ -203,10 +203,9 @@ class Document extends AbstractFactory public function recurring() { return $this->state([ - 'recurring_frequency' => 'yes', + 'recurring_frequency' => 'daily', 'recurring_interval' => '1', - 'recurring_custom_frequency' => $this->faker->randomElement(['monthly', 'weekly']), - 'recurring_count' => '1', + 'recurring_count' => '7', ]); } diff --git a/tests/Feature/Commands/RecurringCheckTest.php b/tests/Feature/Commands/RecurringCheckTest.php new file mode 100644 index 000000000..ff2c53691 --- /dev/null +++ b/tests/Feature/Commands/RecurringCheckTest.php @@ -0,0 +1,60 @@ +recurring_count = 7; + } + + public function testItShouldCreateCorrectNumberOfRecurringInvoices(): void + { + Notification::fake(); + + $this->dispatch(new CreateDocument($this->getRequest())); + + Date::setTestNow(Date::now()); + + $this->artisan('recurring:check'); + + $this->assertDatabaseCount('documents', $this->recurring_count + 1); + + Notification::assertSentToTimes($this->user, InvoiceNotification::class, $this->recurring_count); + } + + public function testItShouldNotCreateAnyRecurringInvoice(): void + { + Notification::fake(); + + $this->dispatch(new CreateDocument($this->getRequest())); + + Date::setTestNow(Date::now()->subDays($this->recurring_count + 1)); + + $this->artisan('recurring:check'); + + $this->assertDatabaseCount('documents', 1); + + Notification::assertNotSentTo($this->user, InvoiceNotification::class); + } + + public function getRequest(): array + { + return Document::factory()->invoice()->items()->recurring()->sent()->raw([ + 'issued_at' => Date::now()->subDays($this->recurring_count + 1), + 'recurring_count' => '20', + ]); + } +}