"""PostgreSQL records owned by the Fiscal Hub.""" from datetime import datetime from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, JSON, String, Text, UniqueConstraint, func from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column class Base(DeclarativeBase): pass class Dataset(Base): __tablename__ = "datasets" __table_args__ = (UniqueConstraint("provider", "sha256", name="uq_dataset_provider_hash"),) id: Mapped[int] = mapped_column(primary_key=True) kind: Mapped[str] = mapped_column(String(32), index=True) provider: Mapped[str] = mapped_column(String(128)) sha256: Mapped[str] = mapped_column(String(64)) raw_sha256: Mapped[str | None] = mapped_column(String(64), nullable=True) version_label: Mapped[str] = mapped_column(String(256)) act: Mapped[str] = mapped_column(Text) source_url: Mapped[str] = mapped_column(Text) downloaded_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) status: Mapped[str] = mapped_column(String(32), index=True) record_count: Mapped[int] = mapped_column(Integer, default=0) class NcmEntry(Base): __tablename__ = "ncm_entries" __table_args__ = (UniqueConstraint("dataset_id", "code", "valid_from", "valid_to", name="uq_ncm_versioned_entry"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) code: Mapped[str] = mapped_column(String(16), index=True) description: Mapped[str] = mapped_column(Text) valid_from: Mapped[str] = mapped_column(String(32)) valid_to: Mapped[str] = mapped_column(String(32)) class LegacyNcmEntry(Base): __tablename__ = "legacy_ncm_entries" code: Mapped[str] = mapped_column(String(16), primary_key=True) source: Mapped[str] = mapped_column(Text) source_date: Mapped[str] = mapped_column(String(32)) reason: Mapped[str] = mapped_column(Text) successor_code: Mapped[str | None] = mapped_column(String(16), nullable=True) class CestEntry(Base): __tablename__ = "cest_entries" __table_args__ = (UniqueConstraint("dataset_id", "code", name="uq_cest_versioned_entry"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) code: Mapped[str] = mapped_column(String(16), index=True) description: Mapped[str] = mapped_column(Text) annex: Mapped[str] = mapped_column(String(8)) class CestNcmEntry(Base): __tablename__ = "cest_ncm_entries" __table_args__ = (UniqueConstraint("cest_entry_id", "ncm_code", name="uq_cest_ncm_link"),) id: Mapped[int] = mapped_column(primary_key=True) cest_entry_id: Mapped[int] = mapped_column(ForeignKey("cest_entries.id"), index=True) ncm_code: Mapped[str] = mapped_column(String(16), index=True) class TipiEntry(Base): __tablename__ = "tipi_entries" id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) ncm: Mapped[str] = mapped_column(String(8), index=True) ex: Mapped[str | None] = mapped_column(String(32), nullable=True) description: Mapped[str] = mapped_column(Text) ipi_rate: Mapped[float | None] = mapped_column(nullable=True) ipi_rate_text: Mapped[str | None] = mapped_column(String(32), nullable=True) source_row: Mapped[int] = mapped_column(Integer) metadata_json: Mapped[dict] = mapped_column(JSON, default=dict) class BaStMvaEntry(Base): __tablename__ = "ba_st_mva_entries" id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) item: Mapped[str] = mapped_column(String(32)) segment: Mapped[str | None] = mapped_column(Text, nullable=True) cest: Mapped[str | None] = mapped_column(String(16), index=True, nullable=True) description: Mapped[str | None] = mapped_column(Text, nullable=True) mva_original: Mapped[str | None] = mapped_column(String(32), nullable=True) mva_adjusted: Mapped[str | None] = mapped_column(String(32), nullable=True) agreement_info: Mapped[str | None] = mapped_column(Text, nullable=True) source_page: Mapped[int] = mapped_column(Integer) metadata_json: Mapped[dict] = mapped_column(JSON, default=dict) class BaStMvaNcmSh(Base): __tablename__ = "ba_st_mva_ncm_sh" __table_args__ = (UniqueConstraint("entry_id", "ncm_sh", name="uq_ba_st_mva_entry_ncm_sh"),) id: Mapped[int] = mapped_column(primary_key=True) entry_id: Mapped[int] = mapped_column(ForeignKey("ba_st_mva_entries.id"), index=True) ncm_sh: Mapped[str] = mapped_column(String(32), index=True) metadata_json: Mapped[dict] = mapped_column(JSON, default=dict) class FiscalCodeEntry(Base): """Small national fiscal code domains sourced from the official NF-e MOC manual: ORIGEM_MERCADORIA, CST_ICMS, CSOSN, CST_PIS, CST_COFINS.""" __tablename__ = "fiscal_code_entries" __table_args__ = (UniqueConstraint("dataset_id", "domain", "code", name="uq_fiscal_code_entry"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) domain: Mapped[str] = mapped_column(String(32), index=True) code: Mapped[str] = mapped_column(String(8), index=True) description: Mapped[str] = mapped_column(Text) source_section: Mapped[str] = mapped_column(String(96)) metadata_json: Mapped[dict] = mapped_column(JSON, default=dict) class CfopEntry(Base): """CFOP (Código Fiscal de Operações e Prestações) — Portal Nacional NF-e.""" __tablename__ = "cfop_entries" __table_args__ = (UniqueConstraint("dataset_id", "code", name="uq_cfop_entry"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) code: Mapped[str] = mapped_column(String(8), index=True) title: Mapped[str] = mapped_column(Text) description: Mapped[str | None] = mapped_column(Text, nullable=True) effective_from: Mapped[str | None] = mapped_column(String(32), nullable=True) effective_to: Mapped[str | None] = mapped_column(String(32), nullable=True) ind_nfe: Mapped[str | None] = mapped_column(String(4), nullable=True) ind_comunica: Mapped[str | None] = mapped_column(String(4), nullable=True) ind_transp: Mapped[str | None] = mapped_column(String(4), nullable=True) ind_devol: Mapped[str | None] = mapped_column(String(4), nullable=True) ind_retor: Mapped[str | None] = mapped_column(String(4), nullable=True) ind_anula: Mapped[str | None] = mapped_column(String(4), nullable=True) ind_remes: Mapped[str | None] = mapped_column(String(4), nullable=True) ind_comb: Mapped[str | None] = mapped_column(String(4), nullable=True) ind_exc_ibscbs: Mapped[str | None] = mapped_column(String(4), nullable=True) class FcpRate(Base): """FCP (Fundo de Combate à Pobreza) alíquotas por UF — Portal Nacional NF-e.""" __tablename__ = "fcp_rates" __table_args__ = (UniqueConstraint("dataset_id", "uf", name="uq_fcp_rate_uf"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) uf_code: Mapped[str] = mapped_column(String(4)) uf: Mapped[str] = mapped_column(String(4), index=True) uf_name: Mapped[str] = mapped_column(String(64)) aliq_1: Mapped[str | None] = mapped_column(String(32), nullable=True) aliq_2: Mapped[str | None] = mapped_column(String(32), nullable=True) aliq_3: Mapped[str | None] = mapped_column(String(32), nullable=True) observacao: Mapped[str | None] = mapped_column(Text, nullable=True) class AnpProductCode(Base): """cProdANP — official code list used by NF-e, published on the Portal Nacional NF-e.""" __tablename__ = "anp_product_codes" __table_args__ = (UniqueConstraint("dataset_id", "cprod_anp", name="uq_anp_product_code"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) cprod_anp: Mapped[str] = mapped_column(String(16), index=True) class NcmUtribEntry(Base): """NCM x uTrib (unidade tributável de comércio exterior) — Portal Nacional NF-e.""" __tablename__ = "ncm_utrib_entries" __table_args__ = (UniqueConstraint("dataset_id", "ncm", name="uq_ncm_utrib_entry"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) ncm: Mapped[str] = mapped_column(String(16), index=True) effective_from: Mapped[str | None] = mapped_column(String(32), nullable=True) effective_to: Mapped[str | None] = mapped_column(String(32), nullable=True) utrib_abbreviation: Mapped[str | None] = mapped_column(String(16), nullable=True) utrib_description: Mapped[str | None] = mapped_column(Text, nullable=True) class RtcBiofuelIndexEntry(Base): """RTC pBioObrigatorio (índice de biocombustível) by cProdANP — Portal Nacional NF-e.""" __tablename__ = "rtc_biofuel_index_entries" id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) cprod_anp: Mapped[str | None] = mapped_column(String(16), index=True, nullable=True) description: Mapped[str | None] = mapped_column(Text, nullable=True) p_bio_percent: Mapped[float | None] = mapped_column(nullable=True) effective_from: Mapped[str | None] = mapped_column(String(32), nullable=True) effective_to: Mapped[str | None] = mapped_column(String(32), nullable=True) is_historical: Mapped[bool] = mapped_column(Boolean, default=False) class RtcCredPresEntry(Base): """RTC cCredPres (crédito presumido IBS/CBS), LC 214/2025 — Portal Nacional NF-e. Keeps every official field distinctly so future automatic determination doesn't require re-parsing the source.""" __tablename__ = "rtc_cred_pres_entries" __table_args__ = (UniqueConstraint("dataset_id", "code", name="uq_rtc_cred_pres_code"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) code: Mapped[str] = mapped_column(String(8), index=True) description: Mapped[str] = mapped_column(Text) legal_basis: Mapped[str | None] = mapped_column(Text, nullable=True) apropria_via_nf: Mapped[bool | None] = mapped_column(Boolean, nullable=True) apropria_via_evento: Mapped[bool | None] = mapped_column(Boolean, nullable=True) ind_deduz_cred_pres: Mapped[str | None] = mapped_column(String(8), nullable=True) ind_g_cbs_cred_pres: Mapped[str | None] = mapped_column(String(8), nullable=True) ind_g_ibs_cred_pres: Mapped[str | None] = mapped_column(String(8), nullable=True) aliquota_cbs_text: Mapped[str | None] = mapped_column(Text, nullable=True) aliquota_ibs_text: Mapped[str | None] = mapped_column(Text, nullable=True) p_aliq_cred_pres_cbs: Mapped[str | None] = mapped_column(Text, nullable=True) p_aliq_cred_pres_ibs: Mapped[str | None] = mapped_column(Text, nullable=True) p_red_transicao_ibs: Mapped[str | None] = mapped_column(Text, nullable=True) # Free text in some rows (e.g. "cClass de alíquota zero"), not always the numeric code. cclass_nota_referenciada: Mapped[str | None] = mapped_column(Text, nullable=True) d_ini_vig_cbs: Mapped[str | None] = mapped_column(String(32), nullable=True) d_fim_vig_cbs: Mapped[str | None] = mapped_column(String(32), nullable=True) d_ini_vig_ibs: Mapped[str | None] = mapped_column(String(32), nullable=True) d_fim_vig_ibs: Mapped[str | None] = mapped_column(String(32), nullable=True) class Tenant(Base): __tablename__ = "tenants" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column(String(128), unique=True) class Agent(Base): __tablename__ = "agents" id: Mapped[int] = mapped_column(primary_key=True) tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id"), index=True) # company_id name: Mapped[str] = mapped_column(String(128)) token_hash: Mapped[str] = mapped_column(String(64), unique=True) enabled: Mapped[bool] = mapped_column(Boolean, default=True, server_default="true") last_seen_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) last_status: Mapped[str | None] = mapped_column(String(32), nullable=True) schema_fingerprint: Mapped[str | None] = mapped_column(String(64), nullable=True) agent_version: Mapped[str | None] = mapped_column(String(32), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) class Job(Base): __tablename__ = "jobs" id: Mapped[int] = mapped_column(primary_key=True) agent_id: Mapped[int] = mapped_column(ForeignKey("agents.id"), index=True) kind: Mapped[str] = mapped_column(String(32), index=True) # REPROCESS_ALL | REPROCESS_PRODUCTS status: Mapped[str] = mapped_column(String(16), index=True, default="PENDING", server_default="PENDING") payload: Mapped[dict] = mapped_column(JSON, default=dict) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) started_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) attempts: Mapped[int] = mapped_column(Integer, default=0, server_default="0") result_summary: Mapped[dict | None] = mapped_column(JSON, nullable=True) error: Mapped[str | None] = mapped_column(Text, nullable=True) class ClassificationRequest(Base): __tablename__ = "classification_requests" id: Mapped[int] = mapped_column(primary_key=True) tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id"), index=True) agent_id: Mapped[int] = mapped_column(ForeignKey("agents.id"), index=True) fingerprint: Mapped[str] = mapped_column(String(64), unique=True) payload: Mapped[dict] = mapped_column(JSON) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) class ClassificationResult(Base): __tablename__ = "classification_results" id: Mapped[int] = mapped_column(primary_key=True) request_id: Mapped[int] = mapped_column(ForeignKey("classification_requests.id"), unique=True) result: Mapped[dict] = mapped_column(JSON) class ClassificationCache(Base): __tablename__ = "classification_cache" fingerprint: Mapped[str] = mapped_column(String(64), primary_key=True) tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id"), index=True) result: Mapped[dict] = mapped_column(JSON) class RtcCstIbsCbsEntry(Base): """RTC CST-IBS/CBS (sheet 'CST ...' of the official cClassTrib workbook).""" __tablename__ = "rtc_cst_ibscbs_entries" __table_args__ = (UniqueConstraint("dataset_id", "cst", name="uq_rtc_cst_ibscbs"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) cst: Mapped[str] = mapped_column(String(8), index=True) description: Mapped[str] = mapped_column(Text) indicators: Mapped[dict] = mapped_column(JSON) class RtcCClassTribEntry(Base): """RTC cClassTrib (sheet 'cClass ...'). Every official column is kept: key fields typed, all ind_* flags in `indicators` keyed by official header.""" __tablename__ = "rtc_cclasstrib_entries" __table_args__ = (UniqueConstraint("dataset_id", "cclasstrib", name="uq_rtc_cclasstrib"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) cst: Mapped[str] = mapped_column(String(8), index=True) cst_description: Mapped[str | None] = mapped_column(Text, nullable=True) cclasstrib: Mapped[str] = mapped_column(String(8), index=True) name: Mapped[str | None] = mapped_column(Text, nullable=True) description: Mapped[str | None] = mapped_column(Text, nullable=True) lc_redacao: Mapped[str | None] = mapped_column(Text, nullable=True) lc_214: Mapped[str | None] = mapped_column(Text, nullable=True) regulamento_cbs: Mapped[str | None] = mapped_column(Text, nullable=True) regulamento_ibs: Mapped[str | None] = mapped_column(Text, nullable=True) tipo_aliquota: Mapped[str | None] = mapped_column(String(64), nullable=True) p_red_ibs: Mapped[str | None] = mapped_column(String(32), nullable=True) p_red_cbs: Mapped[str | None] = mapped_column(String(32), nullable=True) d_ini_vig: Mapped[str | None] = mapped_column(String(32), nullable=True) d_fim_vig: Mapped[str | None] = mapped_column(String(32), nullable=True) data_atualizacao: Mapped[str | None] = mapped_column(String(32), nullable=True) anexo: Mapped[str | None] = mapped_column(Text, nullable=True) link: Mapped[str | None] = mapped_column(Text, nullable=True) indicators: Mapped[dict] = mapped_column(JSON) class RtcCbsRateEntry(Base): """RTC CBS rate per year (IT 2026.002). rate is NULL when the official text has no number yet (e.g. 'aguarda legislação').""" __tablename__ = "rtc_cbs_rate_entries" __table_args__ = (UniqueConstraint("dataset_id", "year", name="uq_rtc_cbs_rate_year"),) id: Mapped[int] = mapped_column(primary_key=True) dataset_id: Mapped[int] = mapped_column(ForeignKey("datasets.id"), index=True) year: Mapped[int] = mapped_column(Integer, index=True) rate_text: Mapped[str] = mapped_column(Text) rate: Mapped[float | None] = mapped_column(nullable=True) class FiscalContextEntry(Base): """Operator-supplied fiscal fact (product role, origin, ST retention, treatment, ...). Hierarchy: PRODUCT > GROUP > COMPANY.""" __tablename__ = "fiscal_context_entries" __table_args__ = (UniqueConstraint("tenant_id", "company_key", "scope", "scope_key", "key", name="uq_fiscal_context_entry"),) id: Mapped[int] = mapped_column(primary_key=True) tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id"), index=True) company_key: Mapped[str] = mapped_column(String(64), index=True) scope: Mapped[str] = mapped_column(String(8)) scope_key: Mapped[str] = mapped_column(String(64), default="", server_default="") key: Mapped[str] = mapped_column(String(48)) value: Mapped[str] = mapped_column(Text) source: Mapped[str] = mapped_column(String(16), default="OPERATOR", server_default="OPERATOR") reason: Mapped[str | None] = mapped_column(Text, nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())