From 9be913c47d9e4a2cb27bd8c796cfd7323966f118 Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Tue, 15 Jun 2021 23:45:11 -0500 Subject: [PATCH] Add inventory entries post --- source/app/controllers/main.py | 16 +++ source/app/controllers/utils.py | 4 +- source/app/main.py | 4 +- source/app/models/db.py | 6 ++ source/app/models/main.py | 136 +++++++++++++++++++++--- source/static/js/controller/products.js | 8 +- 6 files changed, 153 insertions(+), 21 deletions(-) diff --git a/source/app/controllers/main.py b/source/app/controllers/main.py index 52c88db..4635e5c 100644 --- a/source/app/controllers/main.py +++ b/source/app/controllers/main.py @@ -680,3 +680,19 @@ class AppPartnerProducts(object): req.context['result'] = self._db.partner_products_post(values) resp.status = falcon.HTTP_200 + +class AppInventoryEntries(object): + + def __init__(self, db): + self._db = db + + def on_get(self, req, resp): + values = req.params + req.context['result'] = self._db.inventory_entries_get(values) + resp.status = falcon.HTTP_200 + + def on_post(self, req, resp): + values = req.params + req.context['result'] = self._db.inventory_entries_post(values) + resp.status = falcon.HTTP_200 + diff --git a/source/app/controllers/utils.py b/source/app/controllers/utils.py index f72a858..0341169 100644 --- a/source/app/controllers/utils.py +++ b/source/app/controllers/utils.py @@ -800,8 +800,8 @@ def _products_from_xml(rfc, data): del result['data']['timbre']['SelloSAT'] emisor = result['data']['emisor'] - emisor['rfc'] = emisor['Rfc'] - emisor['nombre'] = emisor['Nombre'] + emisor['rfc'] = emisor.pop('Rfc') + emisor['nombre'] = emisor.pop('Nombre') result['data']['emisor'] = emisor products = result['data']['conceptos'] diff --git a/source/app/main.py b/source/app/main.py index dd6bbad..b4ce8f8 100644 --- a/source/app/main.py +++ b/source/app/main.py @@ -18,7 +18,8 @@ from controllers.main import (AppEmpresas, AppMovimientosBanco, AppTickets, AppStudents, AppEmployees, AppNomina, AppInvoicePay, AppCfdiPay, AppSATBancos, AppSociosCuentasBanco, AppSATFormaPago, AppSATLeyendaFiscales, AppCert, AppSucursales, - AppPartnerProducts + AppPartnerProducts, + AppInventoryEntries, ) @@ -66,6 +67,7 @@ api.add_route('/leyendasfiscales', AppSATLeyendaFiscales(db)) api.add_route('/cert', AppCert(db)) api.add_route('/sucursales', AppSucursales(db)) api.add_route('/partnerproducts', AppPartnerProducts(db)) +api.add_route('/inventoryentries', AppInventoryEntries(db)) session_options = { diff --git a/source/app/models/db.py b/source/app/models/db.py index ad3b107..f2cdff6 100644 --- a/source/app/models/db.py +++ b/source/app/models/db.py @@ -481,6 +481,12 @@ class StorageEngine(object): def partner_products_get(self, filters): return main.PartnerProducts.get_data(filters) + def inventory_entries_get(self, filters): + return main.InventoryEntries.get_data(filters) + + def inventory_entries_post(self, filters): + return main.InventoryEntries.post(filters) + # Companies only in MV def _get_empresas(self, values): return main.companies_get() diff --git a/source/app/models/main.py b/source/app/models/main.py index 869d7de..43f7f93 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -112,13 +112,8 @@ class UploadFile(object): where = ((Socios.rfc == rfc) & (Socios.es_proveedor == True)) try: - row = (Socios - .select(Socios.id) - .where(where) - .get() - .dicts() - ) - emisor['id'] = row['id'] + partner = Socios.select(Socios.id).where(where).get() + emisor['id'] = partner.id except Socios.DoesNotExist: emisor['id'] = 0 @@ -2990,6 +2985,14 @@ class Socios(BaseModel): return {'ok': False} + # ~ v2 + + @classmethod + def post(cls, values): + opt = values['opt'] + args = values['values'] + return getattr(cls, f'_{opt}')(cls, args) + class SociosCuentasBanco(BaseModel): socio = ForeignKeyField(Socios) @@ -9332,9 +9335,11 @@ class PartnerProducts(BaseModel): def _get_product(self, filters): data = {'ok': True} - id = int(filters['partner_id']) - rfc = filters['partner_rfc'] - key = filters['partner_key'] + partner = utils.loads(filters['partner']) + + id = int(partner['id']) + rfc = partner['rfc'] + key = filters['product_key'] where = (Socios.rfc == rfc) if id: @@ -9346,21 +9351,25 @@ class PartnerProducts(BaseModel): Productos.descripcion.alias('description1'), ) try: - row = (PartnerProducts + obj = (PartnerProducts .select(select) .join(Socios).switch(PartnerProducts) .join(Productos).switch(PartnerProducts) .where(where) - .get() - .dicts() + .dicts()[0] ) - except PartnerProducts.DoesNotExist: + # ~ except PartnerProducts.DoesNotExist: + # ~ msg = 'No se encontró un producto existente' + # ~ data = {'ok': False, 'msg': msg} + # ~ return data + except IndexError: msg = 'No se encontró un producto existente' data = {'ok': False, 'msg': msg} return data - print('ROW', row) - data['row'] = row + print('ROW', obj) + + data['row'] = obj return data @@ -9426,6 +9435,101 @@ class InventoryEntries(BaseModel): (('storage', 'product', 'date_add', 'cant'), True), ) + @classmethod + def get_data(cls, filters): + method = f"_get_{filters['opt']}" + return getattr(cls, method)(cls, filters) + + def _get_or_create_partner(self, args): + obj = None + msg = '' + id = int(args['id']) + + type_person = 1 + if len(args['rfc']) == 12: + type_person = 2 + if args['rfc'] == RFCS['FOREIGN']: + type_person = 4 + + fields = dict( + tipo_persona = type_person, + rfc = args['rfc'], + nombre = args['nombre'], + slug = utils.to_slug(args['nombre']), + es_proveedor = True, + ) + try: + if id: + obj = Socios.get(Socios.id==id) + else: + obj = Socios.create(**fields) + except IntegrityError as e: + msg = 'Ocurrio un error, al dar de alta el emisor' + + return obj, msg + + # ~ teresa + def _get_or_create_product(self, args): + # ~ almacen = ForeignKeyField(Almacenes, null=True) + # ~ categoria = ForeignKeyField(Categorias, null=True) + # ~ clave = TextField(unique=True, index=True) + # ~ unidad = ForeignKeyField(SATUnidades) + # ~ ultimo_costo = DecimalField(default=0.0, max_digits=18, decimal_places=6, + # ~ auto_round=True) + # ~ existencia = DecimalField(default=0.0, max_digits=18, decimal_places=2, + # ~ auto_round=True) + # ~ impuestos = ManyToManyField(SATImpuestos, related_name='productos') + id = int(args.get('id_product', '0')) + if id: + obj = Productos.get(Productos.id==id) + else: + # ~ {'cant': '1.0'} + fields = dict( + clave_sat = args['key_sat'], + descripcion = args['description'], + valor_unitario = Decimal(args['unit_value']), + inventario = True, + es_activo = True, + ) + obj = Productos.create(**fields) + + return obj + + + def _add_entries(self, partner, products): + msg = '' + + for p in products: + print(type(p), p) + product = self._get_or_create_product(self, p) + + msg = 'Validando' + return msg + + def _create(self, args): + # ~ print('ARGS', args) + + partner, error = self._get_or_create_partner(self, args['partner']) + if error: + result = {'ok': False, 'msg': error} + return result + + # ~ ToDo - Save invoice + + error = self._add_entries(self, partner, args['products']) + if error: + result = {'ok': False, 'msg': error} + return result + + result = {'ok': True, 'msg': ''} + return result + + @classmethod + def post(cls, values): + opt = values['opt'] + args = utils.loads(values['values']) + return getattr(cls, f'_{opt}')(cls, args) + @util.run_in_thread def _save_log(user, action, table): diff --git a/source/static/js/controller/products.js b/source/static/js/controller/products.js index 8a59fb4..760fae8 100644 --- a/source/static/js/controller/products.js +++ b/source/static/js/controller/products.js @@ -452,8 +452,12 @@ function cmd_close_products_add_click(id, e, node){ function _add_entries_inventory(data){ var grid = $$('grid_partner_products') + var values = { + opt: 'create', + values: data, + } - webix.ajax().sync().post('inventoryentries', data, { + webix.ajax().sync().post('inventoryentries', values, { error:function(text, data, XmlHttpRequest){ msg = 'Ocurrio un error, consulta a soporte técnico' msg_error(msg) @@ -627,7 +631,7 @@ function get_partner_product(grid, row){ var filters = { opt: 'product', partner: cfg_products['partner'], - partner_key: row.key, + product_key: row.key, } if(!partner_id){