Refactory class for read and write CFDIs

This commit is contained in:
Mauricio Baeza 2021-06-08 22:54:59 -05:00
parent 702ac88b38
commit 62a0a82699
3 changed files with 97 additions and 6 deletions

View File

@ -0,0 +1,67 @@
#!/usr/bin/env python3
import lxml.etree as ET
from requests.structures import CaseInsensitiveDict as CIDict
NS_CFDI = {
'cfdi': 'http://www.sat.gob.mx/cfd/3',
'tfd': 'http://www.sat.gob.mx/TimbreFiscalDigital',
'nomina12': 'http://www.sat.gob.mx/nomina12',
}
PRE = '/cfdi:Comprobante'
class CfdiRead(object):
def __init__(self, source):
self._source = source
self._data = {}
self._error = ''
self._rfc_emisor = ''
self._rfc_receptor = ''
self._parse()
@property
def source(self):
return self._source
@property
def data(self):
return self._data
@property
def rfc_emisor(self):
return self._rfc_emisor
@property
def rfc_receptor(self):
return self._rfc_receptor
@property
def error(self):
return self._error
def _parse(self):
self._tree = ET.fromstring(self.source)
self._data['cfdi'] = dict(self._tree.attrib)
node_name = f'{PRE}/cfdi:Emisor'
self._data['emisor'] = self._get_attr(node_name)
self._rfc_emisor = self._data['emisor']['Rfc']
node_name = f'{PRE}/cfdi:Receptor'
self._data['receptor'] = self._get_attr(node_name)
self._rfc_receptor = self._data['receptor']['Rfc']
return
def _get_attr(self, node_name):
node = self._tree.xpath(node_name, namespaces=NS_CFDI)[0]
attr = dict(node.attrib)
return attr
class CfdiWrite(object):
pass

View File

@ -59,6 +59,9 @@ from .pacs.cfdi_cert import SATCertificate
from .pacs import PACComercioDigital
from .pacs import PACFinkok
# ~ v2
from .pycfdi import CfdiRead
LOG_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
LOG_DATE = '%d/%m/%Y %H:%M:%S'
@ -779,11 +782,21 @@ def read_csv(path, args={'delimiter': '|'}):
return rows
def _products_from_xml(obj):
return {'status': 'server', 'data': {'uno': 1}}
def _products_from_xml(rfc, data):
result = {'status': 'server', 'error': ''}
cfdi = CfdiRead(data)
if not DEBUG and rfc != cfdi.rfc_receptor:
msg = f'El receptor no es: {rfc}'
result['error'] = msg
return result
result['data'] = cfdi.data
return result
def upload_file(rfc, opt, file_obj):
if opt == 'productsadd':
result = _products_from_xml(file_obj)
result = _products_from_xml(rfc, file_obj.file.read())
return result

View File

@ -481,7 +481,8 @@ function cmd_upload_products_from_xml_click(){
return
}
msg = '¿Estás seguro de importar este archivo?'
msg = '¿Estás seguro de importar este archivo? <br/><br/>\
Si hay datos previos seran reemplazados.'
webix.confirm({
title: 'Importar Productos',
ok: 'Si',
@ -505,6 +506,16 @@ function up_products_from_xml_upload_complete(response){
}
$$('win_add_products_from_xml').close()
msg = 'Archivo subido correctamente.\n\nComenzando importación.'
msg_ok(msg)
if(response.error){
msg_error(response.error)
return
}
var data = response.data
var html = '<span class="webix_icon fa-user"></span><span class="lbl_partner">'
html += data.emisor.Nombre + ' (' + data.emisor.Rfc + ')</span>'
$$('lbl_partner').setValue(html)
}