Skip to content

Commit

Permalink
Merge pull request #67 from koblas/fr_tva
Browse files Browse the repository at this point in the history
fix: spent too much time researching TVA small fixes
  • Loading branch information
koblas authored Jul 4, 2023
2 parents 0123b1a + 95f8f4d commit 759ec96
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
26 changes: 10 additions & 16 deletions src/fr/tva.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@ describe('fr/tva', () => {
expect(result.isValid && result.compact).toEqual('40303265045');
});

it('validate:23334175221', () => {
const result = validate('23334175221');

expect(result.isValid && result.compact).toEqual('23334175221');
});

it('validate:K7399859412', () => {
const result = validate('K7399859412');

expect(result.isValid && result.compact).toEqual('K7399859412');
});

it('validate:4Z123456782', () => {
const result = validate('4Z123456782');

expect(result.isValid && result.compact).toEqual('4Z123456782');
test.each([
'40303265045',
'23334175221',
'K7399859412',
'4Z123456782',
'FR84323140392',
])('validate:%s', value => {
const result = validate(value);

expect(result.isValid).toEqual(true);
});

it('validate:12345678', () => {
Expand Down
41 changes: 28 additions & 13 deletions src/fr/tva.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* two digits. In old style numbers the two digits are numeric, with new
* style numbers at least one is a alphabetic.
*
* Source:
* https://wiki.scn.sap.com/wiki/display/CRM/France
*
* ENTITY
*/

Expand All @@ -27,6 +30,7 @@ function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
return [value, null];
}

// the valid characters for the first two digits (O and I are missing)
const alphabet = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ';

const impl: Validator = {
Expand Down Expand Up @@ -58,36 +62,47 @@ const impl: Validator = {
if (value.length !== 11) {
return { isValid: false, error: new exceptions.InvalidLength() };
}
if (!alphabet.includes(value[0]) || !alphabet.includes(value[1])) {
const [check, back] = strings.splitAt(value, 2);
if (!alphabet.includes(check[0]) || !alphabet.includes(check[1])) {
return { isValid: false, error: new exceptions.InvalidFormat() };
}
if (!strings.isdigits(value.substr(2))) {
if (!strings.isdigits(back)) {
return { isValid: false, error: new exceptions.InvalidFormat() };
}
// numbers from Monaco are valid TVA but not SIREN
if (value.substr(2, 3) !== '000') {
if (value.substring(2, 5) !== '000') {
const r = siren.validate(value.substr(2));
if (!r.isValid) {
return r;
}
}
if (strings.isdigits(value)) {
const [check, back] = strings.splitAt(value, 2);

const sum = parseInt(`${back}12`, 10) % 97;
if (parseInt(check, 10) !== sum) {
if (strings.isdigits(check)) {
const sum = (12 + 3 * (parseInt(back, 10) % 97)) % 97;

if (String(sum) !== check) {
return { isValid: false, error: new exceptions.InvalidChecksum() };
}
} else {
const [check, back] = strings.splitAt(value, 2);
// const FACTORS = {
// 1: [24, 10],
// 0: [34, 100],
// };
// const factors = FACTORS[strings.isdigits(check[0]) ? 1 : 0];

// const cvalue =
// alphabet.indexOf(check[0]) * factors[0] +
// alphabet.indexOf(check[1]) -
// factors[1];

const c0 = alphabet.indexOf(check[0]);
const c1 = alphabet.indexOf(check[1]);

let cvalue;
if (strings.isdigits(check[0])) {
cvalue =
alphabet.indexOf(check[0]) * 24 + alphabet.indexOf(check[1]) - 10;
if (c0 < 10) {
cvalue = c0 * 24 + c1 - 10;
} else {
cvalue =
alphabet.indexOf(check[0]) * 34 + alphabet.indexOf(check[1]) - 100;
cvalue = c0 * 34 + c1 - 100;
}

const sum = (parseInt(back, 10) + 1 + Math.floor(cvalue / 11)) % 11;
Expand Down

0 comments on commit 759ec96

Please sign in to comment.