From e88bf18fe75b8de9e38c5292df548175b8706cac Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 10 Apr 2023 17:52:40 +1000 Subject: [PATCH] Centralize resolution of subregion --- app/DataMapper/Tax/BaseRule.php | 16 +++++++++++---- app/DataMapper/Tax/DE/Rule.php | 2 +- app/DataMapper/Tax/US/Rule.php | 36 +++++++++++++++++++-------------- tests/Unit/Tax/EuTaxTest.php | 1 + 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/app/DataMapper/Tax/BaseRule.php b/app/DataMapper/Tax/BaseRule.php index db501c6aeb68..a4d27f91eef9 100644 --- a/app/DataMapper/Tax/BaseRule.php +++ b/app/DataMapper/Tax/BaseRule.php @@ -134,6 +134,7 @@ class BaseRule implements RuleInterface return $this; } + // Refactor to support switching between shipping / billing country / region / subregion private function resolveRegions(): self { @@ -142,13 +143,20 @@ class BaseRule implements RuleInterface $this->client_region = $this->region_codes[$this->client->country->iso_3166_2] ?? ''; - if($this->client_region == 'US'){ - $this->client_subregion = $this->tax_data->geoState; - } - + match($this->client_region){ + 'US' => $this->client_subregion = $this->tax_data->geoState, + 'EU' => $this->client->country->iso_3166_2, + default => '', + }; + return $this; } + private function isTaxableRegion(): bool + { + return $this->client->company->tax_data->regions->{$this->client_region}->tax_all_subregions || $this->client->company->tax_data->regions->{$this->client_region}->subregions->{$this->client_subregion}->apply_tax; + } + public function setTaxData(Response $tax_data): self { $this->tax_data = $tax_data; diff --git a/app/DataMapper/Tax/DE/Rule.php b/app/DataMapper/Tax/DE/Rule.php index f1787b202308..8473a1af700b 100644 --- a/app/DataMapper/Tax/DE/Rule.php +++ b/app/DataMapper/Tax/DE/Rule.php @@ -50,7 +50,7 @@ class Rule extends BaseRule implements RuleInterface return $this->taxExempt(); - } elseif ($this->client->company->tax_data->regions->EU->tax_all_subregions || $this->client->company->tax_data->regions->EU->subregions->{$this->client_iso_3166_2}->apply_tax) { + } elseif ($this->client->company->tax_data->regions->EU->tax_all_subregions || $this->client->company->tax_data->regions->EU->subregions->{$this->client_subregion}->apply_tax) { $this->taxByType($item->tax_id); diff --git a/app/DataMapper/Tax/US/Rule.php b/app/DataMapper/Tax/US/Rule.php index 0a34474a2de4..c393bc6fb10a 100644 --- a/app/DataMapper/Tax/US/Rule.php +++ b/app/DataMapper/Tax/US/Rule.php @@ -11,15 +11,22 @@ namespace App\DataMapper\Tax\US; -use App\Models\Product; use App\DataMapper\Tax\BaseRule; use App\DataMapper\Tax\RuleInterface; +use App\Models\Product; class Rule extends BaseRule implements RuleInterface { + + public function init(): self + { + $this->calculateRates(); - public function override(): self - { + return $this; + } + + public function override(): self + { return $this; } @@ -28,14 +35,12 @@ class Rule extends BaseRule implements RuleInterface if ($this->client->is_tax_exempt) { return $this->taxExempt(); - } - else if($this->client->company->tax_data->regions->US->tax_all_subregions || $this->client->company->tax_data->regions->US->subregions->{$this->tax_data->geoState}->apply_tax){ + } elseif($this->client->company->tax_data->regions->US->tax_all_subregions || $this->client->company->tax_data->regions->US->subregions->{$this->client_subregion}->apply_tax) { $this->taxByType($item->tax_id); return $this; - } - else if($this->client->company->tax_data->regions->{$this->client_region}->tax_all_subregions || $this->client->company->tax_data->regions->{$this->client_region}->subregions->{$this->client_subregion}->apply_tax){ //other regions outside of US + } elseif($this->client->company->tax_data->regions->{$this->client_region}->tax_all_subregions || $this->client->company->tax_data->regions->{$this->client_region}->subregions->{$this->client_subregion}->apply_tax) { //other regions outside of US } return $this; @@ -45,7 +50,7 @@ class Rule extends BaseRule implements RuleInterface public function taxByType($product_tax_type): self { - match($product_tax_type){ + match($product_tax_type) { Product::PRODUCT_TYPE_EXEMPT => $this->taxExempt(), Product::PRODUCT_TYPE_DIGITAL => $this->taxDigital(), Product::PRODUCT_TYPE_SERVICE => $this->taxService(), @@ -76,16 +81,18 @@ class Rule extends BaseRule implements RuleInterface public function taxService(): self { - if($this->tax_data->txbService == 'Y') + if($this->tax_data->txbService == 'Y') { $this->default(); + } return $this; } public function taxShipping(): self { - if($this->tax_data->txbFreight == 'Y') + if($this->tax_data->txbFreight == 'Y') { $this->default(); + } return $this; } @@ -113,13 +120,12 @@ class Rule extends BaseRule implements RuleInterface return $this; } - public function init(): self - { - return $this; - } - public function calculateRates(): self { + if($this->client_region != 'US' && $this->isTaxableRegion()) { + + } + return $this; } } diff --git a/tests/Unit/Tax/EuTaxTest.php b/tests/Unit/Tax/EuTaxTest.php index 711f63cef22c..9b5fe417b0b8 100644 --- a/tests/Unit/Tax/EuTaxTest.php +++ b/tests/Unit/Tax/EuTaxTest.php @@ -604,6 +604,7 @@ class EuTaxTest extends TestCase ]); $process = new Rule(); + $process->setTaxData(new Response([])); $process->setClient($client); $process->init();