Tax rule refactor

This commit is contained in:
David Bomba 2023-04-10 20:37:09 +10:00
parent e88bf18fe7
commit 55a1ddf3c6
8 changed files with 23 additions and 39 deletions

View File

@ -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 => '',
};

View File

@ -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;
}

View File

@ -13,7 +13,6 @@ namespace App\DataMapper\Tax;
class TaxModel
{
// public string $seller_region = 'US';
public string $seller_subregion = 'CA';

View File

@ -57,7 +57,7 @@ class Response
* ];
*
*/
public string $seller_region = "";
public string $seller_subregion = "";
//US
public string $geoPostalCode = "";

View File

@ -108,7 +108,6 @@ region:
tax_all: false
vat_threshold: 10000
has_sales_above_threshold: false
seller_region: DE
subregions:
AT:
vat: 21

View File

@ -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;

View File

@ -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;

View File

@ -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;