From 417562b80db9a42ae563f11efac8b295b53fc135 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 27 Oct 2020 15:26:04 +1100 Subject: [PATCH] Fixes for phantom JS view invitation --- app/Jobs/Credit/CreateCreditPdf.php | 99 -------------------- app/Jobs/Invoice/CreateInvoicePdf.php | 129 -------------------------- app/Jobs/Quote/CreateQuotePdf.php | 121 ------------------------ app/Utils/PhantomJS/Phantom.php | 49 ++++++++-- 4 files changed, 39 insertions(+), 359 deletions(-) delete mode 100644 app/Jobs/Credit/CreateCreditPdf.php delete mode 100644 app/Jobs/Invoice/CreateInvoicePdf.php delete mode 100644 app/Jobs/Quote/CreateQuotePdf.php diff --git a/app/Jobs/Credit/CreateCreditPdf.php b/app/Jobs/Credit/CreateCreditPdf.php deleted file mode 100644 index 27ba0a9413a6..000000000000 --- a/app/Jobs/Credit/CreateCreditPdf.php +++ /dev/null @@ -1,99 +0,0 @@ -invitation = $invitation; - - $this->credit = $invitation->credit; - - $this->company = $invitation->company; - - $this->contact = $invitation->contact; - - $this->disk = $disk ?? config('filesystems.default'); - } - - public function handle() - { - if (config('ninja.phantomjs_key')) { - return (new Phantom)->generate($this->invitation); - } - - $this->credit->load('client'); - - App::setLocale($this->contact->preferredLocale()); - - $path = $this->credit->client->credit_filepath(); - - $file_path = $path.$this->credit->number.'.pdf'; - - $credit_design_id = $this->credit->design_id ? $this->credit->design_id : $this->decodePrimaryKey($this->credit->client->getSetting('credit_design_id')); - - $design = Design::find($credit_design_id); - - $designer = new Designer($this->credit, $design, $this->credit->client->getSetting('pdf_variables'), 'credit'); - - $html = (new HtmlEngine($designer, $this->invitation, 'credit'))->build(); - - Storage::makeDirectory($path, 0775); - - $pdf = $this->makePdf(null, null, $html); - - $instance = Storage::disk($this->disk)->put($file_path, $pdf); - - return $file_path; - } -} diff --git a/app/Jobs/Invoice/CreateInvoicePdf.php b/app/Jobs/Invoice/CreateInvoicePdf.php deleted file mode 100644 index 44ec8de2b3b0..000000000000 --- a/app/Jobs/Invoice/CreateInvoicePdf.php +++ /dev/null @@ -1,129 +0,0 @@ -invitation = $invitation; - - $this->invoice = $invitation->invoice; - - $this->company = $invitation->company; - - $this->contact = $invitation->contact; - - $this->disk = $disk ?? config('filesystems.default'); - } - - public function handle() - { - - if (config('ninja.phantomjs_key')) { - return (new Phantom)->generate($this->invitation); - } - - App::setLocale($this->contact->preferredLocale()); - - $path = $this->invoice->client->invoice_filepath(); - - $file_path = $path.$this->invoice->number.'.pdf'; - - $invoice_design_id = $this->invoice->design_id ? $this->invoice->design_id : $this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id')); - - $design = Design::find($invoice_design_id); - $html = new HtmlEngine(null, $this->invitation, 'invoice'); - - if ($design->is_custom) { - $options = [ - 'custom_partials' => json_decode(json_encode($design->design), true) - ]; - $template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options); - } else { - $template = new PdfMakerDesign(strtolower($design->name)); - } - - $state = [ - 'template' => $template->elements([ - 'client' => $this->invoice->client, - 'entity' => $this->invoice, - 'pdf_variables' => (array) $this->invoice->company->settings->pdf_variables, - 'products' => $design->design->product, - ]), - 'variables' => $html->generateLabelsAndValues(), - 'options' => [ - 'all_pages_header' => $this->invoice->client->getSetting('all_pages_header'), - 'all_pages_footer' => $this->invoice->client->getSetting('all_pages_footer'), - ], - ]; - - $maker = new PdfMakerService($state); - - $maker - ->design($template) - ->build(); - - //todo - move this to the client creation stage so we don't keep hitting this unnecessarily - Storage::makeDirectory($path, 0775); - - $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true)); - - $instance = Storage::disk($this->disk)->put($file_path, $pdf); - - return $file_path; - } -} diff --git a/app/Jobs/Quote/CreateQuotePdf.php b/app/Jobs/Quote/CreateQuotePdf.php deleted file mode 100644 index 4a3bad8e29ce..000000000000 --- a/app/Jobs/Quote/CreateQuotePdf.php +++ /dev/null @@ -1,121 +0,0 @@ -invitation = $invitation; - - $this->quote = $invitation->quote; - - $this->company = $invitation->company; - - $this->contact = $invitation->contact; - - $this->disk = $disk ?? config('filesystems.default'); - } - - public function handle() - { - if (config('ninja.phantomjs_key')) { - return (new Phantom)->generate($this->invitation); - } - - $this->quote->load('client'); - - App::setLocale($this->contact->preferredLocale()); - - $path = $this->quote->client->quote_filepath(); - - $quote_design_id = $this->quote->design_id ? $this->quote->design_id : $this->decodePrimaryKey($this->quote->client->getSetting('quote_design_id')); - - $design = Design::find($quote_design_id); - $html = new HtmlEngine(null, $this->invitation, 'quote'); - - $template = new PdfMakerDesign(strtolower($design->name)); - - $state = [ - 'template' => $template->elements([ - 'client' => $this->quote->client, - 'entity' => $this->quote, - 'pdf_variables' => (array) $this->quote->company->settings->pdf_variables, - 'products' => $design->design->product, - ]), - 'variables' => $html->generateLabelsAndValues(), - 'options' => [ - 'all_pages_header' => $this->quote->client->getSetting('all_pages_header'), - 'all_pages_footer' => $this->quote->client->getSetting('all_pages_footer'), - ], - ]; - - $maker = new PdfMakerService($state); - - $maker - ->design($template) - ->build(); - - //todo - move this to the client creation stage so we don't keep hitting this unnecessarily - Storage::makeDirectory($path, 0775); - - $pdf = $this->makePdf(null, null, $maker->getCompiledHTML()); - - $file_path = $path.$this->quote->number.'.pdf'; - - $instance = Storage::disk($this->disk)->put($file_path, $pdf); - - return $file_path; - } -} diff --git a/app/Utils/PhantomJS/Phantom.php b/app/Utils/PhantomJS/Phantom.php index 68f3b32c21cf..2334a85a99f0 100644 --- a/app/Utils/PhantomJS/Phantom.php +++ b/app/Utils/PhantomJS/Phantom.php @@ -12,10 +12,13 @@ namespace App\Utils\PhantomJS; use App\Designs\Designer; +use App\Services\PdfMaker\Design as PdfDesignModel; +use App\Services\PdfMaker\PdfMaker as PdfMakerService; use App\Models\CreditInvitation; use App\Models\Design; use App\Models\InvoiceInvitation; use App\Models\QuoteInvitation; +use App\Services\PdfMaker\Design as PdfMakerDesign; use App\Utils\HtmlEngine; use App\Utils\Traits\MakesHash; use Illuminate\Support\Facades\App; @@ -79,28 +82,54 @@ class Phantom public function displayInvitation(string $entity, string $invitation_key) { + $key = $entity.'_id'; $invitation_instance = 'App\Models\\'.ucfirst($entity).'Invitation'; $invitation = $invitation_instance::whereRaw('BINARY `key`= ?', [$invitation_key])->first(); - - + $entity_obj = $invitation->{$entity}; $entity_obj->load('client'); App::setLocale($invitation->contact->preferredLocale()); - // $design_id = $entity_obj->design_id ? $entity_obj->design_id : $this->decodePrimaryKey($entity_obj->client->getSetting($entity_design_id)); - // $design = Design::find($design_id); - // $designer = new Designer($entity_obj, $design, $entity_obj->client->getSetting('pdf_variables'), $entity); - // $data['html'] = (new HtmlEngine($designer, $invitation, $entity))->build(); - $entity_design_id = $entity . '_design_id'; - $entity_design_id = $entity_obj->design_id ? $entity_obj->design_id : $this->decodePrimaryKey($entity_obj->client->getSetting($entity_design_id)); - $design = Design::find($entity_design_id); - $data['html'] = new HtmlEngine(null, $invitation, $entity); + $design_id = $entity_obj->design_id ? $entity_obj->design_id : $this->decodePrimaryKey($entity_obj->client->getSetting($entity_design_id)); + + $design = Design::find($design_id); + $html = new HtmlEngine(null, $invitation, $entity); + + if ($design->is_custom) { + $options = [ + 'custom_partials' => json_decode(json_encode($design->design), true) + ]; + $template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options); + } else { + $template = new PdfMakerDesign(strtolower($design->name)); + } + + $state = [ + 'template' => $template->elements([ + 'client' => $this->entity->client, + 'entity' => $this->entity, + 'pdf_variables' => (array) $this->entity->company->settings->pdf_variables, + 'products' => $design->design->product, + ]), + 'variables' => $html->generateLabelsAndValues(), + 'options' => [ + 'all_pages_header' => $entity_obj->client->getSetting('all_pages_header'), + 'all_pages_footer' => $entity_obj->client->getSetting('all_pages_footer'), + ], + ]; + + $maker = new PdfMakerService($state); + + $data['html'] = $maker->design($template) + ->build() + ->getCompiledHTML(true); + return view('pdf.html', $data); }