feat(finkok): consulta de status en sat de cfdi

parent 778ec79e
# -*- coding: utf-8 -*-
from . import pac_answer
<<<<<<< HEAD
from . import xpd_envelopes
=======
from . import xpd_envelopes
from . import finkok_envelopes
>>>>>>> 63a507f... feat:
# -*- coding: utf-8 -*-
_SOAPENV_CONSULTAR="""<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:can="http://facturacion.finkok.com/cancel">
<soapenv:Header/>
<soapenv:Body>
<can:get_sat_status>
<!--Optional:-->
<can:username>%s</can:username>
<!--Optional:-->
<can:password>%s</can:password>
<!--Optional:-->
<can:taxpayer_id>%s</can:taxpayer_id>
<!--Optional:-->
<can:rtaxpayer_id>%s</can:rtaxpayer_id>
<!--Optional:-->
<can:uuid>%s</can:uuid>
<!--Optional:-->
<can:total>%s</can:total>
</can:get_sat_status>
</soapenv:Body>
</soapenv:Envelope>
"""
......@@ -11,6 +11,9 @@ from suds.client import (
TypeNotFound,
WebFault,
)
import requests
from requests import Request, Session
import xmltodict
from openerp import api, fields, models
from openerp.exceptions import Warning as UserError
......@@ -18,9 +21,10 @@ from openerp.tools.translate import _
from ..lib.pac_answer import PacCancelAnswer
from ..models import exceptions as pac_exceptions
from ..lib import finkok_envelopes
logger = logging.getLogger(__name__)
_logger = logging.getLogger(__name__)
# Finkok does not return message errors
......@@ -38,6 +42,11 @@ _cancel_errors = {
"certificado del atributo certificado",
}
TIMEOUT = 60
_ACTIONS = {"timbrar": "",
"cancelar": "",
"consultar": "\"get_sat_status\""
}
class ParamsPac(models.Model):
_inherit = "params.pac"
......@@ -77,6 +86,25 @@ class ParamsPac(models.Model):
factura_mx_type__fc.update({"pac_finkok": self.cancel_cfdi_finkok})
return factura_mx_type__fc
def get_driver_fc_consult(self):
"""
Implement driver for consult CFDIs
"""
factura_mx_type__fc = super(ParamsPac, self).get_driver_fc_cancel()
if factura_mx_type__fc is None:
factura_mx_type__fc = {}
factura_mx_type__fc.update({"pac_finkok": self.consult_cfdi_finkok_2})
return factura_mx_type__fc
def create_header(self, method):
return {"SOAPAction": "%s" % (_ACTIONS[method]),
# "Accept-Encoding": "gzip,deflate",
"Content-Type": "text/xml; charset=utf-8"
# "Connection": "Keep-Alive"
# "charset": "utf-8"
}
def sign_cfdi_finkok(self, fdata):
"""Function uses to sign CFDIs in Finkok webservices
......@@ -186,3 +214,84 @@ class ParamsPac(models.Model):
cancel_answer.estatus, _cancel_errors.get(cancel_answer.estatus, ""),
)
return cancel_answer
def consult_cfdi_finkok_2(self, uuid):
cfdi = self.env['ir.attachment.facturae.mx'].search([["uuid", "=", uuid]])
username = self.user.encode("utf-8")
password = self.password.encode("utf-8")
emisor = cfdi.rfc_emisor.encode("utf-8")
receptor = cfdi.rfc_receptor.encode("utf-8")
# uuid = cfdi.uuid.encode("utf-8")
total = cfdi.total
client = Client(self.url_webservice_cancel, cache=None)
res = client.service.get_sat_status(username,password,emisor, receptor, uuid, total)
_logger.info(res)
folio = res.sat
try:
estatus_cancelacion = folio["EstatusCancelacion"]
except Exception as e:
estatus_cancelacion = ""
estado_cfdi = folio["Estado"]
consult_answer = PacConsultAnswer(uuid, estado_cfdi, estatus_cancelacion)
return consult_answer
def consult_cfdi_finkok(self, uuid):
""" Consul the cfdi status
:param uuid: UUID of CFDI to consult
"""
method = "consultar"
cfdi = self.env['ir.attachment.facturae.mx'].search([["uuid", "=", uuid]])
url1 = "http://demo-facturacion.finkok.com/servicios/soap/cancel"
try:
data = finkok_envelopes._SOAPENV_CONSULTAR % (
self.user.encode("utf-8"),
self.password.encode("utf-8"),
cfdi.rfc_emisor.encode("utf-8"),
cfdi.rfc_receptor.encode("utf-8"),
cfdi.uuid.encode("utf-8"),
cfdi.total,
)
except Exception as e:
e.message="Error al crear ENVELOPE"
_logger.info(e)
raise e
else:
data = data.encode("utf-8")
_logger.debug(data)
headers =self.create_header(method)
s = Session()
#req = Request("POST", self.url_webservice_cancel, data=data, headers=headers)
req = Request("POST", url1, data=data, headers=headers)
prepped = s.prepare_request(req)
try:
# Timbrar factura
response = s.send(prepped, timeout=TIMEOUT)
res = xmltodict.parse(response.text)
_logger.debug(res)
except Exception as e:
# Error al establecer comunicación con el PAC
_logger.debug(e)
raise e
# Procesar los resultados obtenidos del PAC
try:
resultado = res["soapenv:Envelope"]["soapenv:Body"]["ns2:consultarCfdiResponse"][
"ns2:consultarCfdiResponse"] # noqa
except KeyError as e:
resultado = res["soapenv:Envelope"]["soapenv:Body"]["ns2:consultarCfdiResponse"][
"ns2:consultarCfdiResponse"
] # noqa
_logger.debug(e)
except Exception as e:
_logger.debug(e)
raise UserError(_("Unknow answer.\n%s") % resultado)
res = resultado["ns2:responseGenericoQr"]["ns2:servicio"]
res = res.popitem(True)
res = res[1]
res = xmltodict.parse(res.encode("utf-8"))
resultado = res["s:Envelope"]["s:Body"]["ConsultaResponse"]["ConsultaResult"]
estatus_cancelacion = resultado["a:EstatusCancelacion"]
estado_cfdi = resultado["a:Estado"]
consult_answer = PacConsultAnswer(uuid, estado_cfdi, estatus_cancelacion)
return consult_answer
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment