Updates for custom PDF designer

This commit is contained in:
David Bomba 2023-02-21 18:39:07 +11:00
parent b60f0bc5dc
commit 10793f1d14
5 changed files with 127 additions and 281 deletions

View File

@ -51,13 +51,11 @@ class PdfBuilder
*/
public function __construct(PdfService $service)
{
$this->service = $service;
$this->commonmark = new CommonMarkConverter([
'allow_unsafe_links' => false,
]);
}
/**
@ -68,7 +66,6 @@ class PdfBuilder
*/
public function build(): self
{
$this->getTemplate()
->buildSections()
->getEmptyElements()
@ -99,7 +96,6 @@ class PdfBuilder
*/
private function getTemplate() :self
{
$document = new DOMDocument();
$document->validateOnParse = true;
@ -111,7 +107,6 @@ class PdfBuilder
$this->xpath = new DOMXPath($document);
return $this;
}
/**
@ -122,7 +117,6 @@ class PdfBuilder
*/
private function getProductSections(): self
{
$this->genericSectionBuilder()
->getClientDetails()
->getProductAndTaskTables()
@ -130,16 +124,13 @@ class PdfBuilder
->getProductTotals();
return $this;
}
private function mergeSections(array $section) :self
{
$this->sections = array_merge($this->sections, $section);
return $this;
}
/**
@ -150,7 +141,6 @@ class PdfBuilder
*/
private function getDeliveryNoteSections(): self
{
$this->genericSectionBuilder()
->getProductTotals();
@ -170,7 +160,6 @@ class PdfBuilder
]);
return $this;
}
/**
@ -181,10 +170,9 @@ class PdfBuilder
*/
private function getStatementSections(): self
{
$this->genericSectionBuilder();
$this->mergeSections( [
$this->mergeSections([
'statement-invoice-table' => [
'id' => 'statement-invoice-table',
'elements' => $this->statementInvoiceTable(),
@ -212,7 +200,6 @@ class PdfBuilder
]);
return $this;
}
/**
@ -223,13 +210,11 @@ class PdfBuilder
*/
public function statementInvoiceTableTotals(): array
{
$outstanding = $this->service->options['invoices']->sum('balance');
return [
['element' => 'p', 'content' => '$outstanding_label: ' . Number::formatMoney($outstanding, $this->service->config->client)],
];
}
@ -240,7 +225,6 @@ class PdfBuilder
*/
public function statementPaymentTable(): array
{
if (is_null($this->service->option['payments'])) {
return [];
}
@ -254,9 +238,9 @@ class PdfBuilder
//24-03-2022 show payments per invoice
foreach ($this->service->options['invoices'] as $invoice) {
foreach ($invoice->payments as $payment) {
if($payment->is_deleted)
if ($payment->is_deleted) {
continue;
}
$element = ['element' => 'tr', 'elements' => []];
@ -275,7 +259,6 @@ class PdfBuilder
['element' => 'thead', 'elements' => $this->buildTableHeader('statement_payment')],
['element' => 'tbody', 'elements' => $tbody],
];
}
/**
@ -310,7 +293,6 @@ class PdfBuilder
*/
public function statementAgingTable(): array
{
if (\array_key_exists('show_aging_table', $this->service->options) && $this->service->options['show_aging_table'] === false) {
return [];
}
@ -339,7 +321,6 @@ class PdfBuilder
*/
private function getPurchaseOrderSections(): self
{
$this->genericSectionBuilder()
->getProductAndTaskTables()
->getProductTotals();
@ -356,7 +337,6 @@ class PdfBuilder
]);
return $this;
}
/**
@ -368,7 +348,6 @@ class PdfBuilder
*/
private function genericSectionBuilder(): self
{
$this->mergeSections([
'company-details' => [
'id' => 'company-details',
@ -397,7 +376,6 @@ class PdfBuilder
*/
public function statementInvoiceTable(): array
{
$tbody = [];
foreach ($this->service->options['invoices'] as $invoice) {
@ -516,7 +494,7 @@ class PdfBuilder
$element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['data-ref' => 'product_table-product.tax2-td']];
} elseif ($cell == '$product.tax_rate3') {
$element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['data-ref' => 'product_table-product.tax3-td']];
} else if ($cell == '$product.unit_cost' || $cell == '$task.rate') {
} elseif ($cell == '$product.unit_cost' || $cell == '$task.rate') {
$element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['style' => 'white-space: nowrap;', 'data-ref' => "{$_type}_table-" . substr($cell, 1) . '-td']];
} else {
$element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['data-ref' => "{$_type}_table-" . substr($cell, 1) . '-td']];
@ -543,7 +521,6 @@ class PdfBuilder
*/
public function transformLineItems($items, $table_type = '$product') :array
{
$data = [];
$locale_info = localeconv();
@ -688,7 +665,6 @@ class PdfBuilder
}
return $elements;
}
/**
@ -703,7 +679,6 @@ class PdfBuilder
*/
public function processTaxColumns(string $type): void
{
if ($type == 'product') {
$type_id = 1;
}
@ -746,7 +721,6 @@ class PdfBuilder
array_splice($this->service->config->pdf_variables["{$type}_columns"], $key, 1, $taxes);
}
}
}
/**
@ -777,7 +751,6 @@ class PdfBuilder
['element' => 'script', 'content' => $javascript],
['element' => 'script', 'content' => $html_decode],
]];
}
/**
@ -789,7 +762,6 @@ class PdfBuilder
*/
private function getProductTotals(): self
{
$this->mergeSections([
'table-totals' => [
'id' => 'table-totals',
@ -798,7 +770,6 @@ class PdfBuilder
]);
return $this;
}
/**
@ -812,41 +783,30 @@ class PdfBuilder
*/
private function getProductEntityDetails(): self
{
if($this->service->config->entity_string == 'invoice')
{
$this->mergeSections( [
if ($this->service->config->entity_string == 'invoice') {
$this->mergeSections([
'entity-details' => [
'id' => 'entity-details',
'elements' => $this->invoiceDetails(),
],
]);
}
elseif($this->service->config->entity_string == 'quote')
{
$this->mergeSections( [
} elseif ($this->service->config->entity_string == 'quote') {
$this->mergeSections([
'entity-details' => [
'id' => 'entity-details',
'elements' => $this->quoteDetails(),
],
]);
}
elseif($this->service->config->entity_string == 'credit')
{
$this->mergeSections( [
} elseif ($this->service->config->entity_string == 'credit') {
$this->mergeSections([
'entity-details' => [
'id' => 'entity-details',
'elements' => $this->creditDetails(),
],
]);
}
return $this;
}
/**
@ -857,14 +817,12 @@ class PdfBuilder
*/
private function buildSections() :self
{
return match ($this->service->document_type) {
PdfService::PRODUCT => $this->getProductSections(),
PdfService::DELIVERY_NOTE => $this->getDeliveryNoteSections(),
PdfService::STATEMENT => $this->getStatementSections(),
PdfService::PURCHASE_ORDER => $this->getPurchaseOrderSections(),
};
}
/**
@ -875,7 +833,6 @@ class PdfBuilder
*/
private function statementTableTotals(): array
{
return [
['element' => 'div', 'properties' => ['style' => 'display: flex; flex-direction: column;'], 'elements' => [
['element' => 'div', 'properties' => ['style' => 'margin-top: 1.5rem; display: block; align-items: flex-start; page-break-inside: avoid; visible !important;'], 'elements' => [
@ -883,7 +840,6 @@ class PdfBuilder
]],
]],
];
}
/**
@ -925,7 +881,6 @@ class PdfBuilder
}
return false;
}
//First pass done, need a second pass to abstract this content completely.
@ -980,7 +935,6 @@ class PdfBuilder
if (in_array('$paid_to_date', $variables)) {
$variables = \array_diff($variables, ['$paid_to_date']);
}
}
foreach (['discount'] as $property) {
@ -1057,7 +1011,6 @@ class PdfBuilder
]];
return $elements;
}
/**
@ -1068,8 +1021,7 @@ class PdfBuilder
*/
public function getProductAndTaskTables(): self
{
$this->mergeSections( [
$this->mergeSections([
'product-table' => [
'id' => 'product-table',
'elements' => $this->productTable(),
@ -1081,7 +1033,6 @@ class PdfBuilder
]);
return $this;
}
/**
@ -1092,8 +1043,7 @@ class PdfBuilder
*/
public function getClientDetails(): self
{
$this->mergeSections( [
$this->mergeSections([
'client-details' => [
'id' => 'client-details',
'elements' => $this->clientDetails(),
@ -1101,7 +1051,6 @@ class PdfBuilder
]);
return $this;
}
/**
@ -1111,7 +1060,6 @@ class PdfBuilder
*/
public function productTable(): array
{
$product_items = collect($this->service->config->entity->line_items)->filter(function ($item) {
return $item->type_id == 1 || $item->type_id == 6 || $item->type_id == 5;
});
@ -1124,7 +1072,6 @@ class PdfBuilder
['element' => 'thead', 'elements' => $this->buildTableHeader('product')],
['element' => 'tbody', 'elements' => $this->buildTableBody('$product')],
];
}
/**
@ -1134,7 +1081,6 @@ class PdfBuilder
*/
public function taskTable(): array
{
$task_items = collect($this->service->config->entity->line_items)->filter(function ($item) {
return $item->type_id == 2;
});
@ -1147,7 +1093,6 @@ class PdfBuilder
['element' => 'thead', 'elements' => $this->buildTableHeader('task')],
['element' => 'tbody', 'elements' => $this->buildTableBody('$task')],
];
}
@ -1159,7 +1104,6 @@ class PdfBuilder
*/
public function statementDetails(): array
{
$s_date = $this->translateDate(now(), $this->service->config->client->date_format(), $this->service->config->client->locale());
return [
@ -1176,7 +1120,6 @@ class PdfBuilder
['element' => 'th', 'properties' => [], 'content' => Number::formatMoney($this->service->options['invoices']->sum('balance'), $this->service->config->client)],
]],
];
}
/**
@ -1187,11 +1130,9 @@ class PdfBuilder
*/
public function invoiceDetails(): array
{
$variables = $this->service->config->pdf_variables['invoice_details'];
return $this->genericDetailsBuilder($variables);
}
/**
@ -1202,7 +1143,6 @@ class PdfBuilder
*/
public function quoteDetails(): array
{
$variables = $this->service->config->pdf_variables['quote_details'];
if ($this->service->config->entity->partial > 0) {
@ -1210,7 +1150,6 @@ class PdfBuilder
}
return $this->genericDetailsBuilder($variables);
}
@ -1222,11 +1161,9 @@ class PdfBuilder
*/
public function creditDetails(): array
{
$variables = $this->service->config->pdf_variables['credit_details'];
return $this->genericDetailsBuilder($variables);
}
/**
@ -1236,11 +1173,9 @@ class PdfBuilder
*/
public function purchaseOrderDetails(): array
{
$variables = $this->service->config->pdf_variables['purchase_order_details'];
return $this->genericDetailsBuilder($variables);
}
/**
@ -1251,7 +1186,6 @@ class PdfBuilder
*/
public function deliveryNoteDetails(): array
{
$variables = $this->service->config->pdf_variables['invoice_details'];
$variables = array_filter($variables, function ($m) {
@ -1259,7 +1193,6 @@ class PdfBuilder
});
return $this->genericDetailsBuilder($variables);
}
/**
@ -1271,7 +1204,6 @@ class PdfBuilder
*/
public function genericDetailsBuilder(array $variables): array
{
$elements = [];
@ -1295,7 +1227,6 @@ class PdfBuilder
}
return $elements;
}
@ -1308,11 +1239,11 @@ class PdfBuilder
*/
public function clientDeliveryDetails(): array
{
$elements = [];
if(!$this->service->config->client)
if (!$this->service->config->client) {
return $elements;
}
$elements = [
['element' => 'p', 'content' => ctrans('texts.delivery_note'), 'properties' => ['data-ref' => 'delivery_note-label', 'style' => 'font-weight: bold; text-transform: uppercase']],
@ -1332,7 +1263,6 @@ class PdfBuilder
}
return $elements;
}
/**
@ -1342,11 +1272,11 @@ class PdfBuilder
*/
public function clientDetails(): array
{
$elements = [];
if(!$this->service->config->client)
if (!$this->service->config->client) {
return $elements;
}
$variables = $this->service->config->pdf_variables['client_details'];
@ -1355,7 +1285,6 @@ class PdfBuilder
}
return $elements;
}
/**
@ -1396,7 +1325,6 @@ class PdfBuilder
['element' => 'thead', 'elements' => $thead],
['element' => 'tbody', 'elements' => $this->buildTableBody(PdfService::DELIVERY_NOTE)],
];
}
/**
@ -1409,7 +1337,6 @@ class PdfBuilder
*/
public function processNewLines(array &$items): void
{
foreach ($items as $key => $item) {
foreach ($item as $variable => $value) {
$item[$variable] = str_replace("\n", '<br>', $value);
@ -1417,7 +1344,6 @@ class PdfBuilder
$items[$key] = $item;
}
}
/**
@ -1428,7 +1354,6 @@ class PdfBuilder
*/
public function companyDetails(): array
{
$variables = $this->service->config->pdf_variables['company_details'];
$elements = [];
@ -1438,7 +1363,6 @@ class PdfBuilder
}
return $elements;
}
/**
@ -1450,7 +1374,6 @@ class PdfBuilder
*/
public function companyAddress(): array
{
$variables = $this->service->config->pdf_variables['company_address'];
$elements = [];
@ -1460,7 +1383,6 @@ class PdfBuilder
}
return $elements;
}
/**
@ -1472,7 +1394,6 @@ class PdfBuilder
*/
public function vendorDetails(): array
{
$elements = [];
$variables = $this->service->config->pdf_variables['vendor_details'];
@ -1482,7 +1403,6 @@ class PdfBuilder
}
return $elements;
}
@ -1493,14 +1413,11 @@ class PdfBuilder
public function getSectionNode(string $selector)
{
return $this->document->getElementById($selector);
}
public function updateElementProperties() :self
{
foreach ($this->sections as $element) {
if (isset($element['tag'])) {
$node = $this->document->getElementsByTagName($element['tag'])->item(0);
@ -1522,7 +1439,6 @@ class PdfBuilder
}
return $this;
}
public function updateElementProperty($element, string $attribute, ?string $value)
@ -1542,12 +1458,10 @@ class PdfBuilder
}
return $element;
}
public function createElementContent($element, $children) :self
{
foreach ($children as $child) {
$contains_html = false;
@ -1596,12 +1510,10 @@ class PdfBuilder
}
return $this;
}
public function updateVariables()
{
$html = strtr($this->getCompiledHTML(), $this->service->html_variables['labels']);
$html = strtr($html, $this->service->html_variables['values']);
@ -1611,12 +1523,10 @@ class PdfBuilder
$this->document->saveHTML();
return $this;
}
public function updateVariable(string $element, string $variable, string $value)
{
$element = $this->document->getElementById($element);
$original = $element->nodeValue;
@ -1630,12 +1540,10 @@ class PdfBuilder
);
return $element;
}
public function getEmptyElements() :self
{
foreach ($this->sections as $element) {
if (isset($element['elements'])) {
$this->getEmptyChildrens($element['elements'], $this->service->html_variables);
@ -1643,12 +1551,10 @@ class PdfBuilder
}
return $this;
}
public function getEmptyChildrens(array $children)
{
foreach ($children as $key => $child) {
if (isset($child['content']) && isset($child['show_empty']) && $child['show_empty'] === false) {
$value = strtr($child['content'], $this->service->html_variables['values']);
@ -1663,7 +1569,5 @@ class PdfBuilder
}
return $this;
}
}

View File

@ -14,21 +14,15 @@ namespace App\Services\Pdf;
use App\DataMapper\CompanySettings;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Credit;
use App\Models\CreditInvitation;
use App\Models\Currency;
use App\Models\Design;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Models\PurchaseOrder;
use App\Models\PurchaseOrderInvitation;
use App\Models\Quote;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoice;
use App\Models\RecurringInvoiceInvitation;
use App\Models\Vendor;
use App\Models\VendorContact;
use App\Services\Pdf\PdfService;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Facades\App;
@ -59,6 +53,9 @@ class PdfConfiguration
public Currency $currency;
public ?string $path;
public int $entity_design_id;
/**
* The parent object of the currency
*
@ -67,11 +64,12 @@ class PdfConfiguration
*/
public Client | Vendor $currency_entity;
public function __construct(public PdfService $service){}
public function __construct(public PdfService $service)
{
}
public function init(): self
{
$this->setEntityType()
->setPdfVariables()
->setDesign()
@ -79,12 +77,10 @@ class PdfConfiguration
->setLocale();
return $this;
}
private function setLocale(): self
{
App::forgetInstance('translator');
$t = app('translator');
@ -94,23 +90,19 @@ class PdfConfiguration
$t->replace(Ninja::transformTranslations($this->settings));
return $this;
}
private function setCurrency(): self
{
$this->currency = $this->client ? $this->client->currency() : $this->vendor->currency();
$this->currency_entity = $this->client ? $this->client : $this->vendor;
return $this;
}
private function setPdfVariables() :self
{
$default = (array) CompanySettings::getEntityVariableDefaults();
$variables = (array)$this->service->company->settings->pdf_variables;
@ -126,12 +118,10 @@ class PdfConfiguration
$this->pdf_variables = $variables;
return $this;
}
private function setEntityType()
{
$entity_design_id = '';
if ($this->service->invitation instanceof InvoiceInvitation) {
@ -188,18 +178,14 @@ class PdfConfiguration
$this->path = $this->path.$this->entity->numberFormatter().'.pdf';
return $this;
}
private function setDesign()
{
$design_id = $this->entity->design_id ? : $this->decodePrimaryKey($this->settings_object->getSetting($this->entity_design_id));
$this->design = Design::find($design_id ?: 2);
return $this;
}
}

View File

@ -13,7 +13,6 @@ namespace App\Services\Pdf;
class PdfDesigner
{
const BOLD = 'bold';
const BUSINESS = 'business';
const CLEAN = 'clean';
@ -30,28 +29,18 @@ class PdfDesigner
const STATEMENT = 'statement';
const PURCHASE_ORDER = 'purchase_order';
public PdfService $service;
public string $template;
public function __construct(PdfService $service)
public function __construct(public PdfService $service)
{
$this->service = $service;
}
public function build() :self
{
/*If the design is custom*/
if ($this->service->config->design->is_custom)
{
if ($this->service->config->design->is_custom) {
$this->template = $this->composeFromPartials(json_decode(json_encode($this->service->config->design->design), true));
}
else
{
} else {
$this->template = file_get_contents(config('ninja.designs.base_path') . strtolower($this->service->config->design->name) . '.html');
}
@ -81,5 +70,4 @@ class PdfDesigner
return $html;
}
}

View File

@ -18,8 +18,6 @@ use App\Models\InvoiceInvitation;
use App\Models\PurchaseOrderInvitation;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoiceInvitation;
use App\Services\Pdf\PdfConfiguration;
use App\Services\Pdf\PdfDesigner;
use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom;
@ -29,7 +27,6 @@ use App\Utils\VendorHtmlEngine;
class PdfService
{
use PdfMaker, PageNumbering;
public InvoiceInvitation | QuoteInvitation | CreditInvitation | RecurringInvoiceInvitation | PurchaseOrderInvitation $invitation;
@ -57,7 +54,6 @@ class PdfService
public function __construct($invitation, $document_type = 'product', $options = [])
{
$this->invitation = $invitation;
$this->company = $invitation->company;
@ -77,7 +73,6 @@ class PdfService
$this->options = $options;
$this->builder = (new PdfBuilder($this))->build();
}
/**
@ -90,48 +85,27 @@ class PdfService
*/
public function getPdf()
{
try {
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom')
{
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
$pdf = (new Phantom)->convertHtmlToPdf($this->getHtml());
}
elseif (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja')
{
} elseif (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
$pdf = (new NinjaPdf())->build($this->getHtml());
}
else
{
} else {
$pdf = $this->makePdf(null, null, $this->getHtml());
}
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
if ($numbered_pdf)
{
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
} catch (\Exception $e) {
nlog(print_r($e->getMessage(), 1));
throw new \Exception($e->getMessage(), $e->getCode());
}
return $pdf;
}
/**
@ -142,18 +116,12 @@ class PdfService
*/
public function getHtml(): string
{
$html = $this->builder->getCompiledHTML();
if (config('ninja.log_pdf_html'))
{
if (config('ninja.log_pdf_html')) {
info($html);
}
return $html;
}
}

View File

@ -17,7 +17,7 @@ use Tests\TestCase;
/**
* @test
//@covers App\DataMapper\BaseSettings
* @covers App\DataMapper\BaseSettings
*/
class PdfGenerationTest extends TestCase
{