예제 #1
0
        public ActionResult Delete(decimal id)
        {
            int idcarro;

            //obtiene el id del carrito actual
            idcarro = int.Parse(Request.Cookies["CarritoCompra"].Value);
            int cantidadCarr = int.Parse(Request.Cookies["CantidadCarrito"].Value) - 1;

            Response.Cookies["CantidadCarrito"].Value = cantidadCarr.ToString();
            try
            {
                //validar producto y carrito
                if (id == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }
                if (idcarro == null)
                {
                    return(HttpNotFound());
                }
                //busca DetalleCarrito que contenga producto y carrito anteriores
                DETALLECARRITO productoABorrar = db.DETALLECARRITO.Find(id, idcarro);
                //eliminar detalle carrito de la base
                db.DETALLECARRITO.Remove(productoABorrar);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Carrito", new { idcarrito = Convert.ToDecimal(idcarro) }));
            }
            return(RedirectToAction("Carrito", new { idcarrito = Convert.ToDecimal(idcarro) }));
        }
        public ActionResult DeleteConfirmed(decimal id)
        {
            DETALLECARRITO dETALLECARRITO = db.DETALLECARRITO.Find(id);

            db.DETALLECARRITO.Remove(dETALLECARRITO);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "IDPRODUCTO,IDCARRITO,CANTIDADPROD,PRECIO")] DETALLECARRITO dETALLECARRITO)
 {
     if (ModelState.IsValid)
     {
         db.Entry(dETALLECARRITO).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.IDCARRITO  = new SelectList(db.CARRITOCOMPRA, "IDCARRITO", "LUGARENTREGA", dETALLECARRITO.IDCARRITO);
     ViewBag.IDPRODUCTO = new SelectList(db.PRODUCTO, "IDPRODUCTO", "NOMBREPROD", dETALLECARRITO.IDPRODUCTO);
     return(View(dETALLECARRITO));
 }
        // GET: DetallesCarritos/Delete/5
        public ActionResult Delete(decimal id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            DETALLECARRITO dETALLECARRITO = db.DETALLECARRITO.Find(id);

            if (dETALLECARRITO == null)
            {
                return(HttpNotFound());
            }
            return(View(dETALLECARRITO));
        }
        // GET: DetallesCarritos/Edit/5
        public ActionResult Edit(decimal id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            DETALLECARRITO dETALLECARRITO = db.DETALLECARRITO.Find(id);

            if (dETALLECARRITO == null)
            {
                return(HttpNotFound());
            }
            ViewBag.IDCARRITO  = new SelectList(db.CARRITOCOMPRA, "IDCARRITO", "LUGARENTREGA", dETALLECARRITO.IDCARRITO);
            ViewBag.IDPRODUCTO = new SelectList(db.PRODUCTO, "IDPRODUCTO", "NOMBREPROD", dETALLECARRITO.IDPRODUCTO);
            return(View(dETALLECARRITO));
        }
 public void AñadirProducto([Bind(Include = "IDPRODUCTO,IDCARRITO,CANTIDADPROD,PRECIO")] DETALLECARRITO dETALLECARRITO)
 {
     if (ModelState.IsValid)
     {
         bool                  seañadira             = true;
         int                   cantidadCarrito       = 0;
         decimal               idCarrito             = decimal.Parse(Request.Cookies["CarritoCompra"].Value);
         HttpCookie            galletaCantidad       = Request.Cookies.Get("CantidadCarrito");
         HttpCookie            galletaCantidadSalida = new HttpCookie("CantidadCarrito");
         List <DETALLECARRITO> lsDetalles;
         //se obtiene lista de detalles donde su carrito corresponda al carrito de la request
         lsDetalles = db.DETALLECARRITO.Where(m => m.IDCARRITO == idCarrito).ToList();
         //si la lista tiene elementos
         if (lsDetalles != null)
         {         // para cada uno de los elementos
             foreach (DETALLECARRITO detalle in lsDetalles)
             {     //si el producto ya está en la lista
                 if (detalle.IDPRODUCTO == dETALLECARRITO.IDPRODUCTO)
                 { //se saltará el ingreso
                     seañadira = false;
                 }
             }
             if (seañadira)
             {
                 db.DETALLECARRITO.Add(dETALLECARRITO);
                 db.SaveChanges();
             }
         }
         //si la cookie ya existe
         if (galletaCantidad != null)
         {
             if (galletaCantidad.Value != null)
             {
                 //cantidad de productos en el carrito es el valor anterior + 1
                 cantidadCarrito = int.Parse(galletaCantidad.Value) + 1;
             }
             else
             {
                 cantidadCarrito = 1;
             }
             galletaCantidadSalida.Value   = cantidadCarrito.ToString();
             galletaCantidadSalida.Expires = DateTime.Now.AddYears(1);
             Response.Cookies.Set(galletaCantidadSalida);
         }
         else
         {
             //si no existe, se intenta obtener la lista de detallecarrito
             lsDetalles = db.DETALLECARRITO.Where(m => m.IDCARRITO == idCarrito).ToList();
             //si la lista no está vacía
             if (lsDetalles != null)
             {
                 int aux = 0;
                 //para cada uno
                 foreach (DETALLECARRITO detalle in lsDetalles)
                 {
                     aux++;
                 }
                 galletaCantidadSalida.Value   = aux.ToString();
                 galletaCantidadSalida.Expires = DateTime.Now.AddYears(1);
                 Response.Cookies.Set(galletaCantidadSalida);
             }
             else
             {
                 galletaCantidadSalida.Value   = "1";
                 galletaCantidadSalida.Expires = DateTime.Now.AddYears(1);
                 Response.Cookies.Set(galletaCantidadSalida);
             }
         }
     }
     Response.Redirect("~/Detalle/Producto/" + dETALLECARRITO.IDPRODUCTO.ToString());
 }
예제 #7
0
        //FUNCIONAMIENTO DE CARRITO
        // GET: Descripcion del producto
        public ActionResult Producto(int id)
        {
            //obtiene cookie
            HttpCookie carrito = new HttpCookie("CarritoCompra"), cantCarrito = new HttpCookie("CantidadCarrito");
            HttpCookie ckRequest = Request.Cookies.Get("CarritoCompra");

            //valida id de carrito
            if (ckRequest.Value == null)
            {
                id = 1;
                if (db.CARRITOCOMPRA.Count() != 0)
                {
                    id = Convert.ToInt32(db.CARRITOCOMPRA.Max(p => p.IDCARRITO)) + 1;
                }
                //se llena cookie con la información
                Response.Cookies["CarritoCompra"].Value   = id.ToString();
                Response.Cookies["CarritoCompra"].Expires = DateTime.Now.AddYears(1);
                Response.Cookies["CantidadCarrito"].Value = "0";
                CARRITOCOMPRA aux = new CARRITOCOMPRA();
                aux.IDCARRITO     = id;
                aux.IDTARIFAENVIO = 1;
                aux.IDUSUARIO     = 1;
                aux.USUARIO       = db.USUARIO.Find(1);
                aux.TARIFAENVIO   = db.TARIFAENVIO.Find(1);
                //carro.IDCARRITO = id;
                db.CARRITOCOMPRA.Add(aux);
                db.SaveChanges();
            }
            //obtiene valor de cookie (id de carrito)
            decimal idCarrito = decimal.Parse(ckRequest.Value);
            //busca objeto carrito con el id
            CARRITOCOMPRA carro = db.CARRITOCOMPRA.Find(decimal.Parse(Request.Cookies["CarritoCompra"].Value));

            //valida objeto carrito
            if (carro == null)
            {
                carro               = new CARRITOCOMPRA();
                idCarrito           = db.CARRITOCOMPRA.Max(p => p.IDCARRITO) + 1;
                carro.IDCARRITO     = idCarrito;
                carro.USUARIO       = db.USUARIO.Find(1);
                carro.TARIFAENVIO   = db.TARIFAENVIO.Find(1);
                carro.IDUSUARIO     = 1;
                carro.IDTARIFAENVIO = 1;
                db.CARRITOCOMPRA.Add(carro);
                db.SaveChanges();
                ckRequest.Value   = idCarrito.ToString();
                ckRequest.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(ckRequest);
            }
            //valida id de producto
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            //busca objeto producto con el id
            PRODUCTO pRODUCTO = db.PRODUCTO.Find(id);

            //valida objeto
            if (pRODUCTO == null)
            {
                //debe retornar no encontrado pues sería contradictorio que pudiera acceder al detalle de un producto que no existe
                return(HttpNotFound());
            }
            //mete a viewbag producto y carrito
            ViewBag.PROD = pRODUCTO;
            ViewBag.CARR = carro;
            //manda detallecarro en view
            DETALLECARRITO detallecarro = new DETALLECARRITO();

            return(View(detallecarro));
        }