From 9594d0aad844f6fab3e5e0e53a1ebd5d28605c7f Mon Sep 17 00:00:00 2001 From: Rob Date: Sun, 2 Jan 2022 11:07:40 +0100 Subject: [PATCH] Generate a valid BE TAX number (#415) * Generate valid BE VAT number * Add comments Co-authored-by: Rob Van Keilegom --- src/Faker/Provider/nl_BE/Payment.php | 13 ++++++++++--- test/Faker/Provider/nl_BE/PaymentTest.php | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Faker/Provider/nl_BE/Payment.php b/src/Faker/Provider/nl_BE/Payment.php index a43caf0f55..05fd70ed39 100644 --- a/src/Faker/Provider/nl_BE/Payment.php +++ b/src/Faker/Provider/nl_BE/Payment.php @@ -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 * @@ -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' * @@ -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); } } diff --git a/test/Faker/Provider/nl_BE/PaymentTest.php b/test/Faker/Provider/nl_BE/PaymentTest.php index 85144ced90..988f57d396 100644 --- a/test/Faker/Provider/nl_BE/PaymentTest.php +++ b/test/Faker/Provider/nl_BE/PaymentTest.php @@ -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