1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Vendor/CreatePurchaseOrderPdf.php

205 lines
6.0 KiB
PHP
Raw Normal View History

2022-06-06 00:49:41 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @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)
2022-06-06 00:49:41 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Vendor;
use App\Exceptions\FilePermissionsFailure;
use App\Libraries\MultiDB;
use App\Models\Design;
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\Ninja;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
2022-06-06 05:28:10 +02:00
use App\Utils\Traits\Pdf\PageNumbering;
2022-06-06 00:49:41 +02:00
use App\Utils\Traits\Pdf\PdfMaker;
2022-06-06 05:28:10 +02:00
use App\Utils\VendorHtmlEngine;
2022-06-06 00:49:41 +02:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Storage;
class CreatePurchaseOrderPdf implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash, PageNumbering;
public $entity;
public $company;
public $contact;
private $disk;
public $invitation;
public $entity_string = '';
public $vendor;
2022-06-30 05:32:44 +02:00
private string $path = '';
private string $file_path = '';
2022-06-06 00:49:41 +02:00
/**
* Create a new job instance.
*
* @param $invitation
*/
2022-06-14 14:18:20 +02:00
public function __construct($invitation, $disk = null)
2022-06-06 00:49:41 +02:00
{
$this->invitation = $invitation;
2022-06-06 14:27:17 +02:00
$this->company = $invitation->company;
2022-06-06 00:49:41 +02:00
$this->entity = $invitation->purchase_order;
$this->entity_string = 'purchase_order';
$this->contact = $invitation->contact;
$this->vendor = $invitation->contact->vendor;
$this->vendor->load('company');
2022-06-14 14:18:20 +02:00
$this->disk = $disk ?? config('filesystems.default');
2022-06-06 00:49:41 +02:00
}
public function handle()
2022-06-29 03:47:16 +02:00
{
$pdf = $this->rawPdf();
if ($pdf) {
2023-02-16 02:36:09 +01:00
try {
2022-10-23 23:42:38 +02:00
Storage::disk($this->disk)->put($this->file_path, $pdf);
2023-02-16 02:36:09 +01:00
} catch(\Exception $e) {
2022-06-29 03:47:16 +02:00
throw new FilePermissionsFailure($e->getMessage());
}
}
2022-06-30 05:32:44 +02:00
return $this->file_path;
2022-06-29 03:47:16 +02:00
}
public function rawPdf()
2022-06-06 00:49:41 +02:00
{
MultiDB::setDb($this->company->db);
/* Forget the singleton*/
App::forgetInstance('translator');
/* Init a new copy of the translator*/
$t = app('translator');
/* Set the locale*/
2023-08-10 02:56:34 +02:00
App::setLocale($this->vendor->locale());
2022-06-06 00:49:41 +02:00
/* Set customized translations _NOW_ */
$t->replace(Ninja::transformTranslations($this->company->settings));
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2022-11-30 22:49:59 +01:00
return (new Phantom)->generate($this->invitation, true);
2022-06-06 00:49:41 +02:00
}
$entity_design_id = '';
2022-06-30 05:32:44 +02:00
$this->path = $this->vendor->purchase_order_filepath($this->invitation);
2022-06-06 00:49:41 +02:00
$entity_design_id = 'purchase_order_design_id';
2022-06-30 05:32:44 +02:00
$this->file_path = $this->path.$this->entity->numberFormatter().'.pdf';
2022-06-06 00:49:41 +02:00
$entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey('Wpmbk5ezJn');
$design = Design::withTrashed()->find($entity_design_id);
2022-06-06 00:49:41 +02:00
/* Catch all in case migration doesn't pass back a valid design */
2023-02-16 02:36:09 +01:00
if (!$design) {
2023-08-07 07:33:40 +02:00
/** @var \App\Models\Design $design */
2022-06-06 00:49:41 +02:00
$design = Design::find(2);
2023-02-16 02:36:09 +01:00
}
2022-06-06 00:49:41 +02:00
2022-06-06 05:28:10 +02:00
$html = new VendorHtmlEngine($this->invitation);
2022-06-06 00:49:41 +02:00
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 {
2023-02-16 02:36:09 +01:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2022-06-06 00:49:41 +02:00
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
2023-02-16 02:36:09 +01:00
if ($numbered_pdf) {
2022-06-06 00:49:41 +02:00
$pdf = $numbered_pdf;
2023-02-16 02:36:09 +01:00
}
} else {
2022-06-06 00:49:41 +02:00
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
2023-02-16 02:36:09 +01:00
if ($numbered_pdf) {
2022-06-06 00:49:41 +02:00
$pdf = $numbered_pdf;
2023-02-16 02:36:09 +01:00
}
2022-06-06 00:49:41 +02:00
}
} catch (\Exception $e) {
nlog(print_r($e->getMessage(), 1));
}
if (config('ninja.log_pdf_html')) {
nlog($maker->getCompiledHTML());
2022-06-06 00:49:41 +02:00
}
2022-11-01 11:20:28 +01:00
$maker = null;
$state = null;
2022-06-29 03:47:16 +02:00
return $pdf;
2022-06-06 00:49:41 +02:00
}
public function failed($e)
{
}
}