Fixes for invitation creation

This commit is contained in:
David Bomba 2021-01-14 10:00:32 +11:00
parent 617d9714ac
commit ae5f5ac30a
5 changed files with 75 additions and 15 deletions

View File

@ -62,7 +62,7 @@ class SendRemindersCron extends Command
$invoices = Invoice::where('is_deleted', 0) $invoices = Invoice::where('is_deleted', 0)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('balance', '>', 0) ->where('balance', '>', 0)
->whereDate('due_date', now()->subDays(1)->startOfDay()) ->whereDate('due_date', '<=', now()->subDays(1)->startOfDay())
->cursor(); ->cursor();
$invoices->each(function ($invoice) { $invoices->each(function ($invoice) {
@ -74,7 +74,7 @@ class SendRemindersCron extends Command
{ {
$quotes = Quote::where('is_deleted', 0) $quotes = Quote::where('is_deleted', 0)
->where('status_id', Quote::STATUS_SENT) ->where('status_id', Quote::STATUS_SENT)
->whereDate('due_date', now()->subDays(1)->startOfDay()) ->whereDate('due_date', '<=', now()->subDays(1)->startOfDay())
->cursor(); ->cursor();
$quotes->each(function ($quote) { $quotes->each(function ($quote) {

View File

@ -11,6 +11,7 @@
namespace App\Services\Credit; namespace App\Services\Credit;
use App\Factory\ClientContactFactory;
use App\Factory\CreditInvitationFactory; use App\Factory\CreditInvitationFactory;
use App\Models\Credit; use App\Models\Credit;
use App\Models\CreditInvitation; use App\Models\CreditInvitation;
@ -29,6 +30,13 @@ class CreateInvitations extends AbstractService
{ {
$contacts = $this->credit->client->contacts; $contacts = $this->credit->client->contacts;
if($contacts->count() == 0){
$this->createBlankContact();
$this->credit->refresh();
$contacts = $this->credit->client->contacts;
}
$contacts->each(function ($contact) { $contacts->each(function ($contact) {
$invitation = CreditInvitation::whereCompanyId($this->credit->company_id) $invitation = CreditInvitation::whereCompanyId($this->credit->company_id)
->whereClientContactId($contact->id) ->whereClientContactId($contact->id)
@ -47,4 +55,13 @@ class CreateInvitations extends AbstractService
return $this->credit; 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();
}
} }

View File

@ -11,6 +11,7 @@
namespace App\Services\Invoice; namespace App\Services\Invoice;
use App\Factory\ClientContactFactory;
use App\Factory\InvoiceInvitationFactory; use App\Factory\InvoiceInvitationFactory;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\InvoiceInvitation; use App\Models\InvoiceInvitation;
@ -27,7 +28,17 @@ class CreateInvitations extends AbstractService
public function run() 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) $invitation = InvoiceInvitation::whereCompanyId($this->invoice->company_id)
->whereClientContactId($contact->id) ->whereClientContactId($contact->id)
->whereInvoiceId($this->invoice->id) ->whereInvoiceId($this->invoice->id)
@ -46,4 +57,13 @@ class CreateInvitations extends AbstractService
return $this->invoice; 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();
}
} }

View File

@ -11,26 +11,41 @@
namespace App\Services\Quote; namespace App\Services\Quote;
use App\Factory\ClientContactFactory;
use App\Factory\QuoteInvitationFactory; use App\Factory\QuoteInvitationFactory;
use App\Models\Quote;
use App\Models\QuoteInvitation; use App\Models\QuoteInvitation;
class CreateInvitations 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) ->whereClientContactId($contact->id)
->whereQuoteId($quote->id) ->whereQuoteId($this->quote->id)
->first(); ->first();
if (! $invitation && $contact->send_email) { if (! $invitation && $contact->send_email) {
$ii = QuoteInvitationFactory::create($quote->company_id, $quote->user_id); $ii = QuoteInvitationFactory::create($this->quote->company_id, $this->quote->user_id);
$ii->quote_id = $quote->id; $ii->quote_id = $this->quote->id;
$ii->client_contact_id = $contact->id; $ii->client_contact_id = $contact->id;
$ii->save(); $ii->save();
} elseif ($invitation && ! $contact->send_email) { } 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();
}
} }

View File

@ -22,7 +22,7 @@ class QuoteService
{ {
use MakesHash; use MakesHash;
protected $quote; public $quote;
public $invoice; public $invoice;
@ -33,9 +33,7 @@ class QuoteService
public function createInvitations() public function createInvitations()
{ {
$create_invitation = new CreateInvitations(); $this->quote = (new CreateInvitations($this->quote))->run();
$this->quote = $create_invitation->run($this->quote);
return $this; return $this;
} }