Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate a valid BE TAX number #415

Merged
merged 3 commits into from Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Faker/Provider/nl_BE/Payment.php
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
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