1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Jobs/Vendor/CreatePurchaseOrderPdf.php

235 lines
6.4 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
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Vendor;
use App\Exceptions\FilePermissionsFailure;
use App\Libraries\MultiDB;
use App\Models\Account;
use App\Models\Credit;
use App\Models\CreditInvitation;
use App\Models\Design;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Models\Quote;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoice;
use App\Models\RecurringInvoiceInvitation;
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\HtmlEngine;
use App\Utils\Ninja;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
use App\Utils\Traits\Pdf\PDF;
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\Lang;
use Illuminate\Support\Facades\Storage;
use setasign\Fpdi\PdfParser\StreamReader;
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) {
try{
2022-06-30 05:32:44 +02:00
if(!Storage::disk($this->disk)->exists($this->path))
Storage::disk($this->disk)->makeDirectory($this->path, 0775);
2022-06-29 03:47:16 +02:00
2022-06-30 05:32:44 +02:00
Storage::disk($this->disk)->put($this->file_path, $pdf, 'public');
2022-06-29 03:47:16 +02:00
}
catch(\Exception $e)
{
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*/
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 = '';
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::find($entity_design_id);
/* Catch all in case migration doesn't pass back a valid design */
if(!$design)
$design = Design::find(2);
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 {
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());
}
2022-06-29 03:47:16 +02:00
return $pdf;
2022-06-06 00:49:41 +02:00
}
public function failed($e)
{
}
}