1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 01:11:34 +02:00
invoiceninja/app/Jobs/Entity/CreateRawPdf.php

274 lines
8.8 KiB
PHP
Raw Normal View History

2021-09-28 07:29:31 +02:00
<?php
/**
* Entity Ninja (https://entityninja.com).
*
* @link https://github.com/entityninja/entityninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Entity Ninja LLC (https://entityninja.com)
2021-09-28 07:29:31 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Entity;
2023-08-16 11:55:35 +02:00
use App\Utils\Ninja;
use App\Models\Quote;
2021-09-28 07:29:31 +02:00
use App\Models\Credit;
use App\Models\Design;
use App\Models\Invoice;
2023-08-16 11:55:35 +02:00
use App\Utils\HtmlEngine;
use App\Libraries\MultiDB;
use Illuminate\Bus\Queueable;
2021-09-28 07:29:31 +02:00
use App\Models\QuoteInvitation;
2023-08-16 11:55:35 +02:00
use App\Utils\Traits\MakesHash;
use App\Models\CreditInvitation;
2021-09-28 07:29:31 +02:00
use App\Models\RecurringInvoice;
use App\Utils\PhantomJS\Phantom;
2023-08-16 11:55:35 +02:00
use App\Models\InvoiceInvitation;
use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\Traits\Pdf\PdfMaker;
use Illuminate\Support\Facades\App;
use App\Jobs\Invoice\CreateEInvoice;
2021-09-28 07:29:31 +02:00
use App\Utils\Traits\NumberFormatter;
2023-08-16 11:55:35 +02:00
use App\Utils\Traits\MakesInvoiceHtml;
use Illuminate\Queue\SerializesModels;
2022-05-25 14:00:17 +02:00
use App\Utils\Traits\Pdf\PageNumbering;
2023-08-16 11:55:35 +02:00
use Illuminate\Queue\InteractsWithQueue;
use App\Exceptions\FilePermissionsFailure;
use App\Models\RecurringInvoiceInvitation;
2021-09-28 07:29:31 +02:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2023-08-16 11:55:35 +02:00
use horstoeko\zugferd\ZugferdDocumentPdfBuilder;
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
2021-09-28 07:29:31 +02:00
class CreateRawPdf implements ShouldQueue
{
2022-05-25 14:00:17 +02:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash, PageNumbering;
2021-09-28 07:29:31 +02:00
2023-08-16 11:55:35 +02:00
public Invoice | Credit | Quote | RecurringInvoice $entity;
2021-09-28 07:29:31 +02:00
public $company;
public $contact;
public $invitation;
public $entity_string = '';
/**
* Create a new job instance.
*
* @param $invitation
*/
public function __construct($invitation, $db)
{
MultiDB::setDb($db);
2021-09-28 07:29:31 +02:00
$this->invitation = $invitation;
if ($invitation instanceof InvoiceInvitation) {
$this->entity = $invitation->invoice;
$this->entity_string = 'invoice';
} elseif ($invitation instanceof QuoteInvitation) {
$this->entity = $invitation->quote;
$this->entity_string = 'quote';
} elseif ($invitation instanceof CreditInvitation) {
$this->entity = $invitation->credit;
$this->entity_string = 'credit';
} elseif ($invitation instanceof RecurringInvoiceInvitation) {
$this->entity = $invitation->recurring_invoice;
$this->entity_string = 'recurring_invoice';
}
$this->company = $invitation->company;
$this->contact = $invitation->contact;
}
public function handle()
{
/* Forget the singleton*/
App::forgetInstance('translator');
/* Init a new copy of the translator*/
$t = app('translator');
/* Set the locale*/
App::setLocale($this->contact->preferredLocale());
/* Set customized translations _NOW_ */
$t->replace(Ninja::transformTranslations($this->entity->client->getMergedSettings()));
2022-11-30 22:49:59 +01:00
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
return (new Phantom)->generate($this->invitation, true);
}
2021-09-28 07:29:31 +02:00
$entity_design_id = '';
2023-08-16 11:55:35 +02:00
$path = '';
2021-09-28 07:29:31 +02:00
if ($this->entity instanceof Invoice) {
$path = $this->entity->client->invoice_filepath($this->invitation);
$entity_design_id = 'invoice_design_id';
} elseif ($this->entity instanceof Quote) {
$path = $this->entity->client->quote_filepath($this->invitation);
$entity_design_id = 'quote_design_id';
} elseif ($this->entity instanceof Credit) {
$path = $this->entity->client->credit_filepath($this->invitation);
$entity_design_id = 'credit_design_id';
} elseif ($this->entity instanceof RecurringInvoice) {
$path = $this->entity->client->recurring_invoice_filepath($this->invitation);
$entity_design_id = 'invoice_design_id';
}
$file_path = $path.$this->entity->numberFormatter().'.pdf';
$entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting($entity_design_id));
2023-08-08 10:56:31 +02:00
$design = Design::query()->withTrashed()->find($entity_design_id);
2021-09-28 07:29:31 +02:00
/* Catch all in case migration doesn't pass back a valid design */
if (! $design) {
2023-08-08 10:56:31 +02:00
$design = Design::query()->find(2);
}
2021-09-28 07:29:31 +02:00
$html = new HtmlEngine($this->invitation);
if ($design->is_custom) {
$options = [
'custom_partials' => json_decode(json_encode($design->design), true),
];
2021-09-28 07:29:31 +02:00
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
} else {
$template = new PdfMakerDesign(strtolower($design->name));
}
$variables = $html->generateLabelsAndValues();
$state = [
'template' => $template->elements([
'client' => $this->entity->client,
'entity' => $this->entity,
'pdf_variables' => (array) $this->entity->company->settings->pdf_variables,
'$product' => $design->design->product,
'variables' => $variables,
]),
'variables' => $variables,
'options' => [
'all_pages_header' => $this->entity->client->getSetting('all_pages_header'),
'all_pages_footer' => $this->entity->client->getSetting('all_pages_footer'),
],
'process_markdown' => $this->entity->client->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') {
2021-09-28 07:29:31 +02:00
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
$finfo = new \finfo(FILEINFO_MIME);
//fallback in case hosted PDF fails.
if ($finfo->buffer($pdf) != 'application/pdf; charset=binary') {
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
2022-05-25 14:00:17 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
}
} else {
2021-09-28 07:29:31 +02:00
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
2022-05-25 14:00:17 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
2021-09-28 07:29:31 +02:00
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
}
2021-09-28 07:29:31 +02:00
} catch (\Exception $e) {
nlog(print_r($e->getMessage(), 1));
}
2021-11-28 11:23:35 +01:00
if (config('ninja.log_pdf_html')) {
2021-09-28 07:29:31 +02:00
info($maker->getCompiledHTML());
2021-11-28 11:23:35 +01:00
}
2021-09-28 07:29:31 +02:00
if ($pdf) {
2022-11-01 11:20:28 +01:00
$maker =null;
$state = null;
2023-08-16 11:55:35 +02:00
return $this->checkEInvoice($pdf);
}
2021-09-28 07:29:31 +02:00
throw new FilePermissionsFailure('Unable to generate the raw PDF');
2021-09-28 07:29:31 +02:00
}
2023-08-16 11:55:35 +02:00
/**
* Switch to determine if we need to embed the xml into the PDF itself
*
* @param string $pdf
* @return string
*/
private function checkEInvoice(string $pdf): string
{
if(!$this->entity instanceof Invoice)
return $pdf;
$e_invoice_type = $this->entity->client->getSetting('e_invoice_type');
switch ($e_invoice_type) {
case "EN16931":
case "XInvoice_2_2":
case "XInvoice_2_1":
case "XInvoice_2_0":
case "XInvoice_1_0":
case "XInvoice-Extended":
case "XInvoice-BasicWL":
case "XInvoice-Basic":
return $this->embedEInvoiceZuGFerD($pdf) ?? $pdf;
//case "Facturae_3.2":
//case "Facturae_3.2.1":
//case "Facturae_3.2.2":
//
default:
return $pdf;
}
}
/**
* Embed the .xml file into the PDF
*
* @param string $pdf
* @return string
*/
private function embedEInvoiceZuGFerD(string $pdf): string
{
try {
$e_rechnung = (new CreateEInvoice($this->entity, true))->handle();
$pdfBuilder = new ZugferdDocumentPdfBuilder($e_rechnung, $pdf);
$pdfBuilder->generateDocument();
return $pdfBuilder->downloadString(basename($this->entity->getFileName()));
} catch (\Exception $e) {
nlog("E_Invoice Merge failed - " . $e->getMessage());
}
return $pdf;
}
2021-09-28 07:29:31 +02:00
public function failed($e)
{
}
}