From 1d24d211eea81ae4f782e00b93b429d2bd214013 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 8 Mar 2023 23:16:08 +1100 Subject: [PATCH] Code Cleanup --- .../VendorPortal/InvitationController.php | 2 +- app/Services/Email/Email.php | 68 ++++++++++++++++--- app/Services/Email/EmailDefaults.php | 42 ++++++------ app/Utils/HtmlEngine.php | 2 +- app/Utils/VendorHtmlEngine.php | 1 + 5 files changed, 80 insertions(+), 35 deletions(-) diff --git a/app/Http/Controllers/VendorPortal/InvitationController.php b/app/Http/Controllers/VendorPortal/InvitationController.php index fcf88380d5..80bc02e5d5 100644 --- a/app/Http/Controllers/VendorPortal/InvitationController.php +++ b/app/Http/Controllers/VendorPortal/InvitationController.php @@ -91,7 +91,7 @@ class InvitationController extends Controller $file_name = $invitation->purchase_order->numberFormatter().'.pdf'; - $file = (new CreatePurchaseOrderPdf($invitation))->handle(); + $file = (new CreatePurchaseOrderPdf($invitation))->rawPdf(); $headers = ['Content-Type' => 'application/pdf']; diff --git a/app/Services/Email/Email.php b/app/Services/Email/Email.php index 34c78296e9..074525ee46 100644 --- a/app/Services/Email/Email.php +++ b/app/Services/Email/Email.php @@ -64,7 +64,12 @@ class Email implements ShouldQueue public function __construct(public EmailObject $email_object, public Company $company) { } - + + /** + * The backoff time between retries. + * + * @return array + */ public function backoff() { return [10, 30, 60, 240]; @@ -88,14 +93,24 @@ class Email implements ShouldQueue $this->tearDown(); } - + + /** + * Sets the override flag + * + * @return self + */ public function setOverride(): self { $this->override = $this->email_object->override; return $this; } - + + /** + * Initilializes the models + * + * @return self + */ public function initModels(): self { @@ -136,9 +151,16 @@ class Email implements ShouldQueue return $this; } - + + /** + * Generates the correct set of variables + * + * @return self + */ private function resolveVariables(): self { + $_variables = $this->email_object->variables; + match(class_basename($this->email_object->entity)){ "Invoice" => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->makeValues(), "Quote" => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->makeValues(), @@ -147,9 +169,20 @@ class Email implements ShouldQueue default => $this->email_object->variables = [] }; + /** If we have passed some variable overrides we insert them here */ + foreach($_variables as $key => $value) + { + $this->email_object->variables[$key] = $value; + } + return $this; } - + + /** + * tearDown + * + * @return self + */ private function tearDown(): self { @@ -164,7 +197,12 @@ class Email implements ShouldQueue return $this; } - + + /** + * Builds the email defaults + * + * @return self + */ public function setDefaults(): self { @@ -173,7 +211,12 @@ class Email implements ShouldQueue return $this; } - + + /** + * Populates the mailable + * + * @return self + */ public function buildMailable(): self { @@ -182,7 +225,12 @@ class Email implements ShouldQueue return $this; } - + + /** + * Attempts to send the email + * + * @return void + */ public function email() { @@ -600,7 +648,7 @@ class Email implements ShouldQueue * Logs any errors to the SystemLog * * @param string $errors - * @param null | App\Models\Client $recipient_object + * @param null | \App\Models\Client $recipient_object * @return void */ private function logMailError($errors, $recipient_object) :void @@ -627,7 +675,7 @@ class Email implements ShouldQueue /** * Attempts to refresh the Microsoft refreshToken * - * @param App\Models\User + * @param \App\Models\User $user * @return mixed */ private function refreshOfficeToken(User $user): mixed diff --git a/app/Services/Email/EmailDefaults.php b/app/Services/Email/EmailDefaults.php index 96c4bfdb8c..d90ede9e61 100644 --- a/app/Services/Email/EmailDefaults.php +++ b/app/Services/Email/EmailDefaults.php @@ -21,19 +21,19 @@ use App\Models\Invoice; use App\Models\PurchaseOrder; use App\Jobs\Invoice\CreateUbl; use App\Utils\Traits\MakesHash; -use Illuminate\Mail\Attachment; use App\Jobs\Entity\CreateRawPdf; use Illuminate\Support\Facades\App; use Illuminate\Mail\Mailables\Address; use App\DataMapper\EmailTemplateDefaults; use League\CommonMark\CommonMarkConverter; +use App\Jobs\Vendor\CreatePurchaseOrderPdf; class EmailDefaults { use MakesHash; /** * The settings object for this email - * @var CompanySettings $settings + * @var \App\DataMapper\CompanySettings $settings */ protected $settings; @@ -51,7 +51,6 @@ class EmailDefaults /** * @param Email $email job class - * @param EmailObject $email_object the email object class */ public function __construct(protected Email $email) { @@ -67,7 +66,7 @@ class EmailDefaults { $this->settings = $this->email->email_object->settings; - $this->setLocale() // + $this->setLocale() ->setFrom() ->setTo() ->setTemplate() @@ -159,7 +158,6 @@ class EmailDefaults */ private function setBody(): self { - if ($this->email->email_object->body) { // A Custom Message has been set in the email screen. return $this; @@ -274,41 +272,43 @@ class EmailDefaults $documents = []; /* Return early if the user cannot attach documents */ - if (!$this->email->email_object->settings->document_email_attachment || !$this->email->company->account->hasFeature(Account::FEATURE_DOCUMENTS)) + if (!$this->email->email_object->settings->document_email_attachment || !$this->email->company->account->hasFeature(Account::FEATURE_DOCUMENTS)) { return $this; - - if($this->email->email_object->entity instanceof PurchaseOrder) { - } - else if($this->email->email_object->settings->pdf_email_attachment && + + /** Purchase Order / Invoice / Credit / Quote PDF */ + if ($this->email->email_object->entity instanceof PurchaseOrder) { + $pdf = (new CreatePurchaseOrderPdf($this->email->email_object->invitation))->rawPdf(); + + $this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode($pdf), 'name' => $this->email->email_object->entity->numberFormatter().'.pdf']]); + } elseif ($this->email->email_object->settings->pdf_email_attachment && ($this->email->email_object->entity instanceof Invoice || $this->email->email_object->entity instanceof Quote || $this->email->email_object->entity instanceof Credit)) { - $pdf = ((new CreateRawPdf($this->email->email_object->invitation, $this->email->company->db))->handle()); $this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode($pdf), 'name' => $this->email->email_object->entity->numberFormatter().'.pdf']]); + } - } /* Company Documents */ $this->email->email_object->documents = array_merge($this->email->email_object->documents, $this->email->company->documents->pluck('id')->toArray()); + /** Entity Documents */ if ($this->email->email_object->entity?->documents) { $this->email->email_object->documents = array_merge($this->email->email_object->documents, $this->email->email_object->entity->documents->pluck('id')->toArray()); } - if ($this->email->email_object->entity instanceof Invoice && $this->email->email_object->entity->recurring_id != null){ + /** Recurring Invoice Documents */ + if ($this->email->email_object->entity instanceof Invoice && $this->email->email_object->entity->recurring_id != null) { $this->email->email_object->documents = array_merge($this->email->email_object->documents, $this->email->email_object->entity->recurring_invoice->documents->pluck('id')->toArray()); } + /** Task / Expense Documents */ if ($this->email->email_object->entity instanceof Invoice) { - $expense_ids = []; $task_ids = []; - foreach ($this->email->email_object->entity->line_items as $item) - { - + foreach ($this->email->email_object->entity->line_items as $item) { if (property_exists($item, 'expense_id')) { $expense_ids[] = $item->expense_id; } @@ -316,11 +316,9 @@ class EmailDefaults if (property_exists($item, 'task_id')) { $task_ids[] = $item->task_id; } - } if (count($expense_ids) > 0) { - Expense::whereIn('id', $this->transformKeys($expense_ids)) ->where('invoice_documents', 1) ->cursor() @@ -333,13 +331,12 @@ class EmailDefaults Task::whereIn('id', $this->transformKeys($task_ids)) ->cursor() ->each(function ($task) { - $this->email->email_object->documents = array_merge($this->email->email_object->documents, $task->documents->pluck('id')->toArray()); + $this->email->email_object->documents = array_merge($this->email->email_object->documents, $task->documents->pluck('id')->toArray()); }); } - } - + /** UBL xml file */ if ($this->email->email_object->entity instanceof Invoice && $this->email->email_object->settings->ubl_email_attachment) { $ubl_string = (new CreateUbl($this->email->email_object->entity))->handle(); @@ -349,7 +346,6 @@ class EmailDefaults } return $this; - } /** diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index cc4129a536..5fcbf1a2ad 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -129,7 +129,7 @@ class HtmlEngine $data['$line_tax_labels'] = ['value' => $this->lineTaxLabels(), 'label' => ctrans('texts.taxes')]; $data['$line_tax_values'] = ['value' => $this->lineTaxValues(), 'label' => ctrans('texts.taxes')]; $data['$date'] = ['value' => $this->translateDate($this->entity->date, $this->client->date_format(), $this->client->locale()) ?: ' ', 'label' => ctrans('texts.date')]; - $data['$status_logo'] = ['value' => '', 'label' => '']; + $data['$status_logo'] = ['value' => ' ', 'label' => ' ']; $data['$invoice.date'] = &$data['$date']; $data['$invoiceDate'] = &$data['$date']; diff --git a/app/Utils/VendorHtmlEngine.php b/app/Utils/VendorHtmlEngine.php index cddb2af044..aab9b9fb33 100644 --- a/app/Utils/VendorHtmlEngine.php +++ b/app/Utils/VendorHtmlEngine.php @@ -146,6 +146,7 @@ class VendorHtmlEngine $data['$entity.datetime'] = ['value' => $this->formatDatetime($this->entity->created_at, $this->company->date_format()), 'label' => ctrans('texts.date')]; $data['$po_number'] = &$data['$poNumber']; + $data['$status_logo'] = ['value' => ' ', 'label' => ' ']; $data['$entity'] = ['value' => '', 'label' => ctrans('texts.purchase_order')]; $data['$number'] = ['value' => $this->entity->number ?: ' ', 'label' => ctrans('texts.purchase_order_number')]; $data['$number_short'] = ['value' => $this->entity->number ?: ' ', 'label' => ctrans('texts.purchase_order_number_short')];