Skip to content

Commit

Permalink
Merge pull request #22 from spaze/spaze/all-known-rates
Browse files Browse the repository at this point in the history
Get all known rates for a country
  • Loading branch information
spaze committed Oct 30, 2020
2 parents 7b47fc6 + c7f99c4 commit 6a0adbc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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)
- [Get all known rates for a country](#all-known-rates)
- [Countries](#countries)
- [License](#license)
- [Contributing](#contributing)
Expand Down Expand Up @@ -149,6 +150,18 @@ try {
}
```

<a name="all-known-rates"></a>
### Get all known rates for a country

Get all known rates (current, high & low, historical & future, exceptions) for a country by:

```php
$vatRates = new VatRates();
$vatRates->getAllKnownRates('DE');
```

The returned array of rates is unsorted. This method can be useful when you want to prefill a selectbox with known VAT rates for a country for example, or when you want to validate user-provided rates.

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

Expand Down
45 changes: 45 additions & 0 deletions src/VatRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,49 @@ private function getRules(string $countryCode, ?DateTimeInterface $date = null):
return $this->taxRules[$countryCode];
}


/**
* Get an array of all known VAT rates for given country.
*
* Returns current rate, high & low rates, historical & future rates, exceptions, unsorted.
*
* @param string $country
* @return array<integer, float>
*/
public function getAllKnownRates(string $country): array
{
if (!isset($this->taxRules[$country])) {
return [];
}

$rates = $this->getRates($this->taxRules[$country]);
if (isset($this->taxRules[$country]['since'])) {
foreach ($this->taxRules[$country]['since'] as $sinceRules) {
$rates = array_merge($rates, $this->getRates($sinceRules));
}
}
return array_values(array_unique($rates));
}


/**
* @param array{rate: float, rates?: array, exceptions?: array} $taxRules
* @return array<integer, float>
*/
private function getRates(array $taxRules): array
{
$rates = [$taxRules['rate']];
if (isset($taxRules['rates'])) {
foreach ($taxRules['rates'] as $rate) {
$rates[] = $rate;
}
}
if (isset($taxRules['exceptions'])) {
foreach ($taxRules['exceptions'] as $rate) {
$rates[] = $rate;
}
}
return $rates;
}

}
9 changes: 9 additions & 0 deletions tests/VatRatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,13 @@ public function testGetRatesSince(): void
$this->assertEquals(0.19, $this->vatRates->getTaxRateForLocation('DE', null, VatRates::GENERAL, new DateTimeImmutable($date)));
}


public function testGetAllKnownRates(): void
{
$this->assertEquals([0.20, 0.19], $this->vatRates->getAllKnownRates('AT'));
$this->assertEquals([0.21], $this->vatRates->getAllKnownRates('CZ'));
$this->assertEquals([0.19, 0, 0.16], $this->vatRates->getAllKnownRates('DE'));
$this->assertEquals([0.21, 0.09], $this->vatRates->getAllKnownRates('NL'));
}

}

0 comments on commit 6a0adbc

Please sign in to comment.