Skip to content
This repository has been archived by the owner on Oct 27, 2024. It is now read-only.

Commit

Permalink
Remove Norway rate but add an option to add it back again
Browse files Browse the repository at this point in the history
See these as why NO was removed:
driesvints#43
driesvints#14 (comment)
  • Loading branch information
spaze committed May 30, 2020
1 parent d61d453 commit 87d52d9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ $vatCalculator->isValidVatNumber('NL123456789B01');
- [Validate EU VAT numbers](#validate-eu-vat-numbers)
- [Get EU VAT number details](#vat-number-details)
- [Get the IP based country of your user](#get-ip-based-country)
- [Countries](#countries)
- [License](#license)

<a name="installation"></a>
Expand Down Expand Up @@ -143,6 +144,17 @@ try {
}
```

<a name="countries"></a>
## Countries

EU countries are supported as well as some non-EU countries that use VAT. Some countries are not supported even though they also have VAT. Currently, that's the case for Norway which can be added manually with `VatRates::addRateForCountry()`.

```php
$vatRates = new VatRates();
$vatRates->addRateForCountry('NO');
$vatCalculator = new VatCalculator($vatRates);
```

<a name="license"></a>
## License
This library is licensed under the MIT license. Please see [License file](LICENSE.md) for more information.
1 change: 1 addition & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Exceptions (`VatCheckUnavailableException`) are always thrown, `forwardSoapFaults` option has been removed (**BC BREAK**)
* Some countries have various VAT rates depending on location resulting in `getTaxRateForCountry()` removal, use `getTaxRateForLocation()` instead (**BC BREAK**)
* Rates have been moved to a separate class `VatRates`, you need to pass the class to `VatCalculator` constructor (**BC BREAK**)
* Norway VAT rate removed, can be manually added back with `VatRates::addRateForCountry()`
* `getIPBasedCountry()` & `getClientIP()` methods have been removed, use some other package (or `CF-IPCountry` HTTP header if you're behind Cloudflare)
* Some methods have been properly *camelCased*: methods like `getClientIP()` -> `getClientIp()` and `shouldCollectVAT` -> `shouldCollectVat` and a few more
* `VATCheckUnavailableException` has been *camelCased* to `VatCheckUnavailableException`
Expand Down
17 changes: 17 additions & 0 deletions src/VatRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ class VatRates
'TR' => [ // Turkey
'rate' => 0.18,
],
];

/**
* Optional tax rules.
*
* These are added manually by `addRateForCountry()`
*
* @var array<string, array>
*/
private $optionalTaxRules = [
'NO' => [ // Norway
'rate' => 0.25,
],
Expand Down Expand Up @@ -332,6 +342,13 @@ class VatRates
];


public function addRateForCountry(string $country): void
{
$country = strtoupper($country);
$this->taxRules[$country] = $this->optionalTaxRules[$country] ?? null;
}


public function shouldCollectVat(string $countryCode): bool
{
return isset($this->taxRules[strtoupper($countryCode)]);
Expand Down
42 changes: 42 additions & 0 deletions tests/VatRatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types = 1);

namespace Spaze\VatCalculator;

use PHPUnit_Framework_TestCase;

class VatRatesTest extends PHPUnit_Framework_TestCase
{

/** @var VatRates */
private $vatRates;


protected function setUp()
{
$this->vatRates = new VatRates();
}


public function testAddRateKnownCountry(): void
{
$country = 'nO';
$this->assertFalse($this->vatRates->shouldCollectVat($country));
$this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country));
$this->vatRates->addRateForCountry($country);
$this->assertTrue($this->vatRates->shouldCollectVat($country));
$this->assertEquals(0.25, $this->vatRates->getTaxRateForLocation($country));
}


public function testAddRateUnknownCountry(): void
{
$country = 'yEs';
$this->assertFalse($this->vatRates->shouldCollectVat($country));
$this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country));
$this->vatRates->addRateForCountry($country);
$this->assertFalse($this->vatRates->shouldCollectVat($country));
$this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country));
}

}

0 comments on commit 87d52d9

Please sign in to comment.