From 2021f59387b0d0eb5595c0b398c68d19c6ef6cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Tue, 11 Jan 2022 03:12:21 +0100 Subject: [PATCH] VAT now applies in Campione d'Italia too Although the rate is the same as in CH, not as in the rest of IT. Fix #31 --- src/VatRates.php | 17 ++++++++++++----- tests/VatRatesTest.php | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) 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'));