2020-03-05 08:14:57 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-05 08:14:57 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-03-05 08:14:57 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-03-05 08:14:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2021-07-28 03:49:13 +02:00
|
|
|
use App\DataMapper\Analytics\LivePreview;
|
2021-07-27 10:04:46 +02:00
|
|
|
use App\Factory\CreditFactory;
|
2021-07-25 10:05:53 +02:00
|
|
|
use App\Factory\InvoiceFactory;
|
2021-07-27 10:04:46 +02:00
|
|
|
use App\Factory\QuoteFactory;
|
|
|
|
use App\Factory\RecurringInvoiceFactory;
|
2023-01-27 11:38:59 +01:00
|
|
|
use App\Http\Requests\Preview\DesignPreviewRequest;
|
2021-07-26 00:22:29 +02:00
|
|
|
use App\Http\Requests\Preview\PreviewInvoiceRequest;
|
2020-03-05 08:14:57 +01:00
|
|
|
use App\Jobs\Util\PreviewPdf;
|
2021-08-05 11:48:57 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2020-10-05 10:16:36 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
2021-07-27 10:04:46 +02:00
|
|
|
use App\Models\Credit;
|
2020-10-05 10:16:36 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
2021-07-27 10:04:46 +02:00
|
|
|
use App\Models\Quote;
|
|
|
|
use App\Models\RecurringInvoice;
|
|
|
|
use App\Repositories\CreditRepository;
|
2021-07-25 10:05:53 +02:00
|
|
|
use App\Repositories\InvoiceRepository;
|
2021-07-27 10:04:46 +02:00
|
|
|
use App\Repositories\QuoteRepository;
|
|
|
|
use App\Repositories\RecurringInvoiceRepository;
|
2023-02-25 05:01:52 +01:00
|
|
|
use App\Services\Pdf\PdfMock;
|
2023-02-16 02:36:09 +01:00
|
|
|
use App\Services\PdfMaker\Design;
|
2021-07-29 05:37:23 +02:00
|
|
|
use App\Services\PdfMaker\Design as PdfDesignModel;
|
2021-08-05 11:48:57 +02:00
|
|
|
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
2020-08-21 16:47:17 +02:00
|
|
|
use App\Services\PdfMaker\PdfMaker;
|
2021-03-18 10:57:55 +01:00
|
|
|
use App\Utils\HostedPDF\NinjaPdf;
|
2020-08-21 16:47:17 +02:00
|
|
|
use App\Utils\HtmlEngine;
|
2020-12-13 00:20:03 +01:00
|
|
|
use App\Utils\Ninja;
|
2020-11-27 03:24:13 +01:00
|
|
|
use App\Utils\PhantomJS\Phantom;
|
2020-03-05 08:14:57 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use App\Utils\Traits\MakesInvoiceHtml;
|
2022-05-26 23:32:13 +02:00
|
|
|
use App\Utils\Traits\Pdf\PageNumbering;
|
2020-12-13 00:20:03 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2020-12-16 12:52:40 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Response;
|
2021-07-28 03:49:13 +02:00
|
|
|
use Turbo124\Beacon\Facades\LightLogs;
|
2020-03-05 08:14:57 +01:00
|
|
|
|
|
|
|
class PreviewController extends BaseController
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use MakesInvoiceHtml;
|
2022-05-26 23:32:13 +02:00
|
|
|
use PageNumbering;
|
|
|
|
|
2020-03-05 08:14:57 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns a template filled with entity variables.
|
2020-03-05 08:14:57 +01:00
|
|
|
*
|
2023-04-26 14:17:40 +02:00
|
|
|
* @return mixed
|
2020-03-05 08:14:57 +01:00
|
|
|
*/
|
2023-04-26 14:17:40 +02:00
|
|
|
|
2020-03-05 08:14:57 +01:00
|
|
|
public function show()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if (request()->has('entity') &&
|
|
|
|
request()->has('entity_id') &&
|
2020-09-06 11:38:10 +02:00
|
|
|
! empty(request()->input('entity')) &&
|
|
|
|
! empty(request()->input('entity_id')) &&
|
2020-03-21 06:37:30 +01:00
|
|
|
request()->has('body')) {
|
2020-03-12 12:04:53 +01:00
|
|
|
$design_object = json_decode(json_encode(request()->input('design')));
|
2020-03-09 12:12:45 +01:00
|
|
|
|
2020-09-06 11:38:10 +02: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-21 06:37:30 +01:00
|
|
|
}
|
2020-03-09 12:12:45 +01:00
|
|
|
|
2020-03-05 08:14:57 +01:00
|
|
|
$entity = ucfirst(request()->input('entity'));
|
|
|
|
|
|
|
|
$class = "App\Models\\$entity";
|
|
|
|
|
|
|
|
$entity_obj = $class::whereId($this->decodePrimaryKey(request()->input('entity_id')))->company()->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $entity_obj) {
|
2020-03-05 08:14:57 +01:00
|
|
|
return $this->blankEntity();
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-05 08:14:57 +01:00
|
|
|
|
|
|
|
$entity_obj->load('client');
|
|
|
|
|
2020-12-13 00:20:03 +01:00
|
|
|
App::forgetInstance('translator');
|
2021-05-31 12:40:34 +02:00
|
|
|
$t = app('translator');
|
|
|
|
App::setLocale($entity_obj->client->primary_contact()->preferredLocale());
|
|
|
|
$t->replace(Ninja::transformTranslations($entity_obj->client->getMergedSettings()));
|
2020-12-13 00:20:03 +01:00
|
|
|
|
2020-10-27 12:57:12 +01:00
|
|
|
$html = new HtmlEngine($entity_obj->invitations()->first());
|
2020-03-05 08:14:57 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$design_namespace = 'App\Services\PdfMaker\Designs\\'.request()->design['name'];
|
2020-08-21 16:47:17 +02:00
|
|
|
|
|
|
|
$design_class = new $design_namespace();
|
|
|
|
|
|
|
|
$state = [
|
|
|
|
'template' => $design_class->elements([
|
|
|
|
'client' => $entity_obj->client,
|
|
|
|
'entity' => $entity_obj,
|
|
|
|
'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables,
|
2020-10-06 12:49:00 +02:00
|
|
|
'products' => request()->design['design']['product'],
|
2020-08-21 16:47:17 +02:00
|
|
|
]),
|
|
|
|
'variables' => $html->generateLabelsAndValues(),
|
2021-08-09 14:31:31 +02:00
|
|
|
'process_markdown' => $entity_obj->client->company->markdown_enabled,
|
2020-08-21 16:47:17 +02:00
|
|
|
];
|
|
|
|
|
2020-09-04 13:17:30 +02:00
|
|
|
$design = new Design(request()->design['name']);
|
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();
|
2023-04-02 13:42:14 +02:00
|
|
|
|
2020-12-26 10:26:19 +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') {
|
2020-11-27 03:02:05 +01:00
|
|
|
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2023-04-26 16:16:07 +02: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
|
|
|
|
2022-06-21 11:57:17 +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);
|
2022-05-26 23:32:13 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($numbered_pdf) {
|
2022-05-26 23:32:13 +02:00
|
|
|
$pdf = $numbered_pdf;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-26 23:32:13 +02:00
|
|
|
|
|
|
|
return $pdf;
|
2021-03-18 10:57:55 +01:00
|
|
|
}
|
2020-11-27 03:02:05 +01:00
|
|
|
|
2023-04-26 16:11:31 +02:00
|
|
|
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
|
2020-03-05 08:14:57 +01:00
|
|
|
|
2021-04-29 13:07:05 +02:00
|
|
|
return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
|
2020-03-05 08:14:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->blankEntity();
|
|
|
|
}
|
|
|
|
|
2023-01-27 11:38:59 +01:00
|
|
|
public function design(DesignPreviewRequest $request)
|
2023-02-21 10:04:45 +01:00
|
|
|
{
|
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();
|
2023-04-26 14:17:40 +02:00
|
|
|
|
|
|
|
$pdf = (new PdfMock($request->all(), $company))->build()->getPdf();
|
2023-02-21 10:04:45 +01:00
|
|
|
|
|
|
|
$response = Response::make($pdf, 200);
|
|
|
|
$response->header('Content-Type', 'application/pdf');
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2021-07-26 00:22:29 +02:00
|
|
|
public function live(PreviewInvoiceRequest $request)
|
2021-07-25 08:23:10 +02:00
|
|
|
{
|
2023-02-22 07:37:16 +01:00
|
|
|
if (Ninja::isHosted() && !in_array($request->getHost(), ['preview.invoicing.co','staging.invoicing.co'])) {
|
2023-01-27 12:04:02 +01:00
|
|
|
return response()->json(['message' => 'This server cannot handle this request.'], 400);
|
2023-02-22 07:37:16 +01:00
|
|
|
}
|
2023-06-07 04:36:31 +02:00
|
|
|
|
|
|
|
$start = microtime(true);
|
|
|
|
|
2023-04-27 00:05:57 +02:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
$company = $user->company();
|
2021-08-05 14:39:09 +02:00
|
|
|
|
|
|
|
MultiDB::setDb($company->db);
|
2021-08-06 09:53:52 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($request->input('entity') == 'quote') {
|
2021-07-27 10:04:46 +02:00
|
|
|
$repo = new QuoteRepository();
|
2021-08-06 09:35:52 +02:00
|
|
|
$entity_obj = QuoteFactory::create($company->id, auth()->user()->id);
|
2021-07-27 10:04:46 +02:00
|
|
|
$class = Quote::class;
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($request->input('entity') == 'credit') {
|
2021-07-27 10:04:46 +02:00
|
|
|
$repo = new CreditRepository();
|
2021-08-06 09:35:52 +02:00
|
|
|
$entity_obj = CreditFactory::create($company->id, auth()->user()->id);
|
2021-07-27 10:04:46 +02:00
|
|
|
$class = Credit::class;
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($request->input('entity') == 'recurring_invoice') {
|
2021-07-27 10:04:46 +02:00
|
|
|
$repo = new RecurringInvoiceRepository();
|
2021-08-06 09:35:52 +02:00
|
|
|
$entity_obj = RecurringInvoiceFactory::create($company->id, auth()->user()->id);
|
2021-07-27 10:04:46 +02:00
|
|
|
$class = RecurringInvoice::class;
|
2022-06-21 11:57:17 +02:00
|
|
|
} else { //assume it is either an invoice or a null object
|
2021-11-16 03:30:32 +01:00
|
|
|
$repo = new InvoiceRepository();
|
|
|
|
$entity_obj = InvoiceFactory::create($company->id, auth()->user()->id);
|
|
|
|
$class = Invoice::class;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-07-25 08:23:10 +02:00
|
|
|
|
2021-07-28 08:01:30 +02:00
|
|
|
try {
|
2021-08-06 09:48:35 +02:00
|
|
|
DB::connection(config('database.default'))->beginTransaction();
|
2021-07-28 08:12:07 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($request->has('entity_id')) {
|
2023-04-27 00:05:57 +02:00
|
|
|
|
|
|
|
/** @var \App\Models\BaseModel $class */
|
2021-08-06 09:35:52 +02:00
|
|
|
$entity_obj = $class::on(config('database.default'))
|
2021-10-12 12:24:09 +02:00
|
|
|
->with('client.company')
|
2021-08-06 09:35:52 +02:00
|
|
|
->where('id', $this->decodePrimaryKey($request->input('entity_id')))
|
2021-08-05 14:39:09 +02:00
|
|
|
->where('company_id', $company->id)
|
2021-08-05 13:58:07 +02:00
|
|
|
->withTrashed()
|
2021-08-05 13:45:54 +02:00
|
|
|
->first();
|
2021-08-06 09:35:52 +02:00
|
|
|
}
|
2021-08-05 14:39:09 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($request->has('footer') && !$request->filled('footer') && $request->input('entity') == 'recurring_invoice') {
|
2023-01-16 23:43:11 +01:00
|
|
|
$request->merge(['footer' => $company->settings->invoice_footer]);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-01-16 23:43:11 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($request->has('terms') && !$request->filled('terms') && $request->input('entity') == 'recurring_invoice') {
|
2023-01-16 23:43:11 +01:00
|
|
|
$request->merge(['terms' => $company->settings->invoice_terms]);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-01-16 23:43:11 +01:00
|
|
|
|
2021-08-06 09:35:52 +02:00
|
|
|
$entity_obj = $repo->save($request->all(), $entity_obj);
|
2021-09-29 13:42:25 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $request->has('entity_id')) {
|
2021-09-29 13:42:25 +02:00
|
|
|
$entity_obj->service()->fillDefaults()->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2021-07-25 08:23:10 +02:00
|
|
|
App::forgetInstance('translator');
|
|
|
|
$t = app('translator');
|
2021-10-12 12:24:09 +02:00
|
|
|
App::setLocale($entity_obj->client->locale());
|
2021-07-25 08:23:10 +02:00
|
|
|
$t->replace(Ninja::transformTranslations($entity_obj->client->getMergedSettings()));
|
|
|
|
|
|
|
|
$html = new HtmlEngine($entity_obj->invitations()->first());
|
|
|
|
|
2023-04-29 13:01:02 +02:00
|
|
|
$design = \App\Models\Design::withTrashed()->find($entity_obj->design_id);
|
2021-07-25 08:23:10 +02:00
|
|
|
|
2021-07-25 10:05:53 +02:00
|
|
|
/* Catch all in case migration doesn't pass back a valid design */
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $design) {
|
2021-08-02 03:08:03 +02:00
|
|
|
$design = \App\Models\Design::find(2);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-07-25 10:05:53 +02:00
|
|
|
|
|
|
|
if ($design->is_custom) {
|
|
|
|
$options = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'custom_partials' => json_decode(json_encode($design->design), true),
|
|
|
|
];
|
2021-07-25 10:05:53 +02:00
|
|
|
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
|
|
|
|
} else {
|
|
|
|
$template = new PdfMakerDesign(strtolower($design->name));
|
|
|
|
}
|
|
|
|
|
|
|
|
$variables = $html->generateLabelsAndValues();
|
2021-07-25 08:23:10 +02:00
|
|
|
|
|
|
|
$state = [
|
2021-07-25 10:05:53 +02:00
|
|
|
'template' => $template->elements([
|
2021-07-25 08:23:10 +02:00
|
|
|
'client' => $entity_obj->client,
|
|
|
|
'entity' => $entity_obj,
|
|
|
|
'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables,
|
2021-07-25 10:05:53 +02:00
|
|
|
'$product' => $design->design->product,
|
|
|
|
'variables' => $variables,
|
2021-07-25 08:23:10 +02:00
|
|
|
]),
|
2021-07-25 10:05:53 +02:00
|
|
|
'variables' => $variables,
|
|
|
|
'options' => [
|
|
|
|
'all_pages_header' => $entity_obj->client->getSetting('all_pages_header'),
|
|
|
|
'all_pages_footer' => $entity_obj->client->getSetting('all_pages_footer'),
|
|
|
|
],
|
2021-08-09 14:31:31 +02:00
|
|
|
'process_markdown' => $entity_obj->client->company->markdown_enabled,
|
2021-07-25 08:23:10 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMaker($state);
|
|
|
|
|
|
|
|
$maker
|
2021-07-25 10:05:53 +02:00
|
|
|
->design($template)
|
2021-07-25 08:23:10 +02:00
|
|
|
->build();
|
|
|
|
|
2021-08-05 14:55:40 +02:00
|
|
|
DB::connection(config('database.default'))->rollBack();
|
2021-07-28 08:12:07 +02:00
|
|
|
|
2021-07-25 08:23:10 +02:00
|
|
|
if (request()->query('html') == 'true') {
|
2022-03-16 21:57:06 +01:00
|
|
|
return $maker->getCompiledHTML();
|
2021-07-25 08:23:10 +02:00
|
|
|
}
|
2023-02-16 02:36:09 +01:00
|
|
|
} catch(\Exception $e) {
|
2022-06-30 12:12:23 +02:00
|
|
|
nlog($e->getMessage());
|
2021-08-05 14:55:40 +02:00
|
|
|
DB::connection(config('database.default'))->rollBack();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-07-28 08:14:10 +02:00
|
|
|
return;
|
2021-07-28 08:01:30 +02:00
|
|
|
}
|
2021-07-25 08:23:10 +02:00
|
|
|
|
|
|
|
//if phantom js...... inject here..
|
|
|
|
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
|
|
|
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-02-16 02:36:09 +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 16:11:31 +02:00
|
|
|
$numbered_pdf = $this->pageNumbering($pdf, $company);
|
2022-05-26 23:32:13 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($numbered_pdf) {
|
|
|
|
$pdf = $numbered_pdf;
|
|
|
|
}
|
2022-05-26 23:32:13 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
return $pdf;
|
2021-07-25 08:23:10 +02:00
|
|
|
}
|
|
|
|
|
2022-06-24 13:15:14 +02:00
|
|
|
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
|
2021-07-25 14:44:29 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted()) {
|
|
|
|
LightLogs::create(new LivePreview())
|
2021-07-28 03:49:13 +02:00
|
|
|
->increment()
|
2022-09-09 03:07:14 +02:00
|
|
|
->batch();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-07-28 03:49:13 +02:00
|
|
|
|
2021-07-25 14:44:29 +02:00
|
|
|
$response = Response::make($file_path, 200);
|
2021-07-28 03:49:13 +02:00
|
|
|
$response->header('Content-Type', 'application/pdf');
|
2023-06-07 04:36:31 +02:00
|
|
|
$response->header('Server-Timing', microtime(true)-$start);
|
|
|
|
|
2021-07-25 08:23:10 +02:00
|
|
|
|
2021-07-25 14:44:29 +02:00
|
|
|
return $response;
|
2021-07-25 08:23:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 08:14:57 +01:00
|
|
|
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
|
|
|
|
2020-12-13 00:20:03 +01: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));
|
2020-12-13 00:20:03 +01:00
|
|
|
|
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 */
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $invitation) {
|
2021-12-19 21:16:44 +01:00
|
|
|
return $this->mockEntity();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
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,
|
|
|
|
];
|
|
|
|
|
|
|
|
$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') {
|
|
|
|
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-06-21 11:57:17 +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
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($numbered_pdf) {
|
|
|
|
$pdf = $numbered_pdf;
|
|
|
|
}
|
2022-05-26 23:32:13 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return $pdf;
|
2021-12-19 21:16:44 +01:00
|
|
|
}
|
2022-06-21 11:57:17 +02: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();
|
|
|
|
|
2020-07-05 13:39:59 +02:00
|
|
|
|
2023-04-26 14:17:40 +02:00
|
|
|
DB::connection($company->db)->beginTransaction();
|
|
|
|
|
|
|
|
/** @var \App\Models\Client $client */
|
2020-10-01 13:34:05 +02:00
|
|
|
$client = Client::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => auth()->user()->id,
|
2023-04-26 14:17:40 +02:00
|
|
|
'company_id' => $company->id,
|
2022-06-21 11:57:17 +02:00
|
|
|
]);
|
2020-03-05 08:50:50 +01:00
|
|
|
|
2023-04-26 14:17:40 +02:00
|
|
|
/** @var \App\Models\ClientContact $contact */
|
2020-10-01 13:34:05 +02:00
|
|
|
$contact = ClientContact::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => auth()->user()->id,
|
2023-04-26 14:17:40 +02:00
|
|
|
'company_id' => $company->id,
|
2022-06-21 11:57:17 +02:00
|
|
|
'client_id' => $client->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
'send_email' => true,
|
|
|
|
]);
|
2020-03-05 08:50:50 +01:00
|
|
|
|
2023-04-26 14:17:40 +02:00
|
|
|
/** @var \App\Models\Invoice $invoice */
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
$invoice = Invoice::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => auth()->user()->id,
|
2023-04-26 14:17:40 +02:00
|
|
|
'company_id' => $company->id,
|
2022-06-21 11:57:17 +02:00
|
|
|
'client_id' => $client->id,
|
2023-04-26 14:17:40 +02:00
|
|
|
'terms' => $company->settings->invoice_terms,
|
|
|
|
'footer' => $company->settings->invoice_footer,
|
2022-06-21 11:57:17 +02:00
|
|
|
'public_notes' => 'Sample Public Notes',
|
|
|
|
]);
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
$invitation = InvoiceInvitation::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => auth()->user()->id,
|
2023-04-26 14:17:40 +02:00
|
|
|
'company_id' => $company->id,
|
2022-06-21 11:57:17 +02:00
|
|
|
'invoice_id' => $invoice->id,
|
|
|
|
'client_contact_id' => $contact->id,
|
2020-08-04 10:37:28 +02:00
|
|
|
]);
|
2020-03-05 08:50:50 +01:00
|
|
|
|
2020-08-04 10:37:28 +02:00
|
|
|
$invoice->setRelation('invitations', $invitation);
|
2020-03-21 06:37:30 +01:00
|
|
|
$invoice->setRelation('client', $client);
|
2023-04-26 14:17:40 +02:00
|
|
|
$invoice->setRelation('company', $company);
|
2021-10-08 06:00:17 +02:00
|
|
|
$invoice->load('client.company');
|
2020-03-07 23:15:11 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$design_object = json_decode(json_encode(request()->input('design')));
|
2020-03-07 23:15:11 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! is_object($design_object)) {
|
2020-03-21 06:37:30 +01:00
|
|
|
return response()->json(['message' => 'Invalid custom design object'], 400);
|
|
|
|
}
|
2020-03-05 08:50:50 +01:00
|
|
|
|
2020-10-27 12:57:12 +01:00
|
|
|
$html = new HtmlEngine($invoice->invitations()->first());
|
2020-08-28 09:51:02 +02:00
|
|
|
|
2020-09-09 14:47:26 +02:00
|
|
|
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
|
2020-08-28 09:51:02 +02:00
|
|
|
|
|
|
|
$state = [
|
2020-09-04 13:17:30 +02:00
|
|
|
'template' => $design->elements([
|
2020-08-28 09:51:02 +02:00
|
|
|
'client' => $invoice->client,
|
|
|
|
'entity' => $invoice,
|
|
|
|
'pdf_variables' => (array) $invoice->company->settings->pdf_variables,
|
2020-10-06 12:49:00 +02:00
|
|
|
'products' => request()->design['design']['product'],
|
2020-08-28 09:51:02 +02:00
|
|
|
]),
|
|
|
|
'variables' => $html->generateLabelsAndValues(),
|
2021-08-09 14:31:31 +02:00
|
|
|
'process_markdown' => $invoice->client->company->markdown_enabled,
|
2020-08-28 09:51:02 +02:00
|
|
|
];
|
2020-03-05 08:50:50 +01:00
|
|
|
|
2020-08-28 09:51:02 +02:00
|
|
|
$maker = new PdfMaker($state);
|
|
|
|
|
|
|
|
$maker
|
2020-09-04 13:17:30 +02:00
|
|
|
->design($design)
|
2020-08-28 09:51:02 +02:00
|
|
|
->build();
|
|
|
|
|
2023-04-26 14:17:40 +02:00
|
|
|
DB::connection($company->db)->rollBack();
|
2021-12-19 21:16:44 +01:00
|
|
|
|
2020-12-26 10:26:19 +01:00
|
|
|
if (request()->query('html') == 'true') {
|
|
|
|
return $maker->getCompiledHTML();
|
|
|
|
}
|
|
|
|
|
2021-10-11 04:19:38 +02:00
|
|
|
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
2020-11-27 12:08:42 +01:00
|
|
|
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
|
|
|
}
|
2021-03-18 10:57:55 +01:00
|
|
|
|
2022-06-21 11:57:17 +02: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
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($numbered_pdf) {
|
|
|
|
$pdf = $numbered_pdf;
|
|
|
|
}
|
2022-05-26 23:32:13 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return $pdf;
|
2021-03-18 10:57:55 +01:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2023-04-26 14:17:40 +02:00
|
|
|
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
|
2020-03-05 08:50:50 +01:00
|
|
|
|
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;
|
2020-03-05 08:14:57 +01:00
|
|
|
}
|
|
|
|
}
|