From 8a4230c81cf4dbeaf3c7f2108efd8c74651c3b17 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 25 Jul 2021 16:23:10 +1000 Subject: [PATCH] Working on live preview --- app/Http/Controllers/PreviewController.php | 78 ++++++++++++++++++++++ app/Jobs/Util/Import.php | 4 ++ routes/api.php | 1 + 3 files changed, 83 insertions(+) diff --git a/app/Http/Controllers/PreviewController.php b/app/Http/Controllers/PreviewController.php index e0f30fb44f..cb4bd75ad6 100644 --- a/app/Http/Controllers/PreviewController.php +++ b/app/Http/Controllers/PreviewController.php @@ -149,6 +149,84 @@ class PreviewController extends BaseController return $this->blankEntity(); } + public function live() + { + if (request()->has('entity') && + request()->has('entity_id') && + ! empty(request()->input('entity')) && + ! empty(request()->input('entity_id')) && + request()->has('body')) { + $design_object = json_decode(json_encode(request()->input('design'))); + + if (! is_object($design_object)) { + return response()->json(['message' => ctrans('texts.invalid_design_object')], 400); + } + + $entity = ucfirst(request()->input('entity')); + + $class = "App\Models\\$entity"; + + $pdf_class = "App\Jobs\\$entity\\Create{$entity}Pdf"; + + $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'); + $t = app('translator'); + App::setLocale($entity_obj->client->primary_contact()->preferredLocale()); + $t->replace(Ninja::transformTranslations($entity_obj->client->getMergedSettings())); + + $html = new HtmlEngine($entity_obj->invitations()->first()); + + $design_namespace = 'App\Services\PdfMaker\Designs\\'.request()->design['name']; + + $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, + 'products' => request()->design['design']['product'], + ]), + 'variables' => $html->generateLabelsAndValues(), + ]; + + $design = new Design(request()->design['name']); + $maker = new PdfMaker($state); + + $maker + ->design($design) + ->build(); + + if (request()->query('html') == 'true') { + return $maker->getCompiledHTML; + } + + //if phantom js...... inject here.. + if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') { + return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true)); + } + + if(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){ + return (new NinjaPdf())->build($maker->getCompiledHTML(true)); + } + + //else + $file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(true), auth()->user()->company()); + + return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true); + } + + return $this->blankEntity(); + } + + private function blankEntity() { App::forgetInstance('translator'); diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index bf6aac68ed..89685ac119 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -238,6 +238,10 @@ class Import implements ShouldQueue /*After a migration first some basic jobs to ensure the system is up to date*/ VersionCheck::dispatch(); + $account = $this->company->account; + $account->default_company_id = $this->company->id; + $account->save(); + //company size check if ($this->company->invoices()->count() > 1000 || $this->company->products()->count() > 1000 || $this->company->clients()->count() > 1000) { $this->company->is_large = true; diff --git a/routes/api.php b/routes/api.php index ce068d9077..8ec40d2a5d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -114,6 +114,7 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::post('payment_terms/bulk', 'PaymentTermController@bulk')->name('payment_terms.bulk'); Route::post('preview', 'PreviewController@show')->name('preview.show'); + Route::post('live_preview', 'PreviewController@live')->name('preview.show'); Route::resource('products', 'ProductController'); // name = (products. index / create / show / update / destroy / edit Route::post('products/bulk', 'ProductController@bulk')->name('products.bulk');