From aa91604814deb4f69d4ba3a04e16711a4970ca05 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 29 Mar 2023 14:42:08 +1100 Subject: [PATCH] Fixes for eu taxes --- app/DataMapper/Tax/DE/Rule.php | 12 +- tests/Unit/Tax/EuTaxTest.php | 204 ++++++++++++++++++++++++++++++++- 2 files changed, 210 insertions(+), 6 deletions(-) diff --git a/app/DataMapper/Tax/DE/Rule.php b/app/DataMapper/Tax/DE/Rule.php index cd1b032aae4b..56a388dfea6d 100644 --- a/app/DataMapper/Tax/DE/Rule.php +++ b/app/DataMapper/Tax/DE/Rule.php @@ -83,7 +83,7 @@ class Rule extends BaseRule implements RuleInterface //need to add logic here to capture if public function tax(): self { - if($this->client->is_tax_exempt || $this->client->has_valid_vat_number) + if($this->client->is_tax_exempt) return $this->taxExempt(); $this->tax_name1 = $this->vat_rate; @@ -178,10 +178,12 @@ class Rule extends BaseRule implements RuleInterface public function calculateRates(): self { - if( - (($this->vendor_country_code == $this->client_country_code) && $this->client->has_valid_vat_number && $this->business_tax_exempt) || //same country / exempt for tax / valid vat number - (in_array($this->client_country_code, $this->eu_country_codes) && $this->client->has_valid_vat_number && $this->eu_business_tax_exempt) //eu country / exempt for tax / valid vat number - ) { + if ($this->client->is_tax_exempt) { + $this->vat_rate = 0; + $this->reduced_vat_rate = 0; + } + elseif($this->client_country_code != $this->vendor_country_code && in_array($this->client_country_code, $this->eu_country_codes) && $this->client->has_valid_vat_number && $this->eu_business_tax_exempt) + { $this->vat_rate = 0; $this->reduced_vat_rate = 0; nlog("euro zone and tax exempt"); diff --git a/tests/Unit/Tax/EuTaxTest.php b/tests/Unit/Tax/EuTaxTest.php index 995844ac10b1..6f8676cd14f7 100644 --- a/tests/Unit/Tax/EuTaxTest.php +++ b/tests/Unit/Tax/EuTaxTest.php @@ -157,7 +157,6 @@ class EuTaxTest extends TestCase $process->setClient($client); $process->init(); - $this->assertEquals('DE', $process->vendor_country_code); $this->assertEquals('US', $process->client_country_code); @@ -216,5 +215,208 @@ class EuTaxTest extends TestCase //tests with valid vat. + public function testDeWithValidVat() + { + $settings = CompanySettings::defaults(); + $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 = true; + + $company = Company::factory()->create([ + 'account_id' => $this->account->id, + 'settings' => $settings, + 'tax_data' => $tax_data, + ]); + + + $client = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $company->id, + 'country_id' => 276, + 'shipping_country_id' => 276, + 'has_valid_vat_number' => true, + ]); + + $process = new Rule(); + $process->setClient($client); + $process->init(); + + $this->assertInstanceOf(Rule::class, $process); + + $this->assertTrue($client->has_valid_vat_number); + + $this->assertEquals(19, $process->vat_rate); + + $this->assertEquals(7, $process->reduced_vat_rate); + + } + + //tests with valid vat. + public function testDeToEUWithValidVat() + { + $settings = CompanySettings::defaults(); + $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 = true; + + $company = Company::factory()->create([ + 'account_id' => $this->account->id, + 'settings' => $settings, + 'tax_data' => $tax_data, + ]); + + + $client = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $company->id, + 'country_id' => 56, + 'shipping_country_id' => 56, + 'has_valid_vat_number' => true, + ]); + + $process = new Rule(); + $process->setClient($client); + $process->init(); + + $this->assertInstanceOf(Rule::class, $process); + + $this->assertTrue($client->has_valid_vat_number); + + $this->assertEquals(0, $process->vat_rate); + + $this->assertEquals(0, $process->reduced_vat_rate); + + } + + public function testTaxExemption1() + { + $settings = CompanySettings::defaults(); + $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 = true; + + $company = Company::factory()->create([ + 'account_id' => $this->account->id, + 'settings' => $settings, + 'tax_data' => $tax_data, + ]); + + + $client = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $company->id, + 'country_id' => 56, + 'shipping_country_id' => 56, + 'has_valid_vat_number' => true, + 'is_tax_exempt' => true, + ]); + + $process = new Rule(); + $process->setClient($client); + $process->init(); + + $this->assertInstanceOf(Rule::class, $process); + + $this->assertTrue($client->is_tax_exempt); + + $this->assertEquals(0, $process->vat_rate); + + $this->assertEquals(0, $process->reduced_vat_rate); + + } + + public function testTaxExemption2() + { + $settings = CompanySettings::defaults(); + $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 = true; + + $company = Company::factory()->create([ + 'account_id' => $this->account->id, + 'settings' => $settings, + 'tax_data' => $tax_data, + ]); + + + $client = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $company->id, + 'country_id' => 276, + 'shipping_country_id' => 276, + 'has_valid_vat_number' => true, + 'is_tax_exempt' => true, + ]); + + $process = new Rule(); + $process->setClient($client); + $process->init(); + + $this->assertInstanceOf(Rule::class, $process); + + $this->assertTrue($client->is_tax_exempt); + + $this->assertEquals(0, $process->vat_rate); + + $this->assertEquals(0, $process->reduced_vat_rate); + + } + + public function testTaxExemption3() + { + $settings = CompanySettings::defaults(); + $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 = true; + + $company = Company::factory()->create([ + 'account_id' => $this->account->id, + 'settings' => $settings, + 'tax_data' => $tax_data, + ]); + + + $client = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $company->id, + 'country_id' => 840, + 'shipping_country_id' => 840, + 'has_valid_vat_number' => true, + 'is_tax_exempt' => true, + ]); + + $process = new Rule(); + $process->setClient($client); + $process->init(); + + $this->assertInstanceOf(Rule::class, $process); + + $this->assertTrue($client->is_tax_exempt); + + $this->assertEquals(0, $process->vat_rate); + + $this->assertEquals(0, $process->reduced_vat_rate); + + } }