Beispiel #1
0
    def action_check_sefaz(self):
        if self.cnpj_cpf and self.state_id:
            if self.state_id.code == 'AL':
                raise UserError(u'Alagoas não possui consulta de cadastro')
            company = self.env.user.company_id
            if not company.nfe_a1_file and not company.nfe_a1_password:
                raise UserError(u'Configurar o certificado e senha na empresa')
            cert = company.with_context({'bin_size': False}).nfe_a1_file
            cert_pfx = base64.decodestring(cert)
            certificado = Certificado(cert_pfx, company.nfe_a1_password)
            cnpj = re.sub('[^0-9]', '', self.cnpj_cpf)
            obj = {'cnpj': cnpj, 'estado': self.state_id.code}
            resposta = consulta_cadastro(certificado, obj=obj, ambiente=1,
                                         estado=self.state_id.ibge_code)

            obj = resposta['object']
            if "Body" in dir(obj) and \
               "consultaCadastro2Result" in dir(obj.Body):
                info = obj.Body.consultaCadastro2Result.retConsCad.infCons
                if info.cStat == 111 or info.cStat == 112:
                    if not self.inscr_est:
                        self.inscr_est = info.infCad.IE
                    if not self.cnpj_cpf:
                        self.cnpj_cpf = info.infCad.IE

                    def get_value(obj, prop):
                        if prop not in dir(obj):
                            return None
                        return getattr(obj, prop)
                    self.legal_name = get_value(info.infCad, 'xNome')
                    if "ender" not in dir(info.infCad):
                        return
                    cep = get_value(info.infCad.ender, 'CEP') or ''
                    self.zip = str(cep).zfill(8) if cep else ''
                    self.street = get_value(info.infCad.ender, 'xLgr')
                    self.number = get_value(info.infCad.ender, 'nro')
                    self.street2 = get_value(info.infCad.ender, 'xCpl')
                    self.district = get_value(info.infCad.ender, 'xBairro')
                    cMun = get_value(info.infCad.ender, 'cMun')
                    xMun = get_value(info.infCad.ender, 'xMun')
                    city = None
                    if cMun:
                        city = self.env['res.state.city'].search(
                            [('ibge_code', '=', str(cMun)[2:]),
                             ('state_id', '=', self.state_id.id)])
                    if not city and xMun:
                        city = self.env['res.state.city'].search(
                            [('name', 'ilike', xMun),
                             ('state_id', '=', self.state_id.id)])
                    if city:
                        self.city_id = city.id
                else:
                    msg = "%s - %s" % (info.cStat, info.xMotivo)
                    raise UserError(msg)
        else:
            raise UserError(u'Preencha o estado e o CNPJ para pesquisar')
Beispiel #2
0
    def action_check_sefaz(self):
        if self.cnpj_cpf and self.state_id:
            if self.state_id.code == 'AL':
                raise UserError(_(u'Alagoas doesn\'t have this service'))
            if self.state_id.code == 'RJ':
                raise UserError(
                    _(u'Rio de Janeiro doesn\'t have this service'))
            company = self.env.user.company_id
            if not company.nfe_a1_file and not company.nfe_a1_password:
                raise UserError(
                    _(u'Configure the company\'s certificate and password'))
            cert = company.with_context({'bin_size': False}).nfe_a1_file
            cert_pfx = base64.decodestring(cert)
            certificado = Certificado(cert_pfx, company.nfe_a1_password)
            cnpj = re.sub('[^0-9]', '', self.cnpj_cpf)
            obj = {'cnpj': cnpj, 'estado': self.state_id.code}
            resposta = consulta_cadastro(certificado,
                                         obj=obj,
                                         ambiente=1,
                                         estado=self.state_id.ibge_code)

            info = resposta['object'].getchildren()[0]
            info = info.infCons
            if info.cStat == 111 or info.cStat == 112:
                if not self.inscr_est:
                    self.inscr_est = info.infCad.IE.text
                if not self.cnpj_cpf:
                    self.cnpj_cpf = info.infCad.CNPJ.text

                def get_value(obj, prop):
                    if prop not in dir(obj):
                        return None
                    return getattr(obj, prop)

                self.legal_name = get_value(info.infCad, 'xNome')
                if "ender" not in dir(info.infCad):
                    return
                cep = get_value(info.infCad.ender, 'CEP') or ''
                self.zip = str(cep).zfill(8) if cep else ''
                self.street = get_value(info.infCad.ender, 'xLgr')
                self.number = get_value(info.infCad.ender, 'nro')
                self.street2 = get_value(info.infCad.ender, 'xCpl')
                self.district = get_value(info.infCad.ender, 'xBairro')
                cMun = get_value(info.infCad.ender, 'cMun')
                xMun = get_value(info.infCad.ender, 'xMun')
                city = None
                if cMun:
                    city = self.env['res.state.city'].search([
                        ('ibge_code', '=', str(cMun)[2:]),
                        ('state_id', '=', self.state_id.id)
                    ])
                if not city and xMun:
                    city = self.env['res.state.city'].search([
                        ('name', 'ilike', xMun),
                        ('state_id', '=', self.state_id.id)
                    ])
                if city:
                    self.city_id = city.id
            else:
                msg = "%s - %s" % (info.cStat, info.xMotivo)
                raise UserError(msg)
        else:
            raise UserError(_(u'Fill the State and CNPJ fields to search'))
    def test_conta_de_cadastro(self):
        pfx_source = open(os.path.join(self.caminho, 'teste.pfx'), 'rb').read()
        pfx = Certificado(pfx_source, '123456')

        obj = {'cnpj': '12345678901234', 'estado': '42'}
        consulta_cadastro(pfx, obj=obj, ambiente=1, estado='42')