cadrumo.application.live.justificante module¶
Application-live persistence for captured AEAT justificante receipts.
The live justificante pull retrieves the authentic, AEAT-signed
justificante de presentación PDF for a filed work unit through the
read-only sede surface (capture_justificante →
SedeCapture) and persists it
as a bucket-scoped, content-addressed secure object. The persisted
artefact is the durable, official evidence the local reconciler reads —
the operator no longer hand-downloads the receipt.
This service is a stateful SnapshotService sibling of the
Modelo 100 borrador service: it keys supersession on the
(modelo, filing_year, period) axis so a re-filed period’s fresh
capture supersedes the prior ACTIVE one, and it persists each snapshot
through a SecureObjectRepository
at FINANCIAL sensitivity under
cadrumo.adapters.persistence.storage.secure_object_namespaces.LIVE_JUSTIFICANTE_CAPTURE_SNAPSHOT_NAMESPACE,
and records each capture as a lifecycle event via
BucketEventHistoryRepository.
The captured PDF bytes ride inside the encrypted snapshot Envelope
as a base64 str (binary cannot survive the JSON envelope verbatim); the
raw-bytes pdf_sha256 is the content address used for snapshot-id
derivation and dedup.
See also
cadrumo.application.livePublic read-only live facade that orchestrates capture and reports
JustificanteCaptureOutcome.cadrumo.application.live.filed_observation_persistence.enroll_filed_justificante_evidence()Filed-history path that performs the same metadata registration and current-record evidence stamping from declaration-register artefacts.
cadrumo.application.overviewCalendar projection that reads
JustificanteCaptureSnapshotrows and matching domain justificante metadata as AEAT-side evidence.cadrumo.adapters.persistence.profile.snapshots.SecureSnapshotRepositoryShared encrypted snapshot repository used by this bucket-scoped capture repository.
ModeloRecordLocal filing record stamped with live
ExternalEvidenceonly after the receipt matches the current filing axis.
- class JustificanteAuthenticity(*values)[source]¶
Bases:
StrEnumVerdict of the read-only AEAT cotejo check on a captured receipt.
The four members are distinct states and never collapse into a boolean.
NOT_CHECKEDis the default a capture carries until the cotejo viewer has been consulted;UNAVAILABLErecords that the check was attempted and could not be completed. Neither is a denial, and neither may be read as one. OnlyCONFIRMEDmeans AEAT rendered the CSV-bound document viewer for this receipt, and onlyDENIEDmeans AEAT answered that it holds no such document.A verdict is advisory provenance about the captured PDF. It says nothing about the correctness of the declared values and must never be promoted into a filing-grade figure by a downstream consumer.
- NOT_CHECKED¶
- CONFIRMED¶
- DENIED¶
- UNAVAILABLE¶
- exception JustificanteCaptureSnapshotNotFoundError(message=None, *, context=None, translated_message=None)[source]¶
Bases:
SnapshotNotFoundErrorRaised when a live justificante-capture snapshot lookup misses by id.
- Parameters:
- Return type:
None
- code¶
- class JustificanteCaptureSnapshot(**data)[source]¶
Bases:
BaseModelOne live-captured AEAT justificante receipt persisted for a bucket.
Bundles the work-unit axis (
modelo/filing_year/period), the sede provenance (expediente_id/csv), the content address (pdf_sha256over the raw bytes), and the receipt itself (pdf_base64). The officialsource_kindrecords that the receipt came from an authenticated live capture.- Parameters:
data (Any)
- snapshot_id: SnapshotId¶
- bucket_id: BucketId¶
- modelo: str¶
- filing_year: FilingYear¶
- period: Period¶
- expediente_id: AeatExpedienteId¶
- csv: AeatCsv¶
- pdf_sha256: ContentDigest¶
- pdf_base64: str¶
- source_kind: ObservationSourceKind¶
- authenticity: JustificanteAuthenticity¶
- captured_at: datetime¶
- state: SnapshotLifecycleState¶
- superseded_by_snapshot_id: SnapshotId | None¶
- discarded_at: datetime | None¶
- discarded_by: str¶
- discard_reason: str¶
- decoded_pdf_bytes()[source]¶
Return the raw PDF bytes decoded from
pdf_base64.- Return type:
- justificante_capture_snapshot_object_key(bucket_id, snapshot_id)[source]¶
Return the secure-object key for one bucket’s justificante-capture snapshot.
The key shape is the object-key grammar declared by
cadrumo.adapters.persistence.storage.secure_object_namespaces.LIVE_JUSTIFICANTE_CAPTURE_SNAPSHOT_NAMESPACE.
- derive_justificante_capture_snapshot_id(*, modelo, filing_year, period, pdf_sha256)[source]¶
Return the content-addressed id for one justificante capture.
The id is derived from the work-unit axis plus the raw-PDF content address, so re-capturing the identical receipt is idempotent while a re-filed period (a different signed PDF) yields a new id that supersedes the prior ACTIVE snapshot on the same axis.
- resolve_period_expediente(*, declarations, expedientes, modelo, period)[source]¶
Resolve the capturable expediente for one
(modelo, period)filing.The procedure-tree
Expedientecarries no period, so for a multi-period modelo (quarterly 1T-4T) it cannot disambiguate which quarter’s receipt to pull. The period-bearing surface is the filed declarations register (Declaracioncarriesperiodandexpediente_id). This resolver picks the declaration matching the target(modelo, period)(the latest filing for that period when a period was re-filed), then cross-references itsexpediente_idagainst the tree to return the capturable expediente. It NEVER returns a different period’s expediente: a missing or unmatched declaration raises rather than falling back to a wrong-quarter receipt.The within-period tiebreak ranks the accepted (
ALTA) declaration ahead ofpresented_at, matching the two sibling period-resolution surfaces (latest_declarations_by_periodand the sede walker’s latest-selection), so a later cancellation / correction row (a non-ALTAestadosuch asAnuladaorBaja) presented after the accepted filing does not win and pull the wrong-state receipt.- Raises:
LiveApplicationInputError – when no declaration matches the requested period, or the matched declaration’s expediente is absent from the tree.
- Return type:
- Parameters:
declarations (Sequence[JustificanteDeclaration])
expedientes (Sequence[JustificanteExpediente])
modelo (str)
period (Period)
- class JustificanteCaptureSnapshotRepository(*, persistence)[source]¶
Bases:
objectSecure-DB repository for captured justificante snapshots in one bucket.
Composes the shared
SecureSnapshotRepositoryfor the read surface (load / resolve / list / exists), preserving the class identity,JustificanteCaptureSnapshotNotFoundErrormessages, and thecaptured_atlist ordering.saveis kept local because justificante stamps the envelopewritten_atwith the capture time (notnow()), a deliberate divergence from the shared base.The namespace, sensitivity, schema version, and key grammar come from
cadrumo.adapters.persistence.storage.secure_object_namespaces.LIVE_JUSTIFICANTE_CAPTURE_SNAPSHOT_NAMESPACE. EachJustificanteCaptureSnapshotis written through anEnvelopeso the captured PDF, CSV, and expediente metadata stay inside the encryptedSecureObjectRepositorybucket store.- Parameters:
persistence (JustificanteSnapshotPersistencePort)
- load(snapshot_id)[source]¶
Execute this public contract operation.
- Return type:
- Parameters:
snapshot_id (str)
- resolve(snapshot_id)[source]¶
Execute this public contract operation.
- Return type:
- Parameters:
snapshot_id (str)
- save(snapshot)[source]¶
Execute this public contract operation.
- Return type:
- Parameters:
snapshot (JustificanteCaptureSnapshot)
- class JustificanteCaptureSnapshotService(*, bucket_id, repository)[source]¶
Bases:
SnapshotService[JustificanteCaptureSnapshot,_JustificanteCaptureRequest]Canonical backend service for bucket-scoped live justificante captures.
- Parameters:
bucket_id (str)
repository (JustificanteCaptureSnapshotRepository)
- capture(*, modelo, filing_year, period, expediente_id, csv, pdf_bytes, pdf_sha256, captured_at)[source]¶
Persist one captured justificante and return the ACTIVE
JustificanteCaptureSnapshot.Idempotent on the receipt content address: re-capturing the same signed PDF returns the existing snapshot. A re-filed period (a new signed PDF) supersedes the prior ACTIVE snapshot on the
(modelo, filing_year, period)axis.
- list_snapshots(*, filing_year=None, state=SnapshotLifecycleState.ACTIVE)[source]¶
Execute this public contract operation.
- Return type:
- Parameters:
filing_year (int | None)
state (SnapshotLifecycleState | None)
- stamp_authenticity(*, snapshot, authenticity)[source]¶
Persist
authenticityontosnapshotand return the stamped record.The verdict rides the same encrypted snapshot envelope as the receipt it describes, so no plaintext copy of the capture leaves the secure store. Re-stamping is idempotent for an unchanged verdict and overwrites in place otherwise: the cotejo answer is a current-state fact about the document, not an append-only ledger of attempts.
- Return type:
- Parameters:
snapshot (JustificanteCaptureSnapshot)
authenticity (JustificanteAuthenticity)
- parse_capture_to_justificante(snapshot, *, ports)[source]¶
Parse a persisted capture’s PDF into a strict domain
Justificante.Reads the encrypted snapshot’s bytes in memory and runs the inbound parser. Used to register the captured receipt as official filing evidence and to reconcile against it.
- Return type:
- Parameters:
snapshot (JustificanteCaptureSnapshot)
ports (JustificanteRegistrationPorts)
- register_capture_justificante_metadata(*, snapshot, ports)[source]¶
Persist parsed justificante metadata for a live capture.
This records the official AEAT receipt metadata even when the local app has no current
ModeloRecordfor the period. It does not stamp or create a local filing row; that remains owned byregister_capture_as_filing_evidence().Returns the persisted
Justificante, orNonewhen the captured snapshot cannot be parsed into one.- Return type:
- Parameters:
snapshot (JustificanteCaptureSnapshot)
ports (JustificanteRegistrationPorts)
- reconcile_capture(*, work_unit_id, snapshot, actor='operator', operation)[source]¶
Reconcile a work unit against a persisted live capture.
Reads the captured PDF from secure storage into memory and delegates to the existing local-only reconciler; the reconciler never contacts AEAT and never writes the plaintext receipt to disk. This is the live-sourced equivalent of the operator hand-passing a downloaded justificante via the local
reconcile import --file PATHsurface.- Return type:
- Returns:
A
ModeloReconciliationReportfor the in-memory receipt comparison.- Parameters:
work_unit_id (str)
snapshot (JustificanteCaptureSnapshot)
actor (str)
operation (PinnedAuthorityOperation)
- register_capture_as_filing_evidence(*, snapshot, ports)[source]¶
Confirm the period’s filing with a persisted live capture of its receipt.
Loads the period’s current filing record first; with one present, parses the captured receipt into a domain
Justificante(keyed by the capture’s CSV, which is the gate’s evidence reference), registers it, and reconciles the filing chain with it throughreconcile_aeat_register_entry(). A receipt carries only totals, so a pending filing is confirmed only when its computed result matches them. Once confirmed, the cross-period clean-state gate’sMISSING_JUSTIFICANTE_VERIFICATIONblocker clears for the period.Returns the confirmed
ModeloRecord.- Raises:
LiveApplicationInputError – when no current filing record exists for the captured
(modelo, filing_year, period), when the receipt does not match it, or when the reconciliation does not confirm it.- Return type:
- Parameters:
snapshot (JustificanteCaptureSnapshot)
ports (JustificanteRegistrationPorts)
- expected_tax_id_for_filing_record(filing)¶
- Return type:
- Parameters:
filing (ModeloRecord)
- stamp_capture_evidence_if_filed(snapshot, *, ports)[source]¶
Best-effort filing-evidence registration for an already persisted capture.
- Return type:
- Parameters:
snapshot (JustificanteCaptureSnapshot)
ports (JustificanteRegistrationPorts)
- async verify_capture_authenticity(*, snapshot, service, verifier, browser=None, browser_session_factory=None)[source]¶
Ask AEAT’s public cotejo viewer about
snapshotand stamp the verdict.Runs the read-only
verify_csv()round trip against the capture’s own CSV and persists the resultingJustificanteAuthenticityon the snapshot throughservice.The three answers stay distinct. AEAT rendering the CSV-bound document viewer is
JustificanteAuthenticity.CONFIRMED; AEAT answering that it holds no such document isJustificanteAuthenticity.DENIED; a round trip that could not be completed at all — no browser, a navigation failure, a guard refusal — isJustificanteAuthenticity.UNAVAILABLE, which is emphatically not a denial and must never be reported as one. The capture itself is already persisted before this runs, so an unreachable cotejo surface downgrades the provenance available for the receipt without losing the receipt.- Parameters:
snapshot (
JustificanteCaptureSnapshot) – The already-persisted capture whose CSV is checked.service (
JustificanteCaptureSnapshotService) – Snapshot service owningsnapshot’s bucket; it performs the encrypted write of the stamped record.verifier (
JustificanteAuthenticityVerifierPort) – Port that submits the capture CSV to the authenticity surface.browser (
object|None) – An already-constructed browser session to borrow. WhenNone, the verifier builds and closes its own.browser_session_factory (
Callable[[],object] |None) – Optional no-argument factory for the self-owned verifier path. Ignored whenbrowseris supplied.
- Return type:
- Returns:
The stamped
JustificanteCaptureSnapshot.
- class JustificanteCaptureOutcome(snapshot, justificante, filing_record)[source]¶
Bases:
objectOutcome of one live justificante capture and local evidence enrolment.
- Parameters:
snapshot (JustificanteCaptureSnapshot)
justificante (Justificante | None)
filing_record (ModeloRecord | None)
-
snapshot:
JustificanteCaptureSnapshot¶
-
justificante:
Justificante|None¶
-
filing_record:
ModeloRecord|None¶
- property justificante_metadata_registered: bool¶
Return whether the captured receipt yielded stored justificante metadata.
- async capture_justificante_snapshot(*, bucket_id, modelo, year, period, service, read_port, registration_ports, verifier)[source]¶
Capture and persist the official justificante for one filed work unit.
- Return type:
- Parameters:
bucket_id (str)
modelo (str)
year (int)
period (Period)
service (JustificanteCaptureSnapshotService)
read_port (JustificanteLiveReadPort)
registration_ports (JustificanteRegistrationPorts)
verifier (JustificanteAuthenticityVerifierPort)
- async capture_justificante_snapshot_outcome(*, bucket_id, modelo, year, period, service, read_port, registration_ports, verifier)[source]¶
Capture a justificante and report the separate metadata and filing-evidence outcomes.
- Return type:
- Parameters:
bucket_id (str)
modelo (str)
year (int)
period (Period)
service (JustificanteCaptureSnapshotService)
read_port (JustificanteLiveReadPort)
registration_ports (JustificanteRegistrationPorts)
verifier (JustificanteAuthenticityVerifierPort)