aeat.domain.calculations.registry._ledger_bindings module¶
Ledger-backed registry binding helpers.
- class OssIossLedgerObservation(**data)[source]¶
Bases:
BaseModelOne factual ledger line tagged with substrate-grounded classification.
Modelo 369 binding selectors filter these observations by the four classification axes (regime, destination Member State, rate tier, invoice direction) plus the optional transaction kind set; the runtime aggregates the matched lines through the binding’s aggregation operator.
- Variables:
ledger_id – Stable id of the source ledger line.
transaction_date – When the supply takes place.
regime – OSS / IOSS Esquema the line is filed under.
destination_member_state – Member State of consumption (the destination MS for the supply, which determines the applicable IVA rate per the OSS / IOSS rules).
rate_kind – Substrate rate tier (general / reduced / etc.).
invoice_direction – Whether the autónomo issued or received the invoice.
transaction_kind – Substrate
aeat.domain.iva.TransactionKindthe line resolves to.base_amount – Taxable base in EUR.
iva_amount – IVA amount in EUR (already applied at the destination MS rate per OSS / IOSS rules).
- Parameters:
ledger_id (str)
transaction_date (date)
regime (OssIossRegime)
destination_member_state (EUMemberState)
rate_kind (IvaRateKind)
invoice_direction (InvoiceKind)
transaction_kind (TransactionKind)
base_amount (Decimal)
iva_amount (Decimal)
- ledger_id: str¶
- transaction_date: date¶
- regime: OssIossRegime¶
- destination_member_state: EUMemberState¶
- rate_kind: IvaRateKind¶
- invoice_direction: InvoiceKind¶
- transaction_kind: TransactionKind¶
- base_amount: Decimal¶
- iva_amount: Decimal¶
- validate_ledger_oss_aggregation_binding_definition(binding)[source]¶
Validate a
ledger_oss_aggregationbinding’s selector and aggregation.- Parameters:
binding (
DataBindingDefinition) – TheDataBindingDefinitionto validate. Must havesource == "ledger_oss_aggregation".- Raises:
RegistryValidationError – If the selector is malformed (unknown regime / member state / rate kind / invoice direction / transaction kind), or if the aggregation operator is inconsistent with the declared fact.
- Return type:
- resolve_ledger_oss_aggregation_binding_values(revision, observations)[source]¶
Resolve every
ledger_oss_aggregationbinding onrevision.For each binding, observations are filtered by the four classification axes plus the transaction-kind set; matched observations are aggregated through the binding’s declared fact (
iva_amount_sumdefaults;base_amount_sumselects the base). The resolver is deterministic and side-effect-free.- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose bindings to resolve.observations (
Iterable[OssIossLedgerObservation]) – Iterable of substrate-classified ledger lines.
- Return type:
- Returns:
Mapping of binding id to the aggregated Decimal value. Empty match sets resolve to
Decimal("0").
- unsupported_ledger_oss_observations(revision, observations)[source]¶
Return the
OssIossLedgerObservationrows no binding onrevisioncan consume.Fail-closed counterpart to
resolve_ledger_oss_aggregation_binding_values(), mirroringunsupported_ledger_iva_observations(). An observation whose regime/destination/rate/direction/transaction-kind tuple matches noledger_oss_aggregationbinding has its base/cuota silently dropped — a modelling gap, not a legitimate zero.False-fire guard: an observation carrying neither base nor IVA (both zero) contributes nothing whether or not it is routed and is excluded; only a non-zero declarable OSS line reaching no binding is surfaced.
- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose OSS bindings define the supported classification tuples.observations (
Iterable[OssIossLedgerObservation]) – Validated OSS/IOSS observations to screen.
- Return type:
- Returns:
Tuple of observations whose non-zero base/cuota is selected by no
ledger_oss_aggregationbinding.
- class IvaLedgerObservation(**data)[source]¶
Bases:
BaseModelOne factual ledger line tagged with the IVA classification triple.
Modelo 303 / 322 / 353 / 309 / 390 binding selectors filter these observations by category, rate kind, and flow direction; the runtime aggregates the matched lines through the binding’s aggregation operator.
- Variables:
ledger_id – Stable id of the source ledger line.
transaction_date – When the supply takes place.
category – Substrate
IvaCategoryresolved by the classifier.rate_kind – Substrate
IvaRateKindrate tier.flow_direction – Substrate
IvaFlowDirection(output / input / self-assessed reverse charge).base_amount – Taxable base in EUR.
iva_amount – IVA amount in EUR (cuota repercutida or soportada, depending on flow direction).
- Parameters:
ledger_id (str)
transaction_date (date)
category (IvaCategory)
exemption_article (IvaExemptionArticle | None)
rate_kind (IvaRateKind)
flow_direction (IvaFlowDirection)
base_amount (Decimal)
iva_amount (Decimal)
recargo_amount (Decimal)
prorrata_reference_id (str | None)
cash_accounting_treatment (IvaCashAccountingTreatment)
input_classification (InputClassification | None)
prorrata_sector_id (str | None)
- ledger_id: str¶
- transaction_date: date¶
- category: IvaCategory¶
- exemption_article: IvaExemptionArticle | None¶
- rate_kind: IvaRateKind¶
- flow_direction: IvaFlowDirection¶
- base_amount: Decimal¶
- iva_amount: Decimal¶
- recargo_amount: Decimal¶
Recargo de equivalencia cuota the supplier charged on a repercutido sale to a recargo-regime retailer, in EUR.
Decimal("0")on every line that carries no recargo. Modelo 303 recargo cuota casillas select these via therecargo_amount_sumfact, routed by the line’s IVA rate tier.
- prorrata_reference_id: str | None¶
Stable id of the linked
ProrrataLedgerReferencerow, when set.Populated by the aggregator only on
SOPORTADO(input IVA) flows that carry a validated prorrata reference. Downstream Modelo 303 / 390 binding selectors filter prorrata-linked observations without a manual join against the parallelprorrata_referencestuple.
- cash_accounting_treatment: IvaCashAccountingTreatment¶
Independent criterio-de-caja axis.
Settlement observations use
NONEand therefore flow through the normal Modelo 303 base/cuota bindings. Informational observations for boxes 62/63/74/75 carry the actual cash-accounting treatment so ordinary domestic rows cannot be confused with art. 75 projections.
- input_classification: InputClassification | None¶
Operator-declared LIVA art. 106 prorrata-especial per-input use class.
Carried from the source ledger transaction’s
input_classification. Meaningful only forSOPORTADO(input IVA) rows in a bucket under prorrata especial: the regime-aware ledger IVA apportionment routes the deducible cuota by this classification (the art. 106.Uno reglas 100%/0%/general).Nonefor every row not under especial or carrying no per-input use declaration; the general-regime apportionment ignores it.
- prorrata_sector_id: str | None¶
Operator-declared LIVA arts. 9.1.c / 101 differentiated sector.
Carried from the source ledger transaction’s
prorrata_sector_id. Meaningful only forSOPORTADO(input IVA) rows in a sectorized bucket: the sector-aware ledger IVA apportionment applies THAT sector’s provisional percentage to the deducible cuota.Noneis a common-use input in a sectorized bucket (apportioned by the art. 104.Dos common percentage) and the whole-entity default otherwise; the non-sectorized apportionment ignores it.
- validate_ledger_iva_aggregation_binding_definition(binding)[source]¶
Validate a
ledger_iva_aggregationbinding’s selector and aggregation.- Parameters:
binding (
DataBindingDefinition) – TheDataBindingDefinitionto validate. Must havesource == "ledger_iva_aggregation".- Raises:
RegistryValidationError – If the selector is malformed (unknown category / rate kind / flow direction / fact, empty tuple), if the aggregation operator is not “sum”, or if the binding source is not “ledger_iva_aggregation”.
- Return type:
- resolve_ledger_iva_aggregation_binding_values(revision, observations)[source]¶
Resolve every
ledger_iva_aggregationbinding onrevision.Filters observations by the three classification axes (category in selector.categories, rate_kind in selector.rate_kinds, flow_direction matches selector.flow_direction) and aggregates the matched lines’ iva_amount or base_amount per the declared fact.
- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose bindings to resolve.observations (
Iterable[IvaLedgerObservation]) – Iterable of substrate-classified ledger lines.
- Return type:
- Returns:
Mapping of binding id to the aggregated Decimal value. Empty match sets resolve to
Decimal("0").
- unsupported_ledger_iva_observations(revision, observations)[source]¶
Return IVA observations no binding on
revisioncan consume.- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose bindings define the supported IVA classification triples.observations (
Iterable[IvaLedgerObservation]) – Ledger lines to screen.
- Return type:
- Returns:
Tuple of
IvaLedgerObservationinstances not matched by any binding.
This is the fail-closed counterpart to
resolve_ledger_iva_aggregation_binding_values(). Empty match sets on supported bindings still resolve to zero, but a concrete observation whose category/rate/flow triple is not selected by anyledger_iva_aggregationbinding is a modelling gap and must not be silently inferred into an annual or periodic form.Categories that bear no Modelo 303 cuota by law (
CUOTA_LESS_M303_IVA_CATEGORIES— exempt, zero-rated, not-subject, exempt intra-community supplies/exports, triangulation, and régimen simplificado) are excluded: they correctly match no cuota binding, so flagging them would be a false positive. After the M303 routing tail (domestic / intra-community reverse-charge bindings, the import deducible binding) landed, every cuota-bearing declarable category has a consuming binding, so the residual unsupported set is empty for the known declarable categories; the function still fail-closes on any new declarable triple that no binding selects.
- class RentaExpenseObservationProtocol(*args, **kwargs)[source]¶
Bases:
ProtocolStructural protocol for first-slice Renta expense observations.
The registry only needs these four attributes to resolve
ledger_renta_expense_aggregationbindings; the fullRentaDeductibleExpenseObservationsatisfies this protocol without any explicit declaration.Properties are declared read-only so that Literal-typed concrete attributes (e.g.
modelo: Literal[Modelo.M100]) satisfy the protocol under strict covariant checking.- property target_casilla_id: CasillaId¶
- validate_ledger_renta_expense_aggregation_binding_definition(binding)[source]¶
Validate a
ledger_renta_expense_aggregationbinding definition.- Return type:
- Parameters:
binding (DataBindingDefinition)
- resolve_ledger_renta_expense_aggregation_binding_values(revision, observations)[source]¶
Resolve every
ledger_renta_expense_aggregationbinding onrevision.- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose renta-expense bindings to resolve.observations (
Iterable[RentaExpenseObservationProtocol]) – Typed renta-expense observations the bindings aggregate via their declaredselector.factandaggregation.op.
- Return type:
- unsupported_ledger_renta_expense_observations(revision, observations)[source]¶
Return the
RentaExpenseObservationProtocolrows no binding onrevisioncan consume.Fail-closed counterpart to
resolve_ledger_renta_expense_aggregation_binding_values(), mirroringunsupported_ledger_iva_observations(). An observation whose modelo/period/target_casilla_id triple matches noledger_renta_expense_aggregationbinding has its deductible amount silently dropped from the filing — a modelling gap, not a legitimate zero.False-fire guard (
ledger-iva-advisory-only-on-cuota-bearing-categoriesprecedent): an observation that carries a zerodeductible_amountcontributes nothing whether or not it is routed, so it is excluded — only a non-zero declarable expense that reaches no casilla is surfaced.- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose renta-expense bindings define the supported (modelo, period, target_casilla_id) triples.observations (
Iterable[RentaExpenseObservationProtocol]) – First-slice renta-expense observations to screen.
- Return type:
- Returns:
Tuple of observations whose non-zero deductible amount is selected by no
ledger_renta_expense_aggregationbinding.
- renta_first_slice_binding_target_casillas(revision)[source]¶
Return the
target_casilla_idset this revision’s own bindings route to.Unlike
aeat.domain.renta._first_slice_routing.FIRST_SLICE_EXPENSE_CASILLAS(the universal BOE-prescribed routing table spanning every filing year the application supports), this returns only the casillas aledger_renta_expense_aggregationbinding on THIS revision actually targets. Older Modelo 100 revisions (2020-2023) declare no such bindings at all – the first-slice ledger-aggregation mechanism did not yet exist for them – so their required set is legitimately empty even though the universal routing table’s codomain is wider. The snapshot-time referential-integrity gate (aeat.domain.renta._first_slice_routing_integrity) uses this per-revision set rather than the universal table so it only fails when a binding THIS revision actually declares points at a casilla absent from that same revision – the real defect class the gate exists to catch, not “does every filing year’s estimación directa casilla exist on every other filing year’s revision” (it does not, by BOE design: casillas are added, split, and renumbered across years).- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose ownledger_renta_expense_aggregationbinding selectors are inspected.- Return type:
frozenset[TypeAliasType]
- validate_ledger_renta_income_aggregation_binding_definition(binding)[source]¶
Validate a
ledger_renta_income_aggregationbinding definition.- Return type:
- Parameters:
binding (DataBindingDefinition)
- class RentaIncomeObservationProtocol(*args, **kwargs)[source]¶
Bases:
ProtocolStructural protocol for actividad-económica income observations.
The registry only needs these attributes to resolve
ledger_renta_income_aggregationbindings; the fullRentaIncomeObservationsatisfies this protocol without any explicit declaration.- property target_casilla_id: CasillaId¶
- resolve_ledger_renta_income_aggregation_binding_values(revision, observations)[source]¶
Resolve every
ledger_renta_income_aggregationbinding onrevision.The
factdeclared in the binding selector controls which field is summed:"ingresos_integros_sum"→observation.taxable_base_amountwhen declared, elseobservation.gross_amount(per-observation fallback);"gross_income_sum"→observation.gross_amount;"taxable_base_sum"→observation.taxable_base_amount(zero whenNone);"withheld_amount_sum"→observation.withheld_amount.- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose bindings are resolved.observations (
Iterable[RentaIncomeObservationProtocol]) – Renta income ledger lines to aggregate over.
- Return type:
- unsupported_ledger_renta_income_observations(revision, observations)[source]¶
Return the
RentaIncomeObservationProtocolrows no binding onrevisioncan consume.Fail-closed counterpart to
resolve_ledger_renta_income_aggregation_binding_values(), mirroringunsupported_ledger_iva_observations(). An observation whosetarget_casilla_idmatches noledger_renta_income_aggregationbinding has its income silently dropped from the filing — a modelling gap, not a legitimate zero.False-fire guard: an observation whose declarable income is zero (both
gross_amountand any declaredtaxable_base_amountare zero) contributes nothing whether or not it is routed and is excluded; only a non-zero income reaching no casilla is surfaced.- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose renta-income bindings define the supportedtarget_casilla_idset.observations (
Iterable[RentaIncomeObservationProtocol]) – Actividad-económica income observations to screen.
- Return type:
- Returns:
Tuple of observations whose non-zero income is selected by no
ledger_renta_income_aggregationbinding.
- validate_ledger_impatriado_income_aggregation_binding_definition(binding)[source]¶
Validate a
ledger_impatriado_income_aggregationbinding definition.- Return type:
- Parameters:
binding (DataBindingDefinition)
- validate_ledger_impatriado_income_aggregation_binding(binding)[source]¶
Validate a
ledger_impatriado_income_aggregationbinding at registry-build time.Accumulating
list[str]validator over_ImpatriadoLedgerIncomeSelector; lifts the casilla / fact / aggregation-op invariant via the raise-stylevalidate_ledger_impatriado_income_aggregation_binding_definition().- Return type:
- Parameters:
binding (DataBindingDefinition)
- class ImpatriadoIncomeObservationProtocol(*args, **kwargs)[source]¶
Bases:
ProtocolStructural protocol for Modelo 151 impatriado Spanish-source income observations.
The registry only needs these attributes to resolve
ledger_impatriado_income_aggregationbindings; the fullImpatriadoIncomeObservationsatisfies this protocol without any explicit declaration.- property target_casilla_id: CasillaId¶
- resolve_ledger_impatriado_income_aggregation_binding_values(revision, observations)[source]¶
Resolve every
ledger_impatriado_income_aggregationbinding onrevision.The
factdeclared in the binding selector controls which field is summed:"ingresos_integros_sum"→observation.taxable_base_amountwhen declared, elseobservation.gross_amount;"gross_income_sum"→observation.gross_amount. Only ES-scoped observations reach this resolver; the source-scope segregation is owned by the application classifier.- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose bindings are resolved.observations (
Iterable[ImpatriadoIncomeObservationProtocol]) – ES-scoped impatriado income ledger lines to aggregate over.
- Return type:
- unsupported_ledger_impatriado_income_observations(revision, observations)[source]¶
Return ES-scoped impatriado observations no binding on
revisioncan consume.The
ModeloRevisionsupplies theledger_impatriado_income_aggregationbinding selectors that define which impatriado base casillas are supported.Fail-closed counterpart to
resolve_ledger_impatriado_income_aggregation_binding_values(). An ES-source observation whosetarget_casilla_idmatches noledger_impatriado_income_aggregationbinding would otherwise have its income silently dropped from the impatriado base — a modelling gap, not a legitimate zero. A zero-income observation contributes nothing and is excluded.- Return type:
- Returns:
The unsupported
ImpatriadoIncomeObservationProtocolrows, in input order.- Parameters:
revision (ModeloRevision)
observations (Iterable[ImpatriadoIncomeObservationProtocol])
- class RentaGastoObservationProtocol(*args, **kwargs)[source]¶
Bases:
ProtocolStructural protocol for M130 deductible-expense (gasto) observations.
The registry only needs these two attributes to resolve
ledger_renta_gasto_aggregationbindings; the fullRentaGastoObservationsatisfies this protocol without any explicit declaration. MirrorsRentaIncomeObservationProtocolfor the expense dimension.- property target_casilla_id: CasillaId¶
- validate_ledger_renta_gasto_aggregation_binding_definition(binding)[source]¶
Validate a
ledger_renta_gasto_aggregationbinding definition.- Return type:
- Parameters:
binding (DataBindingDefinition)
- resolve_ledger_renta_gasto_aggregation_binding_values(revision, observations)[source]¶
Resolve every
ledger_renta_gasto_aggregationbinding onrevision.Matches observations by
target_casilla_idand sums theirdeductible_amount, mirroring the income resolver’s casilla-keyed fold.- Parameters:
revision (
ModeloRevision) – TheModeloRevisionwhose gasto bindings are resolved.observations (
Iterable[RentaGastoObservationProtocol]) – M130 deductible-expense observations to aggregate over.
- Return type:
- unsupported_ledger_renta_gasto_observations(revision, observations)[source]¶
Return the gasto observations no binding on the
ModeloRevisionrevisioncan consume.Fail-closed counterpart to
resolve_ledger_renta_gasto_aggregation_binding_values(), mirroringunsupported_ledger_renta_income_observations(). An observation whosetarget_casilla_idmatches noledger_renta_gasto_aggregationbinding has its deductible expense silently dropped — a modelling gap, not a legitimate zero (no-silent-under-declaration).False-fire guard: a zero-
deductible_amountobservation contributes nothing whether or not it is routed and is excluded; only a non-zero declarable gasto reaching no casilla is surfaced.- Return type:
- Returns:
Unsupported
RentaGastoObservationProtocolobservations.- Parameters:
revision (ModeloRevision)
observations (Iterable[RentaGastoObservationProtocol])
- validate_ledger_oss_aggregation_binding(binding)[source]¶
Validate a
ledger_oss_aggregationbinding at registry-build time.Accumulating
list[str]validator: validates the selector shape against_OssIossLedgerSelector(preserving the underlying pydantic field error) then lifts the fact/aggregation-op invariant to build time via the raise-stylevalidate_ledger_oss_aggregation_binding_definition(), which stays as a defence-in-depth resolve-time re-check.- Return type:
- Parameters:
binding (DataBindingDefinition)
- validate_ledger_iva_aggregation_binding(binding)[source]¶
Validate a
ledger_iva_aggregationbinding at registry-build time.Accumulating
list[str]validator over_IvaLedgerSelector; lifts the fact/aggregation-op invariant via the raise-stylevalidate_ledger_iva_aggregation_binding_definition().- Return type:
- Parameters:
binding (DataBindingDefinition)
- validate_ledger_renta_expense_aggregation_binding(binding)[source]¶
Validate a
ledger_renta_expense_aggregationbinding at registry-build time.Accumulating
list[str]validator over_RentaLedgerExpenseSelector; lifts the fact/aggregation-op invariant via the raise-stylevalidate_ledger_renta_expense_aggregation_binding_definition().- Return type:
- Parameters:
binding (DataBindingDefinition)
- validate_ledger_renta_income_aggregation_binding(binding)[source]¶
Validate a
ledger_renta_income_aggregationbinding at registry-build time.Accumulating
list[str]validator over_RentaLedgerIncomeSelector; lifts the fact/aggregation-op invariant via the raise-stylevalidate_ledger_renta_income_aggregation_binding_definition().- Return type:
- Parameters:
binding (DataBindingDefinition)
- validate_ledger_renta_gasto_aggregation_binding(binding)[source]¶
Validate a
ledger_renta_gasto_aggregationbinding at registry-build time.Accumulating
list[str]validator over_RentaLedgerGastoSelector; lifts the casilla / fact / aggregation-op invariant via the raise-stylevalidate_ledger_renta_gasto_aggregation_binding_definition().- Return type:
- Parameters:
binding (DataBindingDefinition)