diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 574153e946..783f79cc94 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -174,8 +174,8 @@ class BaseRepository $model->client_id = $data['client_id']; //pickup changes here to recalculate reminders - if($model instanceof Invoice && ($model->isDirty('date') || $model->isDirty('due_date'))) - $model->service()->setReminder()->save(); + //if($model instanceof Invoice && ($model->isDirty('date') || $model->isDirty('due_date'))) + // $model->service()->setReminder()->save(); $client = Client::where('id', $model->client_id)->withTrashed()->first(); @@ -213,6 +213,9 @@ class BaseRepository /* Model now persisted, now lets do some child tasks */ + if($model instanceof Invoice) + $model->service()->setReminder()->save(); + /* Save any documents */ if (array_key_exists('documents', $data)) $this->saveDocuments($data['documents'], $model); diff --git a/app/Services/Invoice/UpdateReminder.php b/app/Services/Invoice/UpdateReminder.php index 0b655ee916..e693da6f6d 100644 --- a/app/Services/Invoice/UpdateReminder.php +++ b/app/Services/Invoice/UpdateReminder.php @@ -42,6 +42,9 @@ class UpdateReminder extends AbstractService return $this->invoice; //exit early } + if($this->invoice->next_send_date) + $this->invoice->next_send_date = null; + $offset = $this->invoice->client->timezone_offset(); $date_collection = collect(); @@ -52,7 +55,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays($this->settings->num_days_reminder1)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 1 after due date"); } if (is_null($this->invoice->reminder1_sent) && @@ -61,7 +64,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->subDays($this->settings->num_days_reminder1)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 1 before_due_date"); } if (is_null($this->invoice->reminder1_sent) && @@ -70,7 +73,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->addDays($this->settings->num_days_reminder1)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 1 after_due_date"); } if (is_null($this->invoice->reminder2_sent) && @@ -79,7 +82,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays($this->settings->num_days_reminder2)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 2 after_invoice_date = ".$reminder_date->format('Y-m-d')); } if (is_null($this->invoice->reminder2_sent) && @@ -88,7 +91,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->subDays($this->settings->num_days_reminder2)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 2 before_due_date"); } if (is_null($this->invoice->reminder2_sent) && @@ -97,7 +100,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->addDays($this->settings->num_days_reminder2)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 2 after_due_date"); } if (is_null($this->invoice->reminder3_sent) && @@ -106,7 +109,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays($this->settings->num_days_reminder3)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 3 after_invoice_date"); } if (is_null($this->invoice->reminder3_sent) && @@ -115,7 +118,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->subDays($this->settings->num_days_reminder3)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 3 before_due_date"); } if (is_null($this->invoice->reminder3_sent) && @@ -124,7 +127,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->addDays($this->settings->num_days_reminder3)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 3 after_due_date"); } if ($this->invoice->last_sent_date && @@ -137,7 +140,7 @@ class UpdateReminder extends AbstractService $reminder_date->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings endless reminder"); } diff --git a/tests/Feature/ReminderTest.php b/tests/Feature/ReminderTest.php index 03c0dc4a15..54cfc5cc52 100644 --- a/tests/Feature/ReminderTest.php +++ b/tests/Feature/ReminderTest.php @@ -107,12 +107,11 @@ class ReminderTest extends TestCase // ReminderJob::dispatchNow(); } - + /* Cant set a reminder in the past so need to skip reminder 2 and go straigh to reminder 3*/ public function testReminderNextSendRecalculation() { $this->invoice->date = now()->subDays(2)->format('Y-m-d'); - $this->invoice->due_date = now()->addDays(30)->format('Y-m-d'); $this->invoice->reminder1_sent = now()->subDays(1)->format('Y-m-d'); $this->invoice->last_sent_date = now()->subDays(1)->format('Y-m-d'); @@ -136,7 +135,7 @@ class ReminderTest extends TestCase $this->invoice->fresh(); - $this->assertEquals(Carbon::parse($this->invoice->next_send_date)->format('Y-m-d'), now()->format('Y-m-d')); + $this->assertEquals(Carbon::parse($this->invoice->next_send_date)->format('Y-m-d'), now()->addDay()->format('Y-m-d')); }