diff --git a/app/Http/Controllers/PreviewController.php b/app/Http/Controllers/PreviewController.php index 2797bae0eea1..15c5f1612a24 100644 --- a/app/Http/Controllers/PreviewController.php +++ b/app/Http/Controllers/PreviewController.php @@ -68,24 +68,34 @@ class PreviewController extends BaseController public function live(PreviewInvoiceRequest $request) { - $time = time(); - nlog($time); + $start = microtime(true); +nlog("1 ".$start); $invitation = $request->resolveInvitation(); + $client = $request->getClient(); + $settings = $client->getMergedSettings(); App::forgetInstance('translator'); $t = app('translator'); App::setLocale($invitation->contact->preferredLocale()); - $t->replace(Ninja::transformTranslations($invitation->{$request->entity}->client->getMergedSettings())); - - $html = new HtmlEngine($invitation); - $variables = $html->generateLabelsAndValues(); + $t->replace(Ninja::transformTranslations($settings)); +nlog("2 ".microtime(true)); + $entity_prop = str_replace("recurring_", "", $request->entity); $entity_obj = $invitation->{$request->entity}; - // if (! $invitation->{$request->entity}->id ?? true) { - // $invitation->{$request->entity}->service()->fillDefaults(); - // } + if(!$entity_obj->id) { + $entity_obj->design_id = intval($this->decodePrimaryKey($settings->{$entity_prop."_design_id"})); + $entity_obj->footer = $settings->{$entity_prop."_footer"}; + $entity_obj->terms = $settings->{$entity_prop."_terms"}; + $entity_obj->public_notes = $request->getClient()->public_notes; + $invitation->{$request->entity} = $entity_obj; + } + + $html = new HtmlEngine($invitation); + $html->settings = $settings; + $variables = $html->generateLabelsAndValues(); +nlog("3 ".microtime(true)); $design = \App\Models\Design::withTrashed()->find($entity_obj->design_id ?? 2); @@ -105,18 +115,18 @@ class PreviewController extends BaseController $state = [ 'template' => $template->elements([ - 'client' => $entity_obj->client, + 'client' => $client, 'entity' => $entity_obj, - 'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables, + 'pdf_variables' => (array) $settings->pdf_variables, '$product' => $design->design->product, 'variables' => $variables, ]), 'variables' => $variables, 'options' => [ - 'all_pages_header' => $entity_obj->client->getSetting('all_pages_header'), - 'all_pages_footer' => $entity_obj->client->getSetting('all_pages_footer'), + 'all_pages_header' => $client->getSetting('all_pages_header'), + 'all_pages_footer' => $client->getSetting('all_pages_footer'), ], - 'process_markdown' => $entity_obj->client->company->markdown_enabled, + 'process_markdown' => $client->company->markdown_enabled, ]; $maker = new PdfMaker($state); @@ -125,6 +135,8 @@ class PreviewController extends BaseController ->design($template) ->build(); +nlog("4 ".microtime(true)); + if (request()->query('html') == 'true') { return $maker->getCompiledHTML(); } @@ -153,9 +165,12 @@ class PreviewController extends BaseController $pdf = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle(); +nlog("5 ".microtime(true)); +nlog("total = ".microtime(true)-$start); + return response()->streamDownload(function () use ($pdf) { echo $pdf; - }, 'preview.pdf', ['Content-Type' => 'application/pdf','Cache-Control:' => 'no-cache']); + }, 'preview.pdf', ['Content-Disposition' => 'inline', 'Content-Type' => 'application/pdf','Cache-Control:' => 'no-cache', 'Server-Timing' => microtime(true)-$start]); } diff --git a/app/Http/Requests/Preview/PreviewInvoiceRequest.php b/app/Http/Requests/Preview/PreviewInvoiceRequest.php index 2188efa37c98..57bd0cf22ef0 100644 --- a/app/Http/Requests/Preview/PreviewInvoiceRequest.php +++ b/app/Http/Requests/Preview/PreviewInvoiceRequest.php @@ -15,9 +15,7 @@ use App\Models\Quote; use App\Models\Client; use App\Models\Credit; use App\Models\Invoice; -use App\Libraries\MultiDB; use App\Http\Requests\Request; -use App\Models\CompanyGateway; use App\Models\QuoteInvitation; use App\Utils\Traits\MakesHash; use Illuminate\Validation\Rule; @@ -34,6 +32,8 @@ class PreviewInvoiceRequest extends Request private string $entity_plural = ''; + private ?Client $client = null; + /** * Determine if the user is authorized to make this request. * @@ -104,9 +104,25 @@ class PreviewInvoiceRequest extends Request $invitation = $this->stubInvitation(); } + public function getClient(): ?Client + { + if(!$this->client) + $this->client = Client::query()->with('contacts', 'company', 'user')->withTrashed()->find($this->client_id); + + return $this->client; + } + + public function setClient(Client $client): self + { + $this->client = $client; + + return $this; + } + public function stubInvitation() { $client = Client::query()->with('contacts', 'company', 'user')->withTrashed()->find($this->client_id); + $this->setClient($client); $invitation = false; match($this->entity) { diff --git a/database/factories/RecurringInvoiceInvitationFactory.php b/database/factories/RecurringInvoiceInvitationFactory.php new file mode 100644 index 000000000000..fe7db151f11d --- /dev/null +++ b/database/factories/RecurringInvoiceInvitationFactory.php @@ -0,0 +1,30 @@ + Str::random(40), + ]; + } +}