Files
GTI_GERENCIADOR_FISCAL/fiscal/scripts/bloco2_pis_cofins_cross_db.py
T
gti d21a9d1140 Add 'fiscal/' from commit '102ba84500dbd9655a0471288c3e278652593ca9'
git-subtree-dir: fiscal
git-subtree-mainline: d95165fc8f
git-subtree-split: 102ba84500
2026-09-29 14:43:23 -03:00

96 lines
5.0 KiB
Python

"""Missão 2 / Bloco 2.1 — evidência cruzada PIS/COFINS cadastro-vs-emissão, somente leitura, nos 6 bancos.
Nunca escreve. Não decide regra fiscal: só conta e cruza fatos.
"""
import json
import os
import sys
from pathlib import Path
from collections import Counter
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT)); sys.path.insert(0, str(ROOT / "scripts")); sys.path.insert(0, str(ROOT / "agent" / "src"))
import fdb # noqa: E402
import run_fiscal_decision_v1 as base # noqa: E402
from gti_fiscal_agent import company_context as cc # noqa: E402
DBS = {
"GTI": (r"D:\PROJETOS\GTISISTEMAS\Banco\GTI.FDB", {}),
"TESTESOFICIAIS": (str(ROOT / "TESTESOFICIAIS.FDB"), {}),
"CONSTRUCAO": (str(ROOT / "Banco" / "CONSTRUCAO.FDB"), {"company_code": "000001"}),
"MMMODAS": (str(ROOT / "Banco" / "MMMODAS.FDB"), {"company_code": "000001", "crt_source": "EMISSOR"}),
"RINCAO": (str(ROOT / "Banco" / "RINCAO.FDB"), {"company_code": "000001", "crt_source": "EMISSOR"}),
"UNIAO": (str(ROOT / "Banco" / "UNIAO.FDB"), {"company_code": "000001", "crt_source": "EMISSOR"}),
}
def connect(path):
return fdb.connect(database=path, user=os.environ["FB_USER"], password=os.environ["FB_PASSWORD"],
fb_library_name=os.environ.get("FIREBIRD_CLIENT_LIBRARY"), isolation_level=fdb.ISOLATION_LEVEL_READ_COMMITED_RO)
def analyze_one(name, path, cfg):
conn = connect(path); cur = conn.cursor()
company = cc.resolve(cc.collect_evidence(cur), cc.config_from_env({("COMPANY_CODE" if k == "company_code" else "COMPANY_CRT_SOURCE"): v for k, v in cfg.items()}))
out = {"name": name, "company_status": company["status"], "crt": company.get("company_crt"), "uf": company.get("company_uf")}
try:
cur.execute("SELECT COUNT(*), COUNT(NULLIF(TRIM(CST_PIS_COFINS),'')) FROM NFCE_ITENS")
tot, nonblank = cur.fetchone()
out["items_total"] = tot; out["items_cst_pis_cofins_filled"] = nonblank
out["pct_cst_filled"] = round(100.0 * nonblank / tot, 2) if tot else None
except Exception as e: # noqa: BLE001
out["items_error"] = str(e)[:150]; conn.close(); return out
cur.execute("SELECT TRIM(CST_PIS_COFINS), COUNT(*) FROM NFCE_ITENS WHERE CST_PIS_COFINS IS NOT NULL GROUP BY 1 ORDER BY 2 DESC")
out["cst_distribution"] = {(r[0] or "(blank)"): r[1] for r in cur.fetchall()}
try:
cur.execute("SELECT EXTRACT(YEAR FROM DATA), COUNT(*), COUNT(NULLIF(TRIM(CST_PIS_COFINS),'')) FROM NFCE_ITENS GROUP BY 1 ORDER BY 1")
out["by_year"] = {int(y): {"items": t, "cst_filled": f} for y, t, f in cur.fetchall() if y is not None}
except Exception as e: # noqa: BLE001
out["by_year_error"] = str(e)[:150]
try:
cur.execute("SELECT ROUND(ALIQUOTA_PIS,4), COUNT(*) FROM NFCE_ITENS WHERE ALIQUOTA_PIS IS NOT NULL GROUP BY 1 ORDER BY 2 DESC")
out["aliquota_pis_distribution"] = {str(r[0]): r[1] for r in cur.fetchmany(10)}
cur.execute("SELECT ROUND(ALIQUOTA_COFINS,4), COUNT(*) FROM NFCE_ITENS WHERE ALIQUOTA_COFINS IS NOT NULL GROUP BY 1 ORDER BY 2 DESC")
out["aliquota_cofins_distribution"] = {str(r[0]): r[1] for r in cur.fetchmany(10)}
except Exception as e: # noqa: BLE001
out["aliquota_error"] = str(e)[:150]
# cadastro vs emissão, apenas nos itens cujo NCM emitido == NCM cadastrado (join identity-consistent, evita o viés de 2022-12 já documentado)
try:
cur.execute("""
SELECT COUNT(*),
SUM(CASE WHEN TRIM(COALESCE(p.CLASSIFICACAO_FISCAL,'')) != '' AND p.CLASSIFICACAO_FISCAL = i.NCM THEN 1 ELSE 0 END)
FROM NFCE_ITENS i JOIN C000025 p ON p.CODIGO = i.CODPRODUTO
""")
joined_tot, identity_consistent = cur.fetchone()
out["identity_consistent_items"] = identity_consistent
cur.execute("""
SELECT COUNT(*)
FROM NFCE_ITENS i JOIN C000025 p ON p.CODIGO = i.CODPRODUTO
WHERE p.CLASSIFICACAO_FISCAL = i.NCM
AND COALESCE(TRIM(p.CST_ENTRADA),'') = '' -- produto sem CST/PIS cadastral (a maioria)
AND COALESCE(TRIM(i.CST_PIS_COFINS),'') != ''
""")
out["emitted_cst_present_when_cadastro_blank"] = cur.fetchone()[0]
except Exception as e: # noqa: BLE001
out["cadastro_vs_emissao_error"] = str(e)[:150]
conn.close()
return out
def main():
base.load_dotenv(ROOT / ".env")
results = {}
for name, (path, cfg) in DBS.items():
print(f"== {name}", flush=True)
try:
results[name] = analyze_one(name, path, cfg)
except Exception as e: # noqa: BLE001
results[name] = {"name": name, "error": str(e)[:300]}
print(json.dumps(results[name], ensure_ascii=False, indent=1, default=str)[:800], flush=True)
out_path = ROOT / "docs" / "runs" / "BLOCO2_PIS_COFINS_CROSS_DB_V1.json"
out_path.write_text(json.dumps(results, ensure_ascii=False, indent=1, default=str), encoding="utf-8")
print(f"-> {out_path}")
if __name__ == "__main__":
main()