Source code for cadrumo.domain.calculations.registry.inventory_bindings
"""Typed registry selector contract for the Modelo 100 inventory projection.This module declares only the binding selector and its accumulating shapevalidator. Source resolution, readiness, valuation, provenance, and registryenrollment are owned by later integration steps; a selector declaration cannotmake an inventory schedule authoritative or complete.The filing year is deliberately NOT pinned here. Which years carry an inventoryprojection is a revision-owned fact, declared by the bindings the authoring treeships, so a later revision adding one must be an authoring change rather than anedit to this file. Today only the Modelo 100 2025 revision declares them.The operation-to-casilla identity below is a structural invariant of theprojection vocabulary: an operation names WHICH figure it produces, so itsdestination is what the operation means, not a value the law re-sets per year."""from__future__importannotationsfromtypingimportTYPE_CHECKING,LiteralfrompydanticimportBaseModel,model_validatorfrom....core.aggregationimportBindingAggregationOp,BindingSourceKindfrom....core.casilla_idimportCasillaId,validated_casilla_idfrom....core.errors.hierarchyimportpydantic_validation_boundaryfrom....core.modelsimportSTRICT_FROZEN_CONFIGfrom.binding_selector_utilsimportselector_against_modelfrom.binding_temporalimportBindingTemporalSelector,SameTargetContextfrom.errorsimportRegistryValidationErrorifTYPE_CHECKING:from.schemaimportBindingDefinitiontypeInventoryProjectionOperation=Literal["complete_acquisition_cost","closing_minus_opening_positive","opening_minus_closing_positive",]_INVENTORY_DESTINATION_BY_OPERATION:dict[InventoryProjectionOperation,CasillaId]={"complete_acquisition_cost":validated_casilla_id("0181",surface="inventory complete-acquisition-cost destination",),"closing_minus_opening_positive":validated_casilla_id("0177",surface="inventory positive closing-minus-opening destination",),"opening_minus_closing_positive":validated_casilla_id("0182",surface="inventory positive opening-minus-closing destination",),}
[docs]classInventoryProvider(BaseModel):"""One immutable operation template expanded over runtime activity rows. ``complete_acquisition_cost`` names the legally complete acquisition-cost fact, never the existing IVA-exclusive purchase subtotal. The two stock variation operations name non-negative magnitudes in opposite directions, so no generic signed variation can enter the selector vocabulary. """model_config=STRICT_FROZEN_CONFIGkind:Literal[BindingSourceKind.INVENTORY]=BindingSourceKind.INVENTORYmodelo:Literal["100"]# The source year is the TARGET's year, never an authored constant: the# declaration states timeless intent and the filing context supplies the# year. The casilla-renumbering hazard the former absolute ``filing_year``# guarded against is carried by the revision the binding is declared in# together with ``_INVENTORY_DESTINATION_BY_OPERATION``, which still pins# each operation to the exact destination casilla of its own revision.temporal:BindingTemporalSelector=SameTargetContext()projection_grain:Literal["taxpayer_year_activity"]fact:Literal["row_field"]record:Literal["inventory_activity"]grouping:Literal["per_inventory_activity"]row_field:InventoryProjectionOperationtarget_casilla_id:CasillaId@model_validator(mode="after")@pydantic_validation_boundarydef_require_operation_destination_identity(self)->InventoryProvider:expected=_INVENTORY_DESTINATION_BY_OPERATION[self.row_field]ifself.target_casilla_id!=expected:raiseRegistryValidationError(f"inventory operation {self.row_field!r} must target casilla {expected!r}, "f"not {self.target_casilla_id!r}",)returnself
[docs]defvalidate_inventory_binding(binding:BindingDefinition)->list[str]:"""Validate the inventory operation invariants for snapshot build. The operation-to-destination identity is re-run here through the shared selector gate rather than trusted to have happened earlier: a snapshot is built from provider objects that may reach this boundary without having passed model validation, and an operation silently landing on another operation's casilla is a filing-grade misdeclaration, not a shape detail. The identity itself is never restated here -- the union member owns it. """failures=selector_against_model(binding,InventoryProvider)ifbinding.aggregationisNoneorbinding.aggregation.opisnotBindingAggregationOp.ROWS:failures.append(f"binding {binding.id!r} inventory operation template requires aggregation op 'rows'")returnfailures