Source code for cadrumo.domain.categories.iva_hint
"""Typed projection of registry-owned category IVA hint vocabulary."""from__future__importannotationsfromcollections.abcimportMappingfromdataclassesimportdataclassfromdatetimeimportdatefromtypesimportMappingProxyTypefromtypingimportFinalfrom...core.time.clockimporttoday_madridfrom..calculations.registry.errorsimportRegistryValidationErrorfrom..calculations.registry.facts.resolutionimport(MappingFactQuery,ResolvedMappingFact,required_mapping_entry,unique_mapping_tokens,)from..calculations.registry.facts.schemaimportFactSelectorfrom..calculations.registry.governed_fact_scopeimportGovernedFactSource,governed_facts_in_scopefrom..calculations.registry.schema_baseimportDateAxisfrom.profileimportIvaDeductibilityHint_ENTRY_SUBJECT:Final="IVA deductibility hint mapping"_UNIQUE_TOKENS_REQUIREMENT:Final="must declare unique tokens"_FACT_ID="categories.profile"_SCOPE_SELECTOR=FactSelector(name="scope",value="iva_deductibility_hint")_ORDER_KEY="iva_deductibility_hint.order"_SEMANTICS_KEY="iva_deductibility_hint.semantics"
[docs]@dataclass(frozen=True,slots=True)classIvaDeductibilityHintCatalogue:"""Registry-projected, dated IVA hint vocabulary."""values:tuple[IvaDeductibilityHint,...]semantics:str@propertydefall_hints(self)->frozenset[IvaDeductibilityHint]:"""Return every registry-declared hint token."""returnfrozenset(self.values)
[docs]defrequire(self,value:object)->IvaDeductibilityHint:"""Validate one opaque token against the registry projection."""ifisinstance(value,IvaDeductibilityHint):token=valueelifisinstance(value,str):token=IvaDeductibilityHint(value.strip())else:raiseRegistryValidationError("IVA deductibility hint must be a string token")ifnotstr(token):raiseRegistryValidationError("IVA deductibility hint token must not be blank")iftokennotinself.all_hints:raiseRegistryValidationError(f"IVA deductibility hint {str(token)!r} is not registry-declared")returntoken
def_mapping_entries(resolved:ResolvedMappingFact)->Mapping[str,str]:"""Narrow a resolved mapping payload to a unique string-to-string map."""entries:dict[str,str]={}forentryinresolved.payload.entries:ifnotisinstance(entry.key,str)ornotisinstance(entry.value,str):raiseRegistryValidationError("IVA deductibility hint entries must be string-to-string")ifentry.keyinentries:raiseRegistryValidationError(f"duplicate IVA deductibility hint key {entry.key!r}")entries[entry.key]=entry.valuereturnMappingProxyType(entries)
[docs]defresolve_iva_deductibility_hint_catalogue(*,effective_date:date|None=None,authority:GovernedFactSource|None=None,)->IvaDeductibilityHintCatalogue:"""Resolve the dated IVA hint vocabulary through the facts authority."""selected_authority=authorityorgoverned_facts_in_scope()ifselected_authorityisNone:raiseRegistryValidationError("IVA deductibility hint catalogue requires an explicit authority operation or scope")resolved=selected_authority.resolve_governed_fact(MappingFactQuery(fact_id=_FACT_ID,date_axis=DateAxis.FILING_PERIOD,effective_date=effective_dateortoday_madrid(),selectors=(_SCOPE_SELECTOR,),),)ifnotisinstance(resolved,ResolvedMappingFact):raiseRegistryValidationError("categories.profile IVA hint vocabulary must resolve as a mapping fact")entries=_mapping_entries(resolved)values=tuple(IvaDeductibilityHint(token)fortokeninunique_mapping_tokens(entries,_ORDER_KEY,subject=_ENTRY_SUBJECT,requirement=_UNIQUE_TOKENS_REQUIREMENT))returnIvaDeductibilityHintCatalogue(values=values,semantics=required_mapping_entry(entries,_SEMANTICS_KEY,subject=_ENTRY_SUBJECT))
[docs]defrequire_iva_deductibility_hint(value:object,*,effective_date:date|None=None,authority:GovernedFactSource|None=None,)->IvaDeductibilityHint:"""Return one registry-declared IVA hint token or refuse it."""returnresolve_iva_deductibility_hint_catalogue(effective_date=effective_date,authority=authority,).require(value)