cadrumo.application.aggregation.renta_gasto_ledger module

Repository-backed Renta deductible-expense (gasto) aggregation.

Loads ledger rows through the application-facing TransactionCatalogueRepositoryProtocol.

Used by the source mesh for the selected deductible-expense target.

This is the OUTGOING sibling of renta_income_ledger. Where the income pipeline accumulates professional-activity revenue into its selected target, this pipeline accumulates deductible business expenses into its selected target 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), plus the non-recoverable share of iva_amount when the activity’s IVA-deduction ratio (resolve_iva_deduction_ratio() – the SAME resolver the annual counterpart uses, for the SAME ejercicio, so the two filings cannot diverge) is less than full: IVA soportado a taxpayer cannot recover through The indirect-tax input is PGC NRV 12.ª acquisition cost, same as the annual side (LIRPF arts. 28-30 base-imponible deductibility governs the pago fraccionado’s gasto determination identically to the annual declaration). A declarable expense without taxable_base is surfaced as missing_taxable_base instead of being gross-folded into the selected target. A MIXED transaction contributes its business fraction.

This module deliberately does NOT reuse the annual 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 quarterly cumulative gasto sum. It DOES share that module’s single IVA-deduction-ratio resolver (resolve_iva_deduction_ratio()), the one taxpayer-fact lookup the two constraint-shapes have no reason to diverge on.

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:

data (Any)

model_config: ClassVar[ConfigDict]

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

transaction_id: TransactionId
reason: RentaGastoLedgerAggregationIssueReason
detail: IssueDetail
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:

data (Any)

model_config: ClassVar[ConfigDict]

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

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

Bases: LedgerAggregationResultBase[RentaGastoObservation, RentaGastoLedgerAggregationIssue]

Cumulative deductible-expense observations for one 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:
out_of_window_summary: OutOfWindowTransactionSummary | None
model_config: ClassVar[ConfigDict]

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

aggregate_renta_gasto_ledger_from_repositories(*, bucket_id, period, modelo, target_casilla_id, accept_activity_marker, transaction_repository, profile_record=None, prorrata_register_repository)[source]

Load the transaction catalogue and aggregate a cumulative expense window.

Derives the activity’s IVA-deduction ratio through resolve_iva_deduction_ratio() – the SAME resolver the The annual counterpart uses, for the SAME ejercicio (period.filing_year), so the two filings cannot diverge on it. profile_record (a UserProfileRecord) and prorrata_register_repository supply the profile and canonical register directly.

Returns a RentaGastoLedgerAggregation.

Return type:

RentaGastoLedgerAggregation

Parameters:
aggregate_renta_gasto_ledger(transactions, *, bucket_id, period, modelo, target_casilla_id, accept_activity_marker, iva_deduction_ratio=None)[source]

Aggregate OUTGOING deductible-expense transactions into a selected target.

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.

  • modelo (str) – Selected model identifier receiving the aggregation.

  • target_casilla_id (TypeAliasType) – Registry-selected output casilla for deductible expenses.

  • accept_activity_marker (bool) – Whether activity-marked transactions are eligible.

  • iva_deduction_ratio (Decimal | None) – Optional activity-wide IVA-deduction fraction (LIVA arts. 94/104), joining the non-recoverable share of a row’s iva_amount to its deductible base (PGC NRV 12.ª). None (the default) preserves the historic base-only behaviour.

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).