示例#1
0
        public ActionResult Edit(CategoriaEditViewModel categoriaVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(categoriaVm));
            }

            var categoria = Mapper.Map <CategoriaEditViewModel, Categoria>(categoriaVm);

            try
            {
                if (_dbContext.Categorias.Any(ct => ct.NombreCategoria == categoria.NombreCategoria &&
                                              ct.CategoriaId != categoria.CategoriaId))
                {
                    ModelState.AddModelError(string.Empty, "Registro repetido");
                    return(View(categoriaVm));
                }

                _dbContext.Entry(categoria).State = EntityState.Modified;
                _dbContext.SaveChanges();
                TempData["Msg"] = "Registro editado";
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, "Error inesperado al intentar editar un registro");
                return(View(categoriaVm));
            }
        }
        public ActionResult Create(ProductoEditViewModel productoVm)
        {
            if (!ModelState.IsValid)
            {
                productoVm.Categorias  = CombosHelper.GetCategorias();
                productoVm.Proveedores = CombosHelper.GetProveedores();

                return(View(productoVm));
            }

            var producto = Mapper.Map <ProductoEditViewModel, Producto>(productoVm);

            if (!_dbContext.Productos.Any(ct => ct.NombreProducto == producto.NombreProducto &&
                                          ct.CategoriaId == producto.CategoriaId))
            {
                using (var tran = _dbContext.Database.BeginTransaction())
                {
                    try
                    {
                        var folder = "~/Content/Images/Productos/";
                        var file   = "";
                        _dbContext.Productos.Add(producto);
                        _dbContext.SaveChanges();
                        if (productoVm.FotoFile != null)
                        {
                            file = $"{producto.ProductoId}.jpg";
                            var response = FileHelper.UploadPhoto(productoVm.FotoFile, folder, file);
                            if (!response)
                            {
                                file = "SinImagenDisponible.jpg";
                            }
                        }
                        else
                        {
                            file = "SinImagenDisponible.jpg";
                        }

                        producto.Foto = $"{folder}{file}";
                        _dbContext.Entry(producto).State = EntityState.Modified;
                        _dbContext.SaveChanges();
                        tran.Commit();
                        TempData["Msg"] = "Registro agregado";
                        return(RedirectToAction("Index"));
                    }
                    catch (Exception e)
                    {
                        productoVm.Categorias  = CombosHelper.GetCategorias();
                        productoVm.Proveedores = CombosHelper.GetProveedores();

                        ModelState.AddModelError(string.Empty, e.Message);
                        return(View(productoVm));
                    }
                }
            }
            productoVm.Categorias  = CombosHelper.GetCategorias();
            productoVm.Proveedores = CombosHelper.GetProveedores();

            ModelState.AddModelError(string.Empty, "Registro repetido...");
            return(View(productoVm));
        }
示例#3
0
        public ActionResult Edit([Bind(Include = "PaisId,NombrePais")] Pais pais)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.Entry(pais).State = EntityState.Modified;
                    db.SaveChanges();
                    TempData["Msg"] = "Registro editado!!!";

                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException != null && ex.InnerException.InnerException != null &&
                        ex.InnerException.InnerException.Message.Contains("IX"))
                    {
                        ModelState.AddModelError(string.Empty, "Registro repetido!!!");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Error al intentar guardar un registro!!!");
                    }
                }
            }
            return(View(pais));
        }
 public ActionResult Edit([Bind(Include = "CiudadId,NombreCiudad,PaisId")] Ciudad ciudad)
 {
     if (ModelState.IsValid)
     {
         try
         {
             db.Entry(ciudad).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
         catch (Exception ex)
         {
             if (ex.InnerException != null &&
                 ex.InnerException.InnerException != null &&
                 ex.InnerException.InnerException.Message.Contains("IX"))
             {
                 ModelState.AddModelError(string.Empty, "Registro Duplicado!!!");
             }
             else
             {
                 ModelState.AddModelError(string.Empty, "Error al intentar editar un registro");
             }
         }
     }
     ViewBag.PaisId = new SelectList(Helper.GetPaises(), "PaisId", "NombrePais", ciudad.PaisId);
     return(View(ciudad));
 }
示例#5
0
        public ActionResult Edit(PaisEditViewModel paisVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(paisVm));
            }

            var pais = Mapper.Map <PaisEditViewModel, Pais>(paisVm);

            try
            {
                if (_dbContext.Paises.Any(p => p.NombrePais == pais.NombrePais &&
                                          p.PaisId != pais.PaisId))
                {
                    ModelState.AddModelError(string.Empty, "Registro repetido");
                    return(View(paisVm));
                }

                _dbContext.Entry(pais).State = EntityState.Modified;
                _dbContext.SaveChanges();
                TempData["Msg"] = "Registro editado";
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, "Error inesperado al intentar editar un registro");
                return(View(paisVm));
            }
        }
        public ActionResult Edit(ClienteEditViewModel clienteVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(clienteVm));
            }

            var cliente = Mapper.Map <ClienteEditViewModel, Cliente>(clienteVm);

            clienteVm.Paises   = CombosHelper.GetPaises();
            clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId);
            if (_dbContext.Clientes.Any(ct => ct.NombreCliente == cliente.NombreCliente &&
                                        ct.ClienteId != cliente.ClienteId))
            {
                clienteVm.Paises   = CombosHelper.GetPaises();
                clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId);

                ModelState.AddModelError(string.Empty, "Registro repetido");
                return(View(clienteVm));
            }

            if (Session["emailAnterior"] != null)
            {
                emailAnterior = (string)Session["emailAnterior"];
            }

            using (var tran = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    _dbContext.Entry(cliente).State = EntityState.Modified;
                    _dbContext.SaveChanges();
                    if (emailAnterior == null)
                    {
                        UsersHelper.CreateUserAsp(cliente.Email, "Cliente");
                    }
                    else if (emailAnterior != cliente.Email)
                    {
                        UsersHelper.UpdateUserName(emailAnterior, cliente.Email);
                    }

                    tran.Commit();
                    Session["emailAnterior"] = null;

                    TempData["Msg"] = "Registro editado";
                    return(RedirectToAction("Index"));
                }
                catch (Exception e)
                {
                    tran.Rollback();
                    clienteVm.Paises   = CombosHelper.GetPaises();
                    clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId);

                    ModelState.AddModelError(string.Empty, "Error inesperado al intentar editar un registro");
                    return(View(clienteVm));
                }
            }
        }
示例#7
0
 public ActionResult Edit([Bind(Include = "CategoriaId,NombreCategoria,Descripcion")] Categoria categoria)
 {
     if (ModelState.IsValid)
     {
         db.Entry(categoria).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(categoria));
 }
 public ActionResult Edit([Bind(Include = "ClienteId,NombreCliente,Direccion,CodPostal,CiudadId,PaisId")] Cliente cliente)
 {
     if (ModelState.IsValid)
     {
         db.Entry(cliente).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.PaisId   = new SelectList(Helper.GetPaises(), "PaisId", "NombrePais", cliente.PaisId);
     ViewBag.CiudadId = new SelectList(Helper.GetCiudades(), "CiudadId", "NombreCiudad", cliente.CiudadId);
     return(View(cliente));
 }
示例#9
0
        public ActionResult Edit(CiudadEditViewModel ciudadVm)
        {
            if (!ModelState.IsValid)
            {
                ciudadVm.Paises = _dbContext.Paises
                                  .OrderBy(p => p.NombrePais).ToList();

                return(View(ciudadVm));
            }

            var ciudad = Mapper.Map <CiudadEditViewModel, Ciudad>(ciudadVm);

            try
            {
                if (!_dbContext.Ciudades.Any(c => c.NombreCiudad == ciudadVm.NombreCiudad &&
                                             c.PaisId != ciudadVm.PaisId))
                {
                    ciudadVm.Paises = _dbContext.Paises
                                      .OrderBy(p => p.NombrePais).ToList();

                    ModelState.AddModelError(string.Empty, "Registro repetido");
                    return(View(ciudadVm));
                }

                _dbContext.Entry(ciudad).State = EntityState.Modified;
                _dbContext.SaveChanges();
                TempData["Msg"] = "Registro editado";
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                ciudadVm.Paises = _dbContext.Paises
                                  .OrderBy(p => p.NombrePais).ToList();

                ModelState.AddModelError(string.Empty, "Error inesperado al intentar editar un registro");
                return(View(ciudadVm));
            }
        }
示例#10
0
        public ActionResult Edit(ProveedorEditViewModel proveedorVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(proveedorVm));
            }

            var proveedor = Mapper.Map <ProveedorEditViewModel, Proveedor>(proveedorVm);

            proveedorVm.Paises   = CombosHelper.GetPaises();
            proveedorVm.Ciudades = CombosHelper.GetCiudades(proveedorVm.PaisId);

            try
            {
                if (_dbContext.Proveedores.Any(ct => ct.NombreCompania == proveedor.NombreCompania &&
                                               ct.ProveedorId != proveedor.ProveedorId))
                {
                    proveedorVm.Paises   = CombosHelper.GetPaises();
                    proveedorVm.Ciudades = CombosHelper.GetCiudades(proveedorVm.PaisId);

                    ModelState.AddModelError(string.Empty, "Registro repetido");
                    return(View(proveedorVm));
                }
                _dbContext.Entry(proveedor).State = EntityState.Modified;
                _dbContext.SaveChanges();
                TempData["Msg"] = "Registro editado";
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                proveedorVm.Paises   = CombosHelper.GetPaises();
                proveedorVm.Ciudades = CombosHelper.GetCiudades(proveedorVm.PaisId);

                ModelState.AddModelError(string.Empty, "Error inesperado al intentar editar un registro");
                return(View(proveedorVm));
            }
        }
示例#11
0
        public ActionResult Create(VentaEditViewModel ventaVm)
        {
            if (!ModelState.IsValid)
            {
                ventaVm.Clientes = CombosHelper.GetClientes();
                List <DetalleVentaTmp> detallesTmp = _dbContext
                                                     .DetalleVentasTmp.Include(dvt => dvt.Producto)
                                                     .ToList();
                ventaVm.DetallesVenta = Mapper
                                        .Map <List <DetalleVentaTmp>, List <DetalleVentaListViewModel> >(detallesTmp);

                //return View(ventaVm);
                return(View("Create2", ventaVm));
            }

            var cantidadItems = _dbContext.DetalleVentasTmp.Count(dvt => dvt.Cantidad > 0);

            if (cantidadItems == 0)
            {
                ModelState.AddModelError(string.Empty, "Debe contener al menos un item o items con cantidades superiores a 0");
                ventaVm.Clientes = CombosHelper.GetClientes();
                List <DetalleVentaTmp> detallesTmp = _dbContext
                                                     .DetalleVentasTmp.Include(dvt => dvt.Producto)
                                                     .ToList();
                ventaVm.DetallesVenta = Mapper
                                        .Map <List <DetalleVentaTmp>, List <DetalleVentaListViewModel> >(detallesTmp);

                //return View(ventaVm);
                return(View("Create2", ventaVm));
            }

            LimpiarListaDeItemsTemporales();
            Venta venta = Mapper.Map <VentaEditViewModel, Venta>(ventaVm);

            using (var tran = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    venta.EstadoId = CombosHelper.GetEstado("Facturado");
                    _dbContext.Ventas.Add(venta);
                    _dbContext.SaveChanges();

                    var detalles = _dbContext.DetalleVentasTmp.ToList();
                    foreach (var detalleVentaTmp in detalles)
                    {
                        //Creo el detalle venta
                        DetalleVenta detalle = Mapper
                                               .Map <DetalleVentaTmp, DetalleVenta>(detalleVentaTmp);
                        detalle.VentaId = venta.VentaId;       //le asigno el nro de venta
                        _dbContext.DetalleVentas.Add(detalle); //guardo el detalle
                        //Busco el producto para modificar su stock
                        var productoInDb = _dbContext
                                           .Productos.SingleOrDefault(p => p.ProductoId == detalle.ProductoId);
                        productoInDb.UnidadesEnExistencia   -= detalle.Cantidad;
                        _dbContext.Entry(productoInDb).State = EntityState.Modified; //cambio el estado de la entidad
                        _dbContext.DetalleVentasTmp.Remove(detalleVentaTmp);         //borro el detalle temporal
                    }

                    _dbContext.SaveChanges();
                    tran.Commit();
                    //TempData["Msg"] = "Venta guardada";
                    //return RedirectToAction("Index");
                    ViewBag.VentaId = venta.VentaId;
                    return(View("SuccessfullySave"));
                }
                catch (Exception e)
                {
                    ModelState.AddModelError(string.Empty, "Error al intentar guardar la venta");
                    //return View(ventaVm);
                    return(View("Create2", ventaVm));
                }
            }
        }