From 75788474badba2d0ce816257a30a0f2ec67d0a9f Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Thu, 14 Oct 2021 19:20:23 -0500 Subject: [PATCH] Add controller for warehouse movement --- source/app/models/main.py | 37 ++++++++- source/static/js/controller/products.js | 103 +++++++++++++++++++++--- source/static/js/ui/products.js | 17 ++-- 3 files changed, 140 insertions(+), 17 deletions(-) diff --git a/source/app/models/main.py b/source/app/models/main.py index 70cf1f1..40a9a29 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -3684,7 +3684,6 @@ class Usuarios(BaseModel): class Productos(BaseModel): - # ~ almacen = ForeignKeyField(Almacenes, null=True) categoria = ForeignKeyField(Categorias, null=True) clave = TextField(unique=True, index=True) clave_sat = TextField(default='') @@ -9873,6 +9872,42 @@ class InventoryEntries(BaseModel): result = {'ok': True, 'row': {'existencia': new_cant}} return result + def _warehouse_movement(self, args, user): + if not user.es_admin: + msg = 'Solo un administrador puede usar esta función.' + result = {'ok': False, 'msg': msg} + return result + + product = Productos.get(Productos.id==args['id_product']) + if not product.inventario: + msg = 'Este producto no lleva inventario.' + result = {'ok': False, 'msg': msg} + return result + + storage = args['target'] + cant = Decimal(args['cant']) + values = dict( + storage = storage, + product = product, + cant = cant, + ) + obj = InventoryEntries.create(**values) + msg = 'a {}'.format(obj.id) + _save_log(user.usuario, msg, 'IE') + + WareHouseProduct.update_exists(values) + + values['storage'] = args['source'] + values['cant'] *= -1 + obj = InventoryEntries.create(**values) + msg = 'a {}'.format(obj.id) + _save_log(user.usuario, msg, 'IE') + + WareHouseProduct.update_exists(values) + + result = {'ok': True} + return result + @classmethod def post(cls, values, user=None): diff --git a/source/static/js/controller/products.js b/source/static/js/controller/products.js index ab8526d..c8b60a4 100644 --- a/source/static/js/controller/products.js +++ b/source/static/js/controller/products.js @@ -1,4 +1,5 @@ var cfg_products = new Object() +var gis_admin = false function products_default_config(){ @@ -30,6 +31,7 @@ function products_default_config(){ }, success: function(text, data, xhr) { var values = data.json() + gis_admin = values.is_admin if(values.is_admin){ $$('cmd_add_inventory').show() //~ $$('cmd_products_add').show() @@ -478,11 +480,102 @@ function cmd_show_exists_click(id, e, node){ } win_show_exists.init() $$('txt_id_product').setValue(row.id) - $$('grid_warehouse_exists').load('warehouseproduct?opt=by_product&id='+row.id) + $$('grid_warehouse_exists').load('warehouseproduct?opt=by_product&id=' + row.id) + + if(gis_admin){ + $$('lst_warehouse_target').getList().load('/warehouse?opt=for_select') + }else{ + $$('lbl_title_move').hide() + $$('txt_cant_to_move').hide() + $$('lst_warehouse_target').hide() + $$('cmd_warehouse_move').hide() + } + $$('win_show_exists').show() } +function cmd_warehouse_move_click(id, e, node){ + var id_product = $$('txt_id_product').getValue() + var row = $$('grid_warehouse_exists').getSelectedItem() + if (row == undefined){ + msg_error('Selecciona un Almacen origen') + return + } + var warehouse_source = row.id + var cant_to_move = $$('txt_cant_to_move').getValue() + var warehouse_target = $$('lst_warehouse_target').getValue() + + if(!cant_to_move){ + msg_error('La cantidad no puede ser cero') + return + } + + if(cant_to_move > row.exists){ + msg_error('La cantidad a mover no puede ser mayor a la existencia') + return + } + + if (warehouse_target == ''){ + msg_error('Selecciona un Almacen destino') + return + } + + if (warehouse_source == warehouse_target){ + msg_error('Los almacenes origen y destino deben ser diferentes') + return + } + + var values = { + id_product: id_product, + cant: cant_to_move, + source: warehouse_source, + target: warehouse_target + } + + msg = '¿Estás seguro de hacer este movimiento?' + webix.confirm({ + title: 'Movimiento de Almacen', + ok: 'Si', + cancel: 'No', + type: 'confirm-error', + text: msg, + callback:function(result){ + if(result){ + _warehouse_movement(values) + } + } + }) +} + + +function _warehouse_movement(args){ + var values = { + opt: 'warehouse_movement', + values: args, + } + + webix.ajax().sync().post('inventoryentries', values, { + error:function(text, data, XmlHttpRequest){ + msg = 'Ocurrio un error, consulta a soporte técnico' + msg_error(msg) + }, + success:function(text, data, XmlHttpRequest){ + var values = data.json(); + if (values.ok) { + $$('txt_cant_to_move').setValue(0) + $$('lst_warehouse_target').setValue('') + $$('grid_warehouse_exists').load('warehouseproduct?opt=by_product&id=' + args.id_product) + $$('grid_warehouse_exists').clearSelection() + msg_ok('Movimiento realizado correctamente') + }else{ + msg_error(values.msg) + } + } + }) +} + + function cmd_win_show_exists_close_click(id, e, node){ $$('win_show_exists').close() } @@ -815,13 +908,5 @@ function _add_entries_inventory_manual(row_id, data){ } } }) - } - -function cmd_warehouse_move_click(id, e, node){ - var id_product = $$('txt_id_product').getValue() - msg_ok(id_product) -} - - diff --git a/source/static/js/ui/products.js b/source/static/js/ui/products.js index 38b5887..d908b06 100644 --- a/source/static/js/ui/products.js +++ b/source/static/js/ui/products.js @@ -464,9 +464,11 @@ var win_add_inventory = { var grid_warehouse_exists_cols = [ { id: 'id', header: 'ID', width: 75, hidden: true}, - { id: 'warehouse', header: ['Almacen'], fillspace:true}, + { id: 'warehouse', header: ['Almacen'], fillspace:true, + footer: {text: 'Total', css: 'right'}}, { id: 'exists', header: ['Existencia'], width: 100, sort: 'int', - format: webix.i18n.numberFormat, css: 'right' }, + format: webix.i18n.numberFormat, css: 'right', + footer: {content: 'summColumn', css: 'right'} }, ] @@ -481,7 +483,8 @@ var grid_warehouse_exists = { } -var body_win_show_exists = {rows: [{maxHeight: 10}, +var body_win_show_exists = {rows: [{maxHeight: 10, minHeight: 10}, + {minWidth: 500}, {cols: [ {maxWidth: 10}, {view: 'text', id: 'txt_id_product', readonly: true, hidden: true}, @@ -490,8 +493,8 @@ var body_win_show_exists = {rows: [{maxHeight: 10}, ]}, {maxHeight: 10}, {cols: [{maxWidth: 10}, - {view: 'label', label: 'Primero, selecciona el almacen origen en la tabla superior.'}, - {maxWidth: 10}]}, + {view: 'label', id: 'lbl_title_move', label: 'Primero, selecciona el almacen origen en la tabla superior.'}, + ]}, {cols: [{maxWidth: 10}, {view: 'counter', id: 'txt_cant_to_move', label: 'Cantidad a mover:', labelPosition: 'top', step: 1, value: 0, min: 0.01}, @@ -499,11 +502,11 @@ var body_win_show_exists = {rows: [{maxHeight: 10}, labelPosition: 'top', required: false, options: []}, {maxWidth: 10}, {view: 'button', id: 'cmd_warehouse_move', label: 'Mover', maxWidth: 100}, {maxWidth: 10}]}, - {maxHeight: 20}, + {maxHeight: 20, minHeight: 20}, {cols: [{}, {view: 'button', id: 'cmd_win_show_exists_close', label: 'Cerrar', type: 'danger'}, {}]}, - {maxHeight: 20}, + {maxHeight: 20, minHeight: 20}, ]}