Beispiel #1
0
        public JsonResult CancelarOrden(int orden)
        {
            int    tipo    = 0;
            string mensaje = "";

            using (var db = new VentaArticulosCreditoEntities())
            {
                try
                {
                    var order = db.Factura.Where(o => o.codigo == orden).FirstOrDefault();
                    var user  = (Usuario)Session["usuario"];

                    order.estado          = 4;
                    db.Entry(order).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    //Envío correo
                    MailController().SendCancelOrder(user, ref tipo, ref mensaje, orden);
                }
                catch (Exception ex)
                {
                    mensaje = "Error al cancelar orden";
                }
            }

            return(Json(new { tipo = tipo, mensaje = mensaje }, JsonRequestBehavior.AllowGet));
        }
Beispiel #2
0
        public JsonResult ConfirmarOrden(int direccionEntrega)
        {
            int    tipo    = 0;
            string mensaje = "";
            int    orden   = 0;

            using (var db = new VentaArticulosCreditoEntities())
            {
                try
                {
                    var user        = (Usuario)Session["usuario"];
                    var carrito     = db.Carrito.Include("Inventario.Articulo").Where(c => c.codigoUsuario == user.codigo).ToList();
                    var correlativo = new CorrelativoFactura(Guid.NewGuid().ToString());
                    int posicion    = 1;

                    //Creo el códio de la factura
                    db.CorrelativoFactura.Add(correlativo);
                    db.SaveChanges();
                    //Creo el encabezado de la factura
                    var factura = new Factura(correlativo.correlativo, DateTime.Now, user.codigo, 0, direccionEntrega);
                    db.Factura.Add(factura);
                    db.SaveChanges();

                    foreach (var car in carrito)
                    {
                        //Creo detalle por cada posición del carrito
                        var detalle = new DetalleFactura(posicion, correlativo.correlativo, (decimal)car.Inventario.precioVenta, (int)car.cantidad, car.Inventario.Articulo.codigo);
                        db.DetalleFactura.Add(detalle);
                        db.SaveChanges();
                        //Recupero el inventario de la posición y le resto la cantidad ordenada
                        var inventario = car.Inventario;
                        inventario.cantidad       -= car.cantidad;
                        db.Entry(inventario).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                        //Elimino la posición del carrito
                        db.Carrito.Remove(car);
                        db.SaveChanges();
                        posicion += 1;
                    }

                    orden = correlativo.correlativo;
                    //Envío correo
                    MailController().SendConfirmOrder(user, ref tipo, ref mensaje, orden);
                }catch (Exception ex)
                {
                    mensaje = "Error al confirmar orden";
                }
            }

            return(Json(new { tipo = tipo, mensaje = mensaje, orden = orden }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult EditDirection(EditDirectionModel model)
        {
            int    tipo    = 0;
            string mensaje = "";

            try
            {
                if (!ModelState.IsValid)
                {
                    //Extrae el primer mensaje de error que tenga el modelo
                    mensaje = ModelState.Values.Select(e => e.Errors).Where(e => e.Count > 0).FirstOrDefault().Select(v => v.ErrorMessage).FirstOrDefault();
                }
                else
                {
                    using (var db = new VentaArticulosCreditoEntities())
                    {
                        var user     = (Usuario)Session["usuario"];
                        var oldDirec = db.Direccion_Usuario.Include("Municipio.Departamento").Where(d => d.codigo == model.codigo).FirstOrDefault();

                        if (HuboCambios(oldDirec, model))
                        {
                            oldDirec.nombre          = model.nombre;
                            oldDirec.apellido        = model.apellido;
                            oldDirec.telefono        = Convert.ToInt32(model.telefono);
                            oldDirec.direccion       = model.direccion;
                            oldDirec.codigoMunicipio = model.municipio;

                            db.Entry(oldDirec).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                            tipo    = 1;
                            mensaje = "Dirección modificada correctamente";
                        }
                        else
                        {
                            mensaje = "Asegurese de modificar al menos un registro";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                mensaje = "Error al cambiar contraseña";
            }

            return(Json(new { tipo = tipo, mensaje = mensaje }, JsonRequestBehavior.AllowGet));
        }
Beispiel #4
0
        public JsonResult GuardarEnCarrito(int inventario, int cantidad)
        {
            int     tipo    = 0;
            string  mensaje = "";
            Boolean exitoso = false;

            using (var db = new VentaArticulosCreditoEntities())
            {
                try
                {
                    var user             = (Usuario)Session["usuario"];
                    var carritoExistente = db.Carrito.Where(c => c.codigoUsuario == user.codigo && c.codigoInventario == inventario).FirstOrDefault();

                    if (carritoExistente == null)
                    {
                        var carrito = new Carrito(user.codigo, inventario, cantidad);

                        db.Carrito.Add(carrito);
                        db.SaveChanges();
                        exitoso = true;
                    }
                    else
                    {
                        carritoExistente.cantidad       += cantidad;
                        db.Entry(carritoExistente).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                        exitoso = true;
                    }

                    if (exitoso)
                    {
                        tipo    = 1;
                        mensaje = "Agregado al carrito";
                    }
                }
                catch (Exception ex)
                {
                    mensaje = "Error al agregar al carrito";
                }
            }

            return(Json(new { tipo = tipo, mensaje = mensaje }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult ChangePassword(ChangePasswordModel model)
        {
            string mensaje = "";
            int    tipo    = 0;

            if (!ModelState.IsValid)
            {
                //Extrae el primer mensaje de error que tenga el modelo
                mensaje = ModelState.Values.Select(e => e.Errors).Where(e => e.Count > 0).FirstOrDefault().Select(v => v.ErrorMessage).FirstOrDefault();
            }
            else
            {
                using (var db = new VentaArticulosCreditoEntities())
                {
                    var passEncrypt = Encrypt.Encripta(model.passwordActual);
                    var user        = (Usuario)Session["usuario"];
                    var passUser    = db.Usuario.Where(u => u.codigo == user.codigo && u.password == passEncrypt).FirstOrDefault();

                    if (passUser != null)
                    {
                        try
                        {
                            passUser.password        = Encrypt.Encripta(model.newPassword);
                            db.Entry(passUser).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                            tipo    = 1;
                            mensaje = "Se ha cambiado su contraseña correctamente";
                        }
                        catch (Exception ex)
                        {
                            mensaje = "Error al cambiar contraseña";
                        }
                    }
                    else
                    {
                        mensaje = "Contraseña incorrecta";
                    }
                }
            }

            return(Json(new { tipo = tipo, mensaje = mensaje }, JsonRequestBehavior.AllowGet));
        }
Beispiel #6
0
        public JsonResult ResetPassword(ResetPasswordViewModel model)
        {
            string mensaje = "";
            int    tipo    = 0;

            if (!ModelState.IsValid)
            {
                //Extrae el primer mensaje de error que tenga el modelo
                mensaje = ModelState.Values.Select(e => e.Errors).Where(e => e.Count > 0).FirstOrDefault().Select(v => v.ErrorMessage).FirstOrDefault();
            }
            else
            {
                using (var db = new VentaArticulosCreditoEntities())
                {
                    var user = db.Usuario.Where(u => u.correo == model.correo).FirstOrDefault();

                    if (user != null)
                    {
                        try
                        {
                            user.password        = Encrypt.Encripta(model.password);
                            db.Entry(user).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                            Helpers.Token.ModificarToken(model.correo, model.token);
                            tipo    = 1;
                            mensaje = "Se ha restaurado su contraseña correctamente";
                        }
                        catch (Exception ex)
                        {
                            mensaje = "Error al restaurar contraseña";
                        }
                    }
                    else
                    {
                        mensaje = "El usuario ya no existe";
                    }
                }
            }

            return(Json(new { tipo = tipo, mensaje = mensaje }, JsonRequestBehavior.AllowGet));
        }
Beispiel #7
0
        public JsonResult CompleteRegister(string email)
        {
            int    tipo    = 0;
            string mensaje = "";

            using (var db = new VentaArticulosCreditoEntities())
            {
                var user = db.Usuario.Where(u => u.correo == email).FirstOrDefault();

                if (user != null)
                {
                    if (user.estado == 0)
                    {
                        try
                        {
                            user.estado          = 1;
                            db.Entry(user).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                            tipo    = 1;
                            mensaje = "Registro completado exitosamente";
                        }
                        catch (Exception ex)
                        {
                            mensaje = "Error al completar registro";
                        }
                    }
                    else
                    {
                        tipo = 2;
                    }
                }
                else
                {
                    mensaje = "El registro no puede completarse";
                }
            }

            return(Json(new { tipo = tipo, mensaje = mensaje }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult UpdateInfo(UpdateInfoModel model)
        {
            int     tipo         = 0;
            string  mensaje      = "";
            string  correo       = "";
            Boolean validaNIT    = false;
            Boolean guardar      = true;
            Boolean validaCorreo = false;

            try
            {
                if (!ModelState.IsValid)
                {
                    //Extrae el primer mensaje de error que tenga el modelo
                    mensaje = ModelState.Values.Select(e => e.Errors).Where(e => e.Count > 0).FirstOrDefault().Select(v => v.ErrorMessage).FirstOrDefault();
                }
                else
                {
                    var user = (Usuario)Session["usuario"];

                    if (HuboCambios(user, model, ref validaNIT, ref validaCorreo))
                    {
                        using (var db = new VentaArticulosCreditoEntities())
                        {
                            if (validaNIT)
                            {
                                if (ValidarNIT.ValidaNIT(model.nit))
                                {
                                    var userNIT = db.Usuario.Where(u => u.nit == model.nit).FirstOrDefault();

                                    if (userNIT == null)
                                    {
                                        user.nit = model.nit;
                                    }
                                    else
                                    {
                                        guardar = false;
                                        mensaje = "Este NIT ya está siendo utilzado";
                                    }
                                }
                                else
                                {
                                    guardar = false;
                                    mensaje = "Ingrese un NIT válido";
                                }
                            }

                            if (validaCorreo)
                            {
                                var userMail = db.Usuario.Where(u => u.correo == model.email).FirstOrDefault();

                                if (userMail != null)
                                {
                                    guardar = false;
                                    mensaje = "Este correo ya está siendo utilizado";
                                }
                            }

                            if (guardar)
                            {
                                user.correo          = model.email;
                                user.nombre          = model.nombre;
                                user.apellido        = model.apellido;
                                user.fechaNacimiento = model.fechaNacimiento;
                                db.Entry(user).State = System.Data.Entity.EntityState.Modified;
                                db.SaveChanges();
                                tipo               = 1;
                                mensaje            = "Información actualizada correctamente";
                                Session["usuario"] = user;
                                correo             = user.correo;
                            }
                        }
                    }
                    else
                    {
                        mensaje = "Asegurese de modificar al menos un registro";
                    }
                }
            }
            catch (Exception ex)
            {
                mensaje = "Error al cambiar contraseña";
            }

            return(Json(new { tipo = tipo, mensaje = mensaje, correo = correo }, JsonRequestBehavior.AllowGet));
        }
        public async Task <JsonResult> CreateArticle(CreateArticleModel model, List <HttpPostedFileBase> imagenes)
        {
            int     tipo            = 0;
            string  mensaje         = "";
            Boolean imagenesValidas = true;

            if (!ModelState.IsValid)
            {
                //Extrae el primer mensaje de error que tenga el modelo
                mensaje = ModelState.Values.Select(e => e.Errors).Where(e => e.Count > 0).FirstOrDefault().Select(v => v.ErrorMessage).FirstOrDefault();
            }

            if (mensaje == "")
            {
                if (imagenes == null)
                {
                    imagenes = new List <HttpPostedFileBase>();
                }

                if (imagenes.Count > 0)
                {
                    foreach (var im in imagenes)
                    {
                        if (im.ContentType.Split('/')[0] != "image")
                        {
                            imagenesValidas = false;
                        }
                    }

                    if (imagenesValidas)
                    {
                        try
                        {
                            using (var db = new VentaArticulosCreditoEntities())
                            {
                                var articulo = new Articulo(model.descripcion, model.subCategoria, model.nombre);
                                db.Articulo.Add(articulo);
                                db.SaveChanges();

                                foreach (var im in imagenes)
                                {
                                    var imagen = new Imagen_Articulo(articulo.codigo);

                                    db.Imagen_Articulo.Add(imagen);
                                    db.SaveChanges();
                                    imagen.imagen          = imagen.codigo + "." + im.ContentType.Split('/')[1];
                                    db.Entry(imagen).State = System.Data.Entity.EntityState.Modified;
                                    db.SaveChanges();
                                    im.SaveAs(ImagesPath() + imagen.imagen);
                                }

                                tipo    = 1;
                                mensaje = "Artículo creado correctamente";
                            }
                        }
                        catch (Exception ex)
                        {
                            mensaje = "Error al crear artículo";
                        }
                    }
                    else
                    {
                        mensaje = "Sólo se permiten imágenes";
                    }
                }
                else
                {
                    mensaje = "Seleccione al menos una imágen";
                }
            }

            return(Json(new { tipo = tipo, mensaje = mensaje }, JsonRequestBehavior.AllowGet));
        }