Beispiel #1
0
 public static string add_LogBarabares(LogBarabares p)
 {
     return "{" +
         '"' + "IdUsuario" + '"' + ": " + p.IdUsuario.ToString() + ',' +
         '"' + "Accion" + '"' + ": " + '"' + p.Accion + '"' + ',' +
         '"' + "Servicio" + '"' + ": " + '"' + p.Servicio + '"' + ',' +
         '"' + "Input" + '"' + ": " + '"' + p.Input + '"' + ',' +
         '"' + "Descripcion" + '"' + ": " + '"' + p.Descripcion + '"' + ',' +
         '"' + "Clase" + '"' + ": " + '"' + p.Clase + '"' + ',' +
         '"' + "Aplicacion" + '"' + ": " + '"' + p.Aplicacion + '"' + ',' +
         '"' + "Estado" + '"' + ": " + '"' + p.Estado + '"' + ',' +
         '"' + "Fecha" + '"' + ": " + '"' + Utils.dateToJson(p.Fecha) + '"' + ',' +
         '"' + "Ip" + '"' + ": " + '"' + p.Ip + '"' +
         "}";
 }
Beispiel #2
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();
        }