refactor for data instead of file paths

This commit is contained in:
David Bomba 2022-11-28 12:16:58 +11:00
parent 1ba73a3a54
commit 778c30f7ed
4 changed files with 41 additions and 21 deletions

View File

@ -11,6 +11,7 @@
namespace App\Filters; namespace App\Filters;
use App\Models\Company;
use App\Models\User; use App\Models\User;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
@ -54,6 +55,15 @@ class DocumentFilters extends QueryFilters
return $this->builder->orderBy($sort_col[0], $sort_col[1]); 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. * Returns the base query.
* *

View File

@ -13,6 +13,7 @@ namespace App\Mail\Engine;
use App\DataMapper\EmailTemplateDefaults; use App\DataMapper\EmailTemplateDefaults;
use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Entity\CreateEntityPdf;
use App\Jobs\Entity\CreateRawPdf;
use App\Models\Account; use App\Models\Account;
use App\Models\Expense; use App\Models\Expense;
use App\Models\Task; use App\Models\Task;
@ -126,11 +127,15 @@ class InvoiceEmailEngine extends BaseEmailEngine
->setTextBody($text_body); ->setTextBody($text_body);
if ($this->client->getSetting('pdf_email_attachment') !== false && $this->invoice->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { if ($this->client->getSetting('pdf_email_attachment') !== false && $this->invoice->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
if (Ninja::isHosted()) { // if (Ninja::isHosted()) {
$this->setAttachments([$this->invoice->pdf_file_path($this->invitation, 'url', true)]); // $this->setAttachments([$this->invoice->pdf_file_path($this->invitation, 'url', true)]);
} else { // } else {
$this->setAttachments([$this->invoice->pdf_file_path($this->invitation)]); // $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 //attach third party documents
@ -138,11 +143,11 @@ class InvoiceEmailEngine extends BaseEmailEngine
// Storage::url // Storage::url
foreach ($this->invoice->documents as $document) { 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) { 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; $line_items = $this->invoice->line_items;
@ -160,7 +165,7 @@ class InvoiceEmailEngine extends BaseEmailEngine
->cursor() ->cursor()
->each(function ($expense) { ->each(function ($expense) {
foreach ($expense->documents as $document) { 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() ->cursor()
->each(function ($task) { ->each(function ($task) {
foreach ($task->documents as $document) { 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()]]);
} }
}); });
} }

View File

@ -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 //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) { foreach ($this->build_email->getAttachments() as $file) {
if (is_string($file)) { // if (is_string($file)) {
// nlog($file); // // nlog($file);
// $file_data = file_get_contents($file); // // $file_data = file_get_contents($file);
// $this->attachData($file_data, basename($file)); // // $this->attachData($file_data, basename($file));
$this->attach($file); // $this->attach($file);
} elseif (is_array($file)) { // } elseif (is_array($file)) {
// nlog($file['path']); // // nlog($file['path']);
// $file_data = file_get_contents($file['path']); // // $file_data = file_get_contents($file['path']);
// $this->attachData($file_data, $file['name']); // // $this->attachData($file_data, $file['name']);
$this->attach($file['path'], ['as' => $file['name'], 'mime' => null]); // $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)) { if ($this->invitation && $this->invitation->invoice && $settings->ubl_email_attachment && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {

View File

@ -28,6 +28,6 @@ class NinjaPdf
RequestOptions::JSON => ['html' => $html], RequestOptions::JSON => ['html' => $html],
]); ]);
return $response->getBody(); return $response->getBody()->getContents();
} }
} }