1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Refactor for live previews

This commit is contained in:
David Bomba 2023-10-19 08:53:28 +11:00
parent e17af36cf4
commit ec04f3fd1e
3 changed files with 78 additions and 17 deletions

View File

@ -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]);
}

View File

@ -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) {

View File

@ -0,0 +1,30 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class RecurringInvoiceInvitationFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'key' => Str::random(40),
];
}
}