mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Clean up for PDF code paths
This commit is contained in:
parent
3b86d39e1d
commit
96c15c9716
@ -1,297 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Invoice Ninja (https://entityninja.com).
|
|
||||||
*
|
|
||||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
||||||
*
|
|
||||||
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
|
||||||
*
|
|
||||||
* @license https://www.elastic.co/licensing/elastic-license
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace App\Jobs\Entity;
|
|
||||||
|
|
||||||
use App\Utils\Ninja;
|
|
||||||
use App\Models\Quote;
|
|
||||||
use App\Models\Credit;
|
|
||||||
use App\Models\Design;
|
|
||||||
use App\Models\Invoice;
|
|
||||||
use App\Utils\HtmlEngine;
|
|
||||||
use App\Libraries\MultiDB;
|
|
||||||
use Illuminate\Bus\Queueable;
|
|
||||||
use App\Models\QuoteInvitation;
|
|
||||||
use App\Utils\Traits\MakesHash;
|
|
||||||
use App\Models\CreditInvitation;
|
|
||||||
use App\Models\RecurringInvoice;
|
|
||||||
use App\Utils\PhantomJS\Phantom;
|
|
||||||
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;
|
|
||||||
use App\Utils\Traits\NumberFormatter;
|
|
||||||
use App\Utils\Traits\MakesInvoiceHtml;
|
|
||||||
use Illuminate\Queue\SerializesModels;
|
|
||||||
use App\Utils\Traits\Pdf\PageNumbering;
|
|
||||||
use Illuminate\Support\Facades\Storage;
|
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
|
||||||
use App\Exceptions\FilePermissionsFailure;
|
|
||||||
use App\Models\RecurringInvoiceInvitation;
|
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
|
||||||
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;
|
|
||||||
|
|
||||||
class CreateEntityPdf implements ShouldQueue
|
|
||||||
{
|
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash, PageNumbering;
|
|
||||||
|
|
||||||
public \App\Models\Invoice | \App\Models\Quote | \App\Models\Credit | \App\Models\RecurringInvoice | null $entity;
|
|
||||||
|
|
||||||
public \App\Models\Company | null $company;
|
|
||||||
|
|
||||||
public \App\Models\ClientContact | null $contact;
|
|
||||||
|
|
||||||
private $disk;
|
|
||||||
|
|
||||||
public \App\Models\InvoiceInvitation | \App\Models\QuoteInvitation | \App\Models\CreditInvitation | \App\Models\RecurringInvoiceInvitation | null $invitation;
|
|
||||||
|
|
||||||
public string $entity_string = '';
|
|
||||||
|
|
||||||
public \App\Models\Client | null $client;
|
|
||||||
|
|
||||||
public $deleteWhenMissingModels = true;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new job instance.
|
|
||||||
*
|
|
||||||
* @param $invitation
|
|
||||||
*/
|
|
||||||
public function __construct($invitation, $disk = null)
|
|
||||||
{
|
|
||||||
$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;
|
|
||||||
|
|
||||||
$this->client = $invitation->contact->client;
|
|
||||||
$this->client->load('company');
|
|
||||||
|
|
||||||
$this->disk = $disk ?? config('filesystems.default');
|
|
||||||
}
|
|
||||||
|
|
||||||
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->client->locale());
|
|
||||||
|
|
||||||
/* Set customized translations _NOW_ */
|
|
||||||
$t->replace(Ninja::transformTranslations($this->client->getMergedSettings()));
|
|
||||||
|
|
||||||
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
|
||||||
return (new Phantom)->generate($this->invitation);
|
|
||||||
}
|
|
||||||
|
|
||||||
$entity_design_id = '';
|
|
||||||
$path = '';
|
|
||||||
|
|
||||||
if ($this->entity instanceof Invoice) {
|
|
||||||
$path = $this->client->invoice_filepath($this->invitation);
|
|
||||||
$entity_design_id = 'invoice_design_id';
|
|
||||||
} elseif ($this->entity instanceof Quote) {
|
|
||||||
$path = $this->client->quote_filepath($this->invitation);
|
|
||||||
$entity_design_id = 'quote_design_id';
|
|
||||||
} elseif ($this->entity instanceof Credit) {
|
|
||||||
$path = $this->client->credit_filepath($this->invitation);
|
|
||||||
$entity_design_id = 'credit_design_id';
|
|
||||||
} elseif ($this->entity instanceof RecurringInvoice) {
|
|
||||||
$path = $this->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->client->getSetting($entity_design_id));
|
|
||||||
|
|
||||||
/** @var \App\Models\Design $design */
|
|
||||||
$design = Design::withTrashed()->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' => $this->client,
|
|
||||||
'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->client->getSetting('all_pages_header'),
|
|
||||||
'all_pages_footer' => $this->entity->client->getSetting('all_pages_footer'),
|
|
||||||
'client' => $this->client,
|
|
||||||
'entity' => $this->entity,
|
|
||||||
'variables' => $variables,
|
|
||||||
],
|
|
||||||
'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') {
|
|
||||||
$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($this->entity_string == "invoice" && $this->client->getSetting('enable_e_invoice'))
|
|
||||||
{
|
|
||||||
$pdf = $this->checkEInvoice($pdf);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($pdf) {
|
|
||||||
try {
|
|
||||||
Storage::disk($this->disk)->put($file_path, $pdf);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
throw new FilePermissionsFailure($e->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->invitation = null;
|
|
||||||
// $this->entity = null;
|
|
||||||
$this->company = null;
|
|
||||||
$this->client = null;
|
|
||||||
$this->contact = null;
|
|
||||||
$maker = null;
|
|
||||||
$state = null;
|
|
||||||
|
|
||||||
return $file_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function failed($e)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,8 +17,6 @@ use App\Models\Credit;
|
|||||||
use App\Models\Design;
|
use App\Models\Design;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Utils\HtmlEngine;
|
use App\Utils\HtmlEngine;
|
||||||
use App\Libraries\MultiDB;
|
|
||||||
use Illuminate\Bus\Queueable;
|
|
||||||
use App\Models\QuoteInvitation;
|
use App\Models\QuoteInvitation;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use App\Models\CreditInvitation;
|
use App\Models\CreditInvitation;
|
||||||
@ -32,13 +30,9 @@ use Illuminate\Support\Facades\App;
|
|||||||
use App\Jobs\Invoice\CreateEInvoice;
|
use App\Jobs\Invoice\CreateEInvoice;
|
||||||
use App\Utils\Traits\NumberFormatter;
|
use App\Utils\Traits\NumberFormatter;
|
||||||
use App\Utils\Traits\MakesInvoiceHtml;
|
use App\Utils\Traits\MakesInvoiceHtml;
|
||||||
use Illuminate\Queue\SerializesModels;
|
|
||||||
use App\Utils\Traits\Pdf\PageNumbering;
|
use App\Utils\Traits\Pdf\PageNumbering;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
|
||||||
use App\Exceptions\FilePermissionsFailure;
|
use App\Exceptions\FilePermissionsFailure;
|
||||||
use App\Models\RecurringInvoiceInvitation;
|
use App\Models\RecurringInvoiceInvitation;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
|
||||||
use horstoeko\zugferd\ZugferdDocumentPdfBuilder;
|
use horstoeko\zugferd\ZugferdDocumentPdfBuilder;
|
||||||
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;
|
||||||
|
@ -1,323 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Invoice Ninja (https://invoiceninja.com).
|
|
||||||
*
|
|
||||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
||||||
*
|
|
||||||
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
|
||||||
*
|
|
||||||
* @license https://www.elastic.co/licensing/elastic-license
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace App\Services\Preview;
|
|
||||||
|
|
||||||
use App\Factory\GroupSettingFactory;
|
|
||||||
use App\Jobs\Util\PreviewPdf;
|
|
||||||
use App\Models\Client;
|
|
||||||
use App\Models\ClientContact;
|
|
||||||
use App\Models\Company;
|
|
||||||
use App\Models\Design as DesignModel;
|
|
||||||
use App\Models\Invoice;
|
|
||||||
use App\Models\InvoiceInvitation;
|
|
||||||
use App\Models\User;
|
|
||||||
use App\Models\Vendor;
|
|
||||||
use App\Models\VendorContact;
|
|
||||||
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
|
||||||
use App\Services\PdfMaker\PdfMaker;
|
|
||||||
use App\Utils\HostedPDF\NinjaPdf;
|
|
||||||
use App\Utils\HtmlEngine;
|
|
||||||
use App\Utils\PhantomJS\Phantom;
|
|
||||||
use App\Utils\Traits\MakesHash;
|
|
||||||
use App\Utils\Traits\Pdf\PageNumbering;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Support\Str;
|
|
||||||
|
|
||||||
//@deprecated version
|
|
||||||
class StubBuilder
|
|
||||||
{
|
|
||||||
use PageNumbering;
|
|
||||||
use MakesHash;
|
|
||||||
|
|
||||||
public $entity;
|
|
||||||
|
|
||||||
public $entity_type;
|
|
||||||
|
|
||||||
/** @var Client | Vendor $recipient **/
|
|
||||||
public Client | Vendor $recipient;
|
|
||||||
|
|
||||||
public mixed $contact;
|
|
||||||
|
|
||||||
public mixed $invitation;
|
|
||||||
|
|
||||||
public string $recipient_string;
|
|
||||||
|
|
||||||
public string $html;
|
|
||||||
|
|
||||||
public string $dynamic_settings_type;
|
|
||||||
|
|
||||||
public array $settings;
|
|
||||||
|
|
||||||
public function __construct(public Company $company, public User $user)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setEntityType($entity_type)
|
|
||||||
{
|
|
||||||
$this->entity_type = $entity_type;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function build(): self
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
DB::connection(config('database.default'))->transaction(function () {
|
|
||||||
$this->createRecipient()
|
|
||||||
->initializeSettings()
|
|
||||||
->createEntity()
|
|
||||||
->linkRelations()
|
|
||||||
->buildHtml();
|
|
||||||
});
|
|
||||||
} catch (\Throwable $throwable) {
|
|
||||||
nlog("DB ERROR " . $throwable->getMessage());
|
|
||||||
|
|
||||||
if (DB::connection(config('database.default'))->transactionLevel() > 0) {
|
|
||||||
DB::connection(config('database.default'))->rollBack();
|
|
||||||
}
|
|
||||||
} catch(\Exception $e) {
|
|
||||||
nlog($e->getMessage());
|
|
||||||
|
|
||||||
if (DB::connection(config('database.default'))->transactionLevel() > 0) {
|
|
||||||
DB::connection(config('database.default'))->rollBack();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getPdf(): mixed
|
|
||||||
{
|
|
||||||
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
|
||||||
return (new Phantom)->convertHtmlToPdf($this->html);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
|
||||||
$pdf = (new NinjaPdf())->build($this->html);
|
|
||||||
|
|
||||||
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
|
|
||||||
|
|
||||||
if ($numbered_pdf) {
|
|
||||||
$pdf = $numbered_pdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $pdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (new PreviewPdf($this->html, $this->company))->handle();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function initializeSettings(): self
|
|
||||||
{
|
|
||||||
$this->dynamic_settings_type = 'company';
|
|
||||||
|
|
||||||
match ($this->dynamic_settings_type) {
|
|
||||||
'company' => $this->setCompanySettings(),
|
|
||||||
'client' => $this->setClientSettings(),
|
|
||||||
'group' => $this->setGroupSettings(),
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function setCompanySettings(): self
|
|
||||||
{
|
|
||||||
$this->company->settings = $this->settings;
|
|
||||||
$this->company->save();
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function setClientSettings(): self
|
|
||||||
{
|
|
||||||
$this->recipient->settings = $this->settings;
|
|
||||||
$this->recipient->save();
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function setGroupSettings(): self
|
|
||||||
{
|
|
||||||
$g = GroupSettingFactory::create($this->company->id, $this->user->id);
|
|
||||||
$g->name = Str::random(10);
|
|
||||||
$g->settings = $this->settings;
|
|
||||||
$g->save();
|
|
||||||
|
|
||||||
$this->recipient->group_settings_id = $g->id;
|
|
||||||
$this->recipient->save();
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setSettings($settings): self
|
|
||||||
{
|
|
||||||
$this->settings = $settings;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setSettingsType($type): self
|
|
||||||
{
|
|
||||||
$this->dynamic_settings_type = $type;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function buildHtml(): self
|
|
||||||
{
|
|
||||||
$html = new HtmlEngine($this->invitation);
|
|
||||||
|
|
||||||
$design_string = "{$this->entity_type}_design_id";
|
|
||||||
|
|
||||||
$design = DesignModel::query()->withTrashed()->find($this->decodePrimaryKey($html->settings->{$design_string}));
|
|
||||||
|
|
||||||
$template = new PdfMakerDesign(strtolower($design->name));
|
|
||||||
|
|
||||||
$state = [
|
|
||||||
'template' => $template->elements([
|
|
||||||
'client' => $this->recipient,
|
|
||||||
'entity' => $this->entity,
|
|
||||||
'pdf_variables' => (array) $html->settings->pdf_variables,
|
|
||||||
'$product' => $design->design->product,
|
|
||||||
]),
|
|
||||||
'variables' => $html->generateLabelsAndValues(),
|
|
||||||
'process_markdown' => $this->company->markdown_enabled,
|
|
||||||
'options' => [
|
|
||||||
'client' => $this->recipient,
|
|
||||||
'entity' => $this->entity,
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
$maker = new PdfMaker($state);
|
|
||||||
|
|
||||||
$this->html = $maker->design($template)
|
|
||||||
->build()
|
|
||||||
->getCompiledHTML();
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function linkRelations(): self
|
|
||||||
{
|
|
||||||
$this->entity->setRelation('invitations', $this->invitation);
|
|
||||||
$this->entity->setRelation($this->recipient_string, $this->recipient);
|
|
||||||
$this->entity->setRelation('company', $this->company);
|
|
||||||
$this->entity->load("{$this->recipient_string}.company");
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createRecipient(): self
|
|
||||||
{
|
|
||||||
match ($this->entity_type) {
|
|
||||||
'invoice' => $this->createClient(),
|
|
||||||
'quote' => $this->createClient(),
|
|
||||||
'credit' => $this->createClient(),
|
|
||||||
'purchase_order' => $this->createVendor(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createClient(): self
|
|
||||||
{
|
|
||||||
/** @var \App\Models\Client $client */
|
|
||||||
$client = Client::factory()->create([
|
|
||||||
'user_id' => $this->user->id,
|
|
||||||
'company_id' => $this->company->id,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->recipient = $client;
|
|
||||||
|
|
||||||
$this->contact = ClientContact::factory()->create([
|
|
||||||
'user_id' => $this->user->id,
|
|
||||||
'company_id' => $this->company->id,
|
|
||||||
'client_id' => $this->recipient->id,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->recipient_string = 'client';
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createVendor(): self
|
|
||||||
{
|
|
||||||
/** @var \App\Models\Vendor $vendor */
|
|
||||||
$vendor = Vendor::factory()->create([
|
|
||||||
'user_id' => $this->user->id,
|
|
||||||
'company_id' => $this->user->company()->id,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->recipient = $vendor;
|
|
||||||
|
|
||||||
$this->contact = VendorContact::factory()->create([
|
|
||||||
'user_id' => $this->user->id,
|
|
||||||
'company_id' => $this->company->id,
|
|
||||||
'vendor_id' => $this->recipient->id,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->recipient_string = 'vendor';
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private function createEntity(): self
|
|
||||||
{
|
|
||||||
match ($this->entity_type) {
|
|
||||||
'invoice' => $this->createInvoice(),
|
|
||||||
'quote' => $this->createQuote(),
|
|
||||||
'credit' => $this->createCredit(),
|
|
||||||
'purchase_order' => $this->createPurchaseOrder(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createInvoice()
|
|
||||||
{
|
|
||||||
/** @var \App\Models\Invoice $invoice */
|
|
||||||
$invoice = Invoice::factory()->create([
|
|
||||||
'user_id' => $this->user->id,
|
|
||||||
'company_id' => $this->company->id,
|
|
||||||
'client_id' => $this->recipient->id,
|
|
||||||
'terms' => $this->company->settings->invoice_terms,
|
|
||||||
'footer' => $this->company->settings->invoice_footer,
|
|
||||||
'status_id' => Invoice::STATUS_PAID,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->entity = $invoice;
|
|
||||||
|
|
||||||
$this->invitation = InvoiceInvitation::factory()->create([
|
|
||||||
'user_id' => $this->user->id,
|
|
||||||
'company_id' => $this->company->id,
|
|
||||||
'invoice_id' => $this->entity->id,
|
|
||||||
'client_contact_id' => $this->contact->id,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createQuote()
|
|
||||||
{
|
|
||||||
$this->entity->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createCredit()
|
|
||||||
{
|
|
||||||
$this->entity->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createPurchaseOrder()
|
|
||||||
{
|
|
||||||
$this->entity->save();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user