Beispiel #1
0
        public async Task <ActionResult> Create([Bind(Include = "Id,Time,Duration,Type,MaxAttendance,StartDate,EndDate,SelectedDays")] WeeklyClassSetup weeklyClass)
        {
            List <DayOfWeek>      chosenDays = weeklyClass.SelectedDays.Select(x => (DayOfWeek)Enum.Parse(typeof(DayOfWeek), x)).ToList();
            List <DayOfWeekModel> days       = db.DaysOfWeek.Where(d => chosenDays.Contains(d.Day)).ToList();

            weeklyClass.Days = days;
            foreach (var day in days)
            {
                day.WeeklyClassSetups.Add(weeklyClass);
            }

            // Create Classes
            WeeklySchedule weeklySchedule = new WeeklySchedule
            {
                TimeOfDay       = weeklyClass.Time,
                SchedulingRange = new Period(weeklyClass.StartDate, weeklyClass.EndDate)
            };

            weeklySchedule.SetDays(chosenDays);
            var schedules = new List <Schedule>()
            {
                weeklySchedule
            };
            List <Class> classes = _calendarGenerator.GenerateCalendar(Utilities.Constants.calendarPeriod, schedules).ToList();

            foreach (var item in classes)
            {
                item.Duration      = weeklyClass.Duration;
                item.MaxAttendance = weeklyClass.MaxAttendance;
                item.Type          = weeklyClass.Type;
                item.WeeklyClass   = weeklyClass;
                item.Attendance    = new List <Athlete>();
            }

            weeklyClass.GeneratedClasses = classes;

            ModelState.Remove("Days");
            ModelState.Remove("GeneratedClasses");
            if (ModelState.IsValid)
            {
                db.WeeklyClasses.Add(weeklyClass);
                db.Classes.AddRange(classes);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            else
            {
                var errors = ModelState.Select(x => x.Value.Errors).Where(y => y.Count > 0).ToList();
            }

            return(View(weeklyClass));
        }
        private void generateAppoiment(ViewModelActividadCumplimiento model, int idActcumplimiento)
        {
            //// se asigna fecha inicial a la fecha final para tener solo una fecha de ejecucion
            //model.FechaFinal = model.FechaInicial;
            List <Schedule> schedules = new List <Schedule>();

            if (model.Frecuencia_desc == "norepeat")
            {
                SingleSchedule single1 = new SingleSchedule
                {
                    Name      = model.NombreActividad,
                    TimeOfDay = model.hora, //new TimeSpan(19, 30, 0),
                    Date      = model.FechaInicial.Date
                };
                schedules.Add(single1);
            }
            else if (model.Frecuencia_desc == "daily")
            {
                SimpleRepeatingSchedule simple = new SimpleRepeatingSchedule
                {
                    Name            = model.NombreActividad,
                    TimeOfDay       = model.hora,//new TimeSpan(10, 0, 0),
                    SchedulingRange = new Period(model.FechaInicial.Date, model.FechaFinal.Date),
                    //DaysBetween = model.period
                };
                schedules.Add(simple);
            }
            else if (model.Frecuencia_desc == "weekly")
            {
                WeeklySchedule weekly = new WeeklySchedule
                {
                    Name            = model.NombreActividad,
                    TimeOfDay       = model.hora,//TimeSpan(8, 0, 0),
                    SchedulingRange = new Period(model.FechaInicial.Date, model.FechaFinal.Date),
                };

                //Seteamos loas dias de la semana seleccionados.
                int i = 0;
                List <DayOfWeek> dayOfWeeks = new List <DayOfWeek>();
                if (model.weekly_0 != null)
                {
                    dayOfWeeks.Add(DayOfWeek.Monday);
                    i++;
                }

                if (model.weekly_1 != null)
                {
                    dayOfWeeks.Add(DayOfWeek.Tuesday);
                    i++;
                }

                if (model.weekly_2 != null)
                {
                    dayOfWeeks.Add(DayOfWeek.Wednesday);
                    i++;
                }

                if (model.weekly_3 != null)
                {
                    dayOfWeeks.Add(DayOfWeek.Thursday);
                    i++;
                }

                if (model.weekly_4 != null)
                {
                    dayOfWeeks.Add(DayOfWeek.Friday);
                    i++;
                }

                if (model.weekly_5 != null)
                {
                    dayOfWeeks.Add(DayOfWeek.Saturday);
                    i++;
                }

                if (model.weekly_6 != null)
                {
                    dayOfWeeks.Add(DayOfWeek.Sunday);
                    i++;
                }

                weekly.SetDays(dayOfWeeks);

                schedules.Add(weekly);
            }
            else if (model.Frecuencia_desc == "monthly")
            {
                MonthlySchedule monthly = new MonthlySchedule
                {
                    Name            = model.NombreActividad,
                    TimeOfDay       = model.hora,//TimeSpan(8, 0, 0),
                    DayOfMonth      = model.period,
                    SchedulingRange = new Period(model.FechaInicial.Date, model.FechaFinal.Date),
                };
                schedules.Add(monthly);
            }
            else if (model.Frecuencia_desc == "bimestral")
            {
                EveryXMonthsSchedule everyxMonths = new EveryXMonthsSchedule
                {
                    Name            = model.NombreActividad,
                    MonthsBetween   = 2, //Cada dos meses
                    TimeOfDay       = model.hora,
                    DayOfMonth      = model.period,
                    SchedulingRange = new Period(model.FechaInicial.Date, model.FechaFinal.Date),
                };
                schedules.Add(everyxMonths);
            }
            else if (model.Frecuencia_desc == "trimestral")
            {
                EveryXMonthsSchedule everyxMonths = new EveryXMonthsSchedule
                {
                    Name            = model.NombreActividad,
                    MonthsBetween   = 3, //Cada tres meses
                    TimeOfDay       = model.hora,
                    DayOfMonth      = model.period,
                    SchedulingRange = new Period(model.FechaInicial.Date, model.FechaFinal.Date),
                };
                schedules.Add(everyxMonths);
            }
            else if (model.Frecuencia_desc == "semestral")
            {
                EveryXMonthsSchedule everyxMonths = new EveryXMonthsSchedule
                {
                    Name            = model.NombreActividad,
                    MonthsBetween   = 6, //Cada Seis meses
                    TimeOfDay       = model.hora,
                    DayOfMonth      = model.period,
                    SchedulingRange = new Period(model.FechaInicial.Date, model.FechaFinal.Date),
                };
                schedules.Add(everyxMonths);
            }

            CalendarGenerator         generator    = new CalendarGenerator();
            Period                    period       = new Period(model.FechaInicial.Date, model.FechaFinal.Date);
            IEnumerable <Appointment> appointments = generator.GenerateCalendar(period, schedules);

            foreach (var app in appointments)
            {
                db.Tb_ProgamacionTareas.Add(
                    new ProgamacionTareas
                {
                    ActiCumplimiento_Id = idActcumplimiento,
                    Descripcion         = app.Name,
                    //FechaHora = new DateTime(model.FechaInicial.Year, model.FechaInicial.Month, model.FechaInicial.Day, model.hora.Hours, model.hora.Minutes, model.hora.Seconds),
                    FechaHora  = app.Time,
                    Estado     = true,
                    Finalizada = false,
                }
                    );
            }
            db.SaveChanges();
        }