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

Commit

Permalink
Merge pull request #35 from spaze/spaze/campione-vat-not-zero
Browse files Browse the repository at this point in the history
VAT now applies in Campione d'Italia too
  • Loading branch information
spaze authored Jan 11, 2022
2 parents e7cc47d + 2021f59 commit f35e78b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/VatRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Spaze\VatCalculator\Exceptions\NoVatRulesForCountryException;

/**
* @phpstan-type CountryTaxRules array{rate: float, rates?: array<string, float>, exceptions?: array<string, float>, since?: array<string, array{rate: float, rates?: array<string, float>}>}
* @phpstan-type CountryTaxRules array{rate: float, rates?: array<string, float>, exceptions?: array<string, float|array<string|null, float>>, since?: array<string, array{rate: float, rates?: array<string, float>}>}
* @phpstan-type PostalCodeTaxExceptions array<string, array<int, array{postalCode: string, code: string, name?: string, city?: string}>>
*/
class VatRates
Expand Down Expand Up @@ -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,
],
],
Expand Down Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions tests/VatRatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down

0 comments on commit f35e78b

Please sign in to comment.