From d7b880db6c711db608bec0b9a73d925654e4be62 Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Mon, 16 Oct 2017 23:09:26 -0500 Subject: [PATCH] Importar Clientes --- source/app/controllers/util.py | 90 ++++++++++++++++++++++++++++++++++ source/app/models/main.py | 56 ++++++++++++++++++++- 2 files changed, 144 insertions(+), 2 deletions(-) diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index 6cba2be..3472b6d 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -820,6 +820,7 @@ def _totales(doc, cfdi, version): tn = { '001': 'ISR', '002': 'IVA', + '003': 'IEPS', } traslados = [] retenciones = [] @@ -958,3 +959,92 @@ def send_mail(data): server.close() return {'ok': is_connect, 'msg': msg} + +def get_path_info(path): + path, filename = os.path.split(path) + name, extension = os.path.splitext(filename) + return (path, filename, name, extension) + + +class ImportFacturaLibre(object): + + def __init__(self, path): + self._con = None + self._cursor = None + self._is_connect = self._connect(path) + + @property + def is_connect(self): + return self._is_connect + + def _connect(self, path): + try: + self._con = sqlite3.connect(path) + self._con.row_factory = sqlite3.Row + self._cursor = self._con.cursor() + return True + except Exception as e: + log.error(e) + return False + + def close(self): + try: + self._cursor.close() + self._con.close() + except: + pass + return + + def import_data(self): + data = {} + tables = ( + ('receptores', 'Socios'), + ) + for source, target in tables: + data[target] = self._get_table(source) + return data + + def _get_table(self, table): + return getattr(self, '_{}'.format(table))() + + def _receptores(self): + sql = "SELECT * FROM receptores" + self._cursor.execute(sql) + rows = self._cursor.fetchall() + #~ names = [d[0] for d in self._cursor.description] + fields = ( + ('id', 'id'), + ('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 = [] + + for row in rows: + new = {t: row[s] for s, t in fields} + new['slug'] = to_slug(new['nombre']) + 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 + data.append(new) + return data diff --git a/source/app/models/main.py b/source/app/models/main.py index 24f9dcd..3059944 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -631,6 +631,8 @@ class Socios(BaseModel): es_proveedor = BooleanField(default=False) cuenta_cliente = TextField(default='') cuenta_proveedor = TextField(default='') + saldo_cliente = DecimalField(default=0.0, decimal_places=6, auto_round=True) + saldo_proveedor = DecimalField(default=0.0, decimal_places=6, auto_round=True) web = TextField(default='') correo_facturas = TextField(default='') forma_pago = ForeignKeyField(SATFormaPago, null=True) @@ -1742,6 +1744,40 @@ def _importar_valores(archivo): return +def _importar_factura_libre(archivo): + rfc = input('Introduce el RFC: ').strip().upper() + if not rfc: + msg = 'El RFC es requerido' + log.error(msg) + return + + args = util.get_con(rfc) + if not args: + return + + conectar(args) + + log.info('Importando datos...') + app = util.ImportFacturaLibre(archivo) + if not app.is_connect: + log.error('\tNo se pudo conectar a la base de datos') + return + + data = app.import_data() + for table, rows in data.items(): + log.info('\tImportando: {}'.format(table)) + model = globals()[table] + for row in rows: + try: + model.create(**row) + except IntegrityError: + msg = '\t{}'.format(str(row)) + log.error(msg) + + log.info('Importación terminada...') + return + + CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) help_create_tables = 'Crea las tablas en la base de datos' help_migrate_db = 'Migra las tablas en la base de datos' @@ -1763,10 +1799,11 @@ help_lr = 'Listar RFCs' @click.option('-rfc', '--rfc', help=help_rfc, is_flag=True, default=False) @click.option('-br', '--borrar-rfc', help=help_br, is_flag=True, default=False) @click.option('-lr', '--listar-rfc', help=help_lr, is_flag=True, default=False) -@click.option('-i', '--importar_valores', is_flag=True, default=False) +@click.option('-i', '--importar-valores', is_flag=True, default=False) @click.option('-a', '--archivo') +@click.option('-fl', '--factura-libre', is_flag=True, default=False) def main(iniciar_bd, migrar_bd, nuevo_superusuario, cambiar_contraseña, rfc, - borrar_rfc, listar_rfc, importar_valores, archivo): + borrar_rfc, listar_rfc, importar_valores, archivo, factura_libre): opt = locals() if opt['iniciar_bd']: @@ -1808,6 +1845,21 @@ def main(iniciar_bd, migrar_bd, nuevo_superusuario, cambiar_contraseña, rfc, _importar_valores(opt['archivo']) sys.exit(0) + if opt['factura_libre']: + if not opt['archivo']: + msg = 'Falta la ruta de la base de datos' + raise click.ClickException(msg) + if not util.is_file(opt['archivo']): + msg = 'No es un archivo' + raise click.ClickException(msg) + _, _, _, ext = util.get_path_info(opt['archivo']) + if ext != '.sqlite': + msg = 'No es una base de datos' + raise click.ClickException(msg) + + _importar_factura_libre(opt['archivo']) + sys.exit(0) + return