Exemple #1
0
        public ActionResult Create(string nombre, string desc, int unidad, int moneda, int presentacion, double precio,
            int tipo, string obs, HttpPostedFileBase file, bool perecible = false, bool estado = false)
        {
            //Declaraciones Generales para los request

            ASCIIEncoding encoding = new ASCIIEncoding();
            HttpWebRequest webrequest;
            HttpWebResponse webresponse;
            byte[] data;
            Stream newStream;
            ResponseBD u = new ResponseBD();
            Producto p = new Producto();

            try
            {
                string imagen_url = "";
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Images/barabares_img"), fileName);
                    file.SaveAs(path);
                    //var request = ControllerContext.RequestContext.HttpContext.Request;
                    string base_url = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
                    imagen_url = base_url + Constantes.URL_IMAGENES_BARABARES + fileName;
                }

                p = new Producto()
                {
                    IdProducto = 0,
                    Nombre = nombre,
                    Descripcion = desc,
                    IdUnidadProducto = unidad,
                    Presentacion = presentacion,
                    PrecioUnitario = precio,
                    Observaciones = obs,
                    Perecible = perecible,
                    Activo = estado,
                    IdTipoProducto = tipo,
                    IdMoneda = moneda,
                    FechaCreacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    Imagen = imagen_url
                };

                data = encoding.GetBytes(JsonSerializer.add_Producto(p));

                Debug.WriteLine(JsonSerializer.add_Producto(p));

                webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_Producto);
                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 id = Int32.Parse(u.Mensaje);
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                LogBarabares b = new LogBarabares()
                {
                    Accion = Constantes.LOG_CREAR,
                    Servicio = Constantes.Add_Producto,
                    Input = JsonSerializer.add_Producto(p),
                    Descripcion = ex.ToString(),
                    Clase = (p == null) ? "null" : p.GetType().Name,
                    Aplicacion = Constantes.ENTORNO_SISTEMA,
                    Estado = Constantes.ERROR,
                    Ip = "",
                    IdUsuario = 1 //TODO: obtener usuario de la sesión

                };

                data = encoding.GetBytes(JsonSerializer.add_LogBarabares(b));

                webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_LogBarabares);
                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));
                }

                return RedirectToAction("Index");
            }

            //Select Unidad Producto
            data = encoding.GetBytes("");

            webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Combo_UnidadProducto);
            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.UnidadProducto = (List<UnidadProducto>)js.Deserialize(objText, typeof(List<UnidadProducto>));
            }

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

            webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Combo_Moneda);
            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.Moneda = (List<Moneda>)js.Deserialize(objText, typeof(List<Moneda>));
            }

            if (tipo == 1)
                ViewBag.Title = "Cerveza";
            else if (tipo == 2)
                ViewBag.Title = "Vino";
            else if (tipo == 3)
                ViewBag.Title = "Whisky";
            else if (tipo == 4)
                ViewBag.Title = "Vodka";
            else if (tipo == 5)
                ViewBag.Title = "Ron";
            else if (tipo == 6)
                ViewBag.Title = "Tequila";
            else if (tipo == 7)
                ViewBag.Title = "Pisco";
            else if (tipo == 8)
                ViewBag.Title = "Licores";
            else if (tipo == 9)
                ViewBag.Title = "Bebidas";
            else if (tipo == 10)
                ViewBag.Title = "Snacks";
            else if (tipo == 11)
                ViewBag.Title = "Cigarros";

            ViewBag.Tipo = tipo;

            ViewBag.Mensaje = u.Mensaje;
            return View();
        }
Exemple #2
0
 public static string add_Producto(Producto d)
 {
     return "{" +
         '"' + "IdProducto" + '"' + ": " + d.IdProducto.ToString() + ',' +
         '"' + "IdTipoProducto" + '"' + ": " + d.IdTipoProducto.ToString() + ',' +
         '"' + "Nombre" + '"' + ": " + '"' + d.Nombre + '"' + ',' +
         '"' + "Descripcion" + '"' + ": " + '"' + d.Descripcion + '"' + ',' +
         '"' + "IdUnidadProducto" + '"' + ": " + d.IdUnidadProducto.ToString() + ',' +
         '"' + "IdMoneda" + '"' + ": " + d.IdMoneda.ToString() + ',' +
         '"' + "Presentacion" + '"' + ": " + d.Presentacion.ToString() + ',' +
         '"' + "Perecible" + '"' + ": " + (d.Perecible ? "true" : "false") + ',' +
         '"' + "Activo" + '"' + ": " + (d.Activo ? "true" : "false") + ',' +
         '"' + "PrecioUnitario" + '"' + ": " + d.PrecioUnitario.ToString() + ',' +
         '"' + "Observaciones" + '"' + ": " + '"' + d.Observaciones + '"' + ',' +
         '"' + "FechaCreacion" + '"' + ": " + '"' + Utils.dateToJson(d.FechaCreacion) + '"' + ',' +
         '"' + "Imagen" + '"' + ": " + '"' + d.Imagen + '"' +
         "}";
 }
        public ActionResult Create(string nombrePromocion, string desc, string inicio, string fin, int moneda,
            string obs,int[] cantidad, int[] selectedProducts, int precio = 0, bool estadoPromocion = false, bool semana = false)
        {
            //Declaraciones Generales para los request

            ASCIIEncoding encoding = new ASCIIEncoding();
            HttpWebRequest webrequest;
            HttpWebResponse webresponse;
            byte[] data;
            Stream newStream;
            ResponseBD u = new ResponseBD();

            //Select Producto
            List<Producto> sp = new List<Producto>();

            data = encoding.GetBytes("");

            webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectAll_Producto);
            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();
                sp = (List<Producto>)js.Deserialize(objText, typeof(List<Producto>));
            }

            //Obtener productos seleccionados

            List<Producto> p = (List<Producto>)Session["selectedProducts"];

            if (selectedProducts == null)
            {
                //Guardar promoción

                Promocion promo = new Promocion()
                {
                    IdPromocion = 0,
                    Nombre = nombrePromocion,
                    Descripcion = desc,
                    PrecioUnitario = precio,
                    //Observaciones = obs,
                    IdMoneda = moneda,
                    Semana = semana,
                    FechaInicio = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    FechaFin= DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    Imagen = "Imagen"
                };

                data = encoding.GetBytes(JsonSerializer.add_Promocion(promo));

                webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_Promocion);
                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 id = Int32.Parse(u.Mensaje);

                    //Agregar Productos seleccionados

                    int i = 0;

                    foreach (var prod in p)
                    {
                        DetallePromocion det = new DetallePromocion()
                        {
                            IdPromocion = id,
                            IdDetallePromocion = i + 1,
                            Cantidad = cantidad[i],
                            PrecioUnitario = prod.PrecioUnitario,
                            IdProducto = prod.IdProducto
                        };

                        data = encoding.GetBytes(JsonSerializer.add_DetallePromocion(det));

                        webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_DetallePromocion);
                        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("List");
                }
                else
                {
                    ViewBag.Mensaje = u.Mensaje;
                    ViewBag.Productos = sp;
                    return View(p);
                }
            }

            foreach (var item in selectedProducts)
            {
                Producto prd = new Producto();

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

                webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectById_Producto);
                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 = (Producto)js.Deserialize(objText, typeof(Producto));
                }

                p.Add(prd);

            }

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

            ViewBag.Productos = sp;

            Session["selectedProducts"] = p;

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

            webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Combo_Moneda);
            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.Moneda = (List<Moneda>)js.Deserialize(objText, typeof(List<Moneda>));
            }

            return View(p);
        }