|
| 1 | +""" |
| 2 | +brutils.legal_nature |
| 3 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | +Utilities for consulting and validating the official |
| 6 | +*Natureza Jurídica* (Legal Nature) codes defined by the |
| 7 | +Receita Federal do Brasil (RFB). |
| 8 | +
|
| 9 | +.. note:: |
| 10 | + The codes and descriptions in this module are sourced from the |
| 11 | + official **Tabela de Natureza Jurídica** (RFB), as provided in the |
| 12 | + document used by the Cadastro Nacional (e.g., FCN). |
| 13 | +
|
| 14 | + This module offers simple lookups and validation helpers based on |
| 15 | + the official table. It does not infer the current legal/registration |
| 16 | + status of any entity. |
| 17 | +
|
| 18 | + Source: https://www.gov.br/empresas-e-negocios/pt-br/drei/links-e-downloads/arquivos/TABELADENATUREZAJURDICA.pdf |
| 19 | +""" |
| 20 | + |
| 21 | +from typing import Dict, Optional |
| 22 | + |
| 23 | +# FORMATTING |
| 24 | +############ |
| 25 | + |
| 26 | + |
| 27 | +# Helper to normalize inputs like "101-5" => "1015" |
| 28 | +def _normalize(code: str) -> Optional[str]: |
| 29 | + if not isinstance(code, str): |
| 30 | + return None |
| 31 | + |
| 32 | + digits = "".join(ch for ch in code.strip() if ch.isdigit()) |
| 33 | + |
| 34 | + return digits if len(digits) == 4 else None |
| 35 | + |
| 36 | + |
| 37 | +LEGAL_NATURE: Dict[str, str] = { |
| 38 | + # 1. ADMINISTRAÇÃO PÚBLICA |
| 39 | + "1015": "Órgão Público do Poder Executivo Federal", |
| 40 | + "1023": "Órgão Público do Poder Executivo Estadual ou do Distrito Federal", |
| 41 | + "1031": "Órgão Público do Poder Executivo Municipal", |
| 42 | + "1040": "Órgão Público do Poder Legislativo Federal", |
| 43 | + "1058": "Órgão Público do Poder Legislativo Estadual ou do Distrito Federal", |
| 44 | + "1066": "Órgão Público do Poder Legislativo Municipal", |
| 45 | + "1074": "Órgão Público do Poder Judiciário Federal", |
| 46 | + "1082": "Órgão Público do Poder Judiciário Estadual", |
| 47 | + "1104": "Autarquia Federal", |
| 48 | + "1112": "Autarquia Estadual ou do Distrito Federal", |
| 49 | + "1120": "Autarquia Municipal", |
| 50 | + "1139": "Fundação Federal", |
| 51 | + "1147": "Fundação Estadual ou do Distrito Federal", |
| 52 | + "1155": "Fundação Municipal", |
| 53 | + "1163": "Órgão Público Autônomo da União", |
| 54 | + "1171": "Órgão Público Autônomo Estadual ou do Distrito Federal", |
| 55 | + "1180": "Órgão Público Autônomo Municipal", |
| 56 | + # 2. ENTIDADES EMPRESARIAIS |
| 57 | + "2011": "Empresa Pública", |
| 58 | + "2038": "Sociedade de Economia Mista", |
| 59 | + "2046": "Sociedade Anônima Aberta", |
| 60 | + "2054": "Sociedade Anônima Fechada", |
| 61 | + "2062": "Sociedade Empresária Limitada", |
| 62 | + "2076": "Sociedade Empresária em Nome Coletivo", |
| 63 | + "2089": "Sociedade Empresária em Comandita Simples", |
| 64 | + "2097": "Sociedade Empresária em Comandita por Ações", |
| 65 | + "2100": "Sociedade Mercantil de Capital e Indústria (extinta pelo NCC/2002)", |
| 66 | + "2127": "Sociedade Empresária em Conta de Participação", |
| 67 | + "2135": "Empresário (Individual)", |
| 68 | + "2143": "Cooperativa", |
| 69 | + "2151": "Consórcio de Sociedades", |
| 70 | + "2160": "Grupo de Sociedades", |
| 71 | + "2178": "Estabelecimento, no Brasil, de Sociedade Estrangeira", |
| 72 | + "2194": "Estabelecimento, no Brasil, de Empresa Binacional Argentino-Brasileira", |
| 73 | + "2208": "Entidade Binacional Itaipu", |
| 74 | + "2216": "Empresa Domiciliada no Exterior", |
| 75 | + "2224": "Clube/Fundo de Investimento", |
| 76 | + "2232": "Sociedade Simples Pura", |
| 77 | + "2240": "Sociedade Simples Limitada", |
| 78 | + "2259": "Sociedade em Nome Coletivo", |
| 79 | + "2267": "Sociedade em Comandita Simples", |
| 80 | + "2275": "Sociedade Simples em Conta de Participação", |
| 81 | + "2305": "Empresa Individual de Responsabilidade Limitada", |
| 82 | + # 3. ENTIDADES SEM FINS LUCRATIVOS |
| 83 | + "3034": "Serviço Notarial e Registral (Cartório)", |
| 84 | + "3042": "Organização Social", |
| 85 | + "3050": "Organização da Sociedade Civil de Interesse Público (Oscip)", |
| 86 | + "3069": "Outras Formas de Fundações Mantidas com Recursos Privados", |
| 87 | + "3077": "Serviço Social Autônomo", |
| 88 | + "3085": "Condomínio Edilícios", |
| 89 | + "3093": "Unidade Executora (Programa Dinheiro Direto na Escola)", |
| 90 | + "3107": "Comissão de Conciliação Prévia", |
| 91 | + "3115": "Entidade de Mediação e Arbitragem", |
| 92 | + "3123": "Partido Político", |
| 93 | + "3131": "Entidade Sindical", |
| 94 | + "3204": "Estabelecimento, no Brasil, de Fundação ou Associação Estrangeiras", |
| 95 | + "3212": "Fundação ou Associação Domiciliada no Exterior", |
| 96 | + "3999": "Outras Formas de Associação", |
| 97 | + # 4. PESSOAS FÍSICAS |
| 98 | + "4014": "Empresa Individual Imobiliária", |
| 99 | + "4022": "Segurado Especial", |
| 100 | + "4081": "Contribuinte individual", |
| 101 | + # 5. ORGANIZAÇÕES INTERNACIONAIS E OUTRAS INSTITUIÇÕES EXTRATERRITORIAIS |
| 102 | + "5002": "Organização Internacional e Outras Instituições Extraterritoriais", |
| 103 | +} |
| 104 | + |
| 105 | +# OPERATIONS |
| 106 | +############ |
| 107 | + |
| 108 | + |
| 109 | +def is_valid(code: str) -> bool: |
| 110 | + """ |
| 111 | + Check if a string corresponds to a valid *Natureza Jurídica* code. |
| 112 | +
|
| 113 | + Args: |
| 114 | + code (str): The code to be validated. Accepts either "NNNN" or "NNN-N". |
| 115 | +
|
| 116 | + Returns: |
| 117 | + bool: True if the normalized code exists in the official table, False otherwise. |
| 118 | +
|
| 119 | + Example: |
| 120 | + >>> is_valid("2062") |
| 121 | + True |
| 122 | + >>> is_valid("206-2") |
| 123 | + True |
| 124 | + >>> is_valid("9999") |
| 125 | + False |
| 126 | +
|
| 127 | + .. note:: |
| 128 | + Validation is based solely on the presence of the code in the |
| 129 | + official RFB table. It does not verify the current legal status |
| 130 | + or registration of the entity. |
| 131 | + """ |
| 132 | + normalized = _normalize(code) |
| 133 | + return normalized in LEGAL_NATURE if normalized else False |
| 134 | + |
| 135 | + |
| 136 | +def get_description(code: str) -> Optional[str]: |
| 137 | + """ |
| 138 | + Retrieve the description of a *Natureza Jurídica* code. |
| 139 | +
|
| 140 | + Args: |
| 141 | + code (str): The code to look up. Accepts either "NNNN" or "NNN-N". |
| 142 | +
|
| 143 | + Returns: |
| 144 | + str | None: The full description if the code is valid, otherwise None. |
| 145 | +
|
| 146 | + Example: |
| 147 | + >>> get_description("2062") |
| 148 | + 'Sociedade Empresária Limitada' |
| 149 | + >>> get_description("101-5") |
| 150 | + 'Órgão Público do Poder Executivo Federal' |
| 151 | + >>> get_description("0000") |
| 152 | + None |
| 153 | + """ |
| 154 | + normalized = _normalize(code) |
| 155 | + return LEGAL_NATURE.get(normalized) if normalized else None |
| 156 | + |
| 157 | + |
| 158 | +def list_all() -> Dict[str, str]: |
| 159 | + """ |
| 160 | + Return a copy of the full *Natureza Jurídica* table. |
| 161 | +
|
| 162 | + Returns: |
| 163 | + dict[str, str]: Mapping from "NNNN" codes to descriptions. |
| 164 | + """ |
| 165 | + return dict(LEGAL_NATURE) |
0 commit comments