From 23577a5f0f0ae62b4dd466fbd723ea92585eb075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 21 Aug 2020 11:59:39 +0200 Subject: [PATCH] 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 --- app/Jobs/Invoice/CreateInvoicePdf.php | 4 +--- app/Jobs/Quote/CreateQuotePdf.php | 2 +- app/Services/PdfMaker/Designs/Plain.php | 12 ++++++------ app/Services/PdfMaker/Designs/Playful.php | 14 +++++++------- .../PdfMaker/Designs/Utilities/DesignHelpers.php | 14 +++++++------- tests/Feature/PdfMaker/ExampleIntegrationTest.php | 7 +------ 6 files changed, 23 insertions(+), 30 deletions(-) diff --git a/app/Jobs/Invoice/CreateInvoicePdf.php b/app/Jobs/Invoice/CreateInvoicePdf.php index 5b3ecbdfe724..8a55f9d9a426 100644 --- a/app/Jobs/Invoice/CreateInvoicePdf.php +++ b/app/Jobs/Invoice/CreateInvoicePdf.php @@ -90,13 +90,11 @@ class CreateInvoicePdf implements ShouldQueue $design_class = new $design_namespace(); - $pdf_variables = json_decode(json_encode($this->invoice->company->settings->pdf_variables), 1); - $state = [ 'template' => $design_class->elements([ 'client' => $this->invoice->client, 'entity' => $this->invoice, - 'product-table-columns' => $pdf_variables['product_columns'], + 'pdf_variables' => (array)$this->invoice->company->settings->pdf_variables, ]), 'variables' => $html->generateLabelsAndValues(), ]; diff --git a/app/Jobs/Quote/CreateQuotePdf.php b/app/Jobs/Quote/CreateQuotePdf.php index d1b759d73ef5..7bb9b2a1823e 100644 --- a/app/Jobs/Quote/CreateQuotePdf.php +++ b/app/Jobs/Quote/CreateQuotePdf.php @@ -94,7 +94,7 @@ class CreateQuotePdf implements ShouldQueue 'template' => $design_class->elements([ 'client' => $this->quote->client, 'entity' => $this->quote, - 'product-table-columns' => $pdf_variables['product_columns'], + 'pdf_variables' => (array)$this->quote->company->settings->pdf_variables, ]), 'variables' => $html->generateLabelsAndValues(), ]; diff --git a/app/Services/PdfMaker/Designs/Plain.php b/app/Services/PdfMaker/Designs/Plain.php index d08a91357ba4..09208ade0608 100644 --- a/app/Services/PdfMaker/Designs/Plain.php +++ b/app/Services/PdfMaker/Designs/Plain.php @@ -78,7 +78,7 @@ class Plain extends BaseDesign public function companyAddress(): array { - $variables = $this->entity->company->settings->pdf_variables->company_address; + $variables = $this->context['pdf_variables']['company_address']; $elements = []; @@ -91,10 +91,10 @@ class Plain extends BaseDesign 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) { - $variables = $this->entity->company->settings->pdf_variables->quote_details; + $variables = $this->context['pdf_variables']['quote_details']; } $elements = []; @@ -113,7 +113,7 @@ class Plain extends BaseDesign public function clientDetails(): array { - $variables = $this->entity->company->settings->pdf_variables->client_details; + $variables = $this->context['pdf_variables']['client_details']; $elements = []; @@ -166,7 +166,7 @@ class Plain extends BaseDesign $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']]; } @@ -186,7 +186,7 @@ class Plain extends BaseDesign foreach ($items as $row) { $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']]; } diff --git a/app/Services/PdfMaker/Designs/Playful.php b/app/Services/PdfMaker/Designs/Playful.php index bc9b9c6c1881..a39c756acb00 100644 --- a/app/Services/PdfMaker/Designs/Playful.php +++ b/app/Services/PdfMaker/Designs/Playful.php @@ -82,10 +82,10 @@ class Playful extends BaseDesign 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) { - $variables = $this->entity->company->settings->pdf_variables->quote_details; + $variables = $this->context['pdf_variables']['quote_details']; } $elements = []; @@ -102,7 +102,7 @@ class Playful extends BaseDesign public function companyDetails() { - $variables = $this->entity->company->settings->pdf_variables->company_details; + $variables = $this->context['pdf_variables']['company_details']; $elements = []; @@ -115,7 +115,7 @@ class Playful extends BaseDesign public function companyAddress(): array { - $variables = $this->entity->company->settings->pdf_variables->company_address; + $variables = $this->context['pdf_variables']['company_address']; $elements = []; @@ -128,7 +128,7 @@ class Playful extends BaseDesign public function clientDetails(): array { - $variables = $this->entity->company->settings->pdf_variables->client_details; + $variables = $this->context['pdf_variables']['client_details']; $elements = []; @@ -154,7 +154,7 @@ class Playful extends BaseDesign $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']]; } @@ -174,7 +174,7 @@ class Playful extends BaseDesign foreach ($items as $row) { $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']]; } diff --git a/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php b/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php index 03c73a2dedaa..cf5f5127086f 100644 --- a/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php +++ b/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php @@ -96,7 +96,7 @@ trait DesignHelpers */ 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); $tax1 = $line_items->where('tax_name1', '<>', '')->where('type_id', 1)->count(); @@ -116,10 +116,10 @@ trait DesignHelpers 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) { - 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 { - $total = (int) count($this->context['product-table-columns']); + $total = (int) count($this->context['pdf_variables']['product_columns']); return (int)$total - $taken; } @@ -177,16 +177,16 @@ trait DesignHelpers } if (is_null($this->entity->{$_variable})) { - // info("{$this->entity->id} $_variable is null!"); + // info("{$this->entity->id} $_variable is null!"); return true; } if (empty($this->entity->{$_variable})) { - // info("{$this->entity->id} $_variable is empty!"); + // info("{$this->entity->id} $_variable is empty!"); return true; } - // info("{$this->entity->id} $_variable ALL GOOD!!"); + // info("{$this->entity->id} $_variable ALL GOOD!!"); return false; } } diff --git a/tests/Feature/PdfMaker/ExampleIntegrationTest.php b/tests/Feature/PdfMaker/ExampleIntegrationTest.php index 1996280fdc51..40d97b1a1d8c 100644 --- a/tests/Feature/PdfMaker/ExampleIntegrationTest.php +++ b/tests/Feature/PdfMaker/ExampleIntegrationTest.php @@ -29,16 +29,11 @@ class ExampleIntegrationTest extends TestCase $engine = new HtmlEngine(null, $invitation, 'invoice'); $design = new Playful(); - $product_table_columns = json_decode( - json_encode($invoice->company->settings->pdf_variables), - 1 - )['product_columns']; - $state = [ 'template' => $design->elements([ 'client' => $invoice->client, 'entity' => $invoice, - 'product-table-columns' => $product_table_columns, + 'pdf_variables' => (array)$invoice->company->settings->pdf_variables, ]), 'variables' => $engine->generateLabelsAndValues(), ];