From 55a1ddf3c64c56873ed21af2d48a7e5e3bd9241d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 10 Apr 2023 20:37:09 +1000 Subject: [PATCH] Tax rule refactor --- app/DataMapper/Tax/BaseRule.php | 8 +++++--- app/DataMapper/Tax/DE/Rule.php | 25 +++++++++++-------------- app/DataMapper/Tax/TaxModel.php | 1 - app/DataMapper/Tax/ZipTax/Response.php | 2 +- app/DataMapper/Tax/tax_model.yaml | 1 - tests/Unit/Tax/EuTaxTest.php | 22 ++++++---------------- tests/Unit/Tax/SumTaxTest.php | 2 -- tests/Unit/Tax/UsTaxTest.php | 1 - 8 files changed, 23 insertions(+), 39 deletions(-) diff --git a/app/DataMapper/Tax/BaseRule.php b/app/DataMapper/Tax/BaseRule.php index a4d27f91eef9..dcf747150830 100644 --- a/app/DataMapper/Tax/BaseRule.php +++ b/app/DataMapper/Tax/BaseRule.php @@ -29,8 +29,6 @@ class BaseRule implements RuleInterface public string $vendor_iso_3166_2 = ''; - public string $client_iso_3166_2 = ''; - public string $client_region = ''; public string $client_subregion = ''; @@ -141,11 +139,15 @@ class BaseRule implements RuleInterface if(!array_key_exists($this->client->country->iso_3166_2, $this->region_codes)) throw new \Exception('Country not supported'); + nlog($this->client->country->iso_3166_2); + $this->client_region = $this->region_codes[$this->client->country->iso_3166_2] ?? ''; + nlog($this->client_region); + match($this->client_region){ 'US' => $this->client_subregion = $this->tax_data->geoState, - 'EU' => $this->client->country->iso_3166_2, + 'EU' => $this->client_subregion = $this->client->country->iso_3166_2, default => '', }; diff --git a/app/DataMapper/Tax/DE/Rule.php b/app/DataMapper/Tax/DE/Rule.php index 8473a1af700b..6acc7135cba7 100644 --- a/app/DataMapper/Tax/DE/Rule.php +++ b/app/DataMapper/Tax/DE/Rule.php @@ -19,8 +19,6 @@ class Rule extends BaseRule implements RuleInterface { public string $vendor_iso_3166_2 = 'DE'; - public string $client_iso_3166_2 = 'DE'; - public bool $consumer_tax_exempt = false; public bool $business_tax_exempt = false; @@ -37,7 +35,6 @@ class Rule extends BaseRule implements RuleInterface public function init(): self { - $this->client_iso_3166_2 = $this->client->shipping_country ? $this->client->shipping_country->iso_3166_2 : $this->client->country->iso_3166_2; $this->calculateRates(); return $this; @@ -147,38 +144,38 @@ class Rule extends BaseRule implements RuleInterface public function calculateRates(): self { if ($this->client->is_tax_exempt) { - // nlog("tax exempt"); + nlog("tax exempt"); $this->vat_rate = 0; $this->reduced_vat_rate = 0; } - elseif($this->client_iso_3166_2 != $this->vendor_iso_3166_2 && in_array($this->client_iso_3166_2, $this->eu_country_codes) && $this->client->has_valid_vat_number && $this->eu_business_tax_exempt) + elseif($this->client_subregion != $this->vendor_iso_3166_2 && in_array($this->client_subregion, $this->eu_country_codes) && $this->client->has_valid_vat_number && $this->eu_business_tax_exempt) { - // nlog("euro zone and tax exempt"); + nlog("euro zone and tax exempt"); $this->vat_rate = 0; $this->reduced_vat_rate = 0; } - elseif(!in_array(strtoupper($this->client_iso_3166_2), $this->eu_country_codes) && ($this->foreign_consumer_tax_exempt || $this->foreign_business_tax_exempt)) //foreign + tax exempt - { - // nlog("foreign and tax exempt"); + elseif(!in_array($this->client_subregion, $this->eu_country_codes) && ($this->foreign_consumer_tax_exempt || $this->foreign_business_tax_exempt)) //foreign + tax exempt + {nlog($this->client_subregion); + nlog("foreign and tax exempt"); $this->vat_rate = 0; $this->reduced_vat_rate = 0; } - elseif(in_array(strtoupper($this->client_iso_3166_2), $this->eu_country_codes) && !$this->client->has_valid_vat_number) //eu country / no valid vat + elseif(in_array($this->client_subregion, $this->eu_country_codes) && !$this->client->has_valid_vat_number) //eu country / no valid vat { - if(($this->vendor_iso_3166_2 != $this->client_iso_3166_2) && $this->client->company->tax_data->regions->EU->has_sales_above_threshold) + if(($this->vendor_iso_3166_2 != $this->client_subregion) && $this->client->company->tax_data->regions->EU->has_sales_above_threshold) { - // nlog("eu zone with sales above threshold"); + nlog("eu zone with sales above threshold"); $this->vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->country->iso_3166_2}->vat_rate; $this->reduced_vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->country->iso_3166_2}->reduced_vat_rate; } else { - // nlog("EU with intra-community supply ie DE to DE"); + nlog("EU with intra-community supply ie DE to DE"); $this->vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->vat_rate; $this->reduced_vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_vat_rate; } } else { - // nlog("default tax"); + nlog("default tax"); $this->vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->vat_rate; $this->reduced_vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_vat_rate; } diff --git a/app/DataMapper/Tax/TaxModel.php b/app/DataMapper/Tax/TaxModel.php index 1d6be0261666..1765096e5252 100644 --- a/app/DataMapper/Tax/TaxModel.php +++ b/app/DataMapper/Tax/TaxModel.php @@ -13,7 +13,6 @@ namespace App\DataMapper\Tax; class TaxModel { - // public string $seller_region = 'US'; public string $seller_subregion = 'CA'; diff --git a/app/DataMapper/Tax/ZipTax/Response.php b/app/DataMapper/Tax/ZipTax/Response.php index 3e4893cdd28c..fbc20a5d364b 100644 --- a/app/DataMapper/Tax/ZipTax/Response.php +++ b/app/DataMapper/Tax/ZipTax/Response.php @@ -57,7 +57,7 @@ class Response * ]; * */ - public string $seller_region = ""; + public string $seller_subregion = ""; //US public string $geoPostalCode = ""; diff --git a/app/DataMapper/Tax/tax_model.yaml b/app/DataMapper/Tax/tax_model.yaml index 3a1dd31f91d0..c74c06d664bc 100644 --- a/app/DataMapper/Tax/tax_model.yaml +++ b/app/DataMapper/Tax/tax_model.yaml @@ -108,7 +108,6 @@ region: tax_all: false vat_threshold: 10000 has_sales_above_threshold: false - seller_region: DE subregions: AT: vat: 21 diff --git a/tests/Unit/Tax/EuTaxTest.php b/tests/Unit/Tax/EuTaxTest.php index 9b5fe417b0b8..ff15905d34da 100644 --- a/tests/Unit/Tax/EuTaxTest.php +++ b/tests/Unit/Tax/EuTaxTest.php @@ -50,7 +50,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = true; $tax_data->regions->EU->tax_all_subregions = true; @@ -114,7 +113,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = true; $tax_data->regions->EU->tax_all_subregions = true; @@ -179,7 +177,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = true; $tax_data->regions->EU->tax_all_subregions = true; @@ -246,7 +243,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = true; $tax_data->regions->EU->tax_all_subregions = true; @@ -271,7 +267,7 @@ class EuTaxTest extends TestCase $this->assertEquals('DE', $process->vendor_iso_3166_2); - $this->assertEquals('DE', $process->client_iso_3166_2); + $this->assertEquals('DE', $process->client_subregion); $this->assertFalse($client->has_valid_vat_number); @@ -291,7 +287,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = true; $tax_data->regions->EU->tax_all_subregions = true; @@ -318,7 +313,7 @@ class EuTaxTest extends TestCase $this->assertEquals('DE', $process->vendor_iso_3166_2); - $this->assertEquals('BE', $process->client_iso_3166_2); + $this->assertEquals('BE', $process->client_subregion); $this->assertFalse($client->has_valid_vat_number); @@ -351,13 +346,15 @@ class EuTaxTest extends TestCase ]); $process = new Rule(); - $process->setTaxData(new Response([])); + $process->setTaxData(new Response([ + 'geoState' => 'CA', + ])); $process->setClient($client); $process->init(); $this->assertEquals('DE', $process->vendor_iso_3166_2); - $this->assertEquals('US', $process->client_iso_3166_2); + $this->assertEquals('CA', $process->client_subregion); $this->assertFalse($client->has_valid_vat_number); @@ -377,7 +374,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = false; $tax_data->regions->EU->tax_all_subregions = true; @@ -419,7 +415,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = false; $tax_data->regions->EU->tax_all_subregions = true; @@ -460,7 +455,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = false; $tax_data->regions->EU->tax_all_subregions = true; @@ -500,7 +494,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = false; $tax_data->regions->EU->tax_all_subregions = true; @@ -541,7 +534,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = false; $tax_data->regions->EU->tax_all_subregions = true; @@ -582,8 +574,6 @@ class EuTaxTest extends TestCase $settings->country_id = '276'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'DE'; - $tax_data->seller_subregion = 'DE'; $tax_data->regions->EU->has_sales_above_threshold = false; $tax_data->regions->EU->tax_all_subregions = true; diff --git a/tests/Unit/Tax/SumTaxTest.php b/tests/Unit/Tax/SumTaxTest.php index 1fe7287cee96..f1a4ad5b08fe 100644 --- a/tests/Unit/Tax/SumTaxTest.php +++ b/tests/Unit/Tax/SumTaxTest.php @@ -94,7 +94,6 @@ class SumTaxTest extends TestCase $tax_data = new TaxModel(); - $tax_data->seller_region = 'US'; $tax_data->seller_subregion = 'CA'; $tax_data->regions->US->has_sales_above_threshold = true; $tax_data->regions->US->tax_all_subregions = true; @@ -145,7 +144,6 @@ class SumTaxTest extends TestCase $tax_data = new TaxModel(); - $tax_data->seller_region = 'US'; $tax_data->seller_subregion = 'CA'; $tax_data->regions->US->has_sales_above_threshold = true; $tax_data->regions->US->tax_all_subregions = true; diff --git a/tests/Unit/Tax/UsTaxTest.php b/tests/Unit/Tax/UsTaxTest.php index 6fae1067d0ed..da15968b7ef4 100644 --- a/tests/Unit/Tax/UsTaxTest.php +++ b/tests/Unit/Tax/UsTaxTest.php @@ -88,7 +88,6 @@ class UsTaxTest extends TestCase $settings->country_id = '840'; // germany $tax_data = new TaxModel(); - $tax_data->seller_region = 'US'; $tax_data->seller_subregion = 'CA'; $tax_data->regions->US->has_sales_above_threshold = true; $tax_data->regions->US->tax_all_subregions = true;