-
-
Notifications
You must be signed in to change notification settings - Fork 247
/
tools.py
82 lines (73 loc) · 2.93 KB
/
tools.py
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
# Copyright (C) 2009-Today - Akretion (<http://www.akretion.com>).
# @author Gabriel C. Stabel - Akretion
# @author Renato Lima <[email protected]>
# @author Raphael Valyi <[email protected]>
# @author Magno Costa <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from erpbrasil.base.fiscal import cnpj_cpf, ie
from odoo import _
from odoo.exceptions import ValidationError
def check_ie(env, inscr_est, state, country):
"""
Checks if 'Inscrição Estadual' field is valid
using erpbrasil library
:param env:
:param inscr_est:
:param state:
:param country:
:return:
"""
if env and inscr_est and state and country:
if not country == env.ref("base.br"):
return # skip
disable_ie_validation = env["ir.config_parameter"].sudo().get_param(
"l10n_br_base.disable_ie_validation", default=False
) or env.context.get("disable_ie_validation")
if disable_ie_validation:
return # skip
# TODO: em aberto debate sobre:
# Se no caso da empresa ser 'isenta' do IE o campo
# deve estar vazio ou pode ter algum valor como abaixo
if inscr_est in ("isento", "isenta", "ISENTO", "ISENTA"):
return # skip
if not ie.validar(state.code.lower(), inscr_est):
raise ValidationError(
_(
"Estadual Inscription %(inscr)s Invalid for State %(state)s!",
inscr=inscr_est,
state=state.name,
)
)
def check_cnpj_cpf(env, cnpj_cpf_value, country):
"""
Check CNPJ or CPF is valid using erpbrasil library
:param env:
:param cnpj_cpf_value:
:param country:
:return:
"""
if env and cnpj_cpf_value and country:
if country == env.ref("base.br"):
disable_cpf_cnpj_validation = env["ir.config_parameter"].sudo().get_param(
"l10n_br_base.disable_cpf_cnpj_validation", default=False
) or env.context.get("disable_cpf_cnpj_validation")
if not disable_cpf_cnpj_validation:
if not cnpj_cpf.validar(cnpj_cpf_value):
# Removendo . / - para diferenciar o CNPJ do CPF
# 62.228.384/0001-51 -CNPJ
# 62228384000151 - CNPJ
# 765.865.078-12 - CPF
# 76586507812 - CPF
document = "CPF"
if (
len("".join(char for char in cnpj_cpf_value if char.isdigit()))
== 14
):
document = "CNPJ"
raise ValidationError(
_(
"%(d_type)s %(d_id)s is invalid!",
d_type=document,
d_id=cnpj_cpf_value,
)
)