Пример #1
0
 public static string add_PerfilXUsuario(PerfilXUsuario d)
 {
     return "{" +
         '"' + "IdPerfilXUsuario" + '"' + ": " + d.IdPerfilXUsuario.ToString() + ',' +
         '"' + "IdPerfil" + '"' + ": " + d.IdPerfil.ToString() + ',' +
         '"' + "IdUsuario" + '"' + ": " + d.IdUsuario.ToString() + ',' +
         '"' + "FechaAsignacion" + '"' + ": " + '"' + Utils.dateToJson(d.FechaAsignacion) + '"' +
         "}";
 }
Пример #2
0
        public ActionResult Create(string nombre, int[] selectedPerfiles, bool estado = false, int tienda = 0)
        {
            //Declaraciones Generales para los request
            ASCIIEncoding encoding = new ASCIIEncoding();
            HttpWebRequest webrequest;
            HttpWebResponse webresponse;
            byte[] data;
            Stream newStream;
            ResponseBD u = new ResponseBD();

            //Select Perfiles
            List<Perfil> p = new List<Perfil>();
            encoding = new ASCIIEncoding();
            data = encoding.GetBytes("");

            webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectAll_Perfil);
            webrequest.Method = Constantes.PostMethod;
            webrequest.ContentType = Constantes.ContentType;
            webrequest.ContentLength = data.Length;

            newStream = webrequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            webresponse = (HttpWebResponse)webrequest.GetResponse();

            using (var reader = new StreamReader(webresponse.GetResponseStream()))
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                var objText = reader.ReadToEnd();
                p = (List<Perfil>)js.Deserialize(objText, typeof(List<Perfil>));
            }

            //Obtener Perfiles seleccionados
            List<Perfil> sp = (List<Perfil>)Session["selectedPerfiles"];

            if (selectedPerfiles == null)
            {
                //Guardar Contraseña
                int idContrasena = 1;

                //Guardar Usuario
                Usuario usu = new Usuario()
                {
                    IdUsuario = 0,
                    Nombre = nombre,
                    IdContrasena = idContrasena,
                    Activo = estado,
                    FechaCreacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    UltimaModificacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                };

                data = encoding.GetBytes(JsonSerializer.add_Usuario(usu));

                Debug.WriteLine(JsonSerializer.add_Usuario(usu));

                webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_Usuario);
                webrequest.Method = Constantes.PostMethod;
                webrequest.ContentType = Constantes.ContentType;
                webrequest.ContentLength = data.Length;

                newStream = webrequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                webresponse = (HttpWebResponse)webrequest.GetResponse();

                using (var reader = new StreamReader(webresponse.GetResponseStream()))
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    var objText = reader.ReadToEnd();
                    u = (ResponseBD)js.Deserialize(objText, typeof(ResponseBD));
                }

                if (u.Flujo.Equals(Constantes.OK))
                {
                    int idUsuario = Int32.Parse(u.Mensaje);

                    int i = 0;

                    foreach (var per in sp)
                    {
                        PerfilXUsuario pxu = new PerfilXUsuario()
                        {
                            IdUsuario = idUsuario,
                            IdPerfil = per.IdPerfil,
                            IdPerfilXUsuario = 0,
                            FechaAsignacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture)
                        };

                        data = encoding.GetBytes(JsonSerializer.add_PerfilXUsuario(pxu));

                        webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_PerfilXUsuario);
                        webrequest.Method = Constantes.PostMethod;
                        webrequest.ContentType = Constantes.ContentType;
                        webrequest.ContentLength = data.Length;

                        newStream = webrequest.GetRequestStream();
                        newStream.Write(data, 0, data.Length);
                        newStream.Close();

                        webresponse = (HttpWebResponse)webrequest.GetResponse();

                        using (var reader = new StreamReader(webresponse.GetResponseStream()))
                        {
                            JavaScriptSerializer js = new JavaScriptSerializer();
                            var objText = reader.ReadToEnd();
                            u = (ResponseBD)js.Deserialize(objText, typeof(ResponseBD));
                        }

                        if (u.Flujo.Equals("ERROR"))
                        {
                            ViewBag.Mensaje = u.Mensaje;
                            ViewBag.Productos = sp;
                            return View(p);
                        }

                        i++;
                    }

                    return RedirectToAction("Index");
                }

                //Select Tienda
                data = encoding.GetBytes("");

                webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Combo_Tienda);
                webrequest.Method = Constantes.PostMethod;
                webrequest.ContentType = Constantes.ContentType;
                webrequest.ContentLength = data.Length;

                newStream = webrequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                webresponse = (HttpWebResponse)webrequest.GetResponse();

                using (var reader = new StreamReader(webresponse.GetResponseStream()))
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    var objText = reader.ReadToEnd();
                    ViewBag.Tienda = (List<Tienda>)js.Deserialize(objText, typeof(List<Tienda>));
                }

                ViewBag.Mensaje = u.Mensaje;
                ViewBag.Perfiles = p;
                return View(sp);

            }

            foreach (var item in selectedPerfiles)
            {
                Perfil prd = new Perfil();

                data = encoding.GetBytes(JsonSerializer.selectById_Perfil(item));

                webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectById_Perfil);
                webrequest.Method = Constantes.PostMethod;
                webrequest.ContentType = Constantes.ContentType;
                webrequest.ContentLength = data.Length;

                newStream = webrequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                webresponse = (HttpWebResponse)webrequest.GetResponse();

                using (var reader = new StreamReader(webresponse.GetResponseStream()))
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    var objText = reader.ReadToEnd();
                    prd = (Perfil)js.Deserialize(objText, typeof(Perfil));
                }

                sp.Add(prd);

            }

            foreach (var i in sp)
            {
                p.RemoveAll(s => s.IdPerfil == i.IdPerfil);
            }

            ViewBag.Perfiles = p;

            Session["selectedPerfiles"] = sp;

            //Select Tienda
            data = encoding.GetBytes("");

            webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectAll_Tienda);
            webrequest.Method = Constantes.PostMethod;
            webrequest.ContentType = Constantes.ContentType;
            webrequest.ContentLength = data.Length;

            newStream = webrequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            webresponse = (HttpWebResponse)webrequest.GetResponse();

            using (var reader = new StreamReader(webresponse.GetResponseStream()))
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                var objText = reader.ReadToEnd();
                ViewBag.Tienda = (List<Tienda>)js.Deserialize(objText, typeof(List<Tienda>));
            }

            return View(sp);
        }