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(), 'process_markdown' => $entity_obj->client->company->markdown_enabled, ]; $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(); } public function live(PreviewInvoiceRequest $request) { $company = auth()->user()->company(); MultiDB::setDb($company->db); if($request->input('entity') == 'invoice'){ $repo = new InvoiceRepository(); $entity_obj = InvoiceFactory::create($company->id, auth()->user()->id); $class = Invoice::class; } elseif($request->input('entity') == 'quote'){ $repo = new QuoteRepository(); $entity_obj = QuoteFactory::create($company->id, auth()->user()->id); $class = Quote::class; } elseif($request->input('entity') == 'credit'){ $repo = new CreditRepository(); $entity_obj = CreditFactory::create($company->id, auth()->user()->id); $class = Credit::class; } elseif($request->input('entity') == 'recurring_invoice'){ $repo = new RecurringInvoiceRepository(); $entity_obj = RecurringInvoiceFactory::create($company->id, auth()->user()->id); $class = RecurringInvoice::class; } try { DB::connection(config('database.default'))->beginTransaction(); if($request->has('entity_id')){ $entity_obj = $class::on(config('database.default')) ->where('id', $this->decodePrimaryKey($request->input('entity_id'))) ->where('company_id', $company->id) ->withTrashed() ->first(); } $entity_obj = $repo->save($request->all(), $entity_obj); if(!$request->has('entity_id')) $entity_obj->service()->fillDefaults()->save(); $entity_obj->load('client'); App::forgetInstance('translator'); $t = app('translator'); App::setLocale($entity_obj->client->contacts()->first()->preferredLocale()); $t->replace(Ninja::transformTranslations($entity_obj->client->getMergedSettings())); $html = new HtmlEngine($entity_obj->invitations()->first()); $design = \App\Models\Design::find($entity_obj->design_id); /* Catch all in case migration doesn't pass back a valid design */ if(!$design) $design = \App\Models\Design::find(2); if ($design->is_custom) { $options = [ 'custom_partials' => json_decode(json_encode($design->design), true) ]; $template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options); } else { $template = new PdfMakerDesign(strtolower($design->name)); } $variables = $html->generateLabelsAndValues(); $state = [ 'template' => $template->elements([ 'client' => $entity_obj->client, 'entity' => $entity_obj, 'pdf_variables' => (array) $entity_obj->company->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'), ], 'process_markdown' => $entity_obj->client->company->markdown_enabled, ]; $maker = new PdfMaker($state); $maker ->design($template) ->build(); DB::connection(config('database.default'))->rollBack(); if (request()->query('html') == 'true') { return $maker->getCompiledHTML; } } catch(\Exception $e){ DB::connection(config('database.default'))->rollBack(); return; } //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)); } $file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(true), $company); if(Ninja::isHosted()) { LightLogs::create(new LivePreview()) ->increment() ->batch(); } $response = Response::make($file_path, 200); $response->header('Content-Type', 'application/pdf'); return $response; } private function blankEntity() { App::forgetInstance('translator'); $t = app('translator'); $t->replace(Ninja::transformTranslations(auth()->user()->company()->settings)); DB::connection(auth()->user()->company()->db)->beginTransaction(); $client = Client::factory()->create([ 'user_id' => auth()->user()->id, 'company_id' => auth()->user()->company()->id, ]); $contact = ClientContact::factory()->create([ 'user_id' => auth()->user()->id, 'company_id' => auth()->user()->company()->id, 'client_id' => $client->id, 'is_primary' => 1, 'send_email' => true, ]); $invoice = Invoice::factory()->create([ 'user_id' => auth()->user()->id, 'company_id' => auth()->user()->company()->id, 'client_id' => $client->id, ]); $invitation = InvoiceInvitation::factory()->create([ 'user_id' => auth()->user()->id, 'company_id' => auth()->user()->company()->id, 'invoice_id' => $invoice->id, 'client_contact_id' => $contact->id, ]); $invoice->setRelation('invitations', $invitation); $invoice->setRelation('client', $client); $invoice->setRelation('company', auth()->user()->company()); $invoice->load('client'); // nlog(print_r($invoice->toArray(),1)); $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()); $design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]); $state = [ 'template' => $design->elements([ 'client' => $invoice->client, 'entity' => $invoice, 'pdf_variables' => (array) $invoice->company->settings->pdf_variables, 'products' => request()->design['design']['product'], ]), 'variables' => $html->generateLabelsAndValues(), 'process_markdown' => $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')) { return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true)); } if(config('ninja.invoiceninja_hosted_pdf_generation')){ return (new NinjaPdf())->build($maker->getCompiledHTML(true)); } $file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(true), auth()->user()->company()); DB::connection(auth()->user()->company()->db)->rollBack(); $response = Response::make($file_path, 200); $response->header('Content-Type', 'application/pdf'); return $response; } }