Ejemplo n.º 1
0
        // GET: CenterProducts
        public ActionResult Details(int id)
        {
            CenterProductStockViewModel stockvm = new CenterProductStockViewModel();

            stockvm.ProductStockViewModels = new List <ProductStockViewModel>();
            stockvm.CenterId = id;
            Usuario usuario = db.Usuarios.Find(SessionHelper.GetUser());

            ViewBag.Title   = "Stock del Centro " + usuario.Center?.Descripcion;
            ViewBag.isAdmin = usuario.Rol.IsAdmin;
            List <Product>       products       = db.Products.Where(p => p.Enable).ToList();
            List <CenterProduct> centerProducts = db.CenterProducts.Where(x => x.CenterId == id).ToList();

            foreach (var item in products)
            {
                ProductStockViewModel productStockViewModel = new ProductStockViewModel();
                CenterProduct         centerProduct         = centerProducts.Where(x => x.ProductId == item.Id).FirstOrDefault();
                if (centerProducts.Any(x => x.ProductId == item.Id))
                {
                    productStockViewModel.Stock = centerProduct.Stock;
                }
                else
                {
                    productStockViewModel.Stock = 0;
                }

                productStockViewModel.ProductId = item.Id;
                productStockViewModel.Product   = item;

                stockvm.ProductStockViewModels.Add(productStockViewModel);
            }


            return(View(stockvm));
        }
Ejemplo n.º 2
0
        public JsonResult Create(CenterProductStockViewModel order)
        {
            var responseObject = new
            {
                responseCode = -1
            };

            if (order == null)
            {
                return(Json(new { responseCode = "-10" }));
            }

            try
            {
                int centerId = order.CenterId;
                List <CenterProduct> centerProducts_add = new List <CenterProduct>();
                List <CenterProduct> centerProducts_upd = new List <CenterProduct>();
                List <CenterProduct> centerProducts_dtl = new List <CenterProduct>();
                List <CenterProduct> centerProductsDB   = db.CenterProducts.Where(c => c.CenterId == centerId).ToList();

                //Estos son los que se modificaron o agregaron
                foreach (var item in order.ProductStockViewModels)
                {
                    var centerProductoDB = centerProductsDB.Where(x => x.ProductId == item.ProductId).FirstOrDefault();

                    CenterProduct centerProduct = new CenterProduct
                    {
                        CenterId  = centerId,
                        ProductId = item.ProductId,
                        Stock     = item.Stock
                    };

                    //Si el producto ya estaba dado de alta lo modifico
                    if (centerProductoDB != null)
                    {
                        centerProduct.Id = centerProductoDB.Id;
                        centerProducts_upd.Add(centerProduct);
                    }
                    else
                    {
                        //Sino lo doy de alta
                        centerProducts_add.Add(centerProduct);
                    }
                }
                //Ahora faltan los que ya no vienieron

                List <int> tempIdList = centerProducts_upd.Select(x => x.Id).ToList();

                centerProducts_dtl = centerProductsDB.Where(q => !tempIdList.Contains(q.Id)).ToList();

                foreach (var item in centerProducts_dtl)
                {
                    CenterProduct centerProduct = new CenterProduct
                    {
                        CenterId  = centerId,
                        ProductId = item.ProductId,
                        Stock     = 0,
                        Id        = item.Id
                    };
                    centerProducts_upd.Add(centerProduct);
                }


                db.CenterProducts.AddRange(centerProducts_add);

                foreach (var model in centerProducts_upd)
                {
                    CenterProduct centerProduct = db.CenterProducts.Find(model.Id);
                    centerProduct.Stock           = model.Stock;
                    db.Entry(centerProduct).State = EntityState.Modified;
                }

                db.SaveChanges();


                AuditHelper.Auditar("Modificacion", order.CenterId.ToString(), className, ModuleDescription, WindowDescription);

                responseObject = new
                {
                    responseCode = 0
                };
            }
            catch (Exception e)
            {
                responseObject = new
                {
                    responseCode = -1
                };
            }

            return(Json(responseObject));
        }