Source code for aeat.application.storage.calc_sheets._evidence

"""Evidence adapters for calc-sheets workbook export plans.

:func:`sheet_evidence_from_ledger_filing` projects
:class:`LedgerFilingEvidence` into a :class:`SheetEvidenceFacet` made of
:class:`SheetEvidenceContributorRow` and :class:`SheetEvidenceManualEntry`
records for the workbook evidence surface.

The adapter is deliberately attribution-driven. Ledger evidence names the
contributing transactions and manual facts; the caller supplies the mapping from
each contributor to the canonical :class:`CasillaId` values it supports so this
module never guesses modelo-specific tax meaning from row contents.

See Also:
    :class:`aeat.domain.modelos._ledger_filing_snapshot.LedgerFilingEvidence`
        Bundled fact basis attached to a ledger-derived calculation revision.
    :class:`SheetEvidenceFacet`
        Workbook-plan evidence facet rendered into the Evidencia tab and JSON
        sidecar.
"""

from __future__ import annotations

from collections.abc import Iterable, Mapping

from ....domain.calculations.registry import CasillaId
from ....domain.modelos import LedgerFilingEvidence
from ._errors import CalcSheetsEngineError
from ._records import SheetEvidenceContributorRow, SheetEvidenceFacet, SheetEvidenceManualEntry


[docs] def sheet_evidence_from_ledger_filing( evidence: LedgerFilingEvidence, *, casilla_ids_by_contributor_id: Mapping[str, Iterable[CasillaId]], ) -> SheetEvidenceFacet: """Project bundled filing evidence into the per-casilla workbook facet. :class:`LedgerFilingEvidence` is contributor-oriented; the workbook evidence surface is casilla-oriented. The caller must therefore provide the generic attribution map from each contributor transaction id to the canonical :class:`CasillaId` values it supports. Missing attribution raises :class:`CalcSheetsEngineError` instead of being guessed from modelo-specific tax facts. Returns: :class:`SheetEvidenceFacet`: The projected evidence facet. """ contributor_rows: list[SheetEvidenceContributorRow] = [] for row in evidence.rows: contributor_casilla_ids = tuple(casilla_ids_by_contributor_id.get(row.transaction_id, ())) if not contributor_casilla_ids: raise CalcSheetsEngineError( f"ledger filing evidence contributor {row.transaction_id!r} has no workbook casilla attribution", ) for casilla_id in contributor_casilla_ids: contributor_rows.append( SheetEvidenceContributorRow( casilla_id=casilla_id, transaction_id=row.transaction_id, amount=row.amount, currency=row.currency, taxable_base=row.taxable_base, iva_rate=row.iva_rate, iva_amount=row.iva_amount, counterparty=row.counterparty, attachment_ids=row.attachment_ids, document_link_ids=row.document_link_ids, legal_refs=row.legal_refs, source_refs=row.source_refs, ), ) manual_entries = tuple( SheetEvidenceManualEntry( casilla_id=entry.casilla_id, value=entry.value, kind=entry.kind, note=entry.note, legal_refs=entry.legal_refs, source_refs=entry.source_refs, ) for entry in evidence.manual_entries ) return SheetEvidenceFacet( snapshot_fingerprint=evidence.snapshot_fingerprint, contributor_rows=tuple(contributor_rows), manual_entries=manual_entries, )
__all__ = ["sheet_evidence_from_ledger_filing"]