1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Services/Preview/StubBuilder.php

310 lines
8.2 KiB
PHP
Raw Normal View History

2023-02-21 10:04:45 +01:00
<?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;
2023-02-22 07:37:16 +01:00
use App\Factory\GroupSettingFactory;
use App\Jobs\Util\PreviewPdf;
2023-02-21 10:04:45 +01:00
use App\Models\Client;
2023-02-22 07:37:16 +01:00
use App\Models\ClientContact;
2023-02-21 10:04:45 +01:00
use App\Models\Company;
2023-02-22 07:37:16 +01:00
use App\Models\Design as DesignModel;
2023-02-21 10:04:45 +01:00
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
2023-02-22 07:37:16 +01:00
use App\Models\User;
use App\Models\Vendor;
use App\Models\VendorContact;
use App\Services\PdfMaker\Design as PdfMakerDesign;
2023-02-21 10:04:45 +01:00
use App\Services\PdfMaker\PdfMaker;
2023-02-22 07:37:16 +01:00
use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom;
2023-02-21 10:36:32 +01:00
use App\Utils\Traits\MakesHash;
2023-02-21 10:04:45 +01:00
use App\Utils\Traits\Pdf\PageNumbering;
2023-02-22 07:37:16 +01:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
2023-02-21 10:04:45 +01:00
2023-02-25 04:52:52 +01:00
//@deprecated version
2023-02-21 10:04:45 +01:00
class StubBuilder
{
use PageNumbering;
2023-02-21 10:36:32 +01:00
use MakesHash;
2023-02-21 10:04:45 +01:00
public $entity;
public $entity_type;
public mixed $recipient;
public mixed $contact;
public mixed $invitation;
public string $recipient_string;
public string $html;
2023-02-21 10:26:05 +01:00
public string $dynamic_settings_type;
2023-02-21 10:34:59 +01:00
public array $settings;
2023-02-21 10:26:05 +01:00
2023-02-22 07:37:16 +01:00
public function __construct(public Company $company, public User $user)
{
}
2023-02-21 10:04:45 +01:00
public function setEntityType($entity_type)
{
$this->entity_type = $entity_type;
return $this;
}
public function build(): self
{
2023-02-22 07:37:16 +01:00
try {
DB::connection(config('database.default'))->transaction(function () {
$this->createRecipient()
2023-02-21 10:26:05 +01:00
->initializeSettings()
2023-02-22 07:37:16 +01:00
->createEntity()
2023-02-21 10:04:45 +01:00
->linkRelations()
2023-02-22 07:37:16 +01:00
->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) {
2023-02-21 10:33:50 +01:00
nlog($e->getMessage());
2023-02-21 10:04:45 +01:00
2023-02-22 07:37:16 +01:00
if (DB::connection(config('database.default'))->transactionLevel() > 0) {
DB::connection(config('database.default'))->rollBack();
}
2023-02-21 10:04:45 +01:00
}
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();
}
2023-02-21 10:26:05 +01:00
private function initializeSettings(): self
{
$this->dynamic_settings_type = 'company';
2023-02-22 07:37:16 +01:00
match ($this->dynamic_settings_type) {
2023-02-21 10:26:05 +01:00
'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
{
2023-02-21 10:38:44 +01:00
$g = GroupSettingFactory::create($this->company->id, $this->user->id);
2023-02-21 10:26:05 +01:00
$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;
}
2023-02-21 10:04:45 +01:00
private function buildHtml(): self
{
$html = new HtmlEngine($this->invitation);
2023-02-21 10:26:05 +01:00
$design_string = "{$this->entity_type}_design_id";
2023-02-21 10:44:25 +01:00
$design = DesignModel::withTrashed()->find($this->decodePrimaryKey($html->settings->{$design_string}));
2023-02-21 10:38:44 +01:00
$template = new PdfMakerDesign(strtolower($design->name));
2023-02-21 10:04:45 +01:00
$state = [
2023-02-21 10:38:44 +01:00
'template' => $template->elements([
2023-02-21 10:04:45 +01:00
'client' => $this->recipient,
'entity' => $this->entity,
2023-02-21 10:26:05 +01:00
'pdf_variables' => (array) $html->settings->pdf_variables,
'$product' => $design->design->product,
2023-02-21 10:04:45 +01:00
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $this->company->markdown_enabled,
];
$maker = new PdfMaker($state);
2023-02-21 10:41:23 +01:00
$this->html = $maker->design($template)
2023-02-21 10:04:45 +01:00
->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
{
2023-02-22 07:37:16 +01:00
match ($this->entity_type) {
2023-02-21 10:04:45 +01:00
'invoice' => $this->createClient(),
'quote' => $this->createClient(),
'credit' => $this->createClient(),
'purchase_order' => $this->createVendor(),
};
return $this;
}
private function createClient(): self
{
$this->recipient = Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
]);
$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
{
$this->recipient = Vendor::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->user->company()->id,
]);
$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
{
2023-02-22 07:37:16 +01:00
match ($this->entity_type) {
2023-02-21 10:04:45 +01:00
'invoice' => $this->createInvoice(),
'quote' => $this->createQuote(),
'credit' => $this->createCredit(),
'purchase_order' => $this->createPurchaseOrder(),
};
return $this;
}
private function createInvoice()
{
$this->entity = 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,
2023-02-21 10:04:45 +01:00
]);
$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();
2023-02-22 07:37:16 +01:00
}
2023-02-21 10:04:45 +01:00
private function createCredit()
{
$this->entity->save();
2023-02-22 07:37:16 +01:00
}
2023-02-21 10:04:45 +01:00
private function createPurchaseOrder()
{
$this->entity->save();
2023-02-22 07:37:16 +01:00
}
2023-02-21 10:04:45 +01:00
}