forked from EdgarOSR/receitaws-viacep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapireceitaws.js
More file actions
116 lines (109 loc) · 3.6 KB
/
apireceitaws.js
File metadata and controls
116 lines (109 loc) · 3.6 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function limparDadosCnpj()
{
document.getElementById('cnpj').value = "";
document.getElementById('tipo').value = "";
document.getElementById('porte').value = "";
document.getElementById('nome').value = "";
document.getElementById('fantasia').value = "";
document.getElementById('abertura').value = "";
document.getElementById('cep').value = "";
document.getElementById('logradouro').value = "";
document.getElementById('numero').value = "";
document.getElementById('complemento').value = "";
document.getElementById('bairro').value = "";
document.getElementById('localidade').value = "";
document.getElementById('uf').value = "";
document.getElementById('ibge').value = "";
document.getElementById('ddd').value = "";
document.getElementById('telefone').value = "";
document.getElementById('celular').value = "";
document.getElementById('email').value = "";
document.getElementById('contato').value = "";
}
function retornoReceitaWS(conteudo)
{
// verifica se o callback apresentou algum erro
if (!('message' in conteudo))
{
if (conteudo['situacao'] == 'ATIVA')
{
for (const campo in conteudo)
{
let campoAux = document.querySelector(`#${campo}`)
if (campoAux && campoAux != "telefone")
{
campoAux.value = conteudo[campo].toUpperCase();
}
}
// ajustar telefones
separarTelefones(conteudo['telefone']);
// campo com nome diferente devido a api do cep
document.querySelector(`#localidade`).value = conteudo['municipio'];
// obtem o socio principal do array dos sócios
document.querySelector(`#contato`).value = obterSocio(conteudo['qsa']);
// email em minúsculas
document.querySelector(`#email`).value = conteudo['email'].toLowerCase();
validarCep(conteudo['cep']);
} else
{
alert('CNPJ inativo, não pode ser cadastrado!');
limparDadosCnpj();
}
} else
{
alert(conteudo['message']);
limparDadosCnpj();
}
}
function validarCnpj(valor)
{
// obtém somente os números
let cnpj = valor.replace(/\D/gm,'');
// expressão regular para validar o cnpj
let regEx = /^[0-9]{14}$/;
// verifica se o cep está no formato válido
if (regEx.test(cnpj))
{
consultarCnpj(cnpj);
} else
{
alert('Cnpj em formato inválido!');
limparDadosCnpj();
}
}
function consultarCnpj(valor)
{
let baseUrl = `https://www.receitaws.com.br/v1/cnpj/${valor}/?callback=retornoReceitaWS`;
// cria um elemento javascript
let script = document.createElement('script');
// sincroniza com o callback
script.src = baseUrl;
// insere script no documento e carrega o conteúdo
document.body.appendChild(script);
}
function validarTelefone(valor)
{
let regexTelefone = /^[0-9]{4,5}[0-9]{4}$/;
if (valor !== '')
{
if (!(regexTelefone.test(valor)))
{
alert('Formato de telefone inválido!');
}
}
}
function obterSocio(arrayQSA)
{
let contato = arrayQSA[0].nome
return contato.split(" ",1);
}
function separarTelefones(valor)
{
let arrTelefones = valor.split("/",2);
let numero = null;
for (let i = 0; i < arrTelefones.length; i++) {
numero = arrTelefones[i].replace(/\D/gm,'');
numero.length > 9 ? numero = numero.substring(2,numero.length) : numero = numero;
document.querySelector(`#telefone${i}`).value = numero;
}
}