refactor invoice to entity

This commit is contained in:
Benjamin Beganović 2020-08-05 11:52:01 +02:00
parent 17858dae45
commit 438054744e
4 changed files with 18 additions and 13 deletions

View File

@ -26,12 +26,15 @@ class Plain extends BaseDesign
/** @var App\Models\Client */ /** @var App\Models\Client */
public $client; public $client;
/** @var App\Models\Invoice */ /** @var App\Models\Invoice || @var App\Models\Quote */
public $invoice; public $entity;
/** Global state of the design, @var string */ /** Global state of the design, @var array */
public $context; public $context;
/** Type of entity => invoice||quote */
public $type;
public function html(): ?string public function html(): ?string
{ {
return file_get_contents( return file_get_contents(
@ -39,9 +42,11 @@ class Plain extends BaseDesign
); );
} }
public function elements(array $context): array public function elements(array $context, string $type = 'invoice'): array
{ {
$this->context = $context; $this->context = $context;
$this->type = $type;
$this->setup(); $this->setup();
return [ return [
@ -66,7 +71,7 @@ class Plain extends BaseDesign
public function companyAddress(): array public function companyAddress(): array
{ {
$variables = $this->invoice->company->settings->pdf_variables->company_address; $variables = $this->entity->company->settings->pdf_variables->company_address;
$elements = []; $elements = [];
@ -79,7 +84,7 @@ class Plain extends BaseDesign
public function entityDetails(): array public function entityDetails(): array
{ {
$variables = $this->invoice->company->settings->pdf_variables->invoice_details; $variables = $this->entity->company->settings->pdf_variables->invoice_details;
$elements = []; $elements = [];
@ -95,7 +100,7 @@ class Plain extends BaseDesign
public function clientDetails(): array public function clientDetails(): array
{ {
$variables = $this->invoice->company->settings->pdf_variables->client_details; $variables = $this->entity->company->settings->pdf_variables->client_details;
$elements = []; $elements = [];
@ -146,7 +151,7 @@ class Plain extends BaseDesign
{ {
$elements = []; $elements = [];
$items = $this->transformLineItems($this->invoice->line_items); $items = $this->transformLineItems($this->entity->line_items);
if (count($items) == 0) { if (count($items) == 0) {
return []; return [];

View File

@ -20,8 +20,8 @@ class BaseDesign
$this->client = $this->context['client']; $this->client = $this->context['client'];
} }
if (isset($this->context['invoice'])) { if (isset($this->context['entity'])) {
$this->invoice = $this->context['invoice']; $this->entity = $this->context['entity'];
} }
} }
} }

View File

@ -26,7 +26,7 @@ trait BuildTableHeader
public function processTaxColumns(): void public function processTaxColumns(): void
{ {
if (in_array('$product.tax', $this->context['product-table-columns'])) { if (in_array('$product.tax', $this->context['product-table-columns'])) {
$line_items = collect($this->invoice->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();
$tax2 = $line_items->where('tax_name2', '<>', '')->where('type_id', 1)->count(); $tax2 = $line_items->where('tax_name2', '<>', '')->where('type_id', 1)->count();

View File

@ -30,13 +30,13 @@ class ExampleIntegrationTest extends TestCase
$state = [ $state = [
'template' => $design->elements([ 'template' => $design->elements([
'client' => $invoice->client, 'client' => $invoice->client,
'invoice' => $invoice, 'entity' => $invoice,
'product-table-columns' => $product_table_columns, 'product-table-columns' => $product_table_columns,
]), ]),
'variables' => $engine->generateLabelsAndValues(), 'variables' => $engine->generateLabelsAndValues(),
]; ];
$maker = new PdfMaker($state); $maker = new PdfMaker($state, 'invoice');
$maker $maker
->design(Plain::class) ->design(Plain::class)