-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from RedFroggy/feature/issue-69
feat: #69 - Support spanish company identifier
- Loading branch information
Showing
8 changed files
with
105 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"companyIdFullName": "Código de identificación fiscal", | ||
"companyIdName": "CIF", | ||
"trustedSourceUrl": "https://es.wikipedia.org/wiki/C%C3%B3digo_de_identificaci%C3%B3n_fiscal", | ||
"parentLevel": true, | ||
"pattern": "^([ABCDEFGHJUV][0-9]{8})|([NPQRSW][0-9]{7}[A-Z]{1})$", | ||
"companyIdDescription": "The Spanish VAT number is a 9-digit number where either the first, last\ndigits or both can be letters.\nThe number is either a DNI (Documento Nacional de Identidad, for\nSpaniards), a NIE (Número de Identificación de Extranjero, for\nforeigners) or a CIF (Código de Identificación Fiscal, for legal\nentities and others)." | ||
} | ||
] |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {COMPANY_ES_DATA} from "../../../data/es/es.model"; | ||
import {EsCompanyIdService} from "./es-company-id.service"; | ||
|
||
describe('Spanish company validation', () => { | ||
|
||
const companyInfo = COMPANY_ES_DATA[0]; | ||
const validationService = new EsCompanyIdService(); | ||
|
||
it('should not validate if identifier type is unknown', () => { | ||
expect(validationService.validate('J99216582')).toBeFalsy(); | ||
expect(validationService.validate('J99216582', {companyIdName: 'UNKNOWN'})).toBeFalsy(); | ||
}); | ||
|
||
it('should validate CIF', () => { | ||
expect(validationService.validate('J99216582', companyInfo)).toBeTruthy(); | ||
expect(validationService.validate('A08437642', companyInfo)).toBeTruthy(); | ||
expect(validationService.validate('Q0818003F', companyInfo)).toBeTruthy(); | ||
|
||
// Invalid check digit | ||
expect(validationService.validate('J99216583', companyInfo)).toBeFalsy(); | ||
|
||
// Too long format | ||
expect(validationService.validate('J992165831', companyInfo)).toBeFalsy(); | ||
|
||
// Valid NIF but not valid CIF | ||
expect(validationService.validate('M-1234567-L', companyInfo)).toBeFalsy(); | ||
|
||
// Invalid first character | ||
expect(validationService.validate('O-1234567-L', companyInfo)).toBeFalsy(); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import {COMPANY_ES_DATA} from "../../../data/es/es.model"; | ||
import {CompanyInfo} from "../../../types/company-info.model"; | ||
import {CompanyIdService} from "../../company-id.service"; | ||
import {Injectable} from "../../injectable.decorator"; | ||
|
||
@Injectable | ||
export class EsCompanyIdService extends CompanyIdService { | ||
|
||
constructor() { | ||
super('ES', COMPANY_ES_DATA); | ||
} | ||
|
||
validate(companyId: string, companyInfo?: CompanyInfo): boolean { | ||
if (companyInfo && companyInfo.companyIdName === 'CIF') { | ||
return this.validateCIF(companyId); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private validateCIF(companyId: string): boolean { | ||
const firstDigit = companyId[0]; | ||
const digits = companyId.substring(1, companyId.length -1); | ||
const checkDigit = companyId.substring(companyId.length -1); | ||
|
||
let even_sum = 0, odd_sum = 0, n; | ||
|
||
for ( let i = 0; i < digits.length; i++) { | ||
n = parseInt( digits.charAt(i), 10 ); | ||
if ( i % 2 === 0 ) { | ||
n *= 2; | ||
odd_sum += n < 10 ? n : n - 9; | ||
} else { | ||
even_sum += n; | ||
} | ||
} | ||
|
||
const last_digit = parseInt((even_sum + odd_sum).toString().slice(-1), 10); | ||
// const control_digit = last_digit != 0 ? (10 - last_digit ) : last_digit; | ||
const control_digit = (10 - last_digit ); | ||
const control_letter = 'JABCDEFGHI'.substr( control_digit, 1 ); | ||
|
||
// Control must be a digit | ||
if ( firstDigit.match( /[ABEH]/ ) ) { | ||
return checkDigit.toString() == control_digit.toString(); | ||
|
||
// Control must be a letter | ||
} else if ( firstDigit.match( /[PQSW]/ ) ) { | ||
return checkDigit == control_letter; | ||
|
||
// Can be either | ||
} else { | ||
return checkDigit.toString() == control_digit.toString() | ||
|| checkDigit == control_letter; | ||
} | ||
} | ||
} |
This file contains 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