Push purchase order PDf creator into unified system

This commit is contained in:
David Bomba 2023-01-08 16:15:04 +11:00
parent ea9a3f4ca3
commit 05192c9f87
4 changed files with 41 additions and 190 deletions

View File

@ -110,7 +110,7 @@ class InvitationController extends Controller
$file_name = $invitation->purchase_order->numberFormatter().'.pdf'; $file_name = $invitation->purchase_order->numberFormatter().'.pdf';
$file = (new CreatePurchaseOrderPdf($invitation))->rawPdf(); $file = (new CreatePurchaseOrderPdf($invitation))->handle();
$headers = ['Content-Type' => 'application/pdf']; $headers = ['Content-Type' => 'application/pdf'];

View File

@ -26,6 +26,7 @@ use App\Models\RecurringInvoiceInvitation;
use App\Services\PdfMaker\Design as PdfDesignModel; use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign; use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService; use App\Services\PdfMaker\PdfMaker as PdfMakerService;
use App\Services\Pdf\PdfService;
use App\Utils\HostedPDF\NinjaPdf; use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\HtmlEngine; use App\Utils\HtmlEngine;
use App\Utils\Ninja; use App\Utils\Ninja;
@ -49,25 +50,14 @@ use setasign\Fpdi\PdfParser\StreamReader;
class CreatePurchaseOrderPdf implements ShouldQueue class CreatePurchaseOrderPdf implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash, PageNumbering; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $entity; public $entity;
public $company;
public $contact;
private $disk; private $disk;
public $invitation; public $invitation;
public $entity_string = '';
public $vendor;
private string $path = '';
private string $file_path = '';
/** /**
* Create a new job instance. * Create a new job instance.
@ -77,15 +67,12 @@ class CreatePurchaseOrderPdf implements ShouldQueue
public function __construct($invitation, $disk = null) public function __construct($invitation, $disk = null)
{ {
$this->invitation = $invitation; $this->invitation = $invitation;
$this->company = $invitation->company;
$this->entity = $invitation->purchase_order; $this->entity = $invitation->purchase_order;
$this->entity_string = 'purchase_order';
$this->contact = $invitation->contact; $this->contact = $invitation->contact;
$this->vendor = $invitation->contact->vendor; $this->vendor = $invitation->contact->vendor;
$this->vendor->load('company');
$this->disk = $disk ?? config('filesystems.default'); $this->disk = $disk ?? config('filesystems.default');
@ -94,128 +81,20 @@ class CreatePurchaseOrderPdf implements ShouldQueue
public function handle() public function handle()
{ {
$pdf = $this->rawPdf(); MultiDB::setDb($this->invitation->company->db);
$file_path = $this->vendor->purchase_order_filepath($this->invitation);
$pdf = (new PdfService($this->invitation, 'purchase_order'))->getPdf();
if ($pdf) { if ($pdf) {
try {
try{ Storage::disk($this->disk)->put($file_path, $pdf);
Storage::disk($this->disk)->put($this->file_path, $pdf); } catch (\Exception $e) {
}
catch(\Exception $e)
{
throw new FilePermissionsFailure($e->getMessage()); throw new FilePermissionsFailure($e->getMessage());
} }
} }
return $this->file_path;
}
public function rawPdf()
{
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, true);
}
$entity_design_id = '';
$this->path = $this->vendor->purchase_order_filepath($this->invitation);
$entity_design_id = 'purchase_order_design_id';
$this->file_path = $this->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 VendorHtmlEngine($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());
}
$maker = null;
$state = null;
return $pdf; return $pdf;
} }

View File

@ -55,8 +55,6 @@ class PdfConfiguration
public string $entity_string; public string $entity_string;
public $service;
public array $pdf_variables; public array $pdf_variables;
public Currency $currency; public Currency $currency;
@ -67,20 +65,14 @@ class PdfConfiguration
* @var App\Models\Client | App\Models\Vendor * @var App\Models\Client | App\Models\Vendor
* *
*/ */
public $currency_entity; public Client | Vendor $currency_entity;
public function __construct(PdfService $service) public function __construct(public PdfService $service){}
{
$this->service = $service;
}
public function init(): self public function init(): self
{ {
$this->setEntityType() $this->setEntityType()
->setEntityProperties()
->setPdfVariables() ->setPdfVariables()
->setDesign() ->setDesign()
->setCurrency() ->setCurrency()
@ -140,88 +132,63 @@ class PdfConfiguration
private function setEntityType() private function setEntityType()
{ {
$entity_design_id = '';
if ($this->service->invitation instanceof InvoiceInvitation) { if ($this->service->invitation instanceof InvoiceInvitation) {
$this->entity = $this->service->invitation->invoice; $this->entity = $this->service->invitation->invoice;
$this->entity_string = 'invoice'; $this->entity_string = 'invoice';
} elseif ($this->service->invitation instanceof QuoteInvitation) {
$this->entity = $this->service->invitation->quote;
$this->entity_string = 'quote';
} elseif ($this->service->invitation instanceof CreditInvitation) {
$this->entity = $this->service->invitation->credit;
$this->entity_string = 'credit';
} elseif ($this->service->invitation instanceof RecurringInvoiceInvitation) {
$this->entity = $this->service->invitation->recurring_invoice;
$this->entity_string = 'recurring_invoice';
} elseif ($this->service->invitation instanceof PurchaseOrderInvitation) {
$this->entity = $this->service->invitation->purchase_order;
$this->entity_string = 'purchase_order';
} else {
throw new \Exception('Unable to resolve entity', 500);
}
return $this;
}
private function setEntityProperties()
{
$entity_design_id = '';
if ($this->entity instanceof Invoice) {
$this->client = $this->entity->client; $this->client = $this->entity->client;
$this->contact = $this->service->invitation->contact; $this->contact = $this->service->invitation->contact;
$this->path = $this->client->invoice_filepath($this->service->invitation); $this->path = $this->client->invoice_filepath($this->service->invitation);
$this->entity_design_id = 'invoice_design_id'; $this->entity_design_id = 'invoice_design_id';
$this->settings = $this->client->getMergedSettings(); $this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client; $this->settings_object = $this->client;
} elseif ($this->service->invitation instanceof QuoteInvitation) {
} elseif ($this->entity instanceof Quote) { $this->entity = $this->service->invitation->quote;
$this->entity_string = 'quote';
$this->client = $this->entity->client; $this->client = $this->entity->client;
$this->contact = $this->service->invitation->contact; $this->contact = $this->service->invitation->contact;
$this->path = $this->client->quote_filepath($this->service->invitation); $this->path = $this->client->quote_filepath($this->service->invitation);
$this->entity_design_id = 'quote_design_id'; $this->entity_design_id = 'quote_design_id';
$this->settings = $this->client->getMergedSettings(); $this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client; $this->settings_object = $this->client;
} elseif ($this->service->invitation instanceof CreditInvitation) {
} elseif ($this->entity instanceof Credit) { $this->entity = $this->service->invitation->credit;
$this->entity_string = 'credit';
$this->client = $this->entity->client; $this->client = $this->entity->client;
$this->contact = $this->service->invitation->contact; $this->contact = $this->service->invitation->contact;
$this->path = $this->client->credit_filepath($this->service->invitation); $this->path = $this->client->credit_filepath($this->service->invitation);
$this->entity_design_id = 'credit_design_id'; $this->entity_design_id = 'credit_design_id';
$this->settings = $this->client->getMergedSettings(); $this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client; $this->settings_object = $this->client;
} elseif ($this->service->invitation instanceof RecurringInvoiceInvitation) {
} elseif ($this->entity instanceof RecurringInvoice) { $this->entity = $this->service->invitation->recurring_invoice;
$this->entity_string = 'recurring_invoice';
$this->client = $this->entity->client; $this->client = $this->entity->client;
$this->contact = $this->service->invitation->contact; $this->contact = $this->service->invitation->contact;
$this->path = $this->client->recurring_invoice_filepath($this->service->invitation); $this->path = $this->client->recurring_invoice_filepath($this->service->invitation);
$this->entity_design_id = 'invoice_design_id'; $this->entity_design_id = 'invoice_design_id';
$this->settings = $this->client->getMergedSettings(); $this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client; $this->settings_object = $this->client;
} elseif ($this->service->invitation instanceof PurchaseOrderInvitation) {
} elseif ($this->entity instanceof PurchaseOrder) { $this->entity = $this->service->invitation->purchase_order;
$this->entity_string = 'purchase_order';
$this->vendor = $this->entity->vendor; $this->vendor = $this->entity->vendor;
$this->vendor_contact = $this->service->invitation->contact; $this->vendor_contact = $this->service->invitation->contact;
$this->path = $this->vendor->purchase_order_filepath($this->service->invitation); $this->path = $this->vendor->purchase_order_filepath($this->service->invitation);
$this->entity_design_id = 'invoice_design_id'; $this->entity_design_id = 'invoice_design_id';
$this->entity_design_id = 'purchase_order_design_id'; $this->entity_design_id = 'purchase_order_design_id';
$this->settings = $this->vendor->getMergedSettings(); $this->settings = $this->vendor->company->settings;
$this->settings_object = $this->client; $this->settings_object = $this->vendor;
$this->client = null;
} } else {
else throw new \Exception('Unable to resolve entity', 500);
throw new \Exception('Unable to resolve entity properties type', 500); }
$this->path = $this->path.$this->entity->numberFormatter().'.pdf'; $this->path = $this->path.$this->entity->numberFormatter().'.pdf';
return $this; return $this;
} }
private function setDesign() private function setDesign()

View File

@ -15,20 +15,23 @@ use App\Models\Account;
use App\Models\Company; use App\Models\Company;
use App\Models\CreditInvitation; use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation; use App\Models\InvoiceInvitation;
use App\Models\PurchaseOrderInvitation;
use App\Models\QuoteInvitation; use App\Models\QuoteInvitation;
use App\Models\RecurringInvoiceInvitation;
use App\Services\Pdf\PdfConfiguration; use App\Services\Pdf\PdfConfiguration;
use App\Utils\HostedPDF\NinjaPdf; use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\HtmlEngine; use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom; use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\Pdf\PageNumbering; use App\Utils\Traits\Pdf\PageNumbering;
use App\Utils\Traits\Pdf\PdfMaker; use App\Utils\Traits\Pdf\PdfMaker;
use App\Utils\VendorHtmlEngine;
class PdfService class PdfService
{ {
use PdfMaker, PageNumbering; use PdfMaker, PageNumbering;
public InvoiceInvitation | QuoteInvitation | CreditInvitation $invitation; public InvoiceInvitation | QuoteInvitation | CreditInvitation | RecurringInvoiceInvitation | PurchaseOrderInvitation $invitation;
public Company $company; public Company $company;
@ -62,7 +65,9 @@ class PdfService
$this->config = (new PdfConfiguration($this))->init(); $this->config = (new PdfConfiguration($this))->init();
$this->html_variables = (new HtmlEngine($invitation))->generateLabelsAndValues(); $this->html_variables = $this->config->client ?
(new HtmlEngine($invitation))->generateLabelsAndValues() :
(new VendorHtmlEngine($invitation))->generateLabelsAndValues();
$this->designer = (new PdfDesigner($this))->build(); $this->designer = (new PdfDesigner($this))->build();