Example #1
0
        public ActionResult AgregarArticulo(VentasAgregarViewModel aVM, string articulo)
        {
            if (aVM.ArticuloIdAgregar != 0)
            {
                if (articulo != "" && articulo != null)
                {
                    var art = _articulosServicios.GetByNameOrCode(articulo);
                    aVM.ArticuloIdAgregar = art.FirstOrDefault().Id; //Int64.Parse(articulo);
                }
            }


            if (ModelState.IsValid)
            {
                if (System.Web.HttpContext.Current.Session["SaldoAFavor"] != null)
                {
                    aVM.SaldoAFavor = decimal.Parse(System.Web.HttpContext.Current.Session["SaldoAFavor"].ToString());
                }
                //VentasAgregarViewModel aVM = new VentasAgregarViewModel();
                if (aVM.ArticuloIdAgregar == 0)
                {
                    var items = System.Web.HttpContext.Current.Session["ListaItemsVentaActual"];
                    aVM.Items = (List <VentaItem>)items;
                    try
                    {
                        if (aVM.Items.Count < 1)
                        {
                            ViewBag.Error = "No pudo agregarse el artículo, vuelva a intentarlo.";
                        }
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Error = "No pudo agregarse el artículo, vuelva a intentarlo.";
                        aVM.Items     = new List <VentaItem>();
                    }
                }
                else
                {
                    decimal cantStock = _stockArticuloSucursalServicios.GetStock(aVM.ArticuloIdAgregar, sucID);
                    if (cantStock > 0)
                    {
                        if (aVM.Items.Any(a => a.ArticuloID == aVM.ArticuloIdAgregar))
                        {
                            ModelState.Clear();
                            var cantActual = aVM.Items.Find(a => a.ArticuloID == aVM.ArticuloIdAgregar).Cantidad;
                            if (cantActual >= cantStock)
                            {
                                ViewBag.Error = "No hay stock suficiente para el artículo seleccionado.";
                            }
                            else
                            {
                                aVM.Items.Find(a => a.ArticuloID == aVM.ArticuloIdAgregar).Cantidad = cantActual + 1;
                            }
                            aVM.ArticuloIdAgregar = 0;
                        }
                        else
                        {
                            aVM.Items.Add(_ventaItemsServicios.AgregarArticuloEnVenta(aVM.ArticuloIdAgregar));
                            ModelState.Clear();
                            aVM.ArticuloIdAgregar = 0;
                        }
                        System.Web.HttpContext.Current.Session["ListaItemsVentaActual"] = aVM.Items;
                    }
                    else
                    {
                        ViewBag.Error = "No hay stock suficiente para el artículo seleccionado.";
                    }
                }
            }
            else
            {
                ViewBag.Error = "No pudo agregarse el artículo, vuelva a intentarlo.";
            }
            return(View("Agregar", aVM));
        }