/// <summary>
 /// Create a new CFG_Schedule object.
 /// </summary>
 /// <param name="doctorId">Initial value of the DoctorId property.</param>
 /// <param name="appointmentTime">Initial value of the AppointmentTime property.</param>
 /// <param name="sunday">Initial value of the Sunday property.</param>
 /// <param name="monday">Initial value of the Monday property.</param>
 /// <param name="tuesday">Initial value of the Tuesday property.</param>
 /// <param name="wednesday">Initial value of the Wednesday property.</param>
 /// <param name="thursday">Initial value of the Thursday property.</param>
 /// <param name="friday">Initial value of the Friday property.</param>
 /// <param name="saturday">Initial value of the Saturday property.</param>
 /// <param name="practiceId">Initial value of the PracticeId property.</param>
 public static CFG_Schedule CreateCFG_Schedule(global::System.Int32 doctorId, global::System.Int32 appointmentTime, global::System.Boolean sunday, global::System.Boolean monday, global::System.Boolean tuesday, global::System.Boolean wednesday, global::System.Boolean thursday, global::System.Boolean friday, global::System.Boolean saturday, global::System.Int32 practiceId)
 {
     CFG_Schedule cFG_Schedule = new CFG_Schedule();
     cFG_Schedule.DoctorId = doctorId;
     cFG_Schedule.AppointmentTime = appointmentTime;
     cFG_Schedule.Sunday = sunday;
     cFG_Schedule.Monday = monday;
     cFG_Schedule.Tuesday = tuesday;
     cFG_Schedule.Wednesday = wednesday;
     cFG_Schedule.Thursday = thursday;
     cFG_Schedule.Friday = friday;
     cFG_Schedule.Saturday = saturday;
     cFG_Schedule.PracticeId = practiceId;
     return cFG_Schedule;
 }
        public ActionResult Edit(ConfigScheduleViewModel formModel, string returnUrl)
        {
            for (int i = 0; i < formModel.DaysOfWeek.Count; i++)
            {
                var dayOfWeek = formModel.DaysOfWeek[i];

                if (dayOfWeek.IsBusinessDay)
                {
                    Match workdayStartRegexMatch = null;
                    Match workdayEndRegexMatch = null;

                    if (string.IsNullOrEmpty(dayOfWeek.WorkdayStartTime))
                        this.ModelState.AddModelError(string.Format("DaysOfWeek[{0}].WorkdayStartTime", i), string.Format(ModelStrings.RequiredValidationMessage, DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(model => model.WorkdayStartTime)));
                    else
                        workdayStartRegexMatch = TimeDataTypeAttribute.Regex.Match(dayOfWeek.WorkdayStartTime);

                    if (string.IsNullOrEmpty(dayOfWeek.WorkdayEndTime))
                        this.ModelState.AddModelError(string.Format("DaysOfWeek[{0}].WorkdayEndTime", i), string.Format(ModelStrings.RequiredValidationMessage, DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(model => model.WorkdayEndTime)));
                    else
                        workdayEndRegexMatch = TimeDataTypeAttribute.Regex.Match(dayOfWeek.WorkdayEndTime);

                    if (workdayStartRegexMatch == null || workdayEndRegexMatch == null || !workdayStartRegexMatch.Success || !workdayEndRegexMatch.Success)
                        continue;

                    int workdayIntegerHourStart = int.Parse(workdayStartRegexMatch.Groups[1].Value) * 100 + int.Parse(workdayStartRegexMatch.Groups[2].Value);
                    int workdayIntegerHourEnd = int.Parse(workdayEndRegexMatch.Groups[1].Value) * 100 + int.Parse(workdayEndRegexMatch.Groups[2].Value);

                    if (workdayIntegerHourStart >= workdayIntegerHourEnd)
                        this.ModelState.AddModelError(string.Format("DaysOfWeek[{0}].WorkdayEndTime", i), string.Format("O campo '{0}' não pode ter um valor igual ou menor que o do campo '{1}'", DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(model => model.WorkdayEndTime), DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(model => model.WorkdayStartTime)));

                    // validates lunch time if it exists

                    if (!string.IsNullOrEmpty(dayOfWeek.LunchStartTime) || !string.IsNullOrEmpty(dayOfWeek.LunchEndTime))
                    {
                        Match lunchStartRegexMatch = null;
                        Match lunchEndRegexMatch = null;

                        if (string.IsNullOrEmpty(dayOfWeek.LunchStartTime))
                            this.ModelState.AddModelError(string.Format("DaysOfWeek[{0}].LunchStartTime", i), string.Format(ModelStrings.RequiredValidationMessage, DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(model => model.LunchStartTime)));
                        else
                            lunchStartRegexMatch = TimeDataTypeAttribute.Regex.Match(dayOfWeek.LunchStartTime);

                        if (string.IsNullOrEmpty(dayOfWeek.LunchEndTime))
                            this.ModelState.AddModelError(string.Format("DaysOfWeek[{0}].LunchEndTime", i), string.Format(ModelStrings.RequiredValidationMessage, DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(model => model.LunchEndTime)));
                        else
                            lunchEndRegexMatch = TimeDataTypeAttribute.Regex.Match(dayOfWeek.LunchEndTime);

                        // lunch must have a start AND end time. If it's ok, let's continue
                        if (!string.IsNullOrEmpty(dayOfWeek.LunchStartTime) && !string.IsNullOrEmpty(dayOfWeek.LunchEndTime))
                        {
                            Debug.Assert(lunchStartRegexMatch != null, "lunchStartRegexMatch != null");
                            var lunchIntegerHourStart = int.Parse(lunchStartRegexMatch.Groups[1].Value) * 100 +
                                                        int.Parse(lunchStartRegexMatch.Groups[2].Value);
                            Debug.Assert(lunchEndRegexMatch != null, "lunchEndRegexMatch != null");
                            var lunchIntegerHourEnd = int.Parse(lunchEndRegexMatch.Groups[1].Value) * 100 +
                                                      int.Parse(lunchEndRegexMatch.Groups[2].Value);

                            if (lunchIntegerHourStart <= workdayIntegerHourStart)
                                this.ModelState.AddModelError(
                                    string.Format("DaysOfWeek[{0}].LunchStartTime", i),
                                    string.Format(
                                        "O campo '{0}' não pode ter um valor igual ou menor que o do campo '{1}'",
                                        DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(
                                            model => model.LunchStartTime),
                                        DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(
                                            model => model.WorkdayStartTime)));

                            if (lunchIntegerHourEnd >= workdayIntegerHourEnd)
                                this.ModelState.AddModelError(
                                    string.Format("DaysOfWeek[{0}].LunchEndTime", i),
                                    string.Format(
                                        "O campo '{0}' não pode ter um valor igual ou maior que o do campo '{1}'",
                                        DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(model => model.LunchEndTime),
                                        DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(
                                            model => model.WorkdayEndTime)));

                            if (lunchIntegerHourStart >= lunchIntegerHourEnd)
                                this.ModelState.AddModelError(
                                    string.Format("DaysOfWeek[{0}].LunchEndTime", i),
                                    string.Format(
                                        "O campo '{0}' não pode ter um valor igual ou menor que o do campo '{1}'",
                                        DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(model => model.LunchEndTime),
                                        DataAnnotationsHelper.GetDisplayName<ConfigScheduleViewModel.DayOfWeek>(
                                            model => model.LunchStartTime)));
                        }
                    }
                }
            };

            if (this.ModelState.IsValid)
            {
                var config = this.Doctor.CFG_Schedule;

                if (config == null)
                {
                    config = new CFG_Schedule { PracticeId = this.DbUser.PracticeId, };
                    this.Doctor.CFG_Schedule = config;
                }

                config.AppointmentTime = (int)formModel.AppointmentDuration;

                config.Sunday = formModel.DaysOfWeek[0].IsBusinessDay;
                config.SundayWorkdayStartTime = formModel.DaysOfWeek[0].WorkdayStartTime;
                config.SundayWorkdayEndTime = formModel.DaysOfWeek[0].WorkdayEndTime;
                config.SundayLunchStartTime = formModel.DaysOfWeek[0].LunchStartTime;
                config.SundayLunchEndTime = formModel.DaysOfWeek[0].LunchEndTime;

                config.Monday = formModel.DaysOfWeek[1].IsBusinessDay;
                config.MondayWorkdayStartTime = formModel.DaysOfWeek[1].WorkdayStartTime;
                config.MondayWorkdayEndTime = formModel.DaysOfWeek[1].WorkdayEndTime;
                config.MondayLunchStartTime = formModel.DaysOfWeek[1].LunchStartTime;
                config.MondayLunchEndTime = formModel.DaysOfWeek[1].LunchEndTime;

                config.Tuesday = formModel.DaysOfWeek[2].IsBusinessDay;
                config.TuesdayWorkdayStartTime = formModel.DaysOfWeek[2].WorkdayStartTime;
                config.TuesdayWorkdayEndTime = formModel.DaysOfWeek[2].WorkdayEndTime;
                config.TuesdayLunchStartTime = formModel.DaysOfWeek[2].LunchStartTime;
                config.TuesdayLunchEndTime = formModel.DaysOfWeek[2].LunchEndTime;

                config.Wednesday = formModel.DaysOfWeek[3].IsBusinessDay;
                config.WednesdayWorkdayStartTime = formModel.DaysOfWeek[3].WorkdayStartTime;
                config.WednesdayWorkdayEndTime = formModel.DaysOfWeek[3].WorkdayEndTime;
                config.WednesdayLunchStartTime = formModel.DaysOfWeek[3].LunchStartTime;
                config.WednesdayLunchEndTime = formModel.DaysOfWeek[3].LunchEndTime;

                config.Thursday = formModel.DaysOfWeek[4].IsBusinessDay;
                config.ThursdayWorkdayStartTime = formModel.DaysOfWeek[4].WorkdayStartTime;
                config.ThursdayWorkdayEndTime = formModel.DaysOfWeek[4].WorkdayEndTime;
                config.ThursdayLunchStartTime = formModel.DaysOfWeek[4].LunchStartTime;
                config.ThursdayLunchEndTime = formModel.DaysOfWeek[4].LunchEndTime;

                config.Friday = formModel.DaysOfWeek[5].IsBusinessDay;
                config.FridayWorkdayStartTime = formModel.DaysOfWeek[5].WorkdayStartTime;
                config.FridayWorkdayEndTime = formModel.DaysOfWeek[5].WorkdayEndTime;
                config.FridayLunchStartTime = formModel.DaysOfWeek[5].LunchStartTime;
                config.FridayLunchEndTime = formModel.DaysOfWeek[5].LunchEndTime;

                config.Saturday = formModel.DaysOfWeek[6].IsBusinessDay;
                config.SaturdayWorkdayStartTime = formModel.DaysOfWeek[6].WorkdayStartTime;
                config.SaturdayWorkdayEndTime = formModel.DaysOfWeek[6].WorkdayEndTime;
                config.SaturdayLunchStartTime = formModel.DaysOfWeek[6].LunchStartTime;
                config.SaturdayLunchEndTime = formModel.DaysOfWeek[6].LunchEndTime;

                this.db.SaveChanges();

                if (!string.IsNullOrEmpty(returnUrl))
                    return this.Redirect(returnUrl);

                return this.RedirectToAction("index", "config");
            }
            else
                return this.View(formModel);
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the CFG_Schedule EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToCFG_Schedule(CFG_Schedule cFG_Schedule)
 {
     base.AddObject("CFG_Schedule", cFG_Schedule);
 }