@@ -116,16 +116,27 @@ def _validate(self, response):
116
116
if response .status_code != 200 :
117
117
raise ValidationError (_ ("%s" ) % response .reason )
118
118
119
+ @api .model
120
+ def _get_legal_nature (self , raw_code ):
121
+ code = punctuation_rm (raw_code )
122
+ legal_nature_id = False
123
+ if code :
124
+ legal_nature_id = (
125
+ self .env ["l10n_br_fiscal.legal.nature" ]
126
+ .search ([("code_unmasked" , "=" , code )])
127
+ .id
128
+ )
129
+ return legal_nature_id
130
+
119
131
@api .model
120
132
def _get_cnae (self , raw_code ):
121
133
code = punctuation_rm (raw_code )
122
134
cnae_id = False
123
135
124
136
if code :
125
- formatted_code = code [0 :4 ] + "-" + code [4 ] + "/" + code [5 :]
126
137
cnae_id = (
127
138
self .env ["l10n_br_fiscal.cnae" ]
128
- .search ([("code " , "=" , formatted_code )])
139
+ .search ([("code_unmasked " , "=" , code )])
129
140
.id
130
141
)
131
142
@@ -168,7 +179,7 @@ def _receitaws_import_data(self, data):
168
179
"district" : self .get_data (data , "bairro" , title = True ),
169
180
"street_number" : self .get_data (data , "numero" ),
170
181
"zip" : self .get_data (data , "cep" ),
171
- "legal_nature " : self .get_data (data , "natureza_juridica" ),
182
+ "legal_nature_id " : self ._receitaws_get_legal_nature (data ),
172
183
"phone" : phone ,
173
184
"mobile" : mobile ,
174
185
"state_id" : state_id ,
@@ -221,6 +232,16 @@ def _get_state_city(self, data):
221
232
222
233
return [state_id , city_id ]
223
234
235
+ @api .model
236
+ def _receitaws_get_legal_nature (self , data ):
237
+ legal_nature = data .get ("natureza_juridica" )
238
+ if legal_nature :
239
+ legal_nature = legal_nature .split (" - " )
240
+ if len (legal_nature ) > 1 :
241
+ legal_nature_code = legal_nature [0 ]
242
+ return self ._get_legal_nature (legal_nature_code )
243
+ return False
244
+
224
245
@api .model
225
246
def _receitaws_get_cnae (self , data ):
226
247
if data .get ("atividade_principal" ):
@@ -280,7 +301,6 @@ def _serpro_import_data(self, data):
280
301
name = fantasy_name if fantasy_name else legal_name
281
302
phone , mobile = self ._serpro_get_phones (data )
282
303
address = data .get ("endereco" )
283
- nature = data .get ("naturezaJuridica" )
284
304
cep = self .get_data (address , "cep" )
285
305
286
306
res = {
@@ -291,8 +311,7 @@ def _serpro_import_data(self, data):
291
311
"street2" : self .get_data (address , "complemento" , title = True ),
292
312
"district" : self .get_data (address , "bairro" , title = True ),
293
313
"street_number" : self .get_data (address , "numero" ),
294
- "legal_nature" : self .get_data (nature , "codigo" , title = True )
295
- + self .get_data (nature , "descricao" , title = True ),
314
+ "legal_nature_id" : self ._serpro_get_legal_nature (data ),
296
315
"zip" : cep ,
297
316
"phone" : phone ,
298
317
"mobile" : mobile ,
@@ -388,9 +407,14 @@ def _get_city_id(self, cep):
388
407
389
408
return cep_values .get ("city_id" )
390
409
410
+ @api .model
411
+ def _serpro_get_legal_nature (self , data ):
412
+ legal_nature = data .get ("naturezaJuridica" )
413
+ legal_nature_code = self .get_data (legal_nature , "codigo" )
414
+ return self ._get_legal_nature (legal_nature_code )
415
+
391
416
@api .model
392
417
def _serpro_get_cnae (self , data ):
393
418
cnae_main = data .get ("cnaePrincipal" )
394
419
cnae_code = self .get_data (cnae_main , "codigo" )
395
-
396
420
return self ._get_cnae (cnae_code )
0 commit comments