From 4443b13218f5b29d5853585c76703296bb0cee31 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 27 Sep 2021 11:40:17 +1000 Subject: [PATCH] Performance improvements for PDFs --- .../ClientPortal/InvitationController.php | 38 +++++++++++++++++-- app/Jobs/Entity/CreateEntityPdf.php | 3 -- app/Listeners/Credit/CreditViewedActivity.php | 2 + .../Invoice/InvoiceViewedActivity.php | 2 + app/Listeners/Quote/QuoteViewedActivity.php | 2 + 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/InvitationController.php b/app/Http/Controllers/ClientPortal/InvitationController.php index 1fbf37bedd..4a8f1fe4ee 100644 --- a/app/Http/Controllers/ClientPortal/InvitationController.php +++ b/app/Http/Controllers/ClientPortal/InvitationController.php @@ -16,6 +16,7 @@ use App\Events\Invoice\InvoiceWasViewed; use App\Events\Misc\InvitationWasViewed; use App\Events\Quote\QuoteWasViewed; use App\Http\Controllers\Controller; +use App\Jobs\Entity\CreateRawPdf; use App\Models\Client; use App\Models\ClientContact; use App\Models\Payment; @@ -106,15 +107,12 @@ class InvitationController extends Controller { switch ($entity_string) { case 'invoice': - $invitation->invoice->service()->markSent()->save(); event(new InvoiceWasViewed($invitation, $invitation->company, Ninja::eventVars())); break; case 'quote': - $invitation->quote->service()->markSent()->save(); event(new QuoteWasViewed($invitation, $invitation->company, Ninja::eventVars())); break; case 'credit': - $invitation->credit->service()->markSent()->save(); event(new CreditWasViewed($invitation, $invitation->company, Ninja::eventVars())); break; default: @@ -125,9 +123,43 @@ class InvitationController extends Controller public function routerForDownload(string $entity, string $invitation_key) { + + if(Ninja::isHosted()) + return $this->returnRawPdf($entity, $invitation_key); + return redirect('client/'.$entity.'/'.$invitation_key.'/download_pdf'); } + private function returnRawPdf(string $entity, string $invitation_key) + { + + $key = $entity.'_id'; + + $entity_obj = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation'; + + $invitation = $entity_obj::whereRaw('BINARY `key`= ?', [$invitation_key]) + ->with('contact.client') + ->firstOrFail(); + + if(!$invitation) + return response()->json(["message" => "no record found"], 400); + + $file_name = $invitation->{$entity}->numberFormatter().'.pdf'; + nlog($file_name); + + $file = CreateRawPdf::dispatchNow($invitation, $invitation->company->db); + + $headers = ['Content-Type' => 'application/pdf']; + + if(request()->input('inline') == 'true') + $headers = array_merge($headers, ['Content-Disposition' => 'inline']); + + return response()->streamDownload(function () use($file) { + echo $file; + }, $file_name, $headers); + + } + public function routerForIframe(string $entity, string $client_hash, string $invitation_key) { } diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index fe628350d0..bfa4e000c9 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -104,9 +104,6 @@ class CreateEntityPdf implements ShouldQueue /* Set customized translations _NOW_ */ $t->replace(Ninja::transformTranslations($this->entity->client->getMergedSettings())); - /*This line of code hurts... it deletes ALL $entity PDFs... this causes a race condition when trying to send an email*/ - // $this->entity->service()->deletePdf(); - if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') { return (new Phantom)->generate($this->invitation); } diff --git a/app/Listeners/Credit/CreditViewedActivity.php b/app/Listeners/Credit/CreditViewedActivity.php index 76bb71915d..bce928d8ef 100644 --- a/app/Listeners/Credit/CreditViewedActivity.php +++ b/app/Listeners/Credit/CreditViewedActivity.php @@ -41,6 +41,8 @@ class CreditViewedActivity implements ShouldQueue { MultiDB::setDb($event->company->db); + $event->invitation->credit->service()->markSent()->save(); + $fields = new stdClass; $user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->invitation->user_id; diff --git a/app/Listeners/Invoice/InvoiceViewedActivity.php b/app/Listeners/Invoice/InvoiceViewedActivity.php index 6336734a91..c876b28fd6 100644 --- a/app/Listeners/Invoice/InvoiceViewedActivity.php +++ b/app/Listeners/Invoice/InvoiceViewedActivity.php @@ -45,6 +45,8 @@ class InvoiceViewedActivity implements ShouldQueue $user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->invitation->invoice->user_id; + $event->invitation->invoice->service()->markSent()->save(); + $fields->user_id = $user_id; $fields->company_id = $event->invitation->company_id; $fields->activity_type_id = Activity::VIEW_INVOICE; diff --git a/app/Listeners/Quote/QuoteViewedActivity.php b/app/Listeners/Quote/QuoteViewedActivity.php index c20fb2ee87..8c7041f26a 100644 --- a/app/Listeners/Quote/QuoteViewedActivity.php +++ b/app/Listeners/Quote/QuoteViewedActivity.php @@ -41,6 +41,8 @@ class QuoteViewedActivity implements ShouldQueue { MultiDB::setDb($event->company->db); + $event->invitation->quote->service()->markSent()->save(); + $fields = new stdClass; $fields->user_id = $event->invitation->quote->user_id;