From 765e449d8f7ea414050246d83a057f163f678c6c Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Thu, 22 Feb 2018 14:37:40 -0600 Subject: [PATCH] Timbrar con Ecodex --- source/app/controllers/pac.py | 18 ++++++++++----- source/app/controllers/util.py | 41 +++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/source/app/controllers/pac.py b/source/app/controllers/pac.py index fd6321d..33f3a08 100644 --- a/source/app/controllers/pac.py +++ b/source/app/controllers/pac.py @@ -33,8 +33,10 @@ log = Logger('PAC') class Ecodex(object): - def __init__(self): - self.codes = URL['codes'] + def __init__(self, auth, url): + self.auth = auth + self.url = url + self.codes = self.url['codes'] self.error = '' self.message = '' self._transport = Transport(cache=SqliteCache(), timeout=TIMEOUT) @@ -45,7 +47,7 @@ class Ecodex(object): self._plugins = [self._history] def _get_token(self, rfc): - client = Client(URL['seguridad'], + client = Client(self.url['seguridad'], transport=self._transport, plugins=self._plugins) try: result = client.service.ObtenerToken(rfc, self._get_epoch()) @@ -54,7 +56,7 @@ class Ecodex(object): log.error(self.error) return '' - s = '{}|{}'.format(AUTH['ID'], result.Token) + s = '{}|{}'.format(self.auth['ID'], result.Token) return hashlib.sha1(s.encode()).hexdigest() def _get_token_rest(self, rfc): @@ -99,7 +101,7 @@ class Ecodex(object): def timbra_xml(self, xml): data = self._validate_xml(xml) - client = Client(URL['timbra'], + client = Client(self.url['timbra'], transport=self._transport, plugins=self._plugins) try: result = client.service.TimbraXML(**data) @@ -260,6 +262,12 @@ class Finkok(object): except Fault as e: self.error = str(e) return + except TransportError as e: + if '413' in str(e): + self.error = '413

Documento muy grande para timbrar' + else: + self.error = str(e) + return return self._check_result(method, result) diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index eea83ce..15dd742 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -42,7 +42,7 @@ from settings import DEBUG, MV, log, template_lookup, COMPANIES, DB_SAT, \ PATH_XSLT, PATH_XSLTPROC, PATH_OPENSSL, PATH_TEMPLATES, PATH_MEDIA, PRE, \ PATH_XMLSEC, TEMPLATE_CANCEL, DEFAULT_SAT_PRODUCTO, DECIMALES, DIR_FACTURAS -from settings import SEAFILE_SERVER, USAR_TOKEN, API +from settings import DEBUG, SEAFILE_SERVER, USAR_TOKEN, API from .configpac import AUTH @@ -554,18 +554,49 @@ def timbra_xml(xml, auth): result = {'ok': True, 'error': ''} pac = PAC(auth) - xml = pac.timbra_xml(xml) - if not xml: + new_xml = pac.timbra_xml(xml) + if not new_xml: result['ok'] = False result['error'] = pac.error - return result + if pac.error.startswith('413'): + return _ecodex_timbra_xml(xml) + else: + return result - result['xml'] = xml + result['xml'] = new_xml result['uuid'] = pac.uuid result['fecha'] = pac.fecha return result +def _get_uuid_fecha(xml): + doc = parse_xml(xml) + version = doc.attrib['Version'] + node = doc.find('{}Complemento/{}TimbreFiscalDigital'.format( + PRE[version], PRE['TIMBRE'])) + return node.attrib['UUID'], node.attrib['FechaTimbrado'] + + +def _ecodex_timbra_xml(xml): + from .pac import Ecodex as PAC + from .configpac import ecodex + + auth, url = ecodex(DEBUG) + + pac = PAC(auth, url) + xml = pac.timbra_xml(xml) + if xml: + data = {'ok': True, 'error': ''} + data['xml'] = xml + uuid, fecha = _get_uuid_fecha(xml) + data['uuid'] = uuid + data['fecha'] = fecha + return data + + msg = pac.error + return {'ok': False, 'error': msg} + + def get_sat(xml): from .pac import get_status_sat return get_status_sat(xml)