Repository navigation
fix(bg/vat): check only the length of 10-digit numbers - #173
Open
kallehauge wants to merge 1 commit into
Open
kallehauge wants to merge 1 commit into
kallehauge wants to merge 1 commit into
Conversation
A 10-digit Bulgarian VAT number belongs to an individual, and since 1 January 2024 it is an official number issued by the National Revenue Agency, for which no check digit algorithm is published. The previous check for these numbers compared the computed digit with != instead of ==, so it accepted almost every wrong check digit and rejected the matching one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
For 10-digit Bulgarian VAT numbers that are neither a valid EGN nor a valid PNF,
bg/vatfalls back tocheckOther(), and that comparison is inverted:validate()rejects the number whencheckOther()is false, so a number is rejected exactly when its last digit matches the computed one, and accepted for almost any other last digit:Why this PR checks only the length
Simply flipping the comparison would make the result trustworthy only if the algorithm itself were, and there is no official source for it:
BG+ 9 or 10 digits (archived FAQ).BG+ EGN or personal number of a foreigner; the NRA replaced it withBG+ an official number it generates, following an amendment of the rules implementing the VAT Act (State Gazette no. 55/2023) made to protect personal data (NRA announcement). So the EGN and PNF checks no longer describe the numbers in use.calc_check_digit_other(weights 4, 3, 2, 7, 6, 5, 4, 3, 2). The module cites no source for that algorithm, and I could not find it in any Bulgarian or EU documentation. Copying it would replace one unsourced check with another.With no published algorithm, the defensible check for a 10-digit number is the published structure: 10 digits. 9-digit numbers (legal entities, the ЕИК) keep their check digit.
Fix
checkOther()and the EGN/PNF fallbacks inbg/vatare removed;bg/egnandbg/pnfthemselves are unchanged.BG1234567890BG1234567892InvalidChecksumBG12345678901InvalidLengthInvalidLengthBG123456789AInvalidFormatInvalidFormatBG175074752(9 digits)BG175074754(9 digits, wrong check digit)InvalidChecksumInvalidChecksumThis touches the same lines as #172 (the first-digit rule for 9-digit numbers); whichever merges second needs a small conflict resolution.
Tested
src/bg/vat.spec.ts: 6 new cases.BG1234567892fails before the change and passes after it; the other valid examples pass both before and after.TZ=UTC npx jest --runInBandpasses, 200 suites / 1641 tests.npm run lintandnpm run buildpass.