public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return(RedirectToAction("Index"));
            }
            tbBox tbBox = db.tbBox.Find(id);

            if (tbBox == null)
            {
                return(RedirectToAction("NotFound", "Login"));
            }
            string           UserName = "";
            int              idUser   = 0;
            List <tbUsuario> User     = Function.getUserInformation();
            tbBodega         tbBod    = new tbBodega();

            foreach (tbUsuario Usuario in User)
            {
                UserName = Usuario.usu_Nombres + " " + Usuario.usu_Apellidos;
                idUser   = Convert.ToInt32(Usuario.emp_Id);
            }
            Session["BoxDetalle"] = null;

            ViewBag.Cod_Box  = id;
            ViewBag.bod_Id   = new SelectList(db.tbBodega.Where(x => x.bod_ResponsableBodega == idUser).ToList(), "bod_Id", "bod_Nombre");
            ViewBag.bod_Prod = db.tbBodega.Where(x => x.bod_ResponsableBodega == idUser).Select(x => x.bod_Id).First();
            ViewBag.Producto = db.tbBodegaDetalle.ToList();
            return(View(tbBox));
        }
        public ActionResult Close(string box_Codigo)
        {
            try
            {
                tbBox vBox = db.tbBox.Find(box_Codigo);
                IEnumerable <object> List = null;
                string MsjError           = "";
                List = db.UDP_Inv_tbBox_Update(box_Codigo, vBox.box_Descripcion, vBox.bod_Id, Models.Helpers.vbox_Cerrada, vBox.box_UsuarioCrea, vBox.box_FechaCrea, Function.GetUser(), Function.DatetimeNow());

                foreach (UDP_Inv_tbBox_Update_Result Box in List)
                {
                    MsjError = Box.MensajeError;
                }
                if (MsjError.StartsWith("-1"))
                {
                    LlenarListas();
                    Function.InsertBitacoraErrores("Box/Edit", MsjError, "Edit");
                    ModelState.AddModelError("", "No se pudo actualizar el registro, favor contacte al administrador.");
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception Ex)
            {
                LlenarListas();
                Function.InsertBitacoraErrores("Box/Edit", Ex.Message.ToString(), "Edit");
                ModelState.AddModelError("", "No se pudo actualizar el registro, favor contacte al administrador.");
                return(RedirectToAction("Index"));
            }
        }
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return(RedirectToAction("Index"));
            }
            tbBox tbBox = db.tbBox.Find(id);

            if (tbBox == null)
            {
                return(RedirectToAction("NotFound", "Login"));
            }
            return(View(tbBox));
        }
        public ActionResult Edit(string id, [Bind(Include = "box_Codigo,box_Descripcion,bod_Id,box_UsuarioCrea,box_FechaCrea")] tbBox tbBox)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    tbBox vBox = db.tbBox.Find(id);
                    IEnumerable <object> List = null;
                    string MsjError           = "";
                    List = db.UDP_Inv_tbBox_Update(tbBox.box_Codigo, tbBox.box_Descripcion, tbBox.bod_Id, tbBox.box_Estado, vBox.box_UsuarioCrea, vBox.box_FechaCrea, Function.GetUser(), Function.DatetimeNow());

                    foreach (UDP_Inv_tbBox_Update_Result Box in List)
                    {
                        MsjError = Box.MensajeError;
                    }
                    if (MsjError.StartsWith("-1"))
                    {
                        LlenarListas();
                        Function.InsertBitacoraErrores("Box/Edit", MsjError, "Edit");
                        ModelState.AddModelError("", "No se pudo actualizar el registro, favor contacte al administrador.");
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        return(RedirectToAction("Index"));
                    }
                }
                catch (Exception Ex)
                {
                    LlenarListas();
                    Function.InsertBitacoraErrores("Box/Create", Ex.Message.ToString(), "Create");
                    ModelState.AddModelError("", "No se pudo actualizar el registro, favor contacte al administrador.");
                    return(RedirectToAction("Index"));
                }
            }
            else
            {
                ViewBag.Producto = db.tbBodegaDetalle.ToList();
                var errors = ModelState.Values.SelectMany(v => v.Errors);
                return(View(tbBox));
            }
        }
        public ActionResult Create([Bind(Include = "box_Codigo,box_Descripcion,bod_Id")] tbBox tbBox)
        {
            ViewBag.Producto = db.tbProducto.ToList();
            var    list                         = (List <tbBoxDetalle>)Session["BoxDetalle"];
            string MensajeError                 = "0";
            string MensajeErrorDetalle          = "0";
            IEnumerable <object> listBox        = null;
            IEnumerable <object> listBoxDetalle = null;

            if (db.tbBox.Any(a => a.box_Codigo == tbBox.box_Codigo))
            {
                ModelState.AddModelError("", "Ya existe una caja con ese código");
            }
            if (ModelState.IsValid)
            {
                ViewBag.bod_Id   = new SelectList(db.tbBodega, "bod_Id", "bod_Nombre");
                ViewBag.Producto = db.tbBodegaDetalle.ToList();
                try
                {
                    using (TransactionScope Tran = new TransactionScope())
                    {
                        listBox = db.UDP_Inv_tbBox_Insert(
                            tbBox.box_Codigo,
                            tbBox.box_Descripcion,
                            tbBox.bod_Id,
                            Function.GetUser(), Function.DatetimeNow()
                            );
                        foreach (UDP_Inv_tbBox_Insert_Result Box in listBox)
                        {
                            MensajeError = Box.MensajeError;
                        }
                        if (MensajeError.StartsWith("-1"))
                        {
                            LlenarListas();
                            Function.InsertBitacoraErrores("Box/Create", MensajeError, "Create");
                            ModelState.AddModelError("", "No se pudo insertar el registro, favor contacte al administrador.");
                            return(View(tbBox));
                        }
                        else
                        {
                            if (list != null)
                            {
                                if (list.Count != 0)
                                {
                                    foreach (tbBoxDetalle Detalle in list)
                                    {
                                        Detalle.box_Codigo = MensajeError;
                                        listBoxDetalle     = db.UDP_Inv_tbBoxDetalle_Insert(Detalle.box_Codigo,
                                                                                            Detalle.prod_Codigo,
                                                                                            Detalle.boxd_Cantidad, Function.GetUser(), Function.DatetimeNow());
                                        foreach (UDP_Inv_tbBoxDetalle_Insert_Result spDetalle in listBoxDetalle)
                                        {
                                            MensajeErrorDetalle = spDetalle.MensajeError;
                                            if (MensajeErrorDetalle.StartsWith("-1"))
                                            {
                                                LlenarListas();
                                                Function.InsertBitacoraErrores("Box/Create", MensajeErrorDetalle, "Create");
                                                ModelState.AddModelError("", "No se pudo insertar el registro detalle, favor contacte al administrador.");
                                                return(View(tbBox));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        Tran.Complete();
                        return(RedirectToAction("Index"));
                    }
                }
                catch (Exception Ex)
                {
                    Function.InsertBitacoraErrores("Box/Create", Ex.Message.ToString(), "Create");
                    ModelState.AddModelError("", "No se pudo insertar el registro, favor contacte al administrador.");
                    LlenarListas();
                    return(View(tbBox));
                }
            }
            else
            {
                ViewBag.Producto = db.tbBodegaDetalle.ToList();
                var errors = ModelState.Values.SelectMany(v => v.Errors);
                return(View(tbBox));
            }
        }