From ee6da44a9426d64d0e9037b6c0b82b46595062d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 4 Aug 2020 13:33:38 +0200 Subject: [PATCH] Extract helpers for tax calculations into another file --- app/Services/PdfMaker/Designs/Plain.php | 36 ++---------- .../Designs/Utilities/BuildTableHeader.php | 55 +++++++++++++++++++ 2 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 app/Services/PdfMaker/Designs/Utilities/BuildTableHeader.php diff --git a/app/Services/PdfMaker/Designs/Plain.php b/app/Services/PdfMaker/Designs/Plain.php index 3b523df8f4..de38aa1817 100644 --- a/app/Services/PdfMaker/Designs/Plain.php +++ b/app/Services/PdfMaker/Designs/Plain.php @@ -12,11 +12,12 @@ namespace App\Services\PdfMaker\Designs; +use App\Services\PdfMaker\Designs\Utilities\BuildTableHeader; use App\Utils\Traits\MakesInvoiceValues; class Plain { - use MakesInvoiceValues; + use MakesInvoiceValues, BuildTableHeader; public $elements; @@ -87,35 +88,10 @@ class Plain public function buildTableHeader(): array { + $this->processTaxColumns(); + $elements = []; - if (isset($this->context['product-table-columns']['$product.tax'])) { - $line_items = collect($this->invoice->line_items); - - $tax1 = $line_items->where('tax_name1', '<>', '')->where('type_id', 1)->count(); - $tax2 = $line_items->where('tax_name2', '<>', '')->where('type_id', 1)->count(); - $tax3 = $line_items->where('tax_name3', '<>', '')->where('type_id', 1)->count(); - $taxes = []; - - if ($tax1 > 0) { - array_push($taxes, '$product.tax_rate1'); - } - - if ($tax2 > 0) { - array_push($taxes, '$product.tax_rate2'); - } - - if ($tax3 > 0) { - array_push($taxes, '$product.tax_rate3'); - } - - $key = array_search('$product.tax', $this->context['product-table-columns'], true); - - if ($key) { - array_splice($this->context['product-table-columns'], $key, 1, $taxes); - } - } - foreach ($this->context['product-table-columns'] as $column) { $elements[] = ['element' => 'th', 'content' => $column . '_label', 'properties' => ['class' => 'px-4 py-2']]; } @@ -140,10 +116,6 @@ class Plain $element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['class' => 'border-t-2 border-b border-gray-200 px-4 py-4']]; } - // 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; } diff --git a/app/Services/PdfMaker/Designs/Utilities/BuildTableHeader.php b/app/Services/PdfMaker/Designs/Utilities/BuildTableHeader.php new file mode 100644 index 0000000000..c6e7dbf159 --- /dev/null +++ b/app/Services/PdfMaker/Designs/Utilities/BuildTableHeader.php @@ -0,0 +1,55 @@ +context['product-table-columns']['$product.tax'])) { + $line_items = collect($this->invoice->line_items); + + $tax1 = $line_items->where('tax_name1', '<>', '')->where('type_id', 1)->count(); + $tax2 = $line_items->where('tax_name2', '<>', '')->where('type_id', 1)->count(); + $tax3 = $line_items->where('tax_name3', '<>', '')->where('type_id', 1)->count(); + $taxes = []; + + if ($tax1 > 0) { + array_push($taxes, '$product.tax_rate1'); + } + + if ($tax2 > 0) { + array_push($taxes, '$product.tax_rate2'); + } + + if ($tax3 > 0) { + array_push($taxes, '$product.tax_rate3'); + } + + $key = array_search('$product.tax', $this->context['product-table-columns'], true); + + if ($key) { + array_splice($this->context['product-table-columns'], $key, 1, $taxes); + } + } + } +}