public ActionResult CrearEvento(CrearEventoViewModel vm)
 {
     if (!this.esOrganizador())
     {
         return(RedirectToAction("Logout", "Home", new { mensaje = @"Usted no tiene los permisos necesarios 
                                                     para utilizar el recurso.
                                                     Por favor inicie sesión con las credenciales adecuadas" }));
     }
     else
     {
         if (vm.Fecha < DateTime.Now)
         {
             ModelState.AddModelError("", "La fecha debe ser mayor a la actual.");
         }
         if (vm.Evento.Nombre == null || vm.Evento.Nombre == "")
         {
             ModelState.AddModelError("", "Debe ingresar un Nombre para el evento.");
         }
         if (ModelState.IsValid)
         {
             using (GestionEventosContext db = new GestionEventosContext())
             {
                 Evento auxEv = db.Eventos.Find(vm.Evento.Nombre);
                 if (auxEv != null)
                 {
                     ModelState.AddModelError("", "Ya existe un Evento con ese nombre.");
                 }
                 else
                 {
                     var fechaYDireccion = db.Eventos.Where(p => p.Direccion == vm.Evento.Direccion)
                                           .Where(p => p.Fecha == vm.Fecha).FirstOrDefault();
                     if (fechaYDireccion != null)
                     {
                         ModelState.AddModelError("", "Ya existe un Evento en esa fecha y direccion");
                     }
                     else
                     {
                         TipoEvento tipoEv = db.TipoEventos.Find(vm.IdTipoEvento);
                         db.Entry(tipoEv).Collection(p => p.TiposServicios).Load();
                         vm.Evento.TipoEvento     = tipoEv;
                         Session["CrearEventoVM"] = vm;
                         return(RedirectToAction("SeleccionarProveedoresEvento"));
                     }
                 }
             }
         }
         else
         {
             vm = (CrearEventoViewModel)TempData["Evento"];
             TempData["Evento"] = vm;
         }
         return(View(vm));
     }
 }
 public ActionResult CrearEvento()
 {
     if (!this.esOrganizador())
     {
         return(RedirectToAction("Logout", "Home", new { mensaje = @"Usted no tiene los permisos necesarios 
                                                     para utilizar el recurso.
                                                     Por favor inicie sesión con las credenciales adecuadas" }));
     }
     else
     {
         CrearEventoViewModel vm = null;
         using (GestionEventosContext db = new GestionEventosContext())
         {
             vm = new CrearEventoViewModel(db.TipoEventos.ToList());
         }
         TempData["Evento"] = vm;
         return(View(vm));
     }
 }
        public ActionResult SeleccionarProveedoresEvento()
        {
            if (!this.esOrganizador())
            {
                return(RedirectToAction("Logout", "Home", new { mensaje = @"Usted no tiene los permisos necesarios 
                                                            para utilizar el recurso.
                                                            Por favor inicie sesión con las credenciales adecuadas" }));
            }
            else
            {
                CrearEventoViewModel vm = (CrearEventoViewModel)Session["CrearEventoVM"];
                using (GestionEventosContext db = new GestionEventosContext())
                {
                    Organizador orgLogueado = db.Organizadores.Find(Session["OrganizadorLogueado"].ToString());
                    if (orgLogueado != null)
                    {
                        Evento tmpEvento = new Evento()
                        {
                            Nombre               = vm.Evento.Nombre,
                            Fecha                = vm.Fecha,
                            Direccion            = vm.Evento.Direccion,
                            TipoEvento           = vm.Evento.TipoEvento,
                            Realizado            = false,
                            Organizador          = orgLogueado,
                            ServiciosContratados = new List <ServicioContratado>()
                        };

                        List <TipoServicio> listaTipoServ = tmpEvento.TipoEvento.TiposServicios;
                        vm.TipoServicios = new SelectList(listaTipoServ, "NombreTipoServicio", "NombreTipoServicio");
                        vm.TipoEventos   = new SelectList(db.TipoEventos.ToList(), "NombreTipoEvento", "NombreTipoEvento");
                    }
                }


                return(View(vm));
            }
        }
        public ActionResult CargarServiciosAEvento()
        {
            if (!this.esOrganizador())
            {
                return(RedirectToAction("Logout", "Home", new { mensaje = @"Usted no tiene los permisos necesarios 
                                                            para utilizar el recurso.
                                                            Por favor inicie sesión con las credenciales adecuadas" }));
            }
            else
            {
                CrearEventoViewModel auxVm = (CrearEventoViewModel)Session["CrearEventoVM"];

                using (GestionEventosContext db = new GestionEventosContext())
                {
                    Organizador org = db.Organizadores.Find(Session["OrganizadorLogueado"].ToString());
                    List <ServicioContratado> ContratadosParaElEvento = new List <ServicioContratado>();
                    if (auxVm.Evento.ServiciosContratados != null && auxVm.Evento.ServiciosContratados.Count > 0)
                    {
                        foreach (ServicioContratado tmpServCont in auxVm.Evento.ServiciosContratados)
                        {
                            Servicio           auxSer          = db.Servicios.Find(tmpServCont.Rut, tmpServCont.NombreServicio);
                            ServicioContratado nuevoContratado = new ServicioContratado()
                            {
                                Fecha          = auxVm.Fecha,
                                Rut            = tmpServCont.Rut,
                                NombreServicio = tmpServCont.NombreServicio,
                                NombreEvento   = auxVm.Evento.Nombre,
                                Servicio       = auxSer,
                                yaCalificado   = false
                            };
                            ContratadosParaElEvento.Add(nuevoContratado);
                        }
                    }
                    else
                    {
                        return(RedirectToAction("Error", new { mensaje = "Debe seleccionar al menos un Servicio para poder dar de alta un Evento." }));
                    }
                    TipoEvento tipoEventoElegido = db.TipoEventos.Find(auxVm.IdTipoEvento);
                    Evento     nuevoEvento       = new Evento()
                    {
                        Nombre               = auxVm.Evento.Nombre,
                        Fecha                = auxVm.Fecha,
                        Direccion            = auxVm.Evento.Direccion,
                        Organizador          = org,
                        TipoEvento           = tipoEventoElegido,
                        Realizado            = false,
                        ServiciosContratados = ContratadosParaElEvento
                    };
                    db.Eventos.Add(nuevoEvento);
                    try
                    {
                        db.SaveChanges();
                        return(RedirectToAction("Exito", new { mensaje = "Su evento fue creado exitosamente!" }));
                    }
                    catch
                    {
                        return(RedirectToAction("Error", new { mensaje = "Hubo un problema al agregar el registro en la Base de Datos" }));
                    }
                }
            }
        }
        public ActionResult CargarServiciosAEvento(CrearEventoViewModel vm = null, string rut = "", string nombreServicio = "")
        {
            if (!this.esOrganizador())
            {
                return(RedirectToAction("Logout", "Home", new { mensaje = @"Usted no tiene los permisos necesarios 
                                                            para utilizar el recurso.
                                                            Por favor inicie sesión con las credenciales adecuadas" }));
            }
            else
            {
                CrearEventoViewModel auxVm = (CrearEventoViewModel)Session["CrearEventoVM"];
                if (nombreServicio != "")
                {
                    if (auxVm.Evento.ServiciosContratados != null)
                    {
                        bool mostrarError = auxVm.Evento.ServiciosContratados.Any(p => p.NombreServicio == nombreServicio && p.Rut == rut);
                        if (mostrarError)
                        {
                            ModelState.AddModelError("", "El servicio seleccionado ya existe en la lista de servicios contratados del evento");
                        }
                    }
                    if (ModelState.IsValid)
                    {
                        using (GestionEventosContext db = new GestionEventosContext())
                        {
                            Servicio tmpServicio = db.Servicios.Find(rut, nombreServicio);
                            if (tmpServicio != null)
                            {
                                ServicioContratado tmpServicioContratado = new ServicioContratado()
                                {
                                    Fecha          = auxVm.Fecha,
                                    Rut            = rut,
                                    NombreServicio = nombreServicio,
                                    NombreEvento   = auxVm.Evento.Nombre,
                                    yaCalificado   = false,
                                    Servicio       = tmpServicio
                                };
                                auxVm.Evento.ServiciosContratados.Add(tmpServicioContratado);
                                foreach (Proveedor auxProv in auxVm.Proveedores)
                                {
                                    if (auxProv.Rut == rut)
                                    {
                                        auxProv.ServiciosOfrecidos.RemoveAll(x => x.NombreServicio == nombreServicio);
                                    }
                                }
                                Session["CrearEventoVM"] = auxVm;
                            }
                        }
                    }//
                }
                else
                {
                    if (rut != "")
                    {
                        ViewBag.Rut = rut;
                        foreach (Proveedor tmpProv in auxVm.Proveedores)
                        {
                            if (tmpProv.Rut == rut)
                            {
                                auxVm.ProveedorElegido   = tmpProv;
                                Session["CrearEventoVM"] = auxVm;
                            }
                        }
                    }
                }

                return(View(auxVm));
            }
        }
 public ActionResult SeleccionarProveedoresEvento(CrearEventoViewModel vm = null, string rut = "")
 {
     if (!this.esOrganizador())
     {
         return(RedirectToAction("Logout", "Home", new { mensaje = @"Usted no tiene los permisos necesarios 
                                                     para utilizar el recurso.
                                                     Por favor inicie sesión con las credenciales adecuadas" }));
     }
     else
     {
         CrearEventoViewModel auxVm = (CrearEventoViewModel)Session["CrearEventoVM"];
         if (vm != null)
         {
             auxVm.IdTipoServicio     = vm.IdTipoServicio;
             Session["CrearEventoVM"] = auxVm;
         }
         if (rut != "")
         {
             foreach (Proveedor tmpProv in auxVm.Proveedores)
             {
                 if (tmpProv.Rut == rut)
                 {
                     auxVm.ProveedorElegido   = tmpProv;
                     Session["CrearEventoVM"] = auxVm;
                 }
             }
         }
         else
         {
             if (auxVm.IdTipoServicio != null)
             {
                 using (GestionEventosContext db = new GestionEventosContext())
                 {
                     List <Servicio>  listaServicios    = db.Servicios.Where(p => p.TipoServicio.NombreTipoServicio == auxVm.IdTipoServicio).ToList();
                     List <Proveedor> listaProveedores  = db.Proveedores.Include("Calificaciones").ToList();
                     List <Proveedor> listaProvAMostrar = new List <Proveedor>();
                     foreach (Proveedor auxProveedor in listaProveedores)
                     {
                         auxProveedor.ServiciosOfrecidos.Clear();//limpio la lista de servicios del proveedor para no mostrar servicios no adecuados
                         bool aMostrar = false;
                         foreach (Servicio auxServicio in listaServicios)
                         {
                             if (auxServicio.Rut == auxProveedor.Rut)
                             {
                                 auxProveedor.ServiciosOfrecidos.Add(auxServicio);
                                 aMostrar = true;
                             }
                         }
                         if (aMostrar)
                         {
                             listaProvAMostrar.Add(auxProveedor);
                         }
                     }
                     auxVm.Proveedores = listaProvAMostrar.OrderByDescending(p => p.Vip)
                                         .ThenByDescending(p => p.NomFantasia).ToList();
                     auxVm.ServiciosProveedores = listaServicios;
                     Session["CrearEventoVM"]   = auxVm;
                 }
             }
         }
         return(View(auxVm));
     }
 }