Пример #1
0
        public ActionResult AddCity(Ciudad ciudad)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.Ciudades.Add(ciudad);
                    db.SaveChanges();
                    return(RedirectToAction($"Details/{ciudad.PaisId}"));
                }
                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!!!");
                    }
                }
            }

            ciudad.Pais = db.Paises.Find(ciudad.PaisId);
            return(View(ciudad));
        }
Пример #2
0
        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 Create([Bind(Include = "CiudadId,NombreCiudad,PaisId")] Ciudad ciudad)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.Ciudades.Add(ciudad);
                    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 dar de alta un registro");
                    }
                }
            }

            ViewBag.PaisId = new SelectList(Helper.GetPaises(), "PaisId", "NombrePais", ciudad.PaisId);
            return(View(ciudad));
        }
Пример #4
0
        public ActionResult Create(ProveedorEditViewModel proveedorVm)
        {
            if (!ModelState.IsValid)
            {
                proveedorVm.Paises   = CombosHelper.GetPaises();
                proveedorVm.Ciudades = CombosHelper.GetCiudades(proveedorVm.PaisId);

                return(View(proveedorVm));
            }

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

            if (!_dbContext.Proveedores.Any(ct => ct.NombreCompania == proveedor.NombreCompania))
            {
                _dbContext.Proveedores.Add(proveedor);
                _dbContext.SaveChanges();
                TempData["Msg"] = "Registro agregado";

                return(RedirectToAction("Index"));
            }
            proveedorVm.Paises   = CombosHelper.GetPaises();
            proveedorVm.Ciudades = CombosHelper.GetCiudades(proveedorVm.PaisId);

            ModelState.AddModelError(string.Empty, "Registro repetido...");
            return(View(proveedorVm));
        }
Пример #5
0
        public ActionResult Create(CiudadEditViewModel ciudadVm)
        {
            if (!ModelState.IsValid)
            {
                ciudadVm.Paises = _dbContext.Paises
                                  .OrderBy(p => p.NombrePais).ToList();

                return(View(ciudadVm));
            }

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

            if (!_dbContext.Ciudades.Any(c => c.NombreCiudad == ciudadVm.NombreCiudad &&
                                         c.PaisId != ciudadVm.PaisId))
            {
                _dbContext.Ciudades.Add(ciudad);
                _dbContext.SaveChanges();
                TempData["Msg"] = "Registro agregado";

                return(RedirectToAction("Index"));
            }

            ciudadVm.Paises = _dbContext.Paises
                              .OrderBy(p => p.NombrePais).ToList();

            ModelState.AddModelError(string.Empty, "Registro repetido...");
            return(View(ciudadVm));
        }
Пример #6
0
        public ActionResult Create([Bind(Include = "CategoriaId,NombreCategoria,Descripcion")] Categoria categoria)
        {
            if (ModelState.IsValid)
            {
                db.Categorias.Add(categoria);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(categoria));
        }
Пример #7
0
        public ActionResult Create([Bind(Include = "ClienteId,NombreCliente,Direccion,CodPostal,CiudadId,PaisId")] Cliente cliente)
        {
            if (ModelState.IsValid)
            {
                db.Clientes.Add(cliente);
                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));
        }
Пример #8
0
        public ActionResult Create(ClienteEditViewModel clienteVm)
        {
            if (!ModelState.IsValid)
            {
                clienteVm.Paises   = CombosHelper.GetPaises();
                clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId);

                return(View(clienteVm));
            }

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

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

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

            using (var tran = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    _dbContext.Clientes.Add(cliente);
                    _dbContext.SaveChanges();

                    UsersHelper.CreateUserAsp(cliente.Email, "Cliente");
                    tran.Commit();
                    TempData["Msg"] = "Registro agregado";

                    return(RedirectToAction("Index"));
                }
                catch (Exception)
                {
                    tran.Rollback();
                    clienteVm.Paises   = CombosHelper.GetPaises();
                    clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId);

                    ModelState.AddModelError(string.Empty, "Registro repetido...");
                    return(View(clienteVm));
                }
            }
        }
Пример #9
0
        public ActionResult Create(CategoriaEditViewModel categoriaVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(categoriaVm));
            }

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

            if (!_dbContext.Categorias.Any(ct => ct.NombreCategoria == categoriaVm.NombreCategoria))
            {
                _dbContext.Categorias.Add(categoria);
                _dbContext.SaveChanges();
                TempData["Msg"] = "Registro agregado";

                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError(string.Empty, "Registro repetido...");
            return(View(categoriaVm));
        }
Пример #10
0
        public ActionResult Create(PaisEditViewModel paisVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(paisVm));
            }

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

            if (!_dbContext.Paises.Any(p => p.NombrePais == paisVm.NombrePais))
            {
                _dbContext.Paises.Add(pais);
                _dbContext.SaveChanges();
                TempData["Msg"] = "Registro agregado";

                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError(string.Empty, "Registro repetido...");
            return(View(paisVm));
        }
Пример #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));
                }
            }
        }