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

View File

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

View File

@ -13,7 +13,6 @@ namespace App\Services\Pdf;
class PdfDesigner class PdfDesigner
{ {
const BOLD = 'bold'; const BOLD = 'bold';
const BUSINESS = 'business'; const BUSINESS = 'business';
const CLEAN = 'clean'; const CLEAN = 'clean';
@ -30,28 +29,18 @@ class PdfDesigner
const STATEMENT = 'statement'; const STATEMENT = 'statement';
const PURCHASE_ORDER = 'purchase_order'; const PURCHASE_ORDER = 'purchase_order';
public PdfService $service;
public string $template; public string $template;
public function __construct(PdfService $service) public function __construct(public PdfService $service)
{ {
$this->service = $service;
} }
public function build() :self public function build() :self
{ {
/*If the design is custom*/ /*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)); $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'); $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; return $html;
} }
} }

View File

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

View File

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