Hipster design

This commit is contained in:
Benjamin Beganović 2020-08-05 15:02:32 +02:00
parent 66ff55cddf
commit 7a9c8ca9a0
4 changed files with 182 additions and 10 deletions

View File

@ -12,12 +12,178 @@
namespace App\Services\PdfMaker\Designs; namespace App\Services\PdfMaker\Designs;
class Hipster use App\Services\PdfMaker\Designs\Utilities\BaseDesign;
use App\Services\PdfMaker\Designs\Utilities\BuildTableHeader;
use App\Utils\Traits\MakesInvoiceValues;
class Hipster 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() public function html()
{ {
return file_get_contents( return file_get_contents(
base_path('resources/views/pdf-designs//hipster.html') base_path('resources/views/pdf-designs/hipster.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' => 'div', 'properties' => ['class' => 'space-x-4'], 'content' => '', 'elements' => [
['element' => 'span', 'content' => $variable . '_label', 'properties' => ['class' => 'font-semibold uppercase text-yellow-600']],
['element' => 'span', 'content' => $variable, 'properties' => ['class' => 'uppercase']],
]];
}
return $elements;
}
public function productTable(): array
{
return [
['element' => 'thead', 'content' => '', 'properties' => ['class' => 'text-left'], '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' => 'px-4 py-4 text-rightt', 'colspan' => '4']],
['element' => 'td', 'content' => '$subtotal_label', 'properties' => ['class' => 'px-4 py-4 text-right', 'colspan' => '2']],
['element' => 'td', 'content' => '$subtotal', 'properties' => ['class' => 'border-l-2 border-black 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' => 'border-l-2 border-black 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', 'colspan' => '6']],
['element' => 'td', 'content' => '$balance_due', 'properties' => ['class' => 'border-l-2 border-black 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' => 'border-l-2 border-black px-4 py-2 uppercase']];
}
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' => 'border-l-2 border-black px-4 py-4']];
}
$elements[] = $element;
}
return $elements;
}
} }

View File

@ -9,6 +9,10 @@
<meta http-equiv="x-ua-compatible" content="ie=edge" /> <meta http-equiv="x-ua-compatible" content="ie=edge" />
<link rel="stylesheet" href="$css" /> <link rel="stylesheet" href="$css" />
<link
href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css"
rel="stylesheet"
/>
</head> </head>
<body class="$global-margin antialiased break-words bg-white"> <body class="$global-margin antialiased break-words bg-white">
@ -30,16 +34,18 @@
</div> </div>
<div class="col-span-3"> <div class="col-span-3">
<img <img
src="$company-logo" src="$company.logo"
alt="$company-name logo" alt="$company.name logo"
class="w-24 col-span-4 sm:w-32" class="w-24 col-span-4 sm:w-32"
/> />
</div> </div>
</div> </div>
<!-- Entity details --> <!-- Entity details -->
<h1 class="mt-6 text-4xl font-semibold uppercase lg:text-6xl">$entity</h1> <h1 class="mt-6 text-4xl font-semibold uppercase lg:text-5xl">
<div id="entity-details" class="flex items-center space-x-6"></div> $entity
</h1>
<div id="entity-details" class="flex flex-wrap items-center space-x-4"></div>
<!-- Product table --> <!-- Product table -->
<table id="product-table" class="w-full mt-10 table-auto"></table> <table id="product-table" class="w-full mt-10 table-auto"></table>

View File

@ -3,7 +3,7 @@
namespace Tests\Feature\PdfMaker; namespace Tests\Feature\PdfMaker;
use App\Models\Invoice; use App\Models\Invoice;
use App\Services\PdfMaker\Designs\Elegant; use App\Services\PdfMaker\Designs\Hipster;
use App\Services\PdfMaker\PdfMaker; use App\Services\PdfMaker\PdfMaker;
use App\Utils\HtmlEngine; use App\Utils\HtmlEngine;
use App\Utils\Traits\MakesInvoiceValues; use App\Utils\Traits\MakesInvoiceValues;
@ -19,7 +19,7 @@ class ExampleIntegrationTest extends TestCase
$invitation = $invoice->invitations()->first(); $invitation = $invoice->invitations()->first();
$engine = new HtmlEngine($invitation, 'invoice'); $engine = new HtmlEngine($invitation, 'invoice');
$design = new Elegant(); $design = new Hipster();
$product_table_columns = json_decode( $product_table_columns = json_decode(
json_encode($invoice->company->settings->pdf_variables), json_encode($invoice->company->settings->pdf_variables),
@ -38,7 +38,7 @@ class ExampleIntegrationTest extends TestCase
$maker = new PdfMaker($state, 'invoice'); $maker = new PdfMaker($state, 'invoice');
$maker $maker
->design(Elegant::class) ->design(Hipster::class)
->build(); ->build();
exec('echo "" > storage/logs/laravel.log'); exec('echo "" > storage/logs/laravel.log');

View File

@ -707,7 +707,7 @@ class PdfMakerDesignsTest extends TestCase
$this->assertTrue(true); $this->assertTrue(true);
} }
public function testElegant() public function testElegant()
{ {
$state = [ $state = [
'template' => [ 'template' => [