diff --git a/src/VatRates.php b/src/VatRates.php index 382975d..c696409 100644 --- a/src/VatRates.php +++ b/src/VatRates.php @@ -8,7 +8,7 @@ use Spaze\VatCalculator\Exceptions\NoVatRulesForCountryException; /** - * @phpstan-type CountryTaxRules array{rate: float, rates?: array, exceptions?: array, since?: array}>} + * @phpstan-type CountryTaxRules array{rate: float, rates?: array, exceptions?: array>, since?: array}>} * @phpstan-type PostalCodeTaxExceptions array> */ class VatRates @@ -125,7 +125,11 @@ class VatRates 'IT' => [ // Italy 'rate' => 0.22, 'exceptions' => [ - 'Campione d\'Italia' => 0, + 'Campione d\'Italia' => [ + self::GENERAL => 0.077, + self::HIGH => 0.077, + self::LOW => 0.025, + ], 'Livigno' => 0, ], ], @@ -428,7 +432,8 @@ public function getTaxRateForLocation(string $countryCode, ?string $postalCode, continue; } if (isset($postalCodeException['name'], $this->taxRules[$postalCodeException['code']]['exceptions'])) { - return $this->taxRules[$postalCodeException['code']]['exceptions'][$postalCodeException['name']]; + $rules = $this->taxRules[$postalCodeException['code']]['exceptions'][$postalCodeException['name']]; + return is_array($rules) ? $rules[$type] : $rules; } return $this->getRules($postalCodeException['code'], $date)['rate']; } @@ -503,8 +508,10 @@ private function getRates(array $taxRules): array } } if (isset($taxRules['exceptions'])) { - foreach ($taxRules['exceptions'] as $rate) { - $rates[] = $rate; + foreach ($taxRules['exceptions'] as $exceptions) { + foreach ((array)$exceptions as $exception) { + $rates[] = $exception; + } } } return $rates; diff --git a/tests/VatRatesTest.php b/tests/VatRatesTest.php index 6a6bf67..19e4ff0 100644 --- a/tests/VatRatesTest.php +++ b/tests/VatRatesTest.php @@ -67,6 +67,21 @@ public function testGetRatesSince(): void } + public function testGetRatesExceptionsItLivigno(): void + { + $this->assertEquals(0, $this->vatRates->getTaxRateForLocation('IT', '23041')); + } + + + public function testGetRatesExceptionsItCampioneDItalia(): void + { + $this->assertEquals(0.077, $this->vatRates->getTaxRateForLocation('IT', '22061')); + $this->assertEquals(0.077, $this->vatRates->getTaxRateForLocation('IT', '22061', VatRates::GENERAL)); + $this->assertEquals(0.077, $this->vatRates->getTaxRateForLocation('IT', '22061', VatRates::HIGH)); + $this->assertEquals(0.025, $this->vatRates->getTaxRateForLocation('IT', '22061', VatRates::LOW)); + } + + public function testGetAllKnownRates(): void { $this->assertEquals([0.20, 0.19], $this->vatRates->getAllKnownRates('AT'));