-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkCNPJOrCPF.m
88 lines (73 loc) · 2.89 KB
/
checkCNPJOrCPF.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function varargout = checkCNPJOrCPF(entityID, searchType)
arguments
entityID (1,:) char
searchType char {mustBeMember(searchType, {'NumberValidation', 'PublicAPI'})} = 'NumberValidation'
end
entityIDNumber = regexprep(entityID, '\D', '');
switch numel(entityIDNumber)
case 11
digitType = 'CPF';
case 14
digitType = 'CNPJ';
otherwise
error('Consulta limitada ao CNPJ ou CPF')
end
[isValid, entityID] = isValidCNPJOrCPF(entityIDNumber, digitType);
if ~isValid
error('CNPJ ou CPF inválido')
end
switch searchType
case 'NumberValidation'
varargout = {entityID};
case 'PublicAPI'
if strcmp(digitType, 'CPF')
error('Consulta limitada ao CNPJ')
end
entityInfo = webread(sprintf('https://www.receitaws.com.br/v1/cnpj/%s', entityIDNumber), weboptions('ContentType', 'json'));
if strcmp(entityInfo.status, 'ERROR')
error(entityInfo.message)
end
varargout = {entityInfo};
end
end
%-------------------------------------------------------------------------%
function [isValid, entityID] = isValidCNPJOrCPF(entityIDNumber, digitType)
arrayCNPJOrCPF = arrayfun(@(x) str2double(x), entityIDNumber);
digit1Value = CheckDigit(arrayCNPJOrCPF(1:end-2), ArrayWeights(digitType, 1));
digit2Value = CheckDigit(arrayCNPJOrCPF(1:end-1), ArrayWeights(digitType, 2));
if isequal(arrayCNPJOrCPF(end-1:end), [digit1Value, digit2Value])
isValid = true;
switch digitType
case 'CNPJ'
entityID = sprintf('%s.%s.%s/%s-%s', entityIDNumber(1:2), entityIDNumber(3:5), entityIDNumber(6:8), entityIDNumber(9:12), entityIDNumber(13:14));
case 'CPF'
entityID = sprintf('%s.%s.%s-%s', entityIDNumber(1:3), entityIDNumber(4:6), entityIDNumber(7:9), entityIDNumber(10:11));
end
else
isValid = false;
entityID = '';
end
end
%-------------------------------------------------------------------------%
function arrayWeights = ArrayWeights(digitType, digitPosition)
arguments
digitType char {mustBeMember(digitType, {'CNPJ', 'CPF'})}
digitPosition double {mustBeMember(digitPosition, [1, 2])}
end
switch digitType
case 'CNPJ'
arrayWeights = [4+digitPosition:-1:2, 9:-1:2];
case 'CPF'
arrayWeights = 9+digitPosition:-1:2;
end
end
%-------------------------------------------------------------------------%
function checkDigit = CheckDigit(arrayCNPJOrCPF, arrayWeights)
sumOfElements = sum(arrayCNPJOrCPF .* arrayWeights);
remainderDiv = mod(sumOfElements, 11);
if remainderDiv < 2
checkDigit = 0;
else
checkDigit = 11 - remainderDiv;
end
end