From 57f8bddd04d026f79cb9d37459fedce14cb01a19 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 6 Jun 2022 08:49:41 +1000 Subject: [PATCH] Vendor PDFs --- app/Jobs/Entity/CreateEntityPdf.php | 6 +- app/Jobs/Vendor/CreatePurchaseOrderPdf.php | 219 +++++++++++++++++++++ 2 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 app/Jobs/Vendor/CreatePurchaseOrderPdf.php diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index a0821fab9773..4a5be2b5887a 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -1,11 +1,11 @@ invitation = $invitation; + + $this->entity = $invitation->purchase_order; + $this->entity_string = 'purchase_order'; + + $this->contact = $invitation->contact; + + $this->vendor = $invitation->contact->vendor; + $this->vendor->load('company'); + + $this->disk = Ninja::isHosted() ? config('filesystems.default') : $disk; + + } + + public function handle() + { + + MultiDB::setDb($this->company->db); + + /* Forget the singleton*/ + App::forgetInstance('translator'); + + /* Init a new copy of the translator*/ + $t = app('translator'); + /* Set the locale*/ + App::setLocale($this->company->locale()); + + /* Set customized translations _NOW_ */ + $t->replace(Ninja::transformTranslations($this->company->settings)); + + if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') { + return (new Phantom)->generate($this->invitation); + } + + $entity_design_id = ''; + + $path = $this->vendor->purchase_order_filepath($this->invitation); + $entity_design_id = 'purchase_order_design_id'; + + $file_path = $path.$this->entity->numberFormatter().'.pdf'; + + $entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey('Wpmbk5ezJn'); + + $design = Design::find($entity_design_id); + + /* Catch all in case migration doesn't pass back a valid design */ + if(!$design) + $design = Design::find(2); + + $html = new HtmlEngine($this->invitation); + + 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' => null, + 'vendor' => $this->vendor, + 'entity' => $this->entity, + 'pdf_variables' => (array) $this->company->settings->pdf_variables, + '$product' => $design->design->product, + 'variables' => $variables, + ]), + 'variables' => $variables, + 'options' => [ + 'all_pages_header' => $this->entity->company->getSetting('all_pages_header'), + 'all_pages_footer' => $this->entity->company->getSetting('all_pages_footer'), + ], + 'process_markdown' => $this->entity->company->markdown_enabled, + ]; + + $maker = new PdfMakerService($state); + + $maker + ->design($template) + ->build(); + + $pdf = null; + + try { + + if(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){ + $pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true)); + + $numbered_pdf = $this->pageNumbering($pdf, $this->company); + + if($numbered_pdf) + $pdf = $numbered_pdf; + + } + else { + + $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true)); + + $numbered_pdf = $this->pageNumbering($pdf, $this->company); + + if($numbered_pdf) + $pdf = $numbered_pdf; + + + } + + } catch (\Exception $e) { + nlog(print_r($e->getMessage(), 1)); + } + + if (config('ninja.log_pdf_html')) { + info($maker->getCompiledHTML()); + } + + if ($pdf) { + + try{ + + if(!Storage::disk($this->disk)->exists($path)) + Storage::disk($this->disk)->makeDirectory($path, 0775); + + Storage::disk($this->disk)->put($file_path, $pdf, 'public'); + + } + catch(\Exception $e) + { + + throw new FilePermissionsFailure($e->getMessage()); + + } + } + + return $file_path; + } + + public function failed($e) + { + + } + + +}