diff --git a/app/Services/Recurring/CreateRecurringInvitations.php b/app/Services/Recurring/CreateRecurringInvitations.php new file mode 100644 index 000000000000..e511114aba50 --- /dev/null +++ b/app/Services/Recurring/CreateRecurringInvitations.php @@ -0,0 +1,62 @@ +entity = $entity; + $this->entity_name = lcfirst(Str::snake(class_basename($entity))); + $this->entity_id_name = $this->entity_name . "_id"; + $this->invitation_class = Str::camel($this->entity_name); + $this->invitation_factory = $this->invitaiton_class . "Factory"; + } + + public function run() + { + $this->entity->client->contacts->each(function ($contact) { + $invitation = $this->invitation_class::whereCompanyId($this->entity->company_id) + ->whereClientContactId($contact->id) + ->where($this->entity_id_name, $this->entity->id) + ->withTrashed() + ->first(); + + if (! $invitation && $contact->send_email) { + $ii = $this->invitation_factory::create($this->entity->company_id, $this->entity->user_id); + $ii->{$this->entity_id} = $this->entity->id; + $ii->client_contact_id = $contact->id; + $ii->save(); + } elseif ($invitation && ! $contact->send_email) { + $invitation->delete(); + } + }); + + return $this->entity; + } +} diff --git a/app/Services/Recurring/RecurringService.php b/app/Services/Recurring/RecurringService.php index f05532c7ae1e..f4720b0156b9 100644 --- a/app/Services/Recurring/RecurringService.php +++ b/app/Services/Recurring/RecurringService.php @@ -12,6 +12,7 @@ namespace App\Services\Recurring; use App\Models\RecurringInvoice; +use App\Services\Recurring\CreateRecurringInvitations; use Illuminate\Support\Carbon; class RecurringService @@ -37,6 +38,13 @@ class RecurringService return $this; } + public function createInvitations() + { + $this->recurring_entity = (new CreateRecurringInvitations($this->recurring_entity))->run(); + + return $this; + } + public function start() { //make sure next_send_date is either now or in the future else return.