public void AddGroupClassroom(GroupClassroom pGroupClassroom)
 {
     gvDatabase.GroupClassrooms.Add(pGroupClassroom);
     gvDatabase.SaveChanges();
 }
        public ActionResult UploadCsvFile()
        {
            gvDatabase.Configuration.AutoDetectChangesEnabled = false;
            gvDatabase.Configuration.ValidateOnSaveEnabled    = false;

            List <GroupAssignmentCSVViewModel> vGroupsList = new List <GroupAssignmentCSVViewModel>();
            IEnumerable <DataCSV> vGroups;
            int vPeriod = int.Parse(Request.Cookies["Periodo"].Value);

            var vAttachedFile = System.Web.HttpContext.Current.Request.Files["CsvDoc"];

            if (vAttachedFile == null || vAttachedFile.ContentLength <= 0)
            {
                return(Json(null));
            }
            var vCsvReader = new StreamReader(vAttachedFile.InputStream);

            using (vCsvReader)
            {
                var vReader = new CsvReader(vCsvReader);
                vReader.Configuration.Encoding = Encoding.UTF8;
                vGroups = vReader.GetRecords <DataCSV>().ToList();
                int vGroupNumber = 0;

                foreach (DataCSV group in vGroups)
                {
                    String vState = "", vDetails = ""; // Variables to show the assignation process

                    // Validate that GroupNumber field is a number
                    try { vGroupNumber = Convert.ToInt32(group.Grupo); }
                    catch { }

                    // Validations of each field in the file
                    int vIDGroup     = getIDGroup(vPeriod, group.Sede, vGroupNumber, group.Nombre);
                    int vIdProfessor = getIDProfessor(group.Profesor);
                    int vIdSchedule  = getIDSchedule(group.Dia, group.HoraInicio, group.HoraFin);
                    int vIdClassroom = getIDClassroom(group.Aula, group.Sede);
                    int vIdBlock     = getIDBlock(vIDGroup);

                    int vValidateProfessorSchedule = getProfessorValidationSchedule(vPeriod, vIdProfessor, vIdSchedule);
                    int vValidateClassroomSchedule = getClassroomValidationSchedule(vPeriod, vIdClassroom, vIdSchedule);

                    // If there some error in validation, writes on vDetails a little about the error ocurred.
                    if (vIDGroup == 0)
                    {
                        vDetails = " - Informacion de Grupo incorrecta";
                    }
                    if (vIdProfessor == 0)
                    {
                        vDetails += " - Profesor no existe";
                    }
                    if (vIdSchedule == 0)
                    {
                        vDetails += " - Horario Inválido";
                    }
                    if (vValidateProfessorSchedule == 0)
                    {
                        vDetails += " - Conflicto de Horario del Profesor";
                    }
                    if (vIdClassroom == 0)
                    {
                        vDetails += " - Aula Incorrecta";
                    }
                    if (vValidateClassroomSchedule == 0)
                    {
                        vDetails += " - Conflicto de Horario del Aula";
                    }

                    if (vIDGroup != 0) // Group is ok
                    {
                        // Creates GroupClassroom objet to save it in the database
                        GroupClassroom vGroupClassroom = new GroupClassroom();
                        vGroupClassroom.ScheduleID  = vIdSchedule;
                        vGroupClassroom.GroupID     = vIDGroup;
                        vGroupClassroom.ClassroomID = vIdClassroom;

                        if (vIdSchedule != 0 && vIdClassroom != 0 && vValidateClassroomSchedule != 0) // There is a classroom and schedule to assign
                        {
                            // Validates Schedule COnflict in Block -> if (vValidateBlockSchedule != 0)

                            vState = "Incompleto";
                            AddGroupClassroom(vGroupClassroom);
                            if (vIdProfessor != 0 && (vValidateProfessorSchedule != 0)) //Professor ok
                            {
                                EditGroup(vIDGroup, vIdProfessor);
                                vState = "Completo";
                            }
                        }
                        else
                        {
                            vState = "Error";
                        }
                    }
                    else
                    {
                        vState   = "Error";
                        vDetails = "Grupo no existe - Compruebe que no ha modificado la información por defecto de la plantilla";
                    }

                    // Creates the Object of ViewModel to show in the view.
                    GroupAssignmentCSVViewModel vGA = new GroupAssignmentCSVViewModel();
                    vGA.Grupo             = Convert.ToInt32(group.Grupo);
                    vGA.Profesor          = group.Profesor;
                    vGA.Curso             = group.Nombre;
                    vGA.Dia               = group.Dia;
                    vGA.HInicio           = group.HoraInicio;
                    vGA.HFinal            = group.HoraFin;
                    vGA.EstadoAsignacion  = vState;
                    vGA.Aula              = group.Aula;
                    vGA.DetalleAsignacion = vDetails;
                    vGroupsList.Add(vGA);
                }
            }

            return(Json(vGroupsList, JsonRequestBehavior.AllowGet));
        }
        public ActionResult Horarios(NewScheduleViewModel pNewSchedule)
        {
            //*****************************************************************************************************
            //******************************* Se obtienen los datos del formulario ********************************
            //*****************************************************************************************************
            int vGroupID = Convert.ToInt32(pNewSchedule.Group);

            // 1 Find the group in the database
            Group vGroup = db.Groups.Find(vGroupID);

            // 2 Remove GroupClassroom fields asociated with the Group
            removeGroupClassroomByGroupID(vGroupID);

            // 3 new GroupClassrooms asociated with Group
            //Get the list of schedules and classrooms from the View
            List <NewSchedule> vNewSchedule = pNewSchedule.NewSchedule;

            bool vIsValidateClassroom = true;// = isInternScheduleValid(vNewSchedule);

            foreach (NewSchedule tempSchedule in vNewSchedule)
            {
                Schedule vTempSchedule = existSchedule(tempSchedule.Day, tempSchedule.StartHour, tempSchedule.EndHour);
                int      vClassroomID  = Convert.ToInt32(tempSchedule.Classroom);

                if (isValidScheduleClassroom(vTempSchedule, vClassroomID, vGroupID))
                {
                    GroupClassroom vNewGroupClassroom = new GroupClassroom();

                    vNewGroupClassroom.ClassroomID = Convert.ToInt32(tempSchedule.Classroom);

                    vNewGroupClassroom.ScheduleID = vTempSchedule.ID;

                    vNewGroupClassroom.GroupID = vGroup.ID;

                    vGroup.GroupsClassroom.Add(vNewGroupClassroom);
                }
                else
                {
                    vIsValidateClassroom = false;
                }
            }
            if (vIsValidateClassroom)
            {
                if (vGroup.ProfessorID != null)
                {
                    var            vProfessorID = (int)vGroup.ProfessorID;
                    var            vPeriod      = Request.Cookies["Periodo"].Value;
                    var            vPeriodID    = db.Periods.Find(int.Parse(vPeriod)).ID;
                    ScheduleHelper dbHelper     = new ScheduleHelper();

                    string validate = dbHelper.validationsEditGroup(vProfessorID, vGroupID, vPeriodID);

                    if (validate.Equals("true"))
                    {
                        db.SaveChanges();

                        TempData[TempDataMessageKeySuccess] = "Cambios guardados satisfactoriamente";
                        return(RedirectToAction("Index"));
                    }
                    // Exist problems in profesor schedule, so the assign is cancelled and the user recive the information of the problem
                    else if (validate.Equals("falseIsGroupShock"))
                    {
                        TempData[TempDataMessageKeyError] = "Existe choque de horario con el profesor asignado al grupo, no se realizaron los cambios";
                        return(RedirectToAction("Index"));
                    }
                    else if (validate.Equals("falseIsProjectShock"))
                    {
                        TempData[TempDataMessageKeyError] = "Existe choque de horario con el profesor asignado al grupo, no se realizaron los cambios";
                        return(RedirectToAction("Index"));
                    }

                    else if (validate.Equals("falseIsCommissionShock"))
                    {
                        TempData[TempDataMessageKeyError] = "Existe choque de horario con el profesor asignado al grupo, no se realizaron los cambios";
                        return(RedirectToAction("Index"));
                    }
                }
                else
                {
                    db.SaveChanges();

                    TempData[TempDataMessageKeySuccess] = "Cambios guardados satisfactoriamente";
                    return(RedirectToAction("Index"));
                }
            }
            else
            {
                TempData[TempDataMessageKeyError] = "Existe choque de horario con la asignacion del aula";
                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("Index"));
        }