From 778c30f7ed0c9230c67499f4a3268108cb969709 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 28 Nov 2022 12:16:58 +1100 Subject: [PATCH 1/5] refactor for data instead of file paths --- app/Filters/DocumentFilters.php | 10 ++++++++++ app/Mail/Engine/InvoiceEmailEngine.php | 23 +++++++++++++--------- app/Mail/TemplateEmail.php | 27 +++++++++++++++----------- app/Utils/HostedPDF/NinjaPdf.php | 2 +- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/app/Filters/DocumentFilters.php b/app/Filters/DocumentFilters.php index fade4950705a..bf7b589aeef0 100644 --- a/app/Filters/DocumentFilters.php +++ b/app/Filters/DocumentFilters.php @@ -11,6 +11,7 @@ namespace App\Filters; +use App\Models\Company; use App\Models\User; use Illuminate\Database\Eloquent\Builder; @@ -54,6 +55,15 @@ class DocumentFilters extends QueryFilters return $this->builder->orderBy($sort_col[0], $sort_col[1]); } + + public function company_documents($value = 'false') + { + if($value == 'true') + return $this->builder->where('documentable_type', Company::class); + + return $this->builder; + } + /** * Returns the base query. * diff --git a/app/Mail/Engine/InvoiceEmailEngine.php b/app/Mail/Engine/InvoiceEmailEngine.php index 000d2f72748f..e41f0fdc8c9f 100644 --- a/app/Mail/Engine/InvoiceEmailEngine.php +++ b/app/Mail/Engine/InvoiceEmailEngine.php @@ -13,6 +13,7 @@ namespace App\Mail\Engine; use App\DataMapper\EmailTemplateDefaults; use App\Jobs\Entity\CreateEntityPdf; +use App\Jobs\Entity\CreateRawPdf; use App\Models\Account; use App\Models\Expense; use App\Models\Task; @@ -126,11 +127,15 @@ class InvoiceEmailEngine extends BaseEmailEngine ->setTextBody($text_body); if ($this->client->getSetting('pdf_email_attachment') !== false && $this->invoice->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { - if (Ninja::isHosted()) { - $this->setAttachments([$this->invoice->pdf_file_path($this->invitation, 'url', true)]); - } else { - $this->setAttachments([$this->invoice->pdf_file_path($this->invitation)]); - } + // if (Ninja::isHosted()) { + // $this->setAttachments([$this->invoice->pdf_file_path($this->invitation, 'url', true)]); + // } else { + // $this->setAttachments([$this->invoice->pdf_file_path($this->invitation)]); + // } + // $file = (new CreateRawPdf($invitation, $invitation->company->db))->handle(); + $pdf = ((new CreateRawPdf($this->invitation, $this->invitation->company->db))->handle()); + + $this->setAttachments([['file' => base64_encode($pdf), 'name' => $this->invoice->numberFormatter().'.pdf']]); } //attach third party documents @@ -138,11 +143,11 @@ class InvoiceEmailEngine extends BaseEmailEngine // Storage::url foreach ($this->invoice->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => $document->getFile()]]); } foreach ($this->invoice->company->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => $document->getFile()]]); } $line_items = $this->invoice->line_items; @@ -160,7 +165,7 @@ class InvoiceEmailEngine extends BaseEmailEngine ->cursor() ->each(function ($expense) { foreach ($expense->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => $document->getFile()]]); } }); } @@ -176,7 +181,7 @@ class InvoiceEmailEngine extends BaseEmailEngine ->cursor() ->each(function ($task) { foreach ($task->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => $document->getFile()]]); } }); } diff --git a/app/Mail/TemplateEmail.php b/app/Mail/TemplateEmail.php index cbf07442c075..8a892db39840 100644 --- a/app/Mail/TemplateEmail.php +++ b/app/Mail/TemplateEmail.php @@ -146,19 +146,24 @@ class TemplateEmail extends Mailable } + // $file = (new CreateRawPdf($invitation, $invitation->company->db))->handle(); + + //22-10-2022 - Performance - To improve the performance/reliability of sending emails, attaching as Data is much better, stubs in place foreach ($this->build_email->getAttachments() as $file) { - if (is_string($file)) { - // nlog($file); - // $file_data = file_get_contents($file); - // $this->attachData($file_data, basename($file)); - $this->attach($file); - } elseif (is_array($file)) { - // nlog($file['path']); - // $file_data = file_get_contents($file['path']); - // $this->attachData($file_data, $file['name']); - $this->attach($file['path'], ['as' => $file['name'], 'mime' => null]); - } + // if (is_string($file)) { + // // nlog($file); + // // $file_data = file_get_contents($file); + // // $this->attachData($file_data, basename($file)); + // $this->attach($file); + // } elseif (is_array($file)) { + // // nlog($file['path']); + // // $file_data = file_get_contents($file['path']); + // // $this->attachData($file_data, $file['name']); + // $this->attach($file['path'], ['as' => $file['name'], 'mime' => null]); + // } + + $this->attachData(base64_decode($file['file']), $file['name']); } if ($this->invitation && $this->invitation->invoice && $settings->ubl_email_attachment && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { diff --git a/app/Utils/HostedPDF/NinjaPdf.php b/app/Utils/HostedPDF/NinjaPdf.php index 7a6da2097b53..e0598b54c928 100644 --- a/app/Utils/HostedPDF/NinjaPdf.php +++ b/app/Utils/HostedPDF/NinjaPdf.php @@ -28,6 +28,6 @@ class NinjaPdf RequestOptions::JSON => ['html' => $html], ]); - return $response->getBody(); + return $response->getBody()->getContents(); } } From 93c96e0d7898e75b1f94fa3410665705075f1455 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 28 Nov 2022 12:24:07 +1100 Subject: [PATCH 2/5] Inject slight delay into emails --- app/Http/Controllers/EmailController.php | 4 ++-- app/Mail/Engine/InvoiceEmailEngine.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index 17b3f8ff0a78..fb081a9a4509 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -141,7 +141,7 @@ class EmailController extends BaseController if (! $invitation->contact->trashed() && $invitation->contact->email) { $entity_obj->service()->markSent()->save(); - EmailEntity::dispatch($invitation->fresh(), $invitation->company, $template, $data); + EmailEntity::dispatch($invitation->fresh(), $invitation->company, $template, $data)->delay(now()->addSeconds(2)); } }); @@ -194,7 +194,7 @@ class EmailController extends BaseController $data['template'] = $template; - PurchaseOrderEmail::dispatch($entity_obj, $entity_obj->company, $data); + PurchaseOrderEmail::dispatch($entity_obj, $entity_obj->company, $data)->delay(now()->addSeconds(2)); return $this->itemResponse($entity_obj); diff --git a/app/Mail/Engine/InvoiceEmailEngine.php b/app/Mail/Engine/InvoiceEmailEngine.php index e41f0fdc8c9f..fa88fa67a815 100644 --- a/app/Mail/Engine/InvoiceEmailEngine.php +++ b/app/Mail/Engine/InvoiceEmailEngine.php @@ -143,11 +143,11 @@ class InvoiceEmailEngine extends BaseEmailEngine // Storage::url foreach ($this->invoice->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => $document->getFile()]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } foreach ($this->invoice->company->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => $document->getFile()]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } $line_items = $this->invoice->line_items; @@ -165,7 +165,7 @@ class InvoiceEmailEngine extends BaseEmailEngine ->cursor() ->each(function ($expense) { foreach ($expense->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => $document->getFile()]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } }); } @@ -181,7 +181,7 @@ class InvoiceEmailEngine extends BaseEmailEngine ->cursor() ->each(function ($task) { foreach ($task->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => $document->getFile()]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } }); } From d0bb55f0c26a894c38a46c50d7fa3ffd001c5806 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 28 Nov 2022 14:16:46 +1100 Subject: [PATCH 3/5] Refactor remaining entities to attach using data rather than paths --- .../Company/ValidCompanyQuantity.php | 2 +- app/Mail/Engine/CreditEmailEngine.php | 21 ++++++---- app/Mail/Engine/PaymentEmailEngine.php | 15 ++++--- app/Mail/Engine/PurchaseOrderEmailEngine.php | 22 +++++++---- app/Mail/Engine/QuoteEmailEngine.php | 19 +++++---- app/Mail/TemplateEmail.php | 34 ++++++++-------- app/Mail/VendorTemplateEmail.php | 39 ++++++++++--------- lang/en/texts.php | 2 +- 8 files changed, 91 insertions(+), 63 deletions(-) diff --git a/app/Http/ValidationRules/Company/ValidCompanyQuantity.php b/app/Http/ValidationRules/Company/ValidCompanyQuantity.php index a90e861c9ae2..fa04edd4a721 100644 --- a/app/Http/ValidationRules/Company/ValidCompanyQuantity.php +++ b/app/Http/ValidationRules/Company/ValidCompanyQuantity.php @@ -39,6 +39,6 @@ class ValidCompanyQuantity implements Rule */ public function message() { - return ctrans('texts.company_limit_reached'); + return ctrans('texts.company_limit_reached', ['limit' => Ninja::isSelfHost() ? 10 : auth()->user()->company()->account->hosted_company_count]); } } diff --git a/app/Mail/Engine/CreditEmailEngine.php b/app/Mail/Engine/CreditEmailEngine.php index 12a12e600d88..25ac405d38ea 100644 --- a/app/Mail/Engine/CreditEmailEngine.php +++ b/app/Mail/Engine/CreditEmailEngine.php @@ -11,6 +11,7 @@ namespace App\Mail\Engine; +use App\Jobs\Entity\CreateRawPdf; use App\Models\Account; use App\Utils\HtmlEngine; use App\Utils\Ninja; @@ -117,11 +118,17 @@ class CreditEmailEngine extends BaseEmailEngine ->setTextBody($text_body); if ($this->client->getSetting('pdf_email_attachment') !== false && $this->credit->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { - if (Ninja::isHosted()) { - $this->setAttachments([$this->credit->pdf_file_path($this->invitation, 'url', true)]); - } else { - $this->setAttachments([$this->credit->pdf_file_path($this->invitation)]); - } + // if (Ninja::isHosted()) { + // $this->setAttachments([$this->credit->pdf_file_path($this->invitation, 'url', true)]); + // } else { + // $this->setAttachments([$this->credit->pdf_file_path($this->invitation)]); + // } + + $pdf = ((new CreateRawPdf($this->invitation, $this->invitation->company->db))->handle()); + + $this->setAttachments([['file' => base64_encode($pdf), 'name' => $this->credit->numberFormatter().'.pdf']]); + + } //attach third party documents @@ -129,11 +136,11 @@ class CreditEmailEngine extends BaseEmailEngine // Storage::url foreach ($this->credit->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } foreach ($this->credit->company->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } } diff --git a/app/Mail/Engine/PaymentEmailEngine.php b/app/Mail/Engine/PaymentEmailEngine.php index bf15b7aa2ca4..e9ad8767d284 100644 --- a/app/Mail/Engine/PaymentEmailEngine.php +++ b/app/Mail/Engine/PaymentEmailEngine.php @@ -12,6 +12,7 @@ namespace App\Mail\Engine; use App\DataMapper\EmailTemplateDefaults; +use App\Jobs\Entity\CreateRawPdf; use App\Models\Account; use App\Utils\Helpers; use App\Utils\Ninja; @@ -89,11 +90,15 @@ class PaymentEmailEngine extends BaseEmailEngine if ($this->client->getSetting('pdf_email_attachment') !== false && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { $this->payment->invoices->each(function ($invoice) { - if (Ninja::isHosted()) { - $this->setAttachments([$invoice->pdf_file_path($invoice->invitations->first(), 'url', true)]); - } else { - $this->setAttachments([$invoice->pdf_file_path($invoice->invitations->first())]); - } + // if (Ninja::isHosted()) { + // $this->setAttachments([$invoice->pdf_file_path($invoice->invitations->first(), 'url', true)]); + // } else { + // $this->setAttachments([$invoice->pdf_file_path($invoice->invitations->first())]); + // } + $pdf = ((new CreateRawPdf($invoice->invitations->first(), $invoice->company->db))->handle()); + + $this->setAttachments([['file' => base64_encode($pdf), 'name' => $invoice->numberFormatter().'.pdf']]); + }); } diff --git a/app/Mail/Engine/PurchaseOrderEmailEngine.php b/app/Mail/Engine/PurchaseOrderEmailEngine.php index fa6477c38c34..ce4a411b4840 100644 --- a/app/Mail/Engine/PurchaseOrderEmailEngine.php +++ b/app/Mail/Engine/PurchaseOrderEmailEngine.php @@ -13,6 +13,7 @@ namespace App\Mail\Engine; use App\DataMapper\EmailTemplateDefaults; use App\Jobs\Entity\CreateEntityPdf; +use App\Jobs\Vendor\CreatePurchaseOrderPdf; use App\Models\Account; use App\Models\Expense; use App\Models\PurchaseOrder; @@ -125,11 +126,16 @@ class PurchaseOrderEmailEngine extends BaseEmailEngine ->setTextBody($text_body); if ($this->vendor->getSetting('pdf_email_attachment') !== false && $this->purchase_order->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { - if (Ninja::isHosted()) { - $this->setAttachments([$this->purchase_order->pdf_file_path($this->invitation, 'url', true)]); - } else { - $this->setAttachments([$this->purchase_order->pdf_file_path($this->invitation)]); - } + // if (Ninja::isHosted()) { + // $this->setAttachments([$this->purchase_order->pdf_file_path($this->invitation, 'url', true)]); + // } else { + // $this->setAttachments([$this->purchase_order->pdf_file_path($this->invitation)]); + // } + + $pdf = (new CreatePurchaseOrderPdf($this->invitation))->rawPdf(); + + $this->setAttachments([['file' => base64_encode($pdf), 'name' => $this->purchase_order->numberFormatter().'.pdf']]); + } //attach third party documents @@ -137,11 +143,13 @@ class PurchaseOrderEmailEngine extends BaseEmailEngine // Storage::url foreach ($this->purchase_order->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); + // $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } foreach ($this->purchase_order->company->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); + // $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } } diff --git a/app/Mail/Engine/QuoteEmailEngine.php b/app/Mail/Engine/QuoteEmailEngine.php index 8b692c2f3e5d..5b1387ed6980 100644 --- a/app/Mail/Engine/QuoteEmailEngine.php +++ b/app/Mail/Engine/QuoteEmailEngine.php @@ -11,6 +11,7 @@ namespace App\Mail\Engine; +use App\Jobs\Entity\CreateRawPdf; use App\Models\Account; use App\Utils\HtmlEngine; use App\Utils\Ninja; @@ -116,11 +117,15 @@ class QuoteEmailEngine extends BaseEmailEngine ->setTextBody($text_body); if ($this->client->getSetting('pdf_email_attachment') !== false && $this->quote->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { - if (Ninja::isHosted()) { - $this->setAttachments([$this->quote->pdf_file_path($this->invitation, 'url', true)]); - } else { - $this->setAttachments([$this->quote->pdf_file_path($this->invitation)]); - } + // if (Ninja::isHosted()) { + // $this->setAttachments([$this->quote->pdf_file_path($this->invitation, 'url', true)]); + // } else { + // $this->setAttachments([$this->quote->pdf_file_path($this->invitation)]); + // } + + $pdf = ((new CreateRawPdf($this->invitation, $this->invitation->company->db))->handle()); + + $this->setAttachments([['file' => base64_encode($pdf), 'name' => $this->quote->numberFormatter().'.pdf']]); } //attach third party documents @@ -128,11 +133,11 @@ class QuoteEmailEngine extends BaseEmailEngine // Storage::url foreach ($this->quote->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } foreach ($this->quote->company->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } } diff --git a/app/Mail/TemplateEmail.php b/app/Mail/TemplateEmail.php index 8a892db39840..eb8bdaaed718 100644 --- a/app/Mail/TemplateEmail.php +++ b/app/Mail/TemplateEmail.php @@ -120,31 +120,31 @@ class TemplateEmail extends Mailable /*In the hosted platform we need to slow things down a little for Storage to catch up.*/ - if(Ninja::isHosted() && $this->invitation){ + // if(Ninja::isHosted() && $this->invitation){ - $path = false; + // $path = false; - if($this->invitation->invoice) - $path = $this->client->invoice_filepath($this->invitation).$this->invitation->invoice->numberFormatter().'.pdf'; - elseif($this->invitation->quote) - $path = $this->client->quote_filepath($this->invitation).$this->invitation->quote->numberFormatter().'.pdf'; - elseif($this->invitation->credit) - $path = $this->client->credit_filepath($this->invitation).$this->invitation->credit->numberFormatter().'.pdf'; + // if($this->invitation->invoice) + // $path = $this->client->invoice_filepath($this->invitation).$this->invitation->invoice->numberFormatter().'.pdf'; + // elseif($this->invitation->quote) + // $path = $this->client->quote_filepath($this->invitation).$this->invitation->quote->numberFormatter().'.pdf'; + // elseif($this->invitation->credit) + // $path = $this->client->credit_filepath($this->invitation).$this->invitation->credit->numberFormatter().'.pdf'; - sleep(1); + // sleep(1); - if($path && !Storage::disk(config('filesystems.default'))->exists($path)){ + // if($path && !Storage::disk(config('filesystems.default'))->exists($path)){ - sleep(2); + // sleep(2); - if(!Storage::disk(config('filesystems.default'))->exists($path)) { - (new CreateEntityPdf($this->invitation))->handle(); - sleep(2); - } + // if(!Storage::disk(config('filesystems.default'))->exists($path)) { + // (new CreateEntityPdf($this->invitation))->handle(); + // sleep(2); + // } - } + // } - } + // } // $file = (new CreateRawPdf($invitation, $invitation->company->db))->handle(); diff --git a/app/Mail/VendorTemplateEmail.php b/app/Mail/VendorTemplateEmail.php index 670a59e93676..0c8dae6738f2 100644 --- a/app/Mail/VendorTemplateEmail.php +++ b/app/Mail/VendorTemplateEmail.php @@ -116,34 +116,37 @@ class VendorTemplateEmail extends Mailable //}); // ->tag($this->company->company_key); - if(Ninja::isHosted() && $this->invitation){ + // if(Ninja::isHosted() && $this->invitation){ - $path = false; + // $path = false; - if($this->invitation->purchase_order) - $path = $this->vendor->purchase_order_filepath($this->invitation).$this->invitation->purchase_order->numberFormatter().'.pdf'; + // if($this->invitation->purchase_order) + // $path = $this->vendor->purchase_order_filepath($this->invitation).$this->invitation->purchase_order->numberFormatter().'.pdf'; - sleep(1); + // sleep(1); - if($path && !Storage::disk(config('filesystems.default'))->exists($path)){ + // if($path && !Storage::disk(config('filesystems.default'))->exists($path)){ - sleep(2); + // sleep(2); - if(!Storage::disk(config('filesystems.default'))->exists($path)) { - (new CreatePurchaseOrderPdf($this->invitation))->handle(); - sleep(2); - } + // if(!Storage::disk(config('filesystems.default'))->exists($path)) { + // (new CreatePurchaseOrderPdf($this->invitation))->handle(); + // sleep(2); + // } - } + // } - } + // } foreach ($this->build_email->getAttachments() as $file) { - if (is_string($file)) { - $this->attach($file); - } elseif (is_array($file)) { - $this->attach($file['path'], ['as' => $file['name'], 'mime' => null]); - } + // if (is_string($file)) { + // $this->attach($file); + // } elseif (is_array($file)) { + // $this->attach($file['path'], ['as' => $file['name'], 'mime' => null]); + // } + + $this->attachData(base64_decode($file['file']), $file['name']); + } return $this; diff --git a/lang/en/texts.php b/lang/en/texts.php index 1bc294749e4a..10c511a9ab0a 100644 --- a/lang/en/texts.php +++ b/lang/en/texts.php @@ -4061,7 +4061,7 @@ $LANG = array( 'save_payment_method_details' => 'Save payment method details', 'new_card' => 'New card', 'new_bank_account' => 'New bank account', - 'company_limit_reached' => 'Limit of 10 companies per account.', + 'company_limit_reached' => 'Limit of :limit companies per account.', 'credits_applied_validation' => 'Total credits applied cannot be MORE than total of invoices', 'credit_number_taken' => 'Credit number already taken', 'credit_not_found' => 'Credit not found', From 448d3a22464c7bb0630bd8e94f33f096c18a6498 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 28 Nov 2022 15:45:25 +1100 Subject: [PATCH 4/5] Refactor for attachments --- app/Mail/Engine/InvoiceEmailEngine.php | 8 ++++---- app/Mail/Engine/PurchaseOrderEmailEngine.php | 8 ++++---- app/Mail/Engine/QuoteEmailEngine.php | 6 +++--- app/Mail/TemplateEmail.php | 7 +++++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/app/Mail/Engine/InvoiceEmailEngine.php b/app/Mail/Engine/InvoiceEmailEngine.php index fa88fa67a815..69810ba626ca 100644 --- a/app/Mail/Engine/InvoiceEmailEngine.php +++ b/app/Mail/Engine/InvoiceEmailEngine.php @@ -143,11 +143,11 @@ class InvoiceEmailEngine extends BaseEmailEngine // Storage::url foreach ($this->invoice->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); } foreach ($this->invoice->company->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); } $line_items = $this->invoice->line_items; @@ -165,7 +165,7 @@ class InvoiceEmailEngine extends BaseEmailEngine ->cursor() ->each(function ($expense) { foreach ($expense->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); } }); } @@ -181,7 +181,7 @@ class InvoiceEmailEngine extends BaseEmailEngine ->cursor() ->each(function ($task) { foreach ($task->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); } }); } diff --git a/app/Mail/Engine/PurchaseOrderEmailEngine.php b/app/Mail/Engine/PurchaseOrderEmailEngine.php index ce4a411b4840..f11e046b68e2 100644 --- a/app/Mail/Engine/PurchaseOrderEmailEngine.php +++ b/app/Mail/Engine/PurchaseOrderEmailEngine.php @@ -143,13 +143,13 @@ class PurchaseOrderEmailEngine extends BaseEmailEngine // Storage::url foreach ($this->purchase_order->documents as $document) { - // $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); + // $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } foreach ($this->purchase_order->company->documents as $document) { - // $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); + // $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); } } diff --git a/app/Mail/Engine/QuoteEmailEngine.php b/app/Mail/Engine/QuoteEmailEngine.php index 5b1387ed6980..63271c0600ba 100644 --- a/app/Mail/Engine/QuoteEmailEngine.php +++ b/app/Mail/Engine/QuoteEmailEngine.php @@ -123,7 +123,7 @@ class QuoteEmailEngine extends BaseEmailEngine // $this->setAttachments([$this->quote->pdf_file_path($this->invitation)]); // } - $pdf = ((new CreateRawPdf($this->invitation, $this->invitation->company->db))->handle()); + $pdf = ((new CreateRawPdf($this->invitation, $this->invitation->company->db))->handle()); $this->setAttachments([['file' => base64_encode($pdf), 'name' => $this->quote->numberFormatter().'.pdf']]); } @@ -133,11 +133,11 @@ class QuoteEmailEngine extends BaseEmailEngine // Storage::url foreach ($this->quote->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); } foreach ($this->quote->company->documents as $document) { - $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); + $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); } } diff --git a/app/Mail/TemplateEmail.php b/app/Mail/TemplateEmail.php index eb8bdaaed718..b5b443657f78 100644 --- a/app/Mail/TemplateEmail.php +++ b/app/Mail/TemplateEmail.php @@ -162,8 +162,11 @@ class TemplateEmail extends Mailable // // $this->attachData($file_data, $file['name']); // $this->attach($file['path'], ['as' => $file['name'], 'mime' => null]); // } - - $this->attachData(base64_decode($file['file']), $file['name']); + if(array_key_exists('file', $file)) + $this->attachData(base64_decode($file['file']), $file['name']); + else + $this->attach($file['path'], ['as' => $file['name'], 'mime' => null]); + } if ($this->invitation && $this->invitation->invoice && $settings->ubl_email_attachment && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { From e68bdeb4539912f3e4bc43407891ed7d0aeed57e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 30 Nov 2022 16:37:20 +1100 Subject: [PATCH 5/5] Minor cleanup --- app/Mail/VendorTemplateEmail.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/Mail/VendorTemplateEmail.php b/app/Mail/VendorTemplateEmail.php index 0c8dae6738f2..ccf9ef06955e 100644 --- a/app/Mail/VendorTemplateEmail.php +++ b/app/Mail/VendorTemplateEmail.php @@ -110,11 +110,7 @@ class VendorTemplateEmail extends Mailable 'whitelabel' => $this->vendor->user->account->isPaid() ? true : false, 'logo' => $this->company->present()->logo($settings), ]); - //->withSymfonyMessage(function ($message) { - // $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); - // $message->invitation = $this->invitation; - //}); - // ->tag($this->company->company_key); + // if(Ninja::isHosted() && $this->invitation){