Remove class for import from FacturaLibre

This commit is contained in:
Mauricio Baeza 2021-01-06 21:44:02 -06:00
parent 4b9df44dac
commit 788a1723b4
3 changed files with 3 additions and 398 deletions

View File

@ -19,7 +19,7 @@ menú "Emisor" ficha "Otros Datos", usuario y token de timbrado.
1. Actualizar `git pull origin master` 1. Actualizar `git pull origin master`
1. Entrar a `source/app/controllers/pacs/comerciodigital` y copiar `conf.py.example` a `conf.py` 1. Entrar a `source/app/controllers/pacs/comerciodigital` y copiar `conf.py.example` a `conf.py`
1. Entrar a `source/app/controllers/pacs/finkok` y copiar `conf.py.example` a `conf.py` 1. Entrar a `source/app/controllers/pacs/finkok` y copiar `conf.py.example` a `conf.py`
1. Agregar la variable `TOKEN` al archivo `source/app/conf.py` 1. Agregar la variable `TOKEN = ''` al archivo `source/app/conf.py`
1. Reiniciar el servicio: `sudo systemctl restart empresalibre` 1. Reiniciar el servicio: `sudo systemctl restart empresalibre`
1. Sube de nuevo tus certificados en el menú "Emisor" ficha "Certificado". 1. Sube de nuevo tus certificados en el menú "Emisor" ficha "Certificado".
1. Ve al menú "Opciones", ficha "Otros". 1. Ve al menú "Opciones", ficha "Otros".

View File

@ -16,4 +16,6 @@ LOG_PATH = '/var/log/empresalibre/empresa-libre.log'
# ~ Establece un token personalizado para encriptar las claves # ~ Establece un token personalizado para encriptar las claves
# ~ from secrets import token_hex # ~ from secrets import token_hex
# ~ token_hex(32) # ~ token_hex(32)
# ~ IMPORTANTE: Cada vez que cambies este valor, debes de subir de nuevo
# ~ tus certificados
TOKEN = '' TOKEN = ''

View File

@ -397,36 +397,6 @@ def to_slug(string):
return value.replace(' ', '_') return value.replace(' ', '_')
# ~ def make_xml(data, certificado):
# ~ from .cfdi_xml import CFDI
# ~ cert = SATCertificate(certificado.cer, certificado.key_enc.encode())
# ~ if DEBUG:
# ~ data['emisor']['Rfc'] = certificado.rfc
# ~ data['emisor']['RegimenFiscal'] = '603'
# ~ cfdi = CFDI()
# ~ xml = cfdi.get_xml(data)
# ~ data = {
# ~ 'xsltproc': PATH_XSLTPROC,
# ~ 'xslt': _join(PATH_XSLT, 'cadena.xslt'),
# ~ 'xml': save_temp(xml, 'w'),
# ~ 'openssl': PATH_OPENSSL,
# ~ 'key': save_temp(certificado.key_enc, 'w'),
# ~ 'pass': token,
# ~ }
# ~ args = '"{xsltproc}" "{xslt}" "{xml}" | ' \
# ~ '"{openssl}" dgst -sha256 -sign "{key}" -passin pass:"{pass}" | ' \
# ~ '"{openssl}" enc -base64 -A'.format(**data)
# ~ sello = _call(args)
# ~ _kill(data['xml'])
# ~ _kill(data['key'])
# ~ return cfdi.add_sello(sello)
def timbra_xml(xml, auth): def timbra_xml(xml, auth):
from .pac import Finkok as PAC from .pac import Finkok as PAC
@ -2596,373 +2566,6 @@ def sync_cfdi(files):
return return
class ImportFacturaLibre(object):
def __init__(self, path, rfc):
self._rfc = rfc
self._con = None
self._cursor = None
self._error = ''
self._is_connect = self._connect(path)
self._clientes = []
self._clientes_rfc = []
@property
def error(self):
return self._error
@property
def is_connect(self):
return self._is_connect
def _validate_rfc(self):
sql = "SELECT rfc FROM emisor LIMIT 1"
self._cursor.execute(sql)
obj = self._cursor.fetchone()
if obj is None:
self._error = 'No se encontró al emisor: {}'.format(self._rfc)
return False
if not DEBUG:
if obj['rfc'] != self._rfc:
self._error = 'Los datos no corresponden al RFC: {}'.format(self._rfc)
return False
return True
def _connect(self, path):
try:
self._con = sqlite3.connect(path)
self._con.row_factory = sqlite3.Row
self._cursor = self._con.cursor()
return self._validate_rfc()
except Exception as e:
log.error(e)
self._error = 'No se pudo conectar a la base de datos'
return False
def close(self):
try:
self._cursor.close()
self._con.close()
except:
pass
return
def import_data(self):
data = {}
tables = (
('receptores', 'Socios'),
('cfdfacturas', 'Facturas'),
('categorias', 'Categorias'),
)
for source, target in tables:
data[target] = self._get_table(source)
data['Socios'] += self._clientes
return data
def _get_table(self, table):
return getattr(self, '_{}'.format(table))()
def import_productos(self):
sql = "SELECT * FROM productos"
self._cursor.execute(sql)
rows = self._cursor.fetchall()
fields = (
('id_categoria', 'categoria'),
('noIdentificacion', 'clave'),
('descripcion', 'descripcion'),
('unidad', 'unidad'),
('valorUnitario', 'valor_unitario'),
('existencia', 'existencia'),
('inventario', 'inventario'),
('codigobarras', 'codigo_barras'),
('CuentaPredial', 'cuenta_predial'),
('precio_compra', 'ultimo_precio'),
('minimo', 'minimo'),
)
data = []
sql = """
SELECT nombre, tasa, tipo
FROM impuestos, productos, productosimpuestos
WHERE productos.id=productosimpuestos.id_producto
AND productosimpuestos.id_impuesto=impuestos.id
AND productos.id = ?
"""
for row in rows:
new = {t: row[s] for s, t in fields}
if new['categoria'] == 0:
new['categoria'] = None
new['descripcion'] = ' '.join(new['descripcion'].split())
new['clave_sat'] = DEFAULT_SAT_PRODUCTO
self._cursor.execute(sql, (row['id'],))
impuestos = self._cursor.fetchall()
new['impuestos'] = tuple(impuestos)
data.append(new)
return data
def _categorias(self):
sql = "SELECT * FROM categorias ORDER BY id_padre"
self._cursor.execute(sql)
rows = self._cursor.fetchall()
fields = (
('id', 'id'),
('categoria', 'categoria'),
('id_padre', 'padre'),
)
data = []
for row in rows:
new = {t: row[s] for s, t in fields}
if new['padre'] == 0:
new['padre'] = None
data.append(new)
return data
def _get_cliente(self, invoice):
sql = "SELECT rfc, nombre FROM receptores WHERE id=?"
self._cursor.execute(sql, [invoice['id_cliente']])
obj = self._cursor.fetchone()
if not obj is None:
data = {
'rfc': obj['rfc'],
'slug': to_slug(obj['nombre']),
}
return data
if not invoice['xml']:
return {}
doc = parse_xml(invoice['xml'])
version = doc.attrib['version']
node = doc.find('{}Receptor'.format(PRE[version]))
rfc = node.attrib['rfc']
nombre = node.attrib['nombre']
# ~ Validaciones especiales
tipo_persona = 1
if rfc == 'XEXX010101000':
tipo_persona = 4
elif rfc == 'XAXX010101000':
tipo_persona = 3
elif len(rfc) == 12:
tipo_persona = 2
data = {
'tipo_persona': tipo_persona,
'rfc': rfc,
'nombre': nombre,
'slug': to_slug(nombre),
'es_cliente': True,
'es_activo': False,
}
if not rfc in self._clientes_rfc:
self._clientes_rfc.append(rfc)
self._clientes.append(data)
data = {
'rfc': data['rfc'],
'slug': data['slug'],
}
return data
def _get_detalles(self, id):
sql = "SELECT * FROM cfddetalle WHERE id_cfd=?"
self._cursor.execute(sql, [id])
rows = self._cursor.fetchall()
fields = (
('categoria', 'categoria'),
('cantidad', 'cantidad'),
('unidad', 'unidad'),
('noIdentificacion', 'clave'),
('descripcion', 'descripcion'),
('valorUnitario', 'valor_unitario'),
('importe', 'importe'),
('numero', 'pedimento'),
('fecha', 'fecha_pedimento'),
('aduana', 'aduana'),
('CuentaPredial', 'cuenta_predial'),
('alumno', 'alumno'),
('curp', 'curp'),
('nivel', 'nivel'),
('autorizacion', 'autorizacion'),
)
data = []
for row in rows:
new = {t: row[s] for s, t in fields if row[s]}
data.append(new)
return data
def _get_impuestos(self, id):
sql = "SELECT * FROM cfdimpuestos WHERE id_cfd=?"
self._cursor.execute(sql, [id])
rows = self._cursor.fetchall()
tasas = {
'0': 0.0,
'16': 0.16,
'16.00': 0.16,
'-16': 0.16,
'11': 0.11,
'-10': 0.10,
'-2': 0.02,
'-0.5': 0.005,
'-2/3': 0.106667,
'-10.6667': 0.106667,
'-10.6666': 0.106667,
'-10.666666': 0.106667,
'-10.66660': 0.106667,
'-10.67': 0.106667,
'-10.66666666666667': 0.106667,
'-4': 0.04,
'1': 0.01,
'25': 0.25,
'26.5': 0.265,
'30': 0.30,
'8': 0.08,
}
data = []
for row in rows:
# ~ print (id, dict(row))
filtro = {
'name': row['nombre'],
'tasa': tasas[row['tasa']],
'tipo': row['tipo'][0],
}
new = {
'importe': row['importe'],
'filtro': filtro
}
data.append(new)
return data
def _cfdfacturas(self):
sql = "SELECT * FROM cfdfacturas"
self._cursor.execute(sql)
rows = self._cursor.fetchall()
fields = (
('version', 'version'),
('serie', 'serie'),
('folio', 'folio'),
('fecha', 'fecha'),
('fecha_timbrado', 'fecha_timbrado'),
('formaDePago', 'forma_pago'),
('condicionesDePago', 'condiciones_pago'),
('subTotal', 'subtotal'),
('descuento', 'descuento'),
('TipoCambio', 'tipo_cambio'),
('Moneda', 'moneda'),
('total', 'total'),
('tipoDeComprobante', 'tipo_comprobante'),
('metodoDePago', 'metodo_pago'),
('LugarExpedicion', 'lugar_expedicion'),
('totalImpuestosRetenidos', 'total_retenciones'),
('totalImpuestosTrasladados', 'total_traslados'),
('xml', 'xml'),
('id_cliente', 'cliente'),
('notas', 'notas'),
('uuid', 'uuid'),
('donativo', 'donativo'),
('estatus', 'estatus'),
('regimen', 'regimen_fiscal'),
('xml_acuse', 'acuse'),
)
data = []
for row in rows:
row = dict(row)
if not 'xml_acuse'in row:
row['xml_acuse'] = ''
new = {t: row[s] for s, t in fields}
if not 'uuid' in new or not new['uuid']:
new['uuid'] = None
if not 'xml' in new or new['xml'] is None:
new['xml'] = ''
if row['estatus'] == 'Pagada':
new['pagada'] = True
elif row['estatus'] in ('Cancelada', 'Validada'):
new['cancelada'] = True
if new['fecha'] is None:
new['fecha'] = str(now())
new['total_mn'] = round(row['TipoCambio'] * row['total'], 2)
new['detalles'] = self._get_detalles(row['id'])
new['impuestos'] = self._get_impuestos(row['id'])
new['cliente'] = self._get_cliente(row)
data.append(new)
return data
def _receptores(self):
sql = "SELECT * FROM receptores"
self._cursor.execute(sql)
rows = self._cursor.fetchall()
fields = (
('rfc', 'rfc'),
('nombre', 'nombre'),
('calle', 'calle'),
('noExterior', 'no_exterior'),
('noInterior', 'no_interior'),
('colonia', 'colonia'),
('municipio', 'municipio'),
('estado', 'estado'),
('pais', 'pais'),
('codigoPostal', 'codigo_postal'),
('extranjero', 'es_extranjero'),
('activo', 'es_activo'),
('fechaalta', 'fecha_alta'),
('notas', 'notas'),
('cuentaCliente', 'cuenta_cliente'),
('cuentaProveedor', 'cuenta_proveedor'),
('saldoCliente', 'saldo_cliente'),
('saldoProveedor', 'saldo_proveedor'),
('esCliente', 'es_cliente'),
('esProveedor', 'es_proveedor'),
)
data = []
sql1 = "SELECT correo FROM correos WHERE id_cliente=?"
sql2 = "SELECT telefono FROM telefonos WHERE id_cliente=?"
for row in rows:
new = {t: row[s] for s, t in fields}
new['slug'] = to_slug(new['nombre'])
new['fecha_alta'] = str(parser.parse(new['fecha_alta']))
for _, f in fields:
new[f] = new[f] or ''
if new['es_extranjero']:
new['tipo_persona'] = 4
elif new['rfc'] == 'XAXX010101000':
new['tipo_persona'] = 3
elif len(new['rfc']) == 12:
new['tipo_persona'] = 2
self._cursor.execute(sql1, (row['id'],))
tmp = self._cursor.fetchall()
if tmp:
new['correo_facturas'] = ', '.join([r[0] for r in tmp])
self._cursor.execute(sql2, (row['id'],))
tmp = self._cursor.fetchall()
if tmp:
new['telefonos'] = ', '.join([r[0] for r in tmp])
data.append(new)
return data
class ImportCFDI(object): class ImportCFDI(object):
def __init__(self, xml): def __init__(self, xml):