Refactor context['product-table-columns'] to generic pdf_variables:

- Pass 'pdf_variables' to CreateQuotePdf & CreateInvoicPdf
- Update Playful & Plain to support new variable engine
- Update DesignHelpers trait to support new variable engine
- Make tests pass for ExampleIntegrationTest
This commit is contained in:
Benjamin Beganović 2020-08-21 11:59:39 +02:00
parent ba41e22b3b
commit 23577a5f0f
6 changed files with 23 additions and 30 deletions

View File

@ -90,13 +90,11 @@ class CreateInvoicePdf implements ShouldQueue
$design_class = new $design_namespace(); $design_class = new $design_namespace();
$pdf_variables = json_decode(json_encode($this->invoice->company->settings->pdf_variables), 1);
$state = [ $state = [
'template' => $design_class->elements([ 'template' => $design_class->elements([
'client' => $this->invoice->client, 'client' => $this->invoice->client,
'entity' => $this->invoice, 'entity' => $this->invoice,
'product-table-columns' => $pdf_variables['product_columns'], 'pdf_variables' => (array)$this->invoice->company->settings->pdf_variables,
]), ]),
'variables' => $html->generateLabelsAndValues(), 'variables' => $html->generateLabelsAndValues(),
]; ];

View File

@ -94,7 +94,7 @@ class CreateQuotePdf implements ShouldQueue
'template' => $design_class->elements([ 'template' => $design_class->elements([
'client' => $this->quote->client, 'client' => $this->quote->client,
'entity' => $this->quote, 'entity' => $this->quote,
'product-table-columns' => $pdf_variables['product_columns'], 'pdf_variables' => (array)$this->quote->company->settings->pdf_variables,
]), ]),
'variables' => $html->generateLabelsAndValues(), 'variables' => $html->generateLabelsAndValues(),
]; ];

View File

@ -78,7 +78,7 @@ class Plain extends BaseDesign
public function companyAddress(): array public function companyAddress(): array
{ {
$variables = $this->entity->company->settings->pdf_variables->company_address; $variables = $this->context['pdf_variables']['company_address'];
$elements = []; $elements = [];
@ -91,10 +91,10 @@ class Plain extends BaseDesign
public function entityDetails(): array public function entityDetails(): array
{ {
$variables = $this->entity->company->settings->pdf_variables->invoice_details; $variables = $this->context['pdf_variables']['invoice_details'];
if ($this->entity instanceof \App\Models\Quote) { if ($this->entity instanceof \App\Models\Quote) {
$variables = $this->entity->company->settings->pdf_variables->quote_details; $variables = $this->context['pdf_variables']['quote_details'];
} }
$elements = []; $elements = [];
@ -113,7 +113,7 @@ class Plain extends BaseDesign
public function clientDetails(): array public function clientDetails(): array
{ {
$variables = $this->entity->company->settings->pdf_variables->client_details; $variables = $this->context['pdf_variables']['client_details'];
$elements = []; $elements = [];
@ -166,7 +166,7 @@ class Plain extends BaseDesign
$elements = []; $elements = [];
foreach ($this->context['product-table-columns'] as $column) { foreach ($this->context['pdf_variables']['product_columns'] as $column) {
$elements[] = ['element' => 'th', 'content' => $column . '_label', 'properties' => ['class' => 'px-4 py-2']]; $elements[] = ['element' => 'th', 'content' => $column . '_label', 'properties' => ['class' => 'px-4 py-2']];
} }
@ -186,7 +186,7 @@ class Plain extends BaseDesign
foreach ($items as $row) { foreach ($items as $row) {
$element = ['element' => 'tr', 'content' => '', 'elements' => []]; $element = ['element' => 'tr', 'content' => '', 'elements' => []];
foreach ($this->context['product-table-columns'] as $key => $cell) { foreach ($this->context['pdf_variables']['product_columns'] as $key => $cell) {
$element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['class' => 'border-t-2 border-b border-gray-200 px-4 py-4']]; $element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['class' => 'border-t-2 border-b border-gray-200 px-4 py-4']];
} }

View File

@ -82,10 +82,10 @@ class Playful extends BaseDesign
public function entityDetails(): array public function entityDetails(): array
{ {
$variables = $this->entity->company->settings->pdf_variables->invoice_details; $variables = $this->context['pdf_variables']['invoice_details'];
if ($this->entity instanceof \App\Models\Quote) { if ($this->entity instanceof \App\Models\Quote) {
$variables = $this->entity->company->settings->pdf_variables->quote_details; $variables = $this->context['pdf_variables']['quote_details'];
} }
$elements = []; $elements = [];
@ -102,7 +102,7 @@ class Playful extends BaseDesign
public function companyDetails() public function companyDetails()
{ {
$variables = $this->entity->company->settings->pdf_variables->company_details; $variables = $this->context['pdf_variables']['company_details'];
$elements = []; $elements = [];
@ -115,7 +115,7 @@ class Playful extends BaseDesign
public function companyAddress(): array public function companyAddress(): array
{ {
$variables = $this->entity->company->settings->pdf_variables->company_address; $variables = $this->context['pdf_variables']['company_address'];
$elements = []; $elements = [];
@ -128,7 +128,7 @@ class Playful extends BaseDesign
public function clientDetails(): array public function clientDetails(): array
{ {
$variables = $this->entity->company->settings->pdf_variables->client_details; $variables = $this->context['pdf_variables']['client_details'];
$elements = []; $elements = [];
@ -154,7 +154,7 @@ class Playful extends BaseDesign
$elements = []; $elements = [];
foreach ($this->context['product-table-columns'] as $column) { foreach ($this->context['pdf_variables']['product_columns'] as $column) {
$elements[] = ['element' => 'th', 'content' => $column . '_label', 'properties' => ['class' => 'font-semibold text-white px-4 py-3']]; $elements[] = ['element' => 'th', 'content' => $column . '_label', 'properties' => ['class' => 'font-semibold text-white px-4 py-3']];
} }
@ -174,7 +174,7 @@ class Playful extends BaseDesign
foreach ($items as $row) { foreach ($items as $row) {
$element = ['element' => 'tr', 'properties' => ['class' => 'border-b-2 border-teal-600'], 'content' => '', 'elements' => []]; $element = ['element' => 'tr', 'properties' => ['class' => 'border-b-2 border-teal-600'], 'content' => '', 'elements' => []];
foreach ($this->context['product-table-columns'] as $key => $cell) { foreach ($this->context['pdf_variables']['product_columns'] as $key => $cell) {
$element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['class' => 'px-4 py-4']]; $element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['class' => 'px-4 py-4']];
} }

View File

@ -96,7 +96,7 @@ trait DesignHelpers
*/ */
public function processTaxColumns(): void public function processTaxColumns(): void
{ {
if (in_array('$product.tax', $this->context['product-table-columns'])) { if (in_array('$product.tax', (array)$this->context['pdf_variables']['product_columns'])) {
$line_items = collect($this->entity->line_items); $line_items = collect($this->entity->line_items);
$tax1 = $line_items->where('tax_name1', '<>', '')->where('type_id', 1)->count(); $tax1 = $line_items->where('tax_name1', '<>', '')->where('type_id', 1)->count();
@ -116,10 +116,10 @@ trait DesignHelpers
array_push($taxes, '$product.tax_rate3'); array_push($taxes, '$product.tax_rate3');
} }
$key = array_search('$product.tax', $this->context['product-table-columns'], true); $key = array_search('$product.tax', $this->context['pdf_variables']['product_columns'], true);
if ($key) { if ($key) {
array_splice($this->context['product-table-columns'], $key, 1, $taxes); array_splice($this->context['pdf_variables']['product_columns'], $key, 1, $taxes);
} }
} }
} }
@ -132,7 +132,7 @@ trait DesignHelpers
*/ */
public function calculateColspan(int $taken): int public function calculateColspan(int $taken): int
{ {
$total = (int) count($this->context['product-table-columns']); $total = (int) count($this->context['pdf_variables']['product_columns']);
return (int)$total - $taken; return (int)$total - $taken;
} }
@ -177,16 +177,16 @@ trait DesignHelpers
} }
if (is_null($this->entity->{$_variable})) { if (is_null($this->entity->{$_variable})) {
// info("{$this->entity->id} $_variable is null!"); // info("{$this->entity->id} $_variable is null!");
return true; return true;
} }
if (empty($this->entity->{$_variable})) { if (empty($this->entity->{$_variable})) {
// info("{$this->entity->id} $_variable is empty!"); // info("{$this->entity->id} $_variable is empty!");
return true; return true;
} }
// info("{$this->entity->id} $_variable ALL GOOD!!"); // info("{$this->entity->id} $_variable ALL GOOD!!");
return false; return false;
} }
} }

View File

@ -29,16 +29,11 @@ class ExampleIntegrationTest extends TestCase
$engine = new HtmlEngine(null, $invitation, 'invoice'); $engine = new HtmlEngine(null, $invitation, 'invoice');
$design = new Playful(); $design = new Playful();
$product_table_columns = json_decode(
json_encode($invoice->company->settings->pdf_variables),
1
)['product_columns'];
$state = [ $state = [
'template' => $design->elements([ 'template' => $design->elements([
'client' => $invoice->client, 'client' => $invoice->client,
'entity' => $invoice, 'entity' => $invoice,
'product-table-columns' => $product_table_columns, 'pdf_variables' => (array)$invoice->company->settings->pdf_variables,
]), ]),
'variables' => $engine->generateLabelsAndValues(), 'variables' => $engine->generateLabelsAndValues(),
]; ];