Source code for cadrumo.domain.calculations.registry.tax_identity_admission
"""Registry-backed admission of tax identities for redaction.Answers the redaction gate from the published authority: the Spanishcontrol-character format decides a NIF/NIE/CIF, and the dated NIF-IVA countrycatalogue decides a prefixed Member State number. A prefix the catalogue doesnot declare admits nothing."""from__future__importannotationsfromcollections.abcimportCallablefrom....core.errors.hierarchyimportCadrumoErrorfrom....core.identity.documentsimportIdentityError,SpanishTaxIdFormat,validate_identityfrom....core.time.clockimporttoday_madridfrom.authorityimportbundled_indexed_authorityfrom.governed_fact_scopeimportGovernedFactSource,governed_facts_in_scopefrom.nif_iva_catalogueimportNifIvaCatalogue,resolve_nif_iva_cataloguefrom.tax_id_formatimporttax_id_formatdef_with_authority[T](resolve:Callable[[GovernedFactSource],T])->T|None:"""Resolve under the scoped authority, else a lease on the published one. Returns ``None`` when no authority can answer, so redaction applies its own fail-safe instead of logging or raising from inside a log funnel. """try:scoped=governed_facts_in_scope()ifscopedisnotNone:returnresolve(scoped)withbundled_indexed_authority().operation()asoperation:returnresolve(operation)except(CadrumoError,OSError,ValueError):returnNonedef_catalogue(authority:GovernedFactSource)->NifIvaCatalogue:returnresolve_nif_iva_catalogue(effective_date=today_madrid(),authority=authority)def_spanish_format(authority:GovernedFactSource)->SpanishTaxIdFormat:returntax_id_format(authority,effective_date=today_madrid())
[docs]classRegistryTaxIdentityAdmission:"""Admit redaction candidates against the published registry authority."""
[docs]defadmits_spanish_identity(self,normalised:str)->bool|None:"""Return whether ``normalised`` validates against the Spanish tax-ID format."""spanish_format=_with_authority(_spanish_format)ifspanish_formatisNone:returnNonetry:validate_identity(normalised,spanish_format)exceptIdentityError:returnFalsereturnTrue
[docs]defadmits_nif_iva(self,normalised:str)->bool|None:"""Return whether ``normalised`` matches its declared Member State format."""catalogue=_with_authority(_catalogue)ifcatalogueisNone:returnNonespec=catalogue.format_for_country(normalised[:2])returnspecisnotNoneandspec.pattern.match(normalised)isnotNone