aeat.application.aggregation._renta_gasto_ledger module

Repository-backed Renta deductible-expense (gasto) aggregation for Modelo 130.

Loads ledger rows through TransactionCatalogueRepository.

Used by: _modelo_bindings (source mesh) for Modelo 130 casilla 02 (“Gastos”) aggregation.

This is the OUTGOING sibling of _renta_income_ledger. Where the income pipeline accumulates professional-activity revenue into casilla 01, this pipeline accumulates deductible business expenses into casilla 02 over the same cumulative year-to-date quarterly window (RD 439/2007 art. 110.2). The two pipelines share the lightweight ledger-projection mechanism and the cumulative window; they differ only in flow direction and the casilla they feed.

Cumulative window rule (RD 439/2007 art. 110.2):

For period Qn in year Y the window is [Jan 1, Y] through [last day of Qn, Y].

Only ACTIVE, EUR-denominated, OUTGOING transactions whose explicit irpf_category marks actividad_economica or whose business_classification is BUSINESS or MIXED are eligible. The deductible amount is the IVA-exclusive base imponible (taxable_base). IVA soportado is recovered through Modelo 303 and is not a Renta gasto, so a declarable expense without taxable_base is surfaced as missing_taxable_base instead of being gross-folded into casilla 02. A MIXED transaction contributes its business fraction.

This module deliberately does NOT reuse the Modelo 100 first-slice expense pipeline (_renta_ledger): that path layers invoice-evidence reconciliation, category-profile deductibility evaluation, and an annual window that are constraint-shape-divergent from the M130 quarterly cumulative gasto sum.

class RentaGastoLedgerAggregationIssueReason(*values)[source]

Bases: StrEnum

Machine-readable reasons why a ledger row did not produce a gasto observation.

Only reasons that represent a dropped declarable gasto — a BUSINESS / MIXED expense excluded by a downstream gate — are modelled, so every emitted issue is a genuine no-silent-under-declaration signal. INCOMING and PERSONAL / unclassified OUTGOING rows are skipped silently (they are not deductible gastos) and never produce an issue.

UNSUPPORTED_CURRENCY
OUTSIDE_PERIOD
MISSING_TAXABLE_BASE
class RentaGastoLedgerAggregationIssue(**data)[source]

Bases: BaseModel

Traceable exclusion emitted while aggregating gasto ledger rows.

Parameters:
transaction_id: str
reason: RentaGastoLedgerAggregationIssueReason
detail: str
class RentaGastoObservation(**data)[source]

Bases: BaseModel

One eligible OUTGOING deductible-expense ledger row.

Carries the typed deductible amount and the target casilla id it feeds. The domain registry resolver matches target_casilla_id against the binding selector and sums deductible_amount across all observations for that casilla, mirroring the income resolver’s casilla-keyed fold.

deductible_amount is the IVA-exclusive base imponible (transaction.taxable_base) when the row carries an explicit IVA tagging, falling back to the gross transfer amount when no base is declared, and scaled by the business fraction for MIXED transactions.

Parameters:
  • transaction_id (str)

  • target_casilla_id (CasillaId)

  • deductible_amount (Decimal)

  • filing_date (date)

transaction_id: str
target_casilla_id: CasillaId
deductible_amount: Decimal
filing_date: date
class RentaGastoLedgerAggregation(**data)[source]

Bases: BaseModel

Cumulative deductible-expense observations for one M130 quarter window.

out_of_window_summary is populated by repository-backed date partitions. Full-catalogue aggregation keeps row-level issues because every transaction is already loaded for classification.

Parameters:
modelo: str
period: Period
observations: Sequence[RentaGastoObservation]
issues: Sequence[RentaGastoLedgerAggregationIssue]
out_of_window_summary: OutOfWindowTransactionSummary | None
casilla_aggregation: CasillaAggregation
aggregate_renta_gasto_ledger_from_repositories(*, bucket_id, period, transaction_repository=None)[source]

Load the transaction catalogue and aggregate cumulative M130 gastos.

Returns a RentaGastoLedgerAggregation.

Return type:

RentaGastoLedgerAggregation

Parameters:
aggregate_renta_gasto_ledger(transactions, *, bucket_id, period)[source]

Aggregate OUTGOING deductible-expense transactions into M130 casilla 02.

Parameters:
  • transactions (TransactionCatalogue) – The TransactionCatalogue of ledger transactions to aggregate.

  • bucket_id (str) – Bucket identifier carried through to provenance and audit records so the resulting aggregation cannot be silently misattributed.

  • period (Period) – The quarterly Period whose year anchors the cumulative window.

Return type:

RentaGastoLedgerAggregation

Returns a RentaGastoLedgerAggregation covering the cumulative fiscal window. period must be quarterly; the cumulative window extends from Jan 1 of the period’s year through the last day of the declared quarter (RD 439/2007 art. 110.2).