From ae5f5ac30a20d2962ece3880fb2bf5784f068e73 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 14 Jan 2021 10:00:32 +1100 Subject: [PATCH] Fixes for invitation creation --- app/Console/Commands/SendRemindersCron.php | 4 +-- app/Services/Credit/CreateInvitations.php | 17 +++++++++ app/Services/Invoice/CreateInvitations.php | 22 +++++++++++- app/Services/Quote/CreateInvitations.php | 41 +++++++++++++++++----- app/Services/Quote/QuoteService.php | 6 ++-- 5 files changed, 75 insertions(+), 15 deletions(-) diff --git a/app/Console/Commands/SendRemindersCron.php b/app/Console/Commands/SendRemindersCron.php index 7a787ec31933..65271cbf1f8c 100644 --- a/app/Console/Commands/SendRemindersCron.php +++ b/app/Console/Commands/SendRemindersCron.php @@ -62,7 +62,7 @@ class SendRemindersCron extends Command $invoices = Invoice::where('is_deleted', 0) ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) ->where('balance', '>', 0) - ->whereDate('due_date', now()->subDays(1)->startOfDay()) + ->whereDate('due_date', '<=', now()->subDays(1)->startOfDay()) ->cursor(); $invoices->each(function ($invoice) { @@ -74,7 +74,7 @@ class SendRemindersCron extends Command { $quotes = Quote::where('is_deleted', 0) ->where('status_id', Quote::STATUS_SENT) - ->whereDate('due_date', now()->subDays(1)->startOfDay()) + ->whereDate('due_date', '<=', now()->subDays(1)->startOfDay()) ->cursor(); $quotes->each(function ($quote) { diff --git a/app/Services/Credit/CreateInvitations.php b/app/Services/Credit/CreateInvitations.php index 6213c1374c6f..2a819feb3f62 100644 --- a/app/Services/Credit/CreateInvitations.php +++ b/app/Services/Credit/CreateInvitations.php @@ -11,6 +11,7 @@ namespace App\Services\Credit; +use App\Factory\ClientContactFactory; use App\Factory\CreditInvitationFactory; use App\Models\Credit; use App\Models\CreditInvitation; @@ -29,6 +30,13 @@ class CreateInvitations extends AbstractService { $contacts = $this->credit->client->contacts; + if($contacts->count() == 0){ + $this->createBlankContact(); + + $this->credit->refresh(); + $contacts = $this->credit->client->contacts; + } + $contacts->each(function ($contact) { $invitation = CreditInvitation::whereCompanyId($this->credit->company_id) ->whereClientContactId($contact->id) @@ -47,4 +55,13 @@ class CreateInvitations extends AbstractService return $this->credit; } + + private function createBlankContact() + { + $new_contact = ClientContactFactory::create($this->credit->company_id, $this->credit->user_id); + $new_contact->client_id = $this->credit->client_id; + $new_contact->contact_key = Str::random(40); + $new_contact->is_primary = true; + $new_contact->save(); + } } diff --git a/app/Services/Invoice/CreateInvitations.php b/app/Services/Invoice/CreateInvitations.php index 735798c905a1..6ba4e6fbb9b7 100644 --- a/app/Services/Invoice/CreateInvitations.php +++ b/app/Services/Invoice/CreateInvitations.php @@ -11,6 +11,7 @@ namespace App\Services\Invoice; +use App\Factory\ClientContactFactory; use App\Factory\InvoiceInvitationFactory; use App\Models\Invoice; use App\Models\InvoiceInvitation; @@ -27,7 +28,17 @@ class CreateInvitations extends AbstractService public function run() { - $this->invoice->client->contacts->each(function ($contact) { + + $contacts = $this->invoice->client->contacts; + + if($contacts->count() == 0){ + $this->createBlankContact(); + + $this->invoice->refresh(); + $contacts = $this->invoice->client->contacts; + } + + $contacts->each(function ($contact) { $invitation = InvoiceInvitation::whereCompanyId($this->invoice->company_id) ->whereClientContactId($contact->id) ->whereInvoiceId($this->invoice->id) @@ -46,4 +57,13 @@ class CreateInvitations extends AbstractService return $this->invoice; } + + private function createBlankContact() + { + $new_contact = ClientContactFactory::create($this->invoice->company_id, $this->invoice->user_id); + $new_contact->client_id = $this->invoice->client_id; + $new_contact->contact_key = Str::random(40); + $new_contact->is_primary = true; + $new_contact->save(); + } } diff --git a/app/Services/Quote/CreateInvitations.php b/app/Services/Quote/CreateInvitations.php index 329eabd895d6..9ea688641a2b 100644 --- a/app/Services/Quote/CreateInvitations.php +++ b/app/Services/Quote/CreateInvitations.php @@ -11,26 +11,41 @@ namespace App\Services\Quote; +use App\Factory\ClientContactFactory; use App\Factory\QuoteInvitationFactory; +use App\Models\Quote; use App\Models\QuoteInvitation; class CreateInvitations { - public function __construct() + public $quote; + + public function __construct(Quote $quote) { + $this->quote = $quote; } - public function run($quote) + public function run() { - $quote->client->contacts->each(function ($contact) use ($quote) { - $invitation = QuoteInvitation::whereCompanyId($quote->company_id) + + $contacts = $this->quote->client->contacts; + + if($contacts->count() == 0){ + $this->createBlankContact(); + + $this->quote->refresh(); + $contacts = $this->quote->client->contacts; + } + + $contacts->each(function ($contact){ + $invitation = QuoteInvitation::whereCompanyId($this->quote->company_id) ->whereClientContactId($contact->id) - ->whereQuoteId($quote->id) + ->whereQuoteId($this->quote->id) ->first(); if (! $invitation && $contact->send_email) { - $ii = QuoteInvitationFactory::create($quote->company_id, $quote->user_id); - $ii->quote_id = $quote->id; + $ii = QuoteInvitationFactory::create($this->quote->company_id, $this->quote->user_id); + $ii->quote_id = $this->quote->id; $ii->client_contact_id = $contact->id; $ii->save(); } elseif ($invitation && ! $contact->send_email) { @@ -38,6 +53,16 @@ class CreateInvitations } }); - return $quote->fresh(); + return $this->quote->fresh(); } + + private function createBlankContact() + { + $new_contact = ClientContacstFactory::create($this->quote->company_id, $this->quote->user_id); + $new_contact->client_id = $this->quote->client_id; + $new_contact->contact_key = Str::random(40); + $new_contact->is_primary = true; + $new_contact->save(); + } + } diff --git a/app/Services/Quote/QuoteService.php b/app/Services/Quote/QuoteService.php index 33939e362615..92ac2731175a 100644 --- a/app/Services/Quote/QuoteService.php +++ b/app/Services/Quote/QuoteService.php @@ -22,7 +22,7 @@ class QuoteService { use MakesHash; - protected $quote; + public $quote; public $invoice; @@ -33,9 +33,7 @@ class QuoteService public function createInvitations() { - $create_invitation = new CreateInvitations(); - - $this->quote = $create_invitation->run($this->quote); + $this->quote = (new CreateInvitations($this->quote))->run(); return $this; }