aeat.application.aggregation._renta_ledger module

Repository-backed Modelo 100 Renta expense aggregation.

This is the annual first-slice expense projection behind the ledger_renta_expense_aggregation source. It loads both a TransactionCatalogue and a InvoiceCatalogue from the active bucket through TransactionCatalogueRepository and InvoiceCatalogueRepository, uses purchase-invoice evidence to validate deductible-expense facts, and returns binding-ready RentaDeductibleExpenseObservation records.

The source-mesh resolver in _modelo_bindings applies the target ModeloRevision, resolves registry bindings, and reports source issues or unrouted expenses on its CalculationSourceResolution. The M130 quarterly gasto projection is intentionally separate in _renta_gasto_ledger.

Related: _iva_ledger and _renta_income_ledger for sibling ledger projections.

class RentaLedgerAggregationIssueReason(*values)[source]

Bases: StrEnum

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

The five upstream filter rejections (UNSUPPORTED_DIRECTION, UNSUPPORTED_CURRENCY, UNCLASSIFIED_BUSINESS_STATE, PERSONAL_TRANSACTION, OUTSIDE_PERIOD) are shared with IvaLedgerAggregationIssueReason through _shared_issue_reasons. UNSUPPORTED_PERIOD is a Renta-only refusal raised against quarter-level requests; the remaining values describe Renta-specific deductibility checks.

UNSUPPORTED_PERIOD
UNSUPPORTED_DIRECTION
UNSUPPORTED_CURRENCY
UNCLASSIFIED_BUSINESS_STATE
PERSONAL_TRANSACTION
MISSING_CATEGORY
UNKNOWN_CATEGORY
CATEGORY_OUTSIDE_FIRST_SLICE
MISSING_CATEGORY_PROFILE
OUTSIDE_PERIOD
MISSING_PURCHASE_INVOICE_EVIDENCE
UNSUPPORTED_PURCHASE_INVOICE_EVIDENCE_KIND
PURCHASE_INVOICE_EVIDENCE_BUCKET_MISMATCH
PARTIAL_OR_MULTI_TRANSACTION_PURCHASE_INVOICE_EVIDENCE
AMOUNT_MISMATCH
INVALID_LEDGER_FACT
INELIGIBLE_DEDUCTIBILITY
REGION_UNDECLARED_FOR_OVERRIDE
class RentaLedgerAggregationIssue(**data)[source]

Bases: BaseModel

Traceable exclusion emitted while aggregating ledger rows.

Parameters:
transaction_id: str
purchase_invoice_evidence_id: str | None
category_id: str | None
reason: RentaLedgerAggregationIssueReason
detail: str
class RentaLedgerExpenseAggregation(**data)[source]

Bases: BaseModel

First-slice Renta observations plus binding-ready casilla totals.

Parameters:
modelo: str
period: Period
profile_year: int
observations: Sequence[RentaDeductibleExpenseObservation]
issues: Sequence[RentaLedgerAggregationIssue]
casilla_aggregation: CasillaAggregation
property casilla_values: Mapping[CasillaId, Decimal]

Return the frozen mapping of binding-ready casilla totals.

aggregate_renta_ledger_expenses_from_repositories(*, bucket_id, period, transaction_repository=None, invoice_repository=None, profile_year=None, usage_ratios=None, activity_key='default', modelo='100', profile_record=None, region_category_overrides=None)[source]

Load persisted catalogues and aggregate first-slice Renta expenses.

Derives the ordinary-residence comunidad autonoma from the bucket’s active profile (tax_residence.ccaa) and threads it into the aggregation so a territorial-regime deductibility override selects by residence; the axis is fail-closed (an undeclared or unparseable region resolves to the state year profile) and byte-identical while the override layer is empty. Pass profile_record to supply the UserProfileRecord directly (the residence is otherwise loaded from the bucket), and region_category_overrides to supply the per-CCAA override layer forwarded to aggregate_renta_ledger_expenses() (defaulting to the empty registry-provisioned layer, resolve_region_category_profiles()).

Returns a RentaLedgerExpenseAggregation.

Return type:

RentaLedgerExpenseAggregation

Parameters:
aggregate_renta_ledger_expenses(transactions, invoices, *, bucket_id, period, profile_year=None, usage_ratios=None, activity_key='default', modelo='100', residence_ccaa=None, region_category_overrides=None)[source]

Aggregate classified ledger transactions into Renta expense observations.

Parameters:
  • transactions (TransactionCatalogue) – The TransactionCatalogue supplying active ledger entries.

  • invoices (InvoiceCatalogue) – The InvoiceCatalogue used for invoice-linked deductibility checks.

  • bucket_id (str) – Activity bucket identifier for scoping the aggregation.

  • period (Period) – Filing period as a typed Period instance.

  • profile_year (int | None) – Optional category-profile year override; defaults to the period.year when None.

  • usage_ratios (Mapping[SpendingCategory, Decimal] | None) – Optional per-SpendingCategory business-use ratio overrides applied before deductibility calculation.

  • activity_key (str) – Activity identifier carried through to the produced observations’ provenance; defaults to "default".

  • modelo (str) – Modelo identifier ("100" IRPF or "130" pagos fraccionados) selecting the per-modelo deductibility rules.

  • residence_ccaa (CCAA | None) – Optional ordinary residence comunidad autonoma, used only to select a territorial-regime deductibility override; inert while the override layer is empty and general expense deductibility is state law.

  • region_category_overrides (Mapping[CCAA, Mapping[SpendingCategory, CategoryProfile]] | None) – Optional per-CCAA category-profile override layer; defaults to resolve_region_category_profiles() (empty) so every fact falls through to the state year profile.

Return type:

RentaLedgerExpenseAggregation

Returns a RentaLedgerExpenseAggregation containing the accepted observations, exclusion issues, and binding-ready casilla totals.