1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Http/Controllers/PreviewController.php

523 lines
17 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
2024-06-04 11:15:23 +02:00
use App\Utils\Ninja;
2023-11-26 08:41:42 +01:00
use App\Models\Client;
use App\Models\Invoice;
2024-06-04 11:15:23 +02:00
use App\Utils\HtmlEngine;
use Illuminate\Support\Str;
use Twig\Error\SyntaxError;
use App\Jobs\Util\PreviewPdf;
use App\Models\ClientContact;
2023-10-26 04:57:44 +02:00
use App\Services\Pdf\PdfMock;
2024-06-04 11:15:23 +02:00
use App\Utils\Traits\MakesHash;
2023-10-27 12:09:53 +02:00
use App\Services\Pdf\PdfService;
2024-06-04 11:15:23 +02:00
use App\Utils\PhantomJS\Phantom;
use App\Models\InvoiceInvitation;
2023-02-16 02:36:09 +01:00
use App\Services\PdfMaker\Design;
2023-11-26 08:41:42 +01:00
use App\Utils\HostedPDF\NinjaPdf;
2024-06-04 11:15:23 +02:00
use Illuminate\Support\Facades\DB;
use App\Services\PdfMaker\PdfMaker;
use Illuminate\Support\Facades\App;
use App\Utils\Traits\GeneratesCounter;
2023-10-27 12:09:53 +02:00
use App\Utils\Traits\MakesInvoiceHtml;
2024-06-04 11:15:23 +02:00
use Turbo124\Beacon\Facades\LightLogs;
2023-10-27 12:09:53 +02:00
use App\Utils\Traits\Pdf\PageNumbering;
use Illuminate\Support\Facades\Response;
2024-06-04 11:15:23 +02:00
use App\DataMapper\Analytics\LivePreview;
use App\Services\Template\TemplateService;
use App\Http\Requests\Preview\ShowPreviewRequest;
use App\Http\Requests\Preview\DesignPreviewRequest;
use App\Http\Requests\Preview\PreviewInvoiceRequest;
use App\Utils\VendorHtmlEngine;
class PreviewController extends BaseController
{
use GeneratesCounter;
use MakesHash;
use MakesInvoiceHtml;
2022-05-26 23:32:13 +02:00
use PageNumbering;
public function __construct()
{
2023-10-26 04:57:44 +02:00
parent::__construct();
2023-10-17 12:28:18 +02:00
}
2024-01-14 05:05:00 +01:00
2023-10-27 12:09:53 +02:00
public function live(PreviewInvoiceRequest $request): mixed
2023-10-26 04:57:44 +02:00
{
2023-10-27 12:09:53 +02:00
$start = microtime(true);
/** Build models */
$invitation = $request->resolveInvitation();
$client = $request->getClient();
$settings = $client->getMergedSettings();
$entity_prop = str_replace("recurring_", "", $request->entity);
$entity_obj = $invitation->{$request->entity};
$entity_obj->fill($request->all());
if(!$entity_obj->id) {
$entity_obj->design_id = intval($this->decodePrimaryKey($settings->{$entity_prop."_design_id"}));
$entity_obj->footer = empty($entity_obj->footer) ? $settings->{$entity_prop."_footer"} : $entity_obj->footer;
$entity_obj->terms = empty($entity_obj->terms) ? $settings->{$entity_prop."_terms"} : $entity_obj->terms;
$entity_obj->public_notes = empty($entity_obj->public_notes) ? $request->getClient()->public_notes : $entity_obj->public_notes;
$invitation->setRelation($request->entity, $entity_obj);
}
$ps = new PdfService($invitation, 'product', [
'client' => $client ?? false,
"{$entity_prop}s" => [$entity_obj],
]);
$pdf = $ps->boot()->getPdf();
2023-10-19 01:44:19 +02:00
if (Ninja::isHosted()) {
LightLogs::create(new LivePreview())
->increment()
->batch();
}
2023-10-18 23:53:28 +02:00
2023-10-19 01:44:19 +02:00
/** Return PDF */
2023-10-17 12:28:18 +02:00
return response()->streamDownload(function () use ($pdf) {
echo $pdf;
2023-10-19 01:44:19 +02:00
}, 'preview.pdf', [
2023-10-26 04:57:44 +02:00
'Content-Disposition' => 'inline',
'Content-Type' => 'application/pdf',
'Cache-Control:' => 'no-cache',
2024-01-14 05:05:00 +01:00
'Server-Timing' => microtime(true) - $start
2023-10-19 01:44:19 +02:00
]);
2023-10-17 12:28:18 +02:00
}
/**
2023-10-19 01:44:19 +02:00
* Returns the mocked PDF for the invoice design preview.
*
2023-10-19 01:44:19 +02:00
* Only used in Settings > Invoice Design as a general overview
2023-10-26 04:57:44 +02:00
*
2023-10-19 01:44:19 +02:00
* @param DesignPreviewRequest $request
2023-04-26 14:17:40 +02:00
* @return mixed
*/
2023-10-19 01:44:19 +02:00
public function design(DesignPreviewRequest $request): mixed
{
$start = microtime(true);
2023-11-28 14:27:04 +01:00
if($request->has('entity_type') && in_array($request->entity_type, ['payment_receipt', 'payment_refund', 'statement', 'delivery_note'])) {
return $this->liveTemplate($request->all());
}
2023-10-19 01:44:19 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
/** @var \App\Models\Company $company */
$company = $user->company();
$pdf = (new PdfMock($request->all(), $company))->build()->getPdf();
$response = Response::make($pdf, 200);
$response->header('Content-Type', 'application/pdf');
2024-01-14 05:05:00 +01:00
$response->header('Server-Timing', microtime(true) - $start);
2023-10-19 01:44:19 +02:00
return $response;
}
2023-04-26 14:17:40 +02:00
2023-10-19 01:44:19 +02:00
/**
* Returns a template filled with entity variables.
2023-10-26 04:57:44 +02:00
*
2023-10-19 01:44:19 +02:00
* Used in the Custom Designer to preview design changes
* @return mixed
*/
2024-03-25 05:05:30 +01:00
public function show(ShowPreviewRequest $request)
{
2024-06-04 11:11:06 +02:00
2024-03-25 05:05:30 +01:00
if($request->input('design.is_template')) {
2023-09-25 07:56:32 +02:00
return $this->template();
2023-11-22 05:11:08 +01:00
}
2023-09-25 07:56:32 +02:00
if (request()->has('entity') &&
request()->has('entity_id') &&
! empty(request()->input('entity')) &&
2024-06-14 09:09:44 +02:00
! empty(request()->input('entity_id'))) {
2023-10-19 01:44:19 +02:00
$design_object = json_decode(json_encode(request()->input('design')));
2020-03-09 12:12:45 +01:00
if (! is_object($design_object)) {
2021-01-25 00:04:50 +01:00
return response()->json(['message' => ctrans('texts.invalid_design_object')], 400);
}
2020-03-09 12:12:45 +01:00
2024-06-04 11:15:23 +02:00
$entity = Str::camel(request()->input('entity'));
$class = "App\Models\\$entity";
$entity_obj = $class::whereId($this->decodePrimaryKey(request()->input('entity_id')))->company()->first();
if (! $entity_obj) {
return $this->blankEntity();
}
$entity_obj->load('client');
App::forgetInstance('translator');
2021-05-31 12:40:34 +02:00
$t = app('translator');
2024-06-04 11:11:06 +02:00
App::setLocale($entity_obj->client->preferredLocale());
2021-05-31 12:40:34 +02:00
$t->replace(Ninja::transformTranslations($entity_obj->client->getMergedSettings()));
2024-06-14 09:09:44 +02:00
if($entity_obj->client) {
2024-06-04 11:15:23 +02:00
$html = new HtmlEngine($entity_obj->invitations()->first());
2024-06-14 09:09:44 +02:00
} else {
2024-06-04 11:15:23 +02:00
$html = new VendorHtmlEngine($entity_obj->invitations()->first());
}
2024-06-04 11:11:06 +02:00
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
2020-08-21 16:47:17 +02:00
$state = [
2024-06-04 11:11:06 +02:00
'template' => $design->elements([
2020-08-21 16:47:17 +02:00
'client' => $entity_obj->client,
'entity' => $entity_obj,
'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables,
'products' => request()->design['design']['product'],
2020-08-21 16:47:17 +02:00
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $entity_obj->client->company->markdown_enabled,
2023-09-19 02:05:13 +02:00
'options' => [
2023-10-26 08:13:06 +02:00
'client' => $entity_obj->client ?? [],
'vendor' => $entity_obj->vendor ?? [],
request()->input('entity_type', 'invoice')."s" => [$entity_obj],
2023-09-19 02:05:13 +02:00
]
2020-08-21 16:47:17 +02:00
];
$maker = new PdfMaker($state);
$maker
2020-09-04 13:17:30 +02:00
->design($design)
2020-08-21 16:47:17 +02:00
->build();
2024-01-14 05:05:00 +01:00
if (request()->query('html') == 'true') {
2022-03-16 21:57:06 +01:00
return $maker->getCompiledHTML();
2020-12-16 12:52:40 +01:00
}
2020-12-11 21:51:10 +01:00
2020-11-27 03:02:05 +01:00
//if phantom js...... inject here..
2021-05-09 13:30:31 +02:00
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2024-01-14 05:05:00 +01:00
return (new Phantom())->convertHtmlToPdf($maker->getCompiledHTML(true));
2020-11-27 03:02:05 +01:00
}
2023-04-27 00:05:57 +02:00
/** @var \App\Models\User $user */
2023-04-26 16:16:07 +02:00
$user = auth()->user();
$company = $user->company();
2023-04-26 16:11:31 +02:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2023-04-26 16:11:31 +02:00
2022-05-26 23:32:13 +02:00
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
2023-04-26 16:11:31 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $company);
2023-10-26 04:57:44 +02:00
if ($numbered_pdf) {
2022-05-26 23:32:13 +02:00
$pdf = $numbered_pdf;
2023-10-26 04:57:44 +02:00
}
2022-05-26 23:32:13 +02:00
return $pdf;
2020-11-27 03:02:05 +01:00
2023-10-19 01:44:19 +02:00
}
2023-10-19 01:44:19 +02:00
$pdf = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
2023-04-27 00:05:57 +02:00
2023-10-19 01:44:19 +02:00
return response()->streamDownload(function () use ($pdf) {
echo $pdf;
}, 'preview.pdf', [
'Content-Disposition' => 'inline',
'Content-Type' => 'application/pdf',
'Cache-Control:' => 'no-cache',
]);
2023-04-26 14:17:40 +02:00
2023-02-21 10:04:45 +01:00
2023-10-19 01:44:19 +02:00
}
2023-02-21 10:04:45 +01:00
2023-10-19 01:44:19 +02:00
return $this->blankEntity();
2023-02-21 10:04:45 +01:00
}
2023-11-28 14:27:04 +01:00
private function liveTemplate(array $request_data)
2023-09-25 07:56:32 +02:00
{
/** @var \App\Models\User $user */
$user = auth()->user();
/** @var \App\Models\Company $company */
$company = $user->company();
2023-11-28 14:27:04 +01:00
$design = \App\Models\Design::query()
->where('id', $request_data['design_id'])
2023-11-30 07:59:17 +01:00
->where(function ($q) use ($user) {
2023-11-28 14:27:04 +01:00
$q->whereNull('company_id')->orWhere('company_id', $user->companyId());
})
->first();
2023-09-25 07:56:32 +02:00
2023-11-28 14:27:04 +01:00
$ts = (new TemplateService($design));
2023-11-06 05:47:16 +01:00
2023-10-02 02:06:24 +02:00
try {
2023-11-28 14:27:04 +01:00
if(isset($request_data['settings']) && is_array($request_data['settings'])) {
$ts->setSettings(json_decode(json_encode($request_data['settings'])));
}
2023-10-26 04:57:44 +02:00
$ts->setCompany($company)
2023-11-28 14:27:04 +01:00
->compose()
2023-10-26 04:57:44 +02:00
->mock();
} catch(SyntaxError $e) {
2023-10-02 02:06:24 +02:00
// return response()->json(['message' => 'Twig syntax is invalid.', 'errors' => new \stdClass], 422);
}
2023-11-28 14:27:04 +01:00
$response = Response::make($ts->getPdf(), 200);
$response->header('Content-Type', 'application/pdf');
2023-09-25 07:56:32 +02:00
2023-11-28 14:27:04 +01:00
return $response;
2023-09-25 07:56:32 +02:00
2023-11-28 14:27:04 +01:00
}
private function template()
{
/** @var \App\Models\User $user */
$user = auth()->user();
2023-09-25 07:56:32 +02:00
2023-11-28 14:27:04 +01:00
/** @var \App\Models\Company $company */
$company = $user->company();
2023-09-25 07:56:32 +02:00
2023-11-28 14:27:04 +01:00
$design_object = json_decode(json_encode(request()->input('design')), 1);
2023-09-25 07:56:32 +02:00
2023-11-28 14:27:04 +01:00
$ts = (new TemplateService());
2023-09-25 07:56:32 +02:00
2023-11-28 14:27:04 +01:00
try {
$ts->setCompany($company)
->setTemplate($design_object)
->mock();
} catch(SyntaxError $e) {
2023-09-25 07:56:32 +02:00
}
2023-11-28 14:27:04 +01:00
if (request()->query('html') == 'true') {
return $ts->getHtml();
}
2023-09-25 07:56:32 +02:00
2023-11-28 14:27:04 +01:00
$response = Response::make($ts->getPdf(), 200);
2023-09-25 07:56:32 +02:00
$response->header('Content-Type', 'application/pdf');
return $response;
}
private function blankEntity()
{
2023-04-26 16:11:31 +02:00
2023-04-27 00:05:57 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-04-26 16:11:31 +02:00
/** @var \App\Models\Company $company */
2023-04-27 00:05:57 +02:00
$company = $user->company();
2023-04-26 16:11:31 +02:00
App::forgetInstance('translator');
2021-05-31 12:40:34 +02:00
$t = app('translator');
2023-04-26 16:11:31 +02:00
$t->replace(Ninja::transformTranslations($company->settings));
2023-08-04 09:12:21 +02:00
/** @var \App\Models\InvoiceInvitation $invitation */
2023-04-26 16:11:31 +02:00
$invitation = InvoiceInvitation::where('company_id', $company->id)->orderBy('id', 'desc')->first();
2021-12-19 21:16:44 +01:00
/* If we don't have a valid invitation in the system - create a mock using transactions */
if (! $invitation) {
2021-12-19 21:16:44 +01:00
return $this->mockEntity();
}
2021-12-19 21:16:44 +01:00
$design_object = json_decode(json_encode(request()->input('design')));
if (! is_object($design_object)) {
return response()->json(['message' => 'Invalid custom design object'], 400);
}
$html = new HtmlEngine($invitation);
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
$state = [
'template' => $design->elements([
'client' => $invitation->invoice->client,
'entity' => $invitation->invoice,
'pdf_variables' => (array) $invitation->invoice->company->settings->pdf_variables,
'products' => request()->design['design']['product'],
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $invitation->invoice->client->company->markdown_enabled,
2023-09-19 02:05:13 +02:00
'options' => [
'client' => $invitation->invoice->client,
'invoices' => [$invitation->invoice],
2023-09-19 02:05:13 +02:00
]
2021-12-19 21:16:44 +01:00
];
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
if (request()->query('html') == 'true') {
return $maker->getCompiledHTML();
}
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2024-01-14 05:05:00 +01:00
return (new Phantom())->convertHtmlToPdf($maker->getCompiledHTML(true));
2021-12-19 21:16:44 +01:00
}
2023-04-27 00:05:57 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-04-26 16:11:31 +02:00
/** @var \App\Models\Company $company */
2023-04-27 00:05:57 +02:00
$company = $user->company();
2023-04-26 16:11:31 +02:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
2022-05-26 23:32:13 +02:00
2023-04-26 16:11:31 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $company);
2022-05-26 23:32:13 +02:00
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
2022-05-26 23:32:13 +02:00
return $pdf;
2021-12-19 21:16:44 +01:00
}
2023-04-26 16:11:31 +02:00
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
2021-12-19 21:16:44 +01:00
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
}
private function mockEntity()
{
2023-04-27 00:05:57 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-04-26 14:17:40 +02:00
/** @var \App\Models\Company $company */
2023-04-27 00:05:57 +02:00
$company = $user->company();
try {
DB::connection($company->db)->beginTransaction();
2020-07-05 13:39:59 +02:00
/** @var \App\Models\Client $client */
$client = Client::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
]);
/** @var \App\Models\ClientContact $contact */
$contact = ClientContact::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
'client_id' => $client->id,
'is_primary' => 1,
'send_email' => true,
]);
$settings = $company->settings;
2023-04-26 14:17:40 +02:00
/** @var \App\Models\Invoice $invoice */
$invoice = Invoice::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
'client_id' => $client->id,
'terms' => $company->settings->invoice_terms,
'footer' => $company->settings->invoice_footer,
'public_notes' => 'Sample Public Notes',
]);
2020-08-04 13:00:19 +02:00
if ($settings->invoice_number_pattern) {
$invoice->number = $this->getFormattedEntityNumber(
$invoice,
rand(1, 9999),
$settings->counter_padding ?: 4,
$settings->invoice_number_pattern,
);
$invoice->save();
}
$invitation = InvoiceInvitation::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
'invoice_id' => $invoice->id,
'client_contact_id' => $contact->id,
]);
$invoice->setRelation('invitations', $invitation);
$invoice->setRelation('client', $client);
$invoice->setRelation('company', $company);
$invoice->load('client.company');
$design_object = json_decode(json_encode(request()->input('design')));
if (! is_object($design_object)) {
return response()->json(['message' => 'Invalid custom design object'], 400);
}
$html = new HtmlEngine($invoice->invitations()->first());
2020-08-28 09:51:02 +02:00
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
2020-08-28 09:51:02 +02:00
$state = [
'template' => $design->elements([
'client' => $invoice->client,
'entity' => $invoice,
'pdf_variables' => (array) $settings->pdf_variables,
'products' => request()->design['design']['product'],
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $invoice->client->company->markdown_enabled,
'options' => [
'client' => $invoice->client,
'invoices' => [$invoice],
]
];
$maker = new PdfMaker($state);
2020-08-28 09:51:02 +02:00
$maker
->design($design)
->build();
2020-08-28 09:51:02 +02:00
DB::connection($company->db)->rollBack();
2023-10-26 04:57:44 +02:00
} catch(\Exception $e) {
DB::connection($company->db)->rollBack();
return response()->json(['message' => $e->getMessage()], 400);
}
2021-12-19 21:16:44 +01:00
if (request()->query('html') == 'true') {
return $maker->getCompiledHTML();
}
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2024-01-14 05:05:00 +01:00
return (new Phantom())->convertHtmlToPdf($maker->getCompiledHTML(true));
2020-11-27 12:08:42 +01:00
}
2021-03-18 10:57:55 +01:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2022-05-26 23:32:13 +02:00
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
2023-04-26 14:17:40 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $company);
2022-05-26 23:32:13 +02:00
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
2022-05-26 23:32:13 +02:00
return $pdf;
2021-03-18 10:57:55 +01:00
}
2023-04-26 14:17:40 +02:00
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
2020-07-05 12:58:30 +02:00
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf');
2020-08-28 09:51:02 +02:00
return $response;
}
}