Skip to content

Commit

Permalink
Fix postal code exceptions causing VAT to be zero-rated.
Browse files Browse the repository at this point in the history
  • Loading branch information
rossbearman committed Sep 6, 2016
1 parent e17b3fe commit 5c6d1f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Mpociot/VatCalculator/VatCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,8 @@ public function getTaxRateForLocation($countryCode, $postalCode = null, $company
if (isset($this->config) && $this->config->has($taxKey)) {
return $this->config->get($taxKey, 0);
}

if (!isset($this->postalCodeExceptions[$countryCode]) || $postalCode === null) {
return isset($this->taxRules[strtoupper($countryCode)]['rate']) ? $this->taxRules[strtoupper($countryCode)]['rate'] : 0;
} else {

if (isset($this->postalCodeExceptions[$countryCode]) && $postalCode !== null) {
foreach ($this->postalCodeExceptions[$countryCode] as $postalCodeException) {
if (!preg_match($postalCodeException['postalCode'], $postalCode)) {
continue;
Expand All @@ -557,9 +555,9 @@ public function getTaxRateForLocation($countryCode, $postalCode = null, $company

return $this->taxRules[$postalCodeException['code']]['rate'];
}

return 0;
}

return isset($this->taxRules[strtoupper($countryCode)]['rate']) ? $this->taxRules[strtoupper($countryCode)]['rate'] : 0;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/VatCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,28 @@ public function testChecksPostalCodeForVATExceptions()
$this->assertEquals(5.28, $vatCalculator->getTaxValue());
}

public function testPostalCodesWithoutExceptionsGetStandardRate()
{
$net = 24.00;
$vatCalculator = new VatCalculator();

// Invalid post code
$postalCode = 'IGHJ987ERT35';
$result = $vatCalculator->calculate($net, 'ES', $postalCode, false);
//Expect standard rate for Spain
$this->assertEquals(29.04, $result);
$this->assertEquals(0.21, $vatCalculator->getTaxRate());
$this->assertEquals(5.04, $vatCalculator->getTaxValue());

// Valid UK post code
$postalCode = 'S1A 2AA';
$result = $vatCalculator->calculate($net, 'GB', $postalCode, false);
//Expect standard rate for UK
$this->assertEquals(28.80, $result);
$this->assertEquals(0.20, $vatCalculator->getTaxRate());
$this->assertEquals(4.80, $vatCalculator->getTaxValue());
}

public function testShouldCollectVAT()
{
$vatCalculator = new VatCalculator();
Expand Down

0 comments on commit 5c6d1f0

Please sign in to comment.