forked from OCA/l10n-brazil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
91 lines (68 loc) · 2.43 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
83
84
85
86
87
88
89
90
91
# Copyright (C) 2020 Renato Lima - Akretion <[email protected]>
# Copyright (C) 2014 KMEE - www.kmee.com.br
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
import logging
import os
from unicodedata import normalize
from erpbrasil.base.misc import punctuation_rm
from odoo.tools import config
from .constants.fiscal import EVENT_ENV_HML, EVENT_ENV_PROD
_logger = logging.getLogger(__name__)
def domain_field_codes(
field_codes,
field_name="code_unmasked",
delimiter=",",
operator1="=",
operator2="=ilike",
code_size=8,
):
field_codes = field_codes.replace(".", "")
list_codes = field_codes.split(delimiter)
domain = []
if (
len(list_codes) > 1
and operator1 not in ("!=", "not ilike")
and operator2 not in ("!=", "not ilike")
):
domain += ["|"] * (len(list_codes) - 1)
for n in list_codes:
if len(n) == code_size:
domain.append((field_name, operator1, n))
if len(n) < code_size:
domain.append((field_name, operator2, n + "%"))
return domain
def path_edoc_company(company_id):
db_name = company_id._cr.dbname
filestore = config.filestore(db_name)
return "/".join([filestore, "edoc", punctuation_rm(company_id.cnpj_cpf)])
def build_edoc_path(
company_id, ambiente, tipo_documento, ano, mes, serie=False, numero=False
):
caminho = path_edoc_company(company_id)
if ambiente not in (EVENT_ENV_PROD, EVENT_ENV_HML):
_logger.error("Ambiente não informado, salvando na pasta de Homologação!")
if ambiente == EVENT_ENV_PROD:
caminho = os.path.join(caminho, "producao/")
else:
caminho = os.path.join(caminho, "homologacao/")
caminho = os.path.join(caminho, tipo_documento)
caminho = os.path.join(caminho, str(ano) + "-" + str(mes) + "/")
if serie and numero:
caminho = os.path.join(caminho, str(serie) + "-" + str(numero) + "/")
try:
os.makedirs(caminho, exist_ok=True)
except Exception as e:
_logger.error(f"Falha de permissão ao acessar diretorio do e-doc {e}")
return caminho
def remove_non_ascii_characters(value):
result = ""
if value and isinstance(value, str):
result = (
normalize("NFKD", value)
.encode("ASCII", "ignore")
.decode("ASCII")
.replace("\n", "")
.replace("\r", "")
.strip()
)
return result