diff --git a/app/Services/PdfMaker/Designs/Bold.php b/app/Services/PdfMaker/Designs/Bold.php index 64b2dec6076a..e35bc8281e2d 100644 --- a/app/Services/PdfMaker/Designs/Bold.php +++ b/app/Services/PdfMaker/Designs/Bold.php @@ -12,12 +12,178 @@ namespace App\Services\PdfMaker\Designs; -class Bold +use App\Services\PdfMaker\Designs\Utilities\BaseDesign; +use App\Services\PdfMaker\Designs\Utilities\BuildTableHeader; +use App\Utils\Traits\MakesInvoiceValues; + +class Bold extends BaseDesign { + use MakesInvoiceValues, BuildTableHeader; + + /** Global list of table elements, @var array */ + public $elements; + + /** @var App\Models\Client */ + public $client; + + /** @var App\Models\Invoice || @var App\Models\Quote */ + public $entity; + + /** Global state of the design, @var array */ + public $context; + + /** Type of entity => invoice||quote */ + public $type; + public function html() { return file_get_contents( base_path('resources/views/pdf-designs//bold.html') ); } + + public function elements(array $context, string $type = 'invoice'): array + { + $this->context = $context; + $this->type = $type; + + $this->setup(); + + return [ + 'company-details' => [ + 'id' => 'company-details', + 'elements' => $this->companyDetails(), + ], + 'company-address' => [ + 'id' => 'company-address', + 'elements' => $this->companyAddress(), + ], + 'client-details' => [ + 'id' => 'client-details', + 'elements' => $this->clientDetails(), + ], + 'entity-details' => [ + 'id' => 'entity-details', + 'elements' => $this->entityDetails(), + ], + 'product-table' => [ + 'id' => 'product-table', + 'elements' => $this->productTable(), + ], + ]; + } + + public function companyDetails() + { + $variables = $this->entity->company->settings->pdf_variables->company_details; + + $elements = []; + + foreach ($variables as $variable) { + $elements[] = ['element' => 'p', 'content' => $variable]; + } + + return $elements; + } + + public function companyAddress(): array + { + $variables = $this->entity->company->settings->pdf_variables->company_address; + + $elements = []; + + foreach ($variables as $variable) { + $elements[] = ['element' => 'p', 'content' => $variable]; + } + + return $elements; + } + + public function clientDetails(): array + { + $variables = $this->entity->company->settings->pdf_variables->client_details; + + $elements = []; + + foreach ($variables as $variable) { + $elements[] = ['element' => 'p', 'content' => $variable]; + } + + return $elements; + } + + public function entityDetails(): array + { + $variables = $this->entity->company->settings->pdf_variables->invoice_details; + + $elements = []; + + foreach ($variables as $variable) { + $elements[] = ['element' => 'tr', 'content' => '', 'elements' => [ + ['element' => 'th', 'content' => $variable . '_label', 'properties' => ['class' => 'text-left pr-4 font-normal']], + ['element' => 'th', 'content' => $variable, 'properties' => ['class' => 'text-left pr-4 font-normal']], + ]]; + } + + return $elements; + } + + public function productTable(): array + { + return [ + ['element' => 'thead', 'content' => '', 'properties' => ['class' => 'text-left rounded-t-lg'], '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 text-right', 'colspan' => '4']], + ['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' => '6']], + ['element' => 'td', 'content' => '$discount', 'properties' => ['class' => 'px-4 py-2 text-right']], + ]], + ['element' => 'tr', 'content' => '', 'properties' => ['class' => 'mt-8 px-4 py-2'], 'elements' => [ + ['element' => 'td', 'content' => '$balance_due_label', 'properties' => ['class' => 'border-l-4 border-white px-4 text-right text-xl text-teal-600 font-semibold', 'colspan' => '6']], + ['element' => 'td', 'content' => '$balance_due', 'properties' => ['class' => 'px-4 py-2 text-right']], + ]], + ]], + ]; + } + + public function buildTableHeader(): array + { + $this->processTaxColumns(); + + $elements = []; + + foreach ($this->context['product-table-columns'] as $column) { + $elements[] = ['element' => 'th', 'content' => $column . '_label', 'properties' => ['class' => 'text-xl px-4 py-2']]; + } + + return $elements; + } + + public function buildTableBody(): array + { + $elements = []; + + $items = $this->transformLineItems($this->entity->line_items); + + if (count($items) == 0) { + return []; + } + + foreach ($items as $row) { + $element = ['element' => 'tr', 'content' => '', 'elements' => []]; + + foreach ($this->context['product-table-columns'] as $key => $cell) { + $element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['class' => 'px-4 py-4']]; + } + + $elements[] = $element; + } + + return $elements; + } } diff --git a/resources/views/pdf-designs/bold.html b/resources/views/pdf-designs/bold.html index 4a2e7a494981..f8ae4539a63e 100644 --- a/resources/views/pdf-designs/bold.html +++ b/resources/views/pdf-designs/bold.html @@ -23,8 +23,8 @@
$company->name logo
@@ -36,6 +36,7 @@
+
diff --git a/resources/views/pdf-designs/plain.html b/resources/views/pdf-designs/plain.html index 66753866b91e..55af58327409 100644 --- a/resources/views/pdf-designs/plain.html +++ b/resources/views/pdf-designs/plain.html @@ -27,7 +27,7 @@
-
+
diff --git a/tests/Feature/PdfMaker/ExampleIntegrationTest.php b/tests/Feature/PdfMaker/ExampleIntegrationTest.php index 92d6efdb04bc..1a35bae8bb3c 100644 --- a/tests/Feature/PdfMaker/ExampleIntegrationTest.php +++ b/tests/Feature/PdfMaker/ExampleIntegrationTest.php @@ -4,7 +4,7 @@ namespace Tests\Feature\PdfMaker; use App\Models\Design; use App\Models\Invoice; -use App\Services\PdfMaker\Designs\Plain; +use App\Services\PdfMaker\Designs\Bold; use App\Services\PdfMaker\PdfMaker; use App\Utils\HtmlEngine; use App\Utils\Traits\MakesInvoiceValues; @@ -20,7 +20,7 @@ class ExampleIntegrationTest extends TestCase $invitation = $invoice->invitations()->first(); $engine = new HtmlEngine($invitation, 'invoice'); - $design = new Plain(); + $design = new Bold(); $product_table_columns = json_decode( json_encode($invoice->company->settings->pdf_variables), @@ -39,7 +39,7 @@ class ExampleIntegrationTest extends TestCase $maker = new PdfMaker($state, 'invoice'); $maker - ->design(Plain::class) + ->design(Bold::class) ->build(); exec('echo "" > storage/logs/laravel.log');