Skip to content

Commit

Permalink
Generate a valid BE TAX number (#415)
Browse files Browse the repository at this point in the history
* Generate valid BE VAT number

* Add comments

Co-authored-by: Rob Van Keilegom <rvk@organi.be>
  • Loading branch information
robvankeilegom and robvankeilegom committed Jan 2, 2022
1 parent 1123bcd commit 9594d0a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Faker/Provider/nl_BE/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Payment extends \Faker\Provider\Payment
{
/**
* International Bank Account Number (IBAN)
* International Bank Account Number (IBAN).
*
* @see http://en.wikipedia.org/wiki/International_Bank_Account_Number
*
Expand All @@ -21,7 +21,7 @@ public static function bankAccountNumber($prefix = '', $countryCode = 'BE', $len
}

/**
* Value Added Tax (VAT)
* Value Added Tax (VAT).
*
* @example 'BE0123456789', ('spaced') 'BE 0123456789'
*
Expand All @@ -37,6 +37,13 @@ public static function vat($spacedNationalPrefix = true)
{
$prefix = $spacedNationalPrefix ? 'BE ' : 'BE';

return sprintf('%s0%d', $prefix, self::randomNumber(9, true));
// Generate 7 numbers of vat.
$firstSeven = self::randomNumber(7, true);

// Generate checksum for number
$checksum = 97 - fmod($firstSeven, 97);

// '0' + 7 numbers + checksum
return sprintf('%s0%s%s', $prefix, $firstSeven, $checksum);
}
}
19 changes: 19 additions & 0 deletions test/Faker/Provider/nl_BE/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ public function testVatIsValid()
$unspacedVat = $this->faker->vat(false);
self::assertMatchesRegularExpression('/^(BE 0\d{9})$/', $vat);
self::assertMatchesRegularExpression('/^(BE0\d{9})$/', $unspacedVat);

$this->validateChecksum($vat);
$this->validateChecksum($unspacedVat);
}

private function validateChecksum($vat)
{
// Remove the "BE " part from the beginning
$numbers = trim(substr($vat, 2));

$len = strlen($numbers);

self::assertEquals(10, $len);
self::assertStringStartsWith('0', $numbers);

// Mod97 check on first 8 digits
$checksum = 97 - fmod(substr($numbers, 0, 8), 97);

self::assertEquals((string) $checksum, substr($numbers, 8, 10));
}

protected function getProviders(): iterable
Expand Down

0 comments on commit 9594d0a

Please sign in to comment.