Source code for cadrumo.domain.iva.prorrata_especial_parameters
"""Registry-resolved LIVA art-103.Dos.2 mandatory-especial margin.Art. 103.Dos.2 makes the prorrata especial regime compulsory once the deductioncomputed under the general regime exceeds the one under the especial regime by astatutory margin. Both halves of that test are law: the margin ITSELF, andwhether landing exactly on it is enough.The provision has had two redactions and they differ on both. The original (inforce to 31-12-2014) required the excess to be "en un 20 por 100", with no"o más" and so exclusive. Ley 28/2014 art. 1.26 (in force from 01-01-2015)replaced it with "en un 10 por ciento o más" -- lowering the margin AND makingit inclusive. A margin without its comparison direction is therefore only halfthe rule, which is why both travel together here.Only the current redaction is declared in the registry. The resolver derives itsapplicability window from the dated registry value rather than carrying a yearboundary in Python; a filing period outside that window is refused as anungrounded context.See Also: :mod:`domain.iva.prorrata` The predicate and advisory that consume the resolved margin."""from__future__importannotationsfromcollections.abcimportMappingfromdatetimeimportdatefromdecimalimportDecimalfromtypingimportTYPE_CHECKINGfrompydanticimportBaseModel,Fieldfrom...core.errors.hierarchyimportCadrumoErroras_CadrumoErrorfrom...core.modelsimportSTRICT_FROZEN_CONFIGas_STRICT_FROZEN_CONFIGfrom..calculations.registry.formula_runtime_opsimportresolve_dated_valuefrom..calculations.registry.schema_baseimportThresholdComparisonifTYPE_CHECKING:from..calculations.registry.schemaimportModeloRevision#: The registry parameter carrying the art-103.Dos.2 margin.PRORRATA_ESPECIAL_MANDATORY_PARAMETER_ID="m303-prorrata-especial-obligatoria-margen-porcentaje"
[docs]classProrrataEspecialMandatoryParameterError(_CadrumoError):"""Raised when the art-103.Dos.2 margin cannot be grounded for an ejercicio."""
[docs]classProrrataEspecialMandatoryParameters(BaseModel):"""The art-103.Dos.2 margin and its comparison direction, resolved. Attributes: margin_percentage: The percentage the provision names. comparison: Whether the margin must be exceeded strictly or merely reached. Registry data, because the two redactions differ on it. modelo_id: Modelo whose revision hosts the parameter. revision_id: The hosting revision's canonical id. resolved_on: The filing-period date the value was selected for. """model_config=_STRICT_FROZEN_CONFIGmargin_percentage:Decimal=Field(gt=Decimal("0"))comparison:ThresholdComparisonmodelo_id:str=Field(min_length=1,max_length=16)revision_id:str=Field(min_length=1,max_length=64)resolved_on:date@propertydefmultiple(self)->Decimal:"""The factor the especial-regime deduction is scaled by for the threshold."""returnDecimal("1")+self.margin_percentage/Decimal("100")@propertydefinclusive(self)->bool:"""Whether a deduction landing exactly on the threshold already qualifies."""returnself.comparisonisThresholdComparison.INCLUSIVE
[docs]defresolve_prorrata_especial_mandatory_parameters(revision:ModeloRevision,*,modelo_id:str,ejercicio:int,)->ProrrataEspecialMandatoryParameters:"""Resolve the art-103.Dos.2 margin for one ejercicio from a compiled revision. Args: revision: The compiled revision the application layer selected. modelo_id: Modelo the revision belongs to. ejercicio: The filing year whose margin is wanted. Returns: The resolved :class:`ProrrataEspecialMandatoryParameters`. Raises: ProrrataEspecialMandatoryParameterError: When the revision does not declare the parameter or cannot resolve it for that ejercicio. Core types: :class:`~cadrumo.domain.calculations.registry.schema.ModeloRevision`. """declared=next((pforpinrevision.parametersifp.id==PRORRATA_ESPECIAL_MANDATORY_PARAMETER_ID),None,)ifdeclaredisNone:raiseProrrataEspecialMandatoryParameterError(f"modelo {modelo_id} revision {revision.id} declares no "f"{PRORRATA_ESPECIAL_MANDATORY_PARAMETER_ID}; the LIVA art. 103.Dos.2 margin has no ""grounded source for this filing context",)date_context:Mapping[str,date]={"filing_period":date(ejercicio,12,31)}try:value=resolve_dated_value(declared,date_context)exceptExceptionasexc:raiseProrrataEspecialMandatoryParameterError(f"modelo {modelo_id} revision {revision.id} parameter "f"{PRORRATA_ESPECIAL_MANDATORY_PARAMETER_ID} does not resolve for ejercicio "f"{ejercicio}: {exc}",)fromexcreturnProrrataEspecialMandatoryParameters(margin_percentage=Decimal(value.value),comparison=value.comparison,modelo_id=modelo_id,revision_id=revision.id,resolved_on=date_context["filing_period"],)