"""Write docs/FISCAL_FIELD_CONTRACT_V1.json from central.app.field_contract (single source of truth). No database access.""" import json import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from central.app.field_contract import CONTRACT, CONTRACT_VERSION, RTC_CAPABILITY, WRITE_FIELDS # noqa: E402 from central.app.context_keys import ALLOWED_KEYS # noqa: E402 ui = json.loads((ROOT / "docs" / "SGV_PRODUCT_FISCAL_UI_MAP_V1.json").read_text(encoding="utf-8")) bound = {f["field"]: f for f in ui["fields"] if f.get("field")} doc = {"artifact": "FISCAL_FIELD_CONTRACT_V1", "contract_version": CONTRACT_VERSION, "product_table": "C000025", "write_targets": len(WRITE_FIELDS), "final_states_per_product_field": ["ALREADY_CORRECT", "CHANGE_REQUIRED", "CORRECTED", "NOT_APPLICABLE", "MANUAL_INPUT_REQUIRED()"], "declared_valid_policy": "A valid ERP declaration that no proven evidence contradicts is ALREADY_CORRECT with reason DECLARED_VALID_NOT_CONTRADICTED; rule-proven results carry other reasons.", "operator_context_keys": sorted(ALLOWED_KEYS), "context_hierarchy": "PRODUCT > GROUP (CODGRUPO) > NCM > COMPANY (Hub table fiscal_context_entries; API /v1/context; CLI scripts/import_fiscal_context.py)", "unresolved_official_sources": { "PIS/COFINS": "No official rule table in the ACTIVE core datasets maps regime/NCM to CST PIS/COFINS or rates (SPED Tabelas 4.3.x + the Simples Nacional PIS/COFINS guidance are the missing sources; they are manual downloads and were deferred by business scope). Contract answer: company-level `pis_cofins_profile` supplied by the accountant; never assumed 0%.", "ICMS internal rate (regime normal)": "RICMS-BA articles on internal rates/FECP are not in the acquired datasets: `icms_rate` is an operator fact for CRT 2/3 taxed items.", "Monofásico incidence": "Official monophasic NCM list not acquired: INCIDENCIA_PISCOFINS keeps the ERP declaration or takes an operator `incidence`."}, "rtc": {"status": {"RTC_CCLASSTRIB": "SUPPORTED_BY_CURRENT_ERP_SCHEMA_UI_BINDING_UNCONFIRMED", "RTC_CCREDPRES": "ERP_SCHEMA_NOT_READY", "RTC_CBS_RATES": "ERP_SCHEMA_NOT_READY"}, "evidence": "C000025.CCLASSTRIB VARCHAR(6) exists (all NULL) in CONSTRUCAO, MMMODAS, RINCAO and UNIAO (a newer SGV schema) and is absent in TESTESOFICIAIS. The SGV executable available locally (5.0.6.124) neither binds nor mentions it and the fiscal screen shows no such control, so the binding cannot be confirmed; no cCredPres/CBS/IBS column exists in any database.", "write_target": False, "why_not_written": "no proven UI binding and no NCM -> LC 214 annex (cClassTrib) source in the ACTIVE datasets; a value here would be invented", "to_close": ["the newer SGV executable/screen that binds CCLASSTRIB (binding proof)", "official LC 214/2025 annex NCM lists to derive cClassTrib deterministically"]}, "not_persisted_controls": {"CSOSN significado (ComboBox1)": "presentation only, not data-aware", "CODALIQUOTA": "exists in the ERP, empty, not on the fiscal screen: not a requirement without new evidence"}, "fields": {}} for f in WRITE_FIELDS: c = dict(CONTRACT[f]) c["UI_BINDING"] = {"classification": bound[f]["classification"] if f in bound else "CONFIRMED_STRONG", "evidence": bound[f]["evidence_basis"] if f in bound else None} doc["fields"][f] = c # EX_TIPI is not a control on the screen (audited separately) and COD_ORIGEM binding is closed by the DFM OnClick handler: doc["fields"]["EX_TIPI"]["UI_BINDING"] = {"classification": "CONFIRMED_STRONG", "evidence": "user-declared earlier missions; TIPI column EX_TIPI"} doc["fields"]["COD_ORIGEM_MERCADORIA"]["UI_BINDING"] = {"classification": "CONFIRMED_STRONG", "evidence": "DFM: RgOrigemMercadoria (TRadioGroup, OnClick=RgOrigemMercadoriaClick, no DataField) + hidden data-aware TDBEdit CodOrigemMercadoria bound to COD_ORIGEM_MERCADORIA in the same tab; the only origin-bound control of the form; values 0/1/2 observed in 4 databases"} (ROOT / "docs" / "FISCAL_FIELD_CONTRACT_V1.json").write_text(json.dumps(doc, ensure_ascii=False, indent=1), encoding="utf-8") print(len(doc["fields"]), "contracts")