From aba280d9abb62a61ef6591853647d1b35e3fd356 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 30 Apr 2021 09:01:56 +1000 Subject: [PATCH] Fixes for recurring --- app/Jobs/Entity/EmailEntity.php | 2 ++ app/Jobs/RecurringInvoice/SendRecurring.php | 9 ++++++++- app/Models/Invoice.php | 12 +++++++++++- app/Services/Invoice/CreateInvitations.php | 12 ++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/Jobs/Entity/EmailEntity.php b/app/Jobs/Entity/EmailEntity.php index 3dd8118d8c34..7a9f324c0b35 100644 --- a/app/Jobs/Entity/EmailEntity.php +++ b/app/Jobs/Entity/EmailEntity.php @@ -60,6 +60,8 @@ class EmailEntity implements ShouldQueue public $template_data; //The data to be merged into the template + public $tries = 1; + /** * EmailEntity constructor. * diff --git a/app/Jobs/RecurringInvoice/SendRecurring.php b/app/Jobs/RecurringInvoice/SendRecurring.php index 8a70e621bec0..bb5671a8a7e8 100644 --- a/app/Jobs/RecurringInvoice/SendRecurring.php +++ b/app/Jobs/RecurringInvoice/SendRecurring.php @@ -94,7 +94,14 @@ class SendRecurring implements ShouldQueue $invoice->invitations->each(function ($invitation) use ($invoice) { if ($invitation->contact && strlen($invitation->contact->email) >=1) { - EmailEntity::dispatch($invitation, $invoice->company); + + try{ + EmailEntity::dispatch($invitation, $invoice->company); + } + catch(\Exception $e) { + nlog($e->getMessage()); + } + nlog("Firing email for invoice {$invoice->number}"); } }); diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index d210552e2033..63029682b962 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -395,9 +395,19 @@ class Invoice extends BaseModel public function pdf_file_path($invitation = null, string $type = 'url') { if (! $invitation) { - $invitation = $this->invitations->first(); + + if($this->invitations()->exists()) + $invitation = $this->invitations()->first(); + else{ + $this->service()->createInvitations(); + $invitation = $this->invitations()->first(); + } + } + if(!$invitation) + throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?'); + $storage_path = Storage::$type($this->client->invoice_filepath().$this->numberFormatter().'.pdf'); if (! Storage::exists($this->client->invoice_filepath().$this->numberFormatter().'.pdf')) { diff --git a/app/Services/Invoice/CreateInvitations.php b/app/Services/Invoice/CreateInvitations.php index 6ba4e6fbb9b7..d857228b88e6 100644 --- a/app/Services/Invoice/CreateInvitations.php +++ b/app/Services/Invoice/CreateInvitations.php @@ -55,6 +55,16 @@ class CreateInvitations extends AbstractService } }); + if($this->invoice->invitations()->count() == 0) { + + $contact = $this->createBlankContact(); + + $ii = InvoiceInvitationFactory::create($this->invoice->company_id, $this->invoice->user_id); + $ii->invoice_id = $this->invoice->id; + $ii->client_contact_id = $contact->id; + $ii->save(); + } + return $this->invoice; } @@ -65,5 +75,7 @@ class CreateInvitations extends AbstractService $new_contact->contact_key = Str::random(40); $new_contact->is_primary = true; $new_contact->save(); + + return $new_contact; } }