diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index 45474d6..02e9979 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -1696,6 +1696,14 @@ def upload_file(rfc, opt, file_obj): name = '{}_nomina.ods'.format(rfc.lower()) path = _join(PATH_MEDIA, 'tmp', name) + elif opt == 'cfdixml': + tmp = file_obj.filename.split('.') + ext = tmp[-1].lower() + if ext != 'xml': + msg = 'Extensión de archivo incorrecta, selecciona un archivo XML' + return {'status': 'server', 'name': msg, 'ok': False} + + return import_xml(file_obj.file.read()) if save_file(path, file_obj.file.read()): return {'status': 'server', 'name': file_obj.filename, 'ok': True} @@ -2979,3 +2987,14 @@ def get_log(name): if is_file(path): data = open(path).read() return data, name + + +def import_xml(stream): + try: + xml = ET.fromstring(stream.decode()) + except ET.ParseError: + return {'ok': False, 'status': 'error'} + + print (xml) + return {'ok': True, 'status': 'server'} + diff --git a/source/static/js/controller/admin.js b/source/static/js/controller/admin.js index ca1bc11..e8877af 100644 --- a/source/static/js/controller/admin.js +++ b/source/static/js/controller/admin.js @@ -78,7 +78,9 @@ var controllers = { $$('chk_usar_nomina').attachEvent('onItemClick', chk_config_item_click) $$('cmd_subir_bdfl').attachEvent('onItemClick', cmd_subir_bdfl_click) + $$('cmd_subir_cfdixml').attachEvent('onItemClick', cmd_subir_cfdixml_click) $$('up_bdfl').attachEvent('onUploadComplete', up_bdfl_upload_complete) + $$('up_cfdixml').attachEvent('onUploadComplete', up_cfdixml_upload_complete) } } @@ -1593,6 +1595,69 @@ function up_bdfl_upload_complete(response){ } +function cmd_subir_cfdixml_click(){ + var form = $$('form_upload_cfdixml') + + if (!form.validate()){ + msg = 'Valores inválidos' + msg_error(msg) + return + } + + var values = form.getValues() + + if($$('lst_cfdixml').count() < 1){ + msg = 'Selecciona un archivo XML' + msg_error(msg) + return + } + + if($$('lst_cfdixml').count() > 1){ + msg = 'Selecciona solo un archivo' + msg_error(msg) + return + } + + var cfdixml = $$('up_cfdixml').files.getItem($$('up_cfdixml').files.getFirstId()) + + var ext = [] + if(cfdixml.type.toLowerCase() != 'xml'){ + msg = 'Archivo inválido, se requiere un archivo XML' + msg_error(msg) + return + } + + msg = '¿Estás seguro de subir este archivo?' + webix.confirm({ + title: 'Importar CFDI', + ok: 'Si', + cancel: 'No', + type: 'confirm-error', + text: msg, + callback:function(result){ + if(result){ + $$('up_cfdixml').send() + } + } + }) +} + + +function up_cfdixml_upload_complete(response){ + if(response.status != 'server'){ + msg = 'Ocurrio un error al subir los archivos' + msg_error(msg) + return + } + + msg = 'Archivo importado correctamente' + msg_ok(msg) + + $$('form_upload_cfdixml').setValues({}) + $$('up_cfdixml').files.data.clearAll() +} + + function cmd_usuario_agregar_click(){ var form = $$('form_usuario') diff --git a/source/static/js/ui/admin.js b/source/static/js/ui/admin.js index 906df6a..eb84171 100644 --- a/source/static/js/ui/admin.js +++ b/source/static/js/ui/admin.js @@ -675,6 +675,18 @@ var utilidades_archivos = [ {cols: [{}, {view: 'button', id: 'cmd_subir_bdfl', label: 'Subir base de datos de Factura Libre'}, {}]}, ]}, + {maxHeight: 15}, + {template: 'Importar archivo CFDI (XML)', type: 'section'}, + {view: 'form', id: 'form_upload_cfdixml', rows: [ + {cols: [{}, + {view: 'uploader', id: 'up_cfdixml', autosend: false, link: 'lst_cfdixml', + value: 'Seleccionar archivo XML', upload: '/files/cfdixml'}, {}]}, + {cols: [{}, + {view: 'list', id: 'lst_cfdixml', name: 'cfdixml', + type: 'uploader', autoheight: true, borderless: true}, {}]}, + {cols: [{}, {view: 'button', id: 'cmd_subir_cfdixml', + label: 'Importar CFDI'}, {}]}, + ]}, {}]