194 lines
21 KiB
Python
194 lines
21 KiB
Python
"""Compose ERP_FISCAL_FIELD_MAP_V1, FISCAL_CONTEXT_REQUIREMENTS_V1, FISCAL_CONTEXT_COVERAGE_V1 and
|
|
NEXT_FISCAL_FIELD_IMPLEMENTATION_V1 from the read-only discovery outputs (docs/runs/*).
|
|
No database access."""
|
|
import json
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DOCS = ROOT / "docs"
|
|
schema = json.loads((DOCS / "runs" / "erp_schema_inventory.json").read_text(encoding="utf-8"))
|
|
hist = json.loads((DOCS / "runs" / "erp_fiscal_history_analysis.json").read_text(encoding="utf-8"))
|
|
extra = json.loads((DOCS / "runs" / "erp_fiscal_history_extra.json").read_text(encoding="utf-8"))
|
|
audit = json.loads((DOCS / "runs" / "erp_c000025_audit.json").read_text(encoding="utf-8"))
|
|
cov_v1 = json.loads((DOCS / "FISCAL_DECISION_COVERAGE_V1.json").read_text(encoding="utf-8"))
|
|
|
|
tables = schema["tables"]
|
|
fiscal_tables = {t: v for t, v in tables.items() if v["fiscal_cols"] and v["rows"]}
|
|
consistent = extra["agreement_consistent_joins_only"]
|
|
nf = hist["nfce_join"]
|
|
items_analyzed = nf["items_joined_to_C000025"] + hist["C000032"]["joined"]["JOINED"] + hist["C000062"]["joined"]["JOINED"]
|
|
|
|
VENDOR = "vendor scripts D:/PROJETOS/GTISISTEMAS/Diretorios/rel/Novo_Script_Banco.sql + atualiza/v3.5.5.NFCe/script/*"
|
|
|
|
|
|
def f(table, field, dbtype, semantic, usage, conf, evidence, sources, samples, write=False):
|
|
return {"table": table, "field": field, "db_type": dbtype, "semantic_name": semantic, "usage_class": usage, "confidence": conf,
|
|
"evidence": evidence, "source_objects": sources, "sample_values": samples, "write_candidate": write}
|
|
|
|
|
|
agree_all = nf["agreement_item_vs_product_default"]
|
|
FIELDS = [
|
|
f("C000025", "CFOP", "VARCHAR(4)", "CFOP padrão de saída do produto", "PRODUCT_DEFAULT", "HIGH",
|
|
f"NFC-e item CFOP equals product default in {agree_all['CFOP']['MATCH']}/{agree_all['CFOP']['MATCH']+agree_all['CFOP']['MISMATCH']} joined items (identity-consistent: {consistent['CFOP']['MATCH']}/{consistent['CFOP']['MATCH']+consistent['CFOP']['MISMATCH']}); values are official 5xxx CFOPs; item CFOP is the emitted value. Still operation-dependent (C000030 naturezas carry 5102/5405/6102...).",
|
|
["C000025", "NFCE_ITENS", "C000030"], ["5102", "5405", "5656"]),
|
|
f("C000025", "CFOP_ENTRADA", "CHAR(4)", "CFOP padrão de entrada (campo existe no DDL do fornecedor)", "LEGACY_UNUSED", "MEDIUM",
|
|
f"0/521 populated; DDL exists ({VENDOR}); company-level default lives in C000001.IND_CFOP_ENTRADA=1949", ["C000025", "C000001"], [None]),
|
|
f("C000025", "CST", "VARCHAR(10)", "CST ICMS de saída = origem(1)+CST(2)", "PRODUCT_DEFAULT", "HIGH",
|
|
f"vendor table C000083 (11 rows 000..090 'NACIONAL ...') defines CST as 3-digit origin+CST; NFC-e item CST matches product default in {consistent['CST']['MATCH']}/{consistent['CST']['MATCH']} identity-consistent joins",
|
|
["C000025", "C000083", "NFCE_ITENS"], ["040", "000", "060"]),
|
|
f("C000025", "CST_ENTRADA", "CHAR(4)", "CST padrão de entrada", "LEGACY_UNUSED", "MEDIUM", "0/521 populated; DDL exists in vendor scripts", ["C000025"], [None]),
|
|
f("C000025", "CSOSN", "VARCHAR(60)", "CSOSN de saída (Simples Nacional)", "PRODUCT_DEFAULT", "HIGH",
|
|
f"NFC-e item CSOSN equals product default in {consistent['CSOSN']['MATCH']}/{consistent['CSOSN']['MATCH']+consistent['CSOSN']['MISMATCH']} identity-consistent joins; company default C000004.ICMS_CSOSN=102; values 101/102/103/500 in CSOSN dataset",
|
|
["C000025", "C000004", "NFCE_ITENS"], ["102", "500", "101"]),
|
|
f("C000025", "GRUPO_ICMS", "VARCHAR(9)", "grupo ICMS do produto (espelha CSOSN/ST nas NFC-e)", "PRODUCT_DEFAULT", "MEDIUM",
|
|
"487 '102', 2 '90'; NFCE_ITENS.GRUPO_ICMS carries 102/500/blank; exact semantics not documented", ["C000025", "NFCE_ITENS"], ["102", "90"]),
|
|
f("C000025", "ORIGEM", "VARCHAR(1)", "origem (campo textual legado)", "LEGACY_UNUSED", "HIGH", "0/521 and 0/4905 (C000032.ORIGEM) populated", ["C000025", "C000032"], [None]),
|
|
f("C000025", "COD_ORIGEM_MERCADORIA", "INTEGER", "código de origem da mercadoria (0-8)", "PRODUCT_DEFAULT", "LOW",
|
|
f"exists in vendor DDL as INTEGER; only 1/521 populated (value 2); origin is otherwise embedded in CST digit 1; not used in any emitted NFC-e column", ["C000025"], [2]),
|
|
f("C000025", "COD_ENTRADA_PIS", "VARCHAR(3)", "CST PIS de entrada", "LEGACY_UNUSED", "LOW", "2/521 populated with inconsistent formatting ('2','73')", ["C000025"], ["2", "73"]),
|
|
f("C000025", "COD_SAIDA_PIS", "VARCHAR(3)", "CST PIS de saída", "LEGACY_UNUSED", "LOW",
|
|
"3/521 populated ('01','04','2'); NFC-e items carry CST_PIS_COFINS '01' regardless, so the product field is not the source", ["C000025", "NFCE_ITENS"], ["01", "04", "2"]),
|
|
f("C000025", "COD_ENTRADA_COFINS", "VARCHAR(3)", "CST COFINS de entrada", "LEGACY_UNUSED", "LOW", "2/521 populated", ["C000025"], ["2", "73"]),
|
|
f("C000025", "COD_SAIDA_COFINS", "VARCHAR(3)", "CST COFINS de saída", "LEGACY_UNUSED", "LOW", "3/521 populated", ["C000025"], ["01", "04", "2"]),
|
|
f("C000025", "ALIQ_PIS", "VARCHAR(8)", "alíquota PIS (texto)", "LEGACY_UNUSED", "LOW", "3/521 populated; NFC-e ALIQUOTA_PIS is always 0 (Simples)", ["C000025", "NFCE_ITENS"], ["0,65", "2"]),
|
|
f("C000025", "ALIQ_COFINS", "VARCHAR(8)", "alíquota COFINS (texto)", "LEGACY_UNUSED", "LOW", "3/521 populated; NFC-e ALIQUOTA_COFINS is always 0", ["C000025", "NFCE_ITENS"], ["2", "3"]),
|
|
f("C000025", "CST_IPI", "SMALLINT", "CST do IPI", "LEGACY_UNUSED", "LOW", "1/521 populated ('040', 3 digits although IPI CST has 2)", ["C000025"], ["040"]),
|
|
f("C000025", "INCIDENCIA_PISCOFINS", "VARCHAR(30)", "tipo de incidência PIS/COFINS do produto", "PRODUCT_DEFAULT", "HIGH", "NENHUMA 519, MONOFÁSICA 1, SUBST.TRIBUTÁRIA 1; ties to C000025.PISCOFINS flag (N/S)", ["C000025"], ["NENHUMA", "MONOFÁSICA"]),
|
|
f("C000025", "CEST", "VARCHAR(7)", "CEST do produto (7 dígitos sem pontuação)", "PRODUCT_DEFAULT", "HIGH",
|
|
f"NFC-e item CEST digits equal product CEST in {consistent['CEST']['MATCH']}/{consistent['CEST']['MATCH']+consistent['CEST']['MISMATCH']} identity-consistent joins", ["C000025", "NFCE_ITENS"], ["0601100", "1708400"], write=True),
|
|
f("C000025", "IPI", "DOUBLE PRECISION", "alíquota de IPI %", "PRODUCT_DEFAULT", "HIGH", "confirmed in earlier missions and post-write verified; 109 populated", ["C000025"], [19.5, 3.25], write=True),
|
|
f("C000025", "EX_TIPI", "VARCHAR(3)", "código EX da TIPI", "PRODUCT_DEFAULT", "HIGH", "confirmed in earlier missions; 0 populated", ["C000025"], [None], write=True),
|
|
f("C000025", "ALIQUOTA", "NUMERIC(15,2)", "alíquota ICMS % do produto", "UNKNOWN", "LOW",
|
|
"product default 20.5 (492 rows) but NFC-e items use ALIQUOTA 18 (29825 rows) — product value is not what is emitted", ["C000025", "NFCE_ITENS"], [20.5, 17]),
|
|
f("C000025", "MARGEM_AGREGADA", "NUMERIC(15,2)", "candidato a MVA/margem de ST", "LEGACY_UNUSED", "LOW",
|
|
f"0/521 populated; C000062.MARGEM_AGREGADA is 0.0 in all 693 rows even though ICMS_SUB<>0 in {hist['C000062_mva_usage'][2]} rows (no calculation use evidenced); group-level PRODUTOS_MARGEM_AGREGADA(MVA_INDUSTRIA/MVA_DISTRIBUIDOR) has {hist['PRODUTOS_MARGEM_AGREGADA_rows']} rows and no link to C000025",
|
|
["C000025", "C000062", "PRODUTOS_MARGEM_AGREGADA"], [None]),
|
|
f("C000004", "CRT", "VARCHAR(1)", "código de regime tributário da empresa", "COMPANY_DEPENDENT", "HIGH", "value 1; EMISSOR.EMITENTE_CRT=1 agrees; OPTANTE_SIMPLES=S", ["C000004", "EMISSOR"], ["1"]),
|
|
f("C000004", "UF", "VARCHAR(2)", "UF do emitente", "COMPANY_DEPENDENT", "HIGH", "BA; matches IBGE 2928703 (Bahia)", ["C000004"], ["BA"]),
|
|
f("C000004", "ICMS_CSOSN", "VARCHAR(150)", "CSOSN padrão da empresa", "COMPANY_DEPENDENT", "MEDIUM", "value 102; equals 487/521 product defaults", ["C000004"], ["102"]),
|
|
f("C000004", "SUBSTITUTO_TRIBUTARIO / CONTRIBUINTE_IPI / OPTANTE_SIMPLES", "VARCHAR(1)", "flags fiscais da empresa", "COMPANY_DEPENDENT", "HIGH", "N / N / S", ["C000004"], ["N", "N", "S"]),
|
|
f("C000030", "CFOP,NATUREZA,TIPO", "VARCHAR/INTEGER", "cadastro de naturezas de operação (TIPO 1=entrada 272, 2=saída 318)", "OPERATION_DEFAULT", "HIGH",
|
|
f"590 rows loaded by vendor script 'Script CFOP C000030.txt'; CFOP first digit distribution {hist['C000030_cfop_first_digit']}; contains 5102/5405/5656/6102/1102", ["C000030"], ["1102 Compra p/ comercialização", "5102"]),
|
|
f("C000001", "IND_CFOP_ENTRADA/SAIDA/PERDA", "VARCHAR(5)", "CFOP padrão da empresa por finalidade", "OPERATION_DEFAULT", "HIGH", f"values {hist['C000001_cfop_defaults']} (1949/5949/5927)", ["C000001"], hist["C000001_cfop_defaults"]),
|
|
f("NFCE_ITENS", "CFOP,CST,GRUPO_ICMS,CSOSN,NCM,CEST,CST_PIS_COFINS", "mixed", "valores efetivamente emitidos em NFC-e (34,946 itens 2018-03..2026-08)", "DOCUMENT_CALCULATED", "HIGH",
|
|
"item-level emitted fiscal values; NCM/CEST/CSOSN/CST_PIS_COFINS populated only on 4,668 newer items (2022-06+)", ["NFCE_ITENS", "NFCE"], ["5102", "040", "102", "01"]),
|
|
f("NFCE_ITENS", "ALIQUOTA_PIS/ALIQUOTA_COFINS/BASE_CALCULO_*", "DOUBLE/NUMERIC", "PIS/COFINS emitido", "DOCUMENT_CALCULATED", "HIGH", "always 0 on 4,668 items (Simples Nacional: no PIS/COFINS calculation in NFC-e)", ["NFCE_ITENS"], [0]),
|
|
f("NFE_ALIQUOTA_INTERESTADUAL", "ICMS,ORIGEM,...", "mixed", "tabela de alíquotas interestaduais (729 linhas)", "OPERATION_DEFAULT", "MEDIUM", "reference table keyed by UF pair/origin; relevant only for interstate operations", ["NFE_ALIQUOTA_INTERESTADUAL"], []),
|
|
]
|
|
|
|
fmap = {"artifact": "ERP_FISCAL_FIELD_MAP_V1", "database": "TESTESOFICIAIS.FDB (read-only)", "firebird_writes": 0,
|
|
"evidence_sources": ["live RDB$ catalog (334 tables)", "value distributions", "NFCE_ITENS history join", VENDOR],
|
|
"caveat": "57% of NFC-e items with an NCM carry a different NCM than the product currently has for the same CODPRODUTO (identity drift); history comparisons are therefore reported on identity-consistent joins.",
|
|
"fields": FIELDS}
|
|
(DOCS / "ERP_FISCAL_FIELD_MAP_V1.json").write_text(json.dumps(fmap, ensure_ascii=False, indent=1), encoding="utf-8")
|
|
|
|
# ---------------------------------------------------------------- context requirements
|
|
AV, CFG, JOB, MISS = "AVAILABLE_FROM_ERP", "AVAILABLE_FROM_AGENT_CONFIG", "AVAILABLE_FROM_JOB", "MISSING"
|
|
REQ = {
|
|
"CFOP": {"official_basis": "CFOP table (IT 2023.002): direction, UF scope, ST, purpose in title/description/ind* flags", "inputs": {
|
|
"direction (entrada/saída)": [AV, "C000030.TIPO (1/2) per natureza; NFC-e is always saída"],
|
|
"operation_type / natureza": [AV, "C000030 (590 rows) at document level; not derivable from a product alone"],
|
|
"origin_uf": [AV, "C000004.UF=BA"],
|
|
"destination_uf": [JOB, "NFC-e is intra-UF by nature; NF-e needs customer UF (customer tables) — absent from a product-only job"],
|
|
"customer_taxpayer_status": [MISS, "per-document/customer; not in product request"],
|
|
"st_applicability": [AV, "BA_ST_MVA + CEST datasets, only 31 products MVA_RESOLVED"],
|
|
"purpose (revenda/consumo/devolução)": [MISS, "only natureza text; no structured field"]}},
|
|
"CSOSN": {"official_basis": "MOC CSOSN table + CRT=1", "inputs": {
|
|
"CRT": [AV, "C000004.CRT=1, EMISSOR.EMITENTE_CRT=1"],
|
|
"company_default_csosn": [AV, "C000004.ICMS_CSOSN=102"],
|
|
"st_applicability": [AV, "same as CFOP (partial)"],
|
|
"st_already_retained_by_supplier": [MISS, "requires purchase documents per product; not structured"],
|
|
"operation_type": [MISS, "document-level"],
|
|
"destination_context": [JOB, "document-level"]}},
|
|
"CST_ICMS": {"official_basis": "not emitted while CRT=1 (CSOSN used)", "inputs": {
|
|
"CRT": [AV, "C000004.CRT"], "regime_change_flag": [CFG, "only if client leaves Simples"], "operation_type": [MISS, "document-level"]}},
|
|
"ORIGEM": {"official_basis": "MOC origin table 0-8", "inputs": {
|
|
"objective_product_origin (import/national)": [MISS, "C000025.ORIGEM 0/521; COD_ORIGEM_MERCADORIA 1/521; ERP CST digit 1 is a config value not proof"]}},
|
|
"PIS_COFINS": {"official_basis": "CST PIS/COFINS table + regime; rate tables 4.3.x deferred", "inputs": {
|
|
"regime": [AV, "CRT=1 / OPTANTE_SIMPLES=S (NFC-e PIS/COFINS always 0)"],
|
|
"product_treatment": [AV, "C000025.INCIDENCIA_PISCOFINS (519 NENHUMA, 1 MONOFÁSICA, 1 SUBST.TRIBUTÁRIA)"],
|
|
"cst_rule_table": [MISS, "no NCM→CST PIS/COFINS rule in acquired datasets (deferred by business scope)"],
|
|
"operation_type": [MISS, "document-level"]}},
|
|
"FCP": {"official_basis": "FCP rate per UF (dataset) + state item list", "inputs": {
|
|
"company_uf": [AV, "C000004.UF=BA"], "destination_uf": [JOB, "document-level"],
|
|
"item_in_fcp_list_for_uf": [MISS, "no NCM/CEST list of FCP-subject items in datasets"],
|
|
"operation_interstate_or_internal": [JOB, "document-level"]}},
|
|
"RTC_CCLASSTRIB": {"official_basis": "cClassTrib table + LC 214 annexes", "inputs": {
|
|
"ncm_to_annex_mapping": [MISS, "LC 214 annex NCM lists are not in acquired datasets"],
|
|
"operation_nature": [MISS, "document-level"], "customer_type": [MISS, "document-level"], "CRT": [AV, "C000004.CRT"]}},
|
|
"RTC_CBS_RATE": {"official_basis": "IT 2026.002: 2026=0.009, 2027 pending legislation", "inputs": {
|
|
"cclasstrib_resolved": [MISS, "blocked by RTC_CCLASSTRIB"], "reference_year": [JOB, "document date"]}},
|
|
"RTC_CCREDPRES": {"official_basis": "cCredPres official conditions", "inputs": {
|
|
"condition_flags (ZFM, presumed credit eligibility)": [MISS, "no ERP field"]}},
|
|
"MVA": {"official_basis": "RICMS-BA Anexo 1 item/CEST", "inputs": {
|
|
"confirmed_cest": [AV, "only 15 CEST candidates remain, 1 historically supported"], "erp_destination_field": [MISS, "NO_PRODUCT_FIELD proven"]}},
|
|
}
|
|
n_av = n_cfg = n_job = n_miss = 0
|
|
for dom in REQ.values():
|
|
for k, (state, _) in dom["inputs"].items():
|
|
n_av += state == AV; n_cfg += state == CFG; n_job += state == JOB; n_miss += state == MISS
|
|
REQ_DOC = {"artifact": "FISCAL_CONTEXT_REQUIREMENTS_V1", "domains": {d: {"official_basis": v["official_basis"], "inputs": {k: {"status": s, "evidence": e} for k, (s, e) in v["inputs"].items()}} for d, v in REQ.items()},
|
|
"summary": {"AVAILABLE_FROM_ERP": n_av, "AVAILABLE_FROM_AGENT_CONFIG": n_cfg, "AVAILABLE_FROM_JOB": n_job, "MISSING": n_miss}}
|
|
(DOCS / "FISCAL_CONTEXT_REQUIREMENTS_V1.json").write_text(json.dumps(REQ_DOC, ensure_ascii=False, indent=1), encoding="utf-8")
|
|
|
|
|
|
def ctx(d):
|
|
st = [s for s, _ in REQ[d]["inputs"].values()]
|
|
return {"available": sum(1 for s in st if s in (AV, CFG)), "job_dependent": st.count(JOB), "missing": st.count(MISS), "total": len(st), "complete": all(s in (AV, CFG) for s in st)}
|
|
|
|
|
|
sv = hist["shadow_validation"]
|
|
matches = sum(v.get("MATCH", 0) for k, v in sv.items() if k != "items")
|
|
mism = sum(v.get("MISMATCH", 0) for k, v in sv.items() if k != "items")
|
|
unres = sum(v.get("UNRESOLVED", 0) for k, v in sv.items() if k != "items")
|
|
COV = {"artifact": "FISCAL_CONTEXT_COVERAGE_V1", "TOTAL_PRODUCTS": 521, "firebird_writes": 0,
|
|
"ERP_TABLES_INSPECTED": len(tables), "ERP_TABLES_NON_EMPTY": sum(1 for v in tables.values() if v["rows"]),
|
|
"ERP_CONTEXT_TABLES_FOUND": sorted(["C000004", "C000001", "C000030", "C000083", "NFE_ALIQUOTA_INTERESTADUAL", "EMISSOR"]),
|
|
"ERP_FISCAL_TABLES_FOUND": len(fiscal_tables),
|
|
"HISTORICAL_FISCAL_DOCUMENT_TABLES": ["NFCE", "NFCE_ITENS", "C000032", "C000061", "C000062"],
|
|
"HISTORICAL_FISCAL_DOCUMENTS_FOUND": {"NFCE": hist["nfce_docs"]["rows"], "C000061": 50},
|
|
"HISTORICAL_ITEMS_ANALYZED": items_analyzed,
|
|
"CFOP_MAPPING_CONFIRMED": True, "CST_MAPPING_CONFIRMED": True, "CSOSN_MAPPING_CONFIRMED": True,
|
|
"PIS_MAPPING_CONFIRMED": False, "COFINS_MAPPING_CONFIRMED": False, "ORIGIN_MAPPING_CONFIRMED": False, "MVA_FIELD_CONFIRMED": False,
|
|
"CFOP_CONTEXT_AVAILABLE": ctx("CFOP"), "CSOSN_CONTEXT_AVAILABLE": ctx("CSOSN"), "CST_CONTEXT_AVAILABLE": ctx("CST_ICMS"),
|
|
"PIS_COFINS_CONTEXT_AVAILABLE": ctx("PIS_COFINS"), "FCP_CONTEXT_AVAILABLE": ctx("FCP"), "RTC_CONTEXT_AVAILABLE": ctx("RTC_CCLASSTRIB"),
|
|
"CONTEXT_AVAILABLE": n_av + n_cfg, "CONTEXT_JOB_DEPENDENT": n_job, "CONTEXT_MISSING": n_miss,
|
|
"history_agreement_all_joins": agree_all, "history_agreement_identity_consistent_joins": consistent,
|
|
"identity_drift": {"consistent_items": extra["ncm_identity_consistent_items"], "inconsistent_items": extra["ncm_identity_inconsistent_items"]},
|
|
"cest_historically_supported": extra["cest_historically_supported"],
|
|
"shadow_validation_engine_vs_history": {"items": sv["items"], "MATCH": matches, "MISMATCH": mism, "UNRESOLVED": unres, "by_domain": {k: v for k, v in sv.items() if k != "items"},
|
|
"note": "NCM 'mismatch' = product's current NCM differs from the NCM emitted on that CODPRODUTO (identity drift), not an engine error; all operation-dependent domains stay UNRESOLVED by design."}}
|
|
(DOCS / "FISCAL_CONTEXT_COVERAGE_V1.json").write_text(json.dumps(COV, ensure_ascii=False, indent=1, default=str), encoding="utf-8")
|
|
|
|
# ---------------------------------------------------------------- priorities
|
|
d = cov_v1["domains"]
|
|
def hv(field, ok):
|
|
return ok
|
|
ROWS = [
|
|
# field, mapping, ctx_complete, rule, potential, hist_validation, next_action
|
|
("IPI", "CONFIRMED", True, True, d["IPI"]["RULE_RESOLVED"], "n/a in NFC-e items (no IPI column)", "Maintain; add NT/blank policy decision."),
|
|
("CEST", "CONFIRMED", True, True, d["CEST"]["RULE_CANDIDATE"], f"{consistent['CEST']['MATCH']}/{consistent['CEST']['MATCH']+consistent['CEST']['MISMATCH']} product==emitted; {extra['cest_historically_supported']}/15 candidates history-supported", "Use NFC-e history as EVIDENCE to narrow candidates; write only single defensible matches."),
|
|
("EX_TIPI", "CONFIRMED", True, True, d["EX_TIPI"]["RULE_CANDIDATE"], "none", "Description-evidence rule already in engine; review queue only."),
|
|
("NCM", "CONFIRMED", True, True, d["NCM"]["RULE_CANDIDATE"], "identity drift: 57% of NFC-e NCMs differ from current product NCM", "Investigate product-code reuse before trusting history; keep review-only."),
|
|
("CFOP", "CONFIRMED", False, False, 521, f"{consistent['CFOP']['MATCH']}/{consistent['CFOP']['MATCH']+consistent['CFOP']['MISMATCH']} product default==emitted", "Model NFC-e sale context (saída, intra-UF, consumidor final) and define an official-table-backed rule; validate vs NFCE_ITENS."),
|
|
("CSOSN", "CONFIRMED", False, False, 521, f"{consistent['CSOSN']['MATCH']}/{consistent['CSOSN']['MATCH']+consistent['CSOSN']['MISMATCH']} product default==emitted", "Need ST-retention context; build shadow rule (CRT=1 + ST applicability) and score against NFCE_ITENS."),
|
|
("CST_ICMS", "CONFIRMED", False, False, 521, f"{consistent['CST']['MATCH']}/{consistent['CST']['MATCH']} product==emitted", "Not needed while CRT=1; keep as validated default."),
|
|
("MVA", "NO_PRODUCT_FIELD", False, True, d["MVA"]["RULE_RESOLVED"], "none (MARGEM_AGREGADA unused)", "Decide with vendor whether MARGEM_AGREGADA is honoured by the emitter before any write."),
|
|
("PIS_COFINS_CST", "UNCONFIRMED", False, False, 521, "NFC-e always '01'/blank, rates 0", "Needs vendor confirmation of which field feeds CST_PIS_COFINS (not C000025.COD_SAIDA_*)."),
|
|
("ORIGEM", "UNCONFIRMED", False, False, 521, "CST digit 1 consistent (0)", "Vendor confirmation of COD_ORIGEM_MERCADORIA usage; obtain objective origin."),
|
|
("FCP", "n/a (no product field)", False, False, 521, "none", "Needs FCP item list per UF; document-level only."),
|
|
("RTC", "n/a (no ERP field)", False, False, 521, "none", "Needs NCM→LC 214 annex mapping dataset; then document-level."),
|
|
]
|
|
def score(r):
|
|
return (r[1] == "CONFIRMED", r[2], r[3], r[4], "n/a" not in r[5] and r[5] != "none")
|
|
ROWS.sort(key=score, reverse=True)
|
|
md = ["# NEXT_FISCAL_FIELD_IMPLEMENTATION_V1", "",
|
|
"Mechanical ordering: (1) ERP mapping confirmed, (2) required context available, (3) official rule available, (4) potential products, (5) history validation exists. Source: `docs/ERP_FISCAL_FIELD_MAP_V1.json`, `docs/FISCAL_CONTEXT_REQUIREMENTS_V1.json`, `docs/FISCAL_CONTEXT_COVERAGE_V1.json`.", "",
|
|
"| FIELD | ERP_MAPPING | CONTEXT_AVAILABLE | RULE_AVAILABLE | POTENTIAL_PRODUCTS | HISTORICAL_VALIDATION | NEXT_ACTION |", "|---|---|---|---|---|---|---|"]
|
|
for r in ROWS:
|
|
md.append(f"| {r[0]} | {r[1]} | {'YES' if r[2] else 'NO'} | {'YES' if r[3] else 'NO'} | {r[4]} | {r[5]} | {r[6]} |")
|
|
md += ["", "Caveat: NFC-e history joins by CODPRODUTO show identity drift (item NCM != product NCM on 57% of items); trust only identity-consistent joins."]
|
|
(DOCS / "NEXT_FISCAL_FIELD_IMPLEMENTATION_V1.md").write_text("\n".join(md) + "\n", encoding="utf-8")
|
|
print(json.dumps({"tables": len(tables), "fiscal_tables": len(fiscal_tables), "items_analyzed": items_analyzed, "ctx": [n_av, n_cfg, n_job, n_miss], "sim": [matches, mism, unres],
|
|
"confirmed": sum(1 for x in FIELDS if x["confidence"] in ("HIGH", "MEDIUM")), "fields": len(FIELDS)}))
|