From 184f60785fc620692958a11a7a39f647573ca1f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 30 Jul 2020 17:47:40 +0200 Subject: [PATCH] wip with table bodies & footer --- app/Services/PdfMaker/Designs/Plain.php | 82 ++++++++++++++++--- app/Utils/Traits/MakesInvoiceValues.php | 2 +- .../PdfMaker/ExampleIntegrationTest.php | 14 +++- 3 files changed, 84 insertions(+), 14 deletions(-) diff --git a/app/Services/PdfMaker/Designs/Plain.php b/app/Services/PdfMaker/Designs/Plain.php index 54564ff25510..32b9608dddfa 100644 --- a/app/Services/PdfMaker/Designs/Plain.php +++ b/app/Services/PdfMaker/Designs/Plain.php @@ -12,10 +12,19 @@ namespace App\Services\PdfMaker\Designs; +use App\Utils\Traits\MakesInvoiceValues; + class Plain { + use MakesInvoiceValues; + public $elements; + public $client; + + public $invoice; + + public $context; public function html(): ?string { @@ -24,8 +33,20 @@ class Plain ); } - public function elements($elements): array + public function setup(): void { + $this->client = $this->context['client']; + + $this->invoice = $this->context['invoice']; + + /** @todo: Wrap the elements with correct checks & exceptions. */ + } + + public function elements(array $context): array + { + $this->context = $context; + $this->setup(); + return [ 'company-address' => [ 'id' => 'company-address', @@ -33,29 +54,68 @@ class Plain ['element' => 'p', 'content' => '$company.address1'], ], ], - 'product-table' => [ - 'id' => 'product-table', - 'elements' => [ - ['element' => 'thead', 'content' => '', 'properties' => ['class' => 'text-left bg-gray-200'], 'elements' => $this->tableHeader($elements)], - ['element' => 'tbody', 'content' => '', 'elements' => $this->tableBody()], - ], + $this->productTable(), + ]; + } + + public function productTable() + { + return [ + 'id' => 'product-table', + 'elements' => [ + ['element' => 'thead', 'content' => '', 'properties' => ['class' => 'text-left bg-gray-200'], 'elements' => $this->buildTableHeader()], + ['element' => 'tbody', 'content' => '', 'elements' => $this->buildTableBody()], + ['element' => 'tfoot', 'content' => '', 'elements' => [ + ['element' => 'tr', 'content' => '', 'elements' => [ + ['element' => 'td', 'content' => '$entity.public_notes', 'properties' => ['class' => 'border-l-4 border-white px-4 py-4', 'colspan' => '2']], + ['element' => 'td', 'content' => '$subtotal_label', 'properties' => ['class' => 'px-4 py-4 text-right', 'colspan' => '2']], + ['element' => 'td', 'content' => '$subtotal', 'properties' => ['class' => 'px-4 py-2 text-right']], + ]], + + ['element' => 'tr', 'content' => '', 'elements' => [ + ['element' => 'td', 'content' => '$discount_label', 'properties' => ['class' => 'border-l-4 border-white px-4 text-right', 'colspan' => '4']], + ['element' => 'td', 'content' => '$discount', 'properties' => ['class' => 'px-4 py-2 text-right']], + ]], + ['element' => 'tr', 'content' => '', 'properties' => ['class' => 'mt-8 px-4 py-2 bg-gray-300'], 'elements' => [ + ['element' => 'td', 'content' => '$balance_due_label', 'properties' => ['class' => 'border-l-4 border-white px-4 text-right', 'colspan' => '4']], + ['element' => 'td', 'content' => '$balance_due', 'properties' => ['class' => 'px-4 py-2 text-right']], + ]], + ]], ], ]; } - public function tableHeader($columns) + public function buildTableHeader(): array { $elements = []; - foreach ($columns as $column) { + foreach ($this->context['product-table-columns'] as $column) { $elements[] = ['element' => 'th', 'content' => $column . '_label', 'properties' => ['class' => 'px-4 py-2']]; } return $elements; } - public function tableBody() + public function buildTableBody(): array { - return []; + $elements = []; + + $items = $this->transformLineItems($this->invoice->line_items); + + if (count($items) == 0) { + return []; + } + + foreach ($items as $row) { + $element = ['element' => 'tr', 'content' => '', 'elements' => []]; + + foreach ($row as $child) { + $element['elements'][] = ['element' => 'td', 'content' => $child, 'properties' => ['class' => 'border-t-2 border-b border-gray-200 px-4 py-4']]; + } + + $elements[] = $element; + } + + return $elements; } } diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 626241f57894..7982bdf14ee0 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -569,7 +569,7 @@ trait MakesInvoiceValues * @param array $items The array of invoice items * @return array The formatted array of invoice items */ - private function transformLineItems($items, $table_type = '$product') :array + public function transformLineItems($items, $table_type = '$product') :array { $data = []; diff --git a/tests/Feature/PdfMaker/ExampleIntegrationTest.php b/tests/Feature/PdfMaker/ExampleIntegrationTest.php index 8aa201e09fcc..a79d05317106 100644 --- a/tests/Feature/PdfMaker/ExampleIntegrationTest.php +++ b/tests/Feature/PdfMaker/ExampleIntegrationTest.php @@ -7,10 +7,13 @@ use App\Models\Invoice; use App\Services\PdfMaker\Designs\Plain; use App\Services\PdfMaker\PdfMaker; use App\Utils\HtmlEngine; +use App\Utils\Traits\MakesInvoiceValues; use Tests\TestCase; class ExampleIntegrationTest extends TestCase { + use MakesInvoiceValues; + public function testExample() { $invoice = Invoice::first(); @@ -18,10 +21,17 @@ class ExampleIntegrationTest extends TestCase $engine = new HtmlEngine($invitation, 'invoice'); $design = new Plain(); - + + $product_table_columns = json_decode( + json_encode($invoice->company->settings->pdf_variables), 1 + )['product_columns']; $state = [ - 'template' => $design->elements(json_decode(json_encode($invoice->company->settings->pdf_variables), 1)['product_columns']), + 'template' => $design->elements([ + 'client' => $invoice->client, + 'invoice' => $invoice, + 'product-table-columns' => $product_table_columns, + ]), 'variables' => $engine->generateLabelsAndValues(), ];