mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 04:14:33 -04:00
Refactor for live previews
This commit is contained in:
parent
e17af36cf4
commit
ec04f3fd1e
@ -68,24 +68,34 @@ class PreviewController extends BaseController
|
|||||||
public function live(PreviewInvoiceRequest $request)
|
public function live(PreviewInvoiceRequest $request)
|
||||||
{
|
{
|
||||||
|
|
||||||
$time = time();
|
$start = microtime(true);
|
||||||
nlog($time);
|
|
||||||
|
|
||||||
|
nlog("1 ".$start);
|
||||||
$invitation = $request->resolveInvitation();
|
$invitation = $request->resolveInvitation();
|
||||||
|
$client = $request->getClient();
|
||||||
|
$settings = $client->getMergedSettings();
|
||||||
|
|
||||||
App::forgetInstance('translator');
|
App::forgetInstance('translator');
|
||||||
$t = app('translator');
|
$t = app('translator');
|
||||||
App::setLocale($invitation->contact->preferredLocale());
|
App::setLocale($invitation->contact->preferredLocale());
|
||||||
$t->replace(Ninja::transformTranslations($invitation->{$request->entity}->client->getMergedSettings()));
|
$t->replace(Ninja::transformTranslations($settings));
|
||||||
|
|
||||||
$html = new HtmlEngine($invitation);
|
|
||||||
$variables = $html->generateLabelsAndValues();
|
|
||||||
|
|
||||||
|
nlog("2 ".microtime(true));
|
||||||
|
$entity_prop = str_replace("recurring_", "", $request->entity);
|
||||||
$entity_obj = $invitation->{$request->entity};
|
$entity_obj = $invitation->{$request->entity};
|
||||||
|
|
||||||
// if (! $invitation->{$request->entity}->id ?? true) {
|
if(!$entity_obj->id) {
|
||||||
// $invitation->{$request->entity}->service()->fillDefaults();
|
$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);
|
$design = \App\Models\Design::withTrashed()->find($entity_obj->design_id ?? 2);
|
||||||
|
|
||||||
@ -105,18 +115,18 @@ class PreviewController extends BaseController
|
|||||||
|
|
||||||
$state = [
|
$state = [
|
||||||
'template' => $template->elements([
|
'template' => $template->elements([
|
||||||
'client' => $entity_obj->client,
|
'client' => $client,
|
||||||
'entity' => $entity_obj,
|
'entity' => $entity_obj,
|
||||||
'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables,
|
'pdf_variables' => (array) $settings->pdf_variables,
|
||||||
'$product' => $design->design->product,
|
'$product' => $design->design->product,
|
||||||
'variables' => $variables,
|
'variables' => $variables,
|
||||||
]),
|
]),
|
||||||
'variables' => $variables,
|
'variables' => $variables,
|
||||||
'options' => [
|
'options' => [
|
||||||
'all_pages_header' => $entity_obj->client->getSetting('all_pages_header'),
|
'all_pages_header' => $client->getSetting('all_pages_header'),
|
||||||
'all_pages_footer' => $entity_obj->client->getSetting('all_pages_footer'),
|
'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);
|
$maker = new PdfMaker($state);
|
||||||
@ -125,6 +135,8 @@ class PreviewController extends BaseController
|
|||||||
->design($template)
|
->design($template)
|
||||||
->build();
|
->build();
|
||||||
|
|
||||||
|
nlog("4 ".microtime(true));
|
||||||
|
|
||||||
if (request()->query('html') == 'true') {
|
if (request()->query('html') == 'true') {
|
||||||
return $maker->getCompiledHTML();
|
return $maker->getCompiledHTML();
|
||||||
}
|
}
|
||||||
@ -153,9 +165,12 @@ class PreviewController extends BaseController
|
|||||||
|
|
||||||
$pdf = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
|
$pdf = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
|
||||||
|
|
||||||
|
nlog("5 ".microtime(true));
|
||||||
|
nlog("total = ".microtime(true)-$start);
|
||||||
|
|
||||||
return response()->streamDownload(function () use ($pdf) {
|
return response()->streamDownload(function () use ($pdf) {
|
||||||
echo $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]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,9 +15,7 @@ use App\Models\Quote;
|
|||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
use App\Models\Credit;
|
use App\Models\Credit;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Libraries\MultiDB;
|
|
||||||
use App\Http\Requests\Request;
|
use App\Http\Requests\Request;
|
||||||
use App\Models\CompanyGateway;
|
|
||||||
use App\Models\QuoteInvitation;
|
use App\Models\QuoteInvitation;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
@ -34,6 +32,8 @@ class PreviewInvoiceRequest extends Request
|
|||||||
|
|
||||||
private string $entity_plural = '';
|
private string $entity_plural = '';
|
||||||
|
|
||||||
|
private ?Client $client = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the user is authorized to make this request.
|
* Determine if the user is authorized to make this request.
|
||||||
*
|
*
|
||||||
@ -104,9 +104,25 @@ class PreviewInvoiceRequest extends Request
|
|||||||
$invitation = $this->stubInvitation();
|
$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()
|
public function stubInvitation()
|
||||||
{
|
{
|
||||||
$client = Client::query()->with('contacts', 'company', 'user')->withTrashed()->find($this->client_id);
|
$client = Client::query()->with('contacts', 'company', 'user')->withTrashed()->find($this->client_id);
|
||||||
|
$this->setClient($client);
|
||||||
$invitation = false;
|
$invitation = false;
|
||||||
|
|
||||||
match($this->entity) {
|
match($this->entity) {
|
||||||
|
30
database/factories/RecurringInvoiceInvitationFactory.php
Normal file
30
database/factories/RecurringInvoiceInvitationFactory.php
Normal 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),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user