Files
GTI_GERENCIADOR_FISCAL/docs/LOCAL_COMPLETION_V2_REPORT.md
T

33 lines
4.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# LOCAL COMPLETION V2 — semantic audit of the V1 writes
## Origin (COD_ORIGEM_MERCADORIA) — INVALID_RULE, fully rolled back
- Target column: `C000025.COD_ORIGEM_MERCADORIA` (SGV screen offers 0/1/2). BEFORE: NULL in 35,048 rows (+ 12 declared 0/1/2 untouched). AFTER (V1): 0 in all 35,048.
- Rule used: origin = first digit of the ERP CST. Refuted: CST first digit is padding (`040`/`060`; the 12 products with a declared origin 1/2 also carry CST digit 0). No other ERP field carries origin (NFCE_ITENS has none; only FARMACIA_SNGPC.CNPJ_ORIGEM).
- Rollback via guarded executor (`scripts/rollback_field.py`): 35,048/35,048 restored, 0 failures, 0 column mismatches vs BEFORE snapshot (`docs/runs/local_completion/rollback_COD_ORIGEM_MERCADORIA_summary.json`).
- Resolver now: declared 0/1/2 stays; blank → MANUAL(origin); never 0 by default.
## IPI — 6,262 writes: all SEMANTICALLY_PROVEN
NCM valid in dataset, TIPI base row numeric rate equals the value, product has no EX, before NULL, no NT/blank→0 (CONSTRUCAO 1,497 · MMMODAS 1,947 · RINCAO 349 · UNIAO 2,469). Independent live re-check: 0 invalid.
Caveat (business fact, not a defect): the IPI rate is only charged by industrial/importer establishments; all five companies are retail (contribuinte_ipi=N). The field is the official TIPI attribute of the NCM.
A V2 attempt to also write IPI where several TIPI rows share one rate was WRONG (candidate list held only EX rows, so EX rates were written for products without EX); caught by the independent check, 2,928 cells rolled back exactly, rule removed.
## CFOP / CEST / IPPT — table in `docs/LOCAL_WRITE_SEMANTIC_AUDIT_V2_CFOP_CEST_IPPT.csv`
- CFOP 5405→5102 (19 cells): INVALID (ERP declares CST 060/CSOSN 500, ST retained) → rolled back; resolver now raises MANUAL(st_status) on such conflicts.
- CFOP 5102→5101 (2 cells): only kitchen-flag evidence → rolled back. IPPT T→P (10 cells): kitchen flag only → rolled back. Kitchen flag alone no longer proves OWN_PRODUCTION.
- Kept and proven: CEST 1 (NCM 21039021 → 17.035.00, exact official link), CFOP 4, IPPT 8 (recipe/composition evidence).
## 23-field accounting
35,066 physical rows × 23 = 806,518 = accounted 806,518 (V1 summed 806,495: MMMODAS duplicate CODIGO 017421 collapsed in a dict). C000025 has PRIMARY KEY(CODIGO) yet holds 2 physical rows with the same CODIGO (index inconsistency in the copy). Both rows are counted; all 46 cells → MANUAL(DUPLICATE_KEY_BLOCKED); no UPDATE by CODIGO when rowcount != 1.
## Sources (docs/sources/SOURCES_MANIFEST_V2.json)
- ACQUIRED (Internet Archive copy of the official URL; live sped.rfb.gov.br refuses connections): SPED 4.3.3/4.3.4 (CST domains), 4.3.10, 4.3.12, 4.3.13, 4.3.14, 4.3.15 → `docs/sources/SPED_PISCOFINS_TABLES_V2.json` (sha256, version text, NCM prefixes, vigência). Versions are the archived ones (e.g. 4.3.13 v1.28/2023, not the 2024 v1.32 or 2026 updates): re-fetch when the host is reachable.
- ACQUIRED as snapshot only: LC 214 compiled (planalto). RTC stays ERP_SCHEMA_NOT_READY (no compatible SGV field; nothing created/reused).
- NOT ACQUIRED: Lei BA 7.014 / RICMS-BA / Anexo 1 (official host refused, no archive copy at known URLs), Resolução CGSN 140. Not blocking: the four Simples databases have no highlighted ICMS (ALIQUOTA NOT_APPLICABLE); ST/MVA stays the versioned Hub dataset.
## PIS/COFINS rule (new)
ERP evidence: NFC-e history (≈570k items in the 4 client DBs) has blank CST_PIS_COFINS for 100% of items even when the product carries CST 04; purchase items likewise blank → the SGV does not consume these product fields at emission for Simples. For CRT 1 with that evidence: entry fields and rates NOT_APPLICABLE; saída CST written only when the NCM is in an active SPED special table (04 monofásica / 06 alíquota zero …) and the role is RESALE. 1,391 cells written (696 PIS + 695 COFINS), all re-verified (live check: CST equals the table CST, 0 invalid). Never "Simples ⇒ 0/0".
Also fixed an objective bug: `build_refs` only validated saída CFOPs, so every declared CFOP_ENTRADA looked invalid (34k false MANUAL cells).
## Result
Manual cells 255,643 → 55,756. Questions 1,054 → 1,000 (14 company-level questions cover ≈47k cells: origin ×5, pis_cofins_profile ×3, entry_scope ×5, icms_rate ×1). Net cells changed vs the original databases: 7,666 (IPI 6,262; COD_SAIDA_PIS 696; COD_SAIDA_COFINS 695; CEST 1; IPPT 8; CFOP 4). Second pass 0 writes. Full tests 496 passed / 1 skipped.