Пример #1
0
        private void Sincronizaprodsalma()
        {
            try
            {
                migrationTable  = migrationTableController.SelectOne(11);
                migrationFields = migrationFieldController.SelectByTableId(migrationTable.MigrationTableId);
                Sql             = "SELECT ";

                foreach (var f in migrationFields)
                {
                    if (migrationFields.Last() == f)
                    {
                        Sql += f.Expresion + " FROM " + migrationTable.Tabla;
                    }
                    else
                    {
                        Sql += f.Expresion + " , ";
                    }
                }
                Sql += " " + migrationTable.Condicion;
                GetDataTable(Sql);

                productos = productoController.SelectAll();

                decimal i = 0;
                int     I = 0;
                Ambiente.S20 = "AJUSTADO X SINCRONIZACION \n";

                cierreInventario = new CierreInventario();
                cierreInventario.FechaInicial      = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                cierreInventario.FechaFinal        = cierreInventario.FechaInicial.AddMonths(1).AddDays(-1);
                cierreInventario.FechaProgramacion = cierreInventario.FechaFinal.AddDays(1);
                cierreInventario.Etapa1Generada    = true;
                cierreInventario.Etapa2Generada    = false;
                cierreInventario.CreatedAt         = DateTime.Now;
                cierreInventario.CreatedBy         = Ambiente.LoggedUser.UsuarioId;
                cierreInventario.EstacionId        = Ambiente.Estacion.EstacionId;
                cierreInventarioController.InsertOne(cierreInventario);


                foreach (DataRow row in dataTable.Rows)
                {
                    Ambiente.S1       = row["producto"].ToString().Trim().ToUpper();
                    Ambiente.Decimal1 = decimal.TryParse(row["existenc"].ToString().Trim(), out i) == true ? i : 0;
                    Ambiente.Int1     = int.TryParse(row["min"].ToString().Trim(), out I) == true ? I : 0;
                    Ambiente.Int2     = int.TryParse(row["max"].ToString().Trim(), out I) == true ? I : 0;

                    producto = productos.FirstOrDefault(x => x.ProductoId.ToUpper().Equals(Ambiente.S1));

                    if (producto != null)
                    {
                        producto.Stock      = Ambiente.Decimal1;
                        producto.Min        = Ambiente.Int1;
                        producto.Max        = Ambiente.Int2;
                        producto.ValorStock = producto.Costopp * producto.Stock;
                        //**********Cierre inventariop****************//

                        cierreInventariop = new CierreInventariop();
                        cierreInventariop.CierreInventarioId = cierreInventario.CierreInventarioId;
                        cierreInventariop.ProductoId         = producto.ProductoId;
                        cierreInventariop.Descripcion        = producto.Descripcion;
                        cierreInventariop.InvInicial         = producto.Stock;
                        cierreInventariop.Entradas           = 0;
                        cierreInventariop.Salidas            = 0;
                        cierreInventariop.UltimoCosto        = producto.UltimoCosto;
                        cierreInventariop.PrevioVta          = producto.Precio1;
                        cierreInventariop.ValorCosto         = 0;
                        cierreInventariop.ValorVenta         = 0;
                        cierreInventariopController.InsertOne(cierreInventariop);

                        if (producto.TieneLote)
                        {
                            var lotes = loteController.SelecByProducConRestanteCeros(producto);
                            Ambiente.Decimal6 = 0;

                            if (lotes != null)
                            {
                                if (lotes.Count > 0)
                                {
                                    Ambiente.Decimal6 = Ambiente.Decimal1 / lotes.Count;
                                    foreach (var l in lotes)
                                    {
                                        l.StockInicial  = Ambiente.Decimal6;
                                        l.StockRestante = Ambiente.Decimal6;
                                        loteController.Update(l);
                                    }
                                }
                                else
                                {
                                    var l = new Lote();
                                    l.CompraId         = 0;
                                    l.NoLote           = "L0001";
                                    l.ProductoId       = producto.ProductoId;
                                    l.StockInicial     = producto.Stock;
                                    l.StockRestante    = producto.Stock;
                                    l.Caducidad        = DateTime.Now.AddDays(365);
                                    l.CreatedBy        = Ambiente.LoggedUser.UsuarioId;
                                    l.CreatedAt        = DateTime.Now;
                                    l.ReferenciaInt    = 0;
                                    l.ReferenciaString = "AJUSTADO X SINCRONIZACION";
                                    loteController.InsertOne(l);

                                    Ambiente.S20 += " PRODUCTOID: " + producto.ProductoId + " STOCK: " + producto.Stock + " CONTROL_LOTE: TRUE LOTES ENCONTRADOS: " + lotes.Count + "\n";
                                }
                            }
                        }
                        //**************MOVIMIENTO DE INVENTARIO****************//
                        var movInv = new MovInv();
                        movInv.FechaOperacion    = DateTime.Now;
                        movInv.ConceptoMovsInvId = "IIN";
                        movInv.ProductoId        = producto.ProductoId;
                        movInv.CreatedBy         = Ambiente.LoggedUser.UsuarioId;
                        movInv.ProveedorId       = "";
                        movInv.ClienteId         = "";
                        movInv.EstacionId        = Ambiente.Estacion.EstacionId;
                        movInv.ReferenciaId      = 0;
                        movInv.ReferenciapId     = 0;
                        movInv.Es             = "E";
                        movInv.Cantidad       = producto.Stock;
                        movInv.UltimoCosto    = producto.UltimoCosto;
                        movInv.Costopp        = producto.Costopp;
                        movInv.Valor          = producto.Stock * producto.UltimoCosto;
                        movInv.StockAlMomento = 0;
                        movInv.PrecioVta      = producto.Precio1;
                        movInv.Afectacion     = movInv.Es.Equals("E") ? 1 : -1;
                        movInv.IsDeleted      = false;
                        movInv.TieneLote      = false;
                        movInv.NoLote         = "";
                        movInv.Caducidad      = DateTime.Now;
                        movInv.CreatedAt      = DateTime.Now;
                        movInvController.InsertOne(movInv);

                        productoController.Update(producto);
                    }
                    else
                    {
                        Ambiente.S2 += " NO EXISTE " + Ambiente.S1 + " \n";
                    }
                }
                Ambiente.Mensaje("Proceso concluido");
                if (Ambiente.S20.Length > 30)
                {
                    Ambiente.Mensaje(Ambiente.S20);
                }
            }
            catch (Exception ex)
            {
                Ambiente.Mensaje(ex.ToString());
            }
        }