Import from ODS

This commit is contained in:
El Mau 2024-01-18 22:29:49 -06:00
parent c1ce206835
commit d2e92098da
5 changed files with 101 additions and 6 deletions

View File

@ -1571,6 +1571,58 @@ class LIBO(object):
rows = tuple(data[1:])
return rows, ''
def _data_to_dict(self, rows):
data = {k: v for k, v in rows if v}
return data
def _current_region_to_tuple(self, cursor):
cursor.collapseToCurrentRegion()
rows = cursor.getDataArray()[1:]
if len(rows) == 2:
return []
keys = rows[0]
data = [dict(zip(keys, values)) for values in rows[1:]]
return data
def _get_data_ce(self, doc):
msg = ''
data = {}
try:
sheet = doc.Sheets[0]
rango = sheet['A2:B10']
data = self._data_to_dict(rango.DataArray)
rango = sheet['A13:B23']
data['emisor'] = self._data_to_dict(rango.DataArray)
rango = sheet['A26:B36']
data['receptor'] = self._data_to_dict(rango.DataArray)
rango = sheet['A39:B50']
data['destinatario'] = self._data_to_dict(rango.DataArray)
cursor = sheet.createCursorByRange(sheet['E12'])
data['propietarios'] = self._current_region_to_tuple(cursor)
cursor = sheet.createCursorByRange(sheet['A53'])
data['mercancias'] = self._current_region_to_tuple(cursor)
except Exception as e:
msg = str(e)
return data, msg
def get_ce(self, path):
options = {'AsTemplate': True, 'Hidden': True}
doc = self._doc_open(path, options)
if doc is None:
return (), 'No se pudo abrir la plantilla'
data, msg = self._get_data_ce(doc)
doc.close(True)
if len(data) == 1:
msg = 'Sin datos para importar'
return (), msg
return data, ''
def to_pdf(data, emisor_rfc, ods=False, pdf_from='1'):
rfc = data['emisor']['rfc']
@ -2438,6 +2490,15 @@ def upload_file(rfc, opt, file_obj):
name = '{}_nomina.ods'.format(rfc.lower())
path = _join(PATH_MEDIA, 'tmp', name)
elif opt == 'ceods':
tmp = file_obj.filename.split('.')
ext = tmp[-1].lower()
if ext != 'ods':
msg = 'Extensión de archivo incorrecta, selecciona un archivo ODS'
return {'status': 'server', 'name': msg, 'ok': False}
name = '{}_ce.ods'.format(rfc.lower())
path = _join(PATH_MEDIA, 'tmp', name)
if save_file(path, file_obj.file.read()):
return {'status': 'server', 'name': file_obj.filename, 'ok': True}
@ -2915,6 +2976,20 @@ def import_invoice(rfc):
return (), 'No se encontro LibreOffice'
def import_ceods(rfc):
name = '{}_ce.ods'.format(rfc.lower())
path = _join(PATH_MEDIA, 'tmp', name)
if not is_file(path):
return (), 'No se encontró la plantilla'
if APP_LIBO:
app = LIBO()
if app.is_running:
return app.get_ce(path)
return (), 'No se encontro LibreOffice'
def calc_to_date(value):
return datetime.date.fromordinal(int(value) + 693594)

View File

@ -1084,6 +1084,7 @@ def _save_template(rfc, name, file_obj):
rfc = rfc.lower()
path = _join(PATHS['USER'], f'{rfc}{name}')
if save_file(path, file_obj.file.read()):
result['ok'] = True

View File

@ -70,6 +70,9 @@ class StorageEngine(object):
def _get_importinvoice(self, values):
return main.import_invoice()
def _get_importceods(self, values):
return main.import_ceods()
def _get_main(self, values, session):
return main.config_main(session['userobj'])

View File

@ -157,7 +157,7 @@ def upload_file(rfc, opt, file_obj):
result = util.upload_file(rfc, opt, file_obj)
if result['ok']:
names = ('bdfl', 'employees', 'nomina', 'products', 'invoiceods')
names = ('bdfl', 'employees', 'nomina', 'products', 'invoiceods', 'ceods')
if not opt in names:
Configuracion.add({opt: file_obj.filename})
return result
@ -268,6 +268,18 @@ def import_invoice():
return {'ok': True, 'rows': tuple(products)}
def import_ceods():
log.info('Importando plantilla...')
emisor = Emisor.select()[0]
data, msg = util.import_ceods(emisor.rfc)
if not data:
return {'ok': False, 'msg': msg}
log.info('Plantilla importada...')
return {'ok': True, 'data': data}
def get_doc(type_doc, id, rfc):
types = {
'xml': 'application/xml',

View File

@ -2961,9 +2961,13 @@ function up_invoice_json_on_after_file_add(obj){
}
function _set_from_json_comercioe(data){
function _set_from_json_comercioe(data, json){
try{
values = JSON.parse(data)
if(json){
values = JSON.parse(data)
}else{
values = data
}
}catch(e){
msg_error('Revisa el archivo JSON')
webix.alert({
@ -3048,7 +3052,7 @@ function up_invoice_json_comercioe_on_after_file_add(obj){
let reader = new FileReader()
reader.readAsText(obj.file)
reader.onload = function(){
_set_from_json_comercioe(reader.result)
_set_from_json_comercioe(reader.result, true)
}
$$('win_import_json_comercioe').close()
}
@ -3229,8 +3233,8 @@ function ce_up_template_complete(response){
success: function(text, data, xhr){
var values = data.json()
if (values.ok){
//~ set values
msg_ok('OK')
_set_from_json_comercioe(values.data, false)
//~ msg_ok('Plantilla importada correctamente...')
}else{
webix.alert({
title: 'Error al importar',