private List <DateTime> CheckDatesAndTimes() { DateTime hireStartDate, hireEndDate, startTime, endTime, hireStartDateTime, hireEndDateTime; bool validDates; List <DateTime> dates = new List <DateTime>(); validDates = DateTime.TryParseExact(hireStartDateTxt.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out hireStartDate); validDates = DateTime.TryParseExact(hireEndDateTxt.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out hireEndDate); if (OpeningTime.CheckTimeValid(hireStartTimeTxt.Text) == true) { startTime = Convert.ToDateTime(hireStartTimeTxt.Text); } else { startTime = DateTime.Now; validDates = false; inputErrorLbl.Text = inputErrorLbl.Text + "Invalid start date entered <br />"; } if (OpeningTime.CheckTimeValid(hireEndTimeTxt.Text) == true) { endTime = Convert.ToDateTime(hireEndTimeTxt.Text); } else { endTime = DateTime.Now; validDates = false; inputErrorLbl.Text = inputErrorLbl.Text + "Invalid end date entered <br />"; } hireStartDateTime = hireStartDate.Date + startTime.TimeOfDay; hireEndDateTime = hireEndDate.Date + endTime.TimeOfDay; //Check minimum amount of hours is 12 to continue if ((hireEndDateTime - hireStartDateTime).TotalHours <= 12) { validDates = false; inputErrorLbl.Text = inputErrorLbl.Text + "End date must be after start date <br />"; } dates.Add(hireStartDateTime); dates.Add(hireEndDateTime); return(dates); }
/// <summary> /// Updates the database with all the times currently in the table for the selected location. /// </summary> protected void AddUpdateBtn_Click(object sender, EventArgs e) { try { List <OpeningTime> openingTimes; long locationID; TextBox openTimeTxt, closeTimeTxt; CheckBox closedChk; DateTime?openTimeChk = null, closeTimeChk = null; DateTime openTime = DateTime.Now, closeTime = DateTime.Now; bool validDates = true; openingTimesTbl.Visible = true; locationID = Convert.ToInt32(Regex.Match(locationDdl.SelectedValue, @"\d+").Value); openingTimes = OpeningTime.GetOpeningTimesByLocationID(locationID); foreach (OpeningTime openingTime in openingTimes) { openTimeTxt = (TextBox)openingTimesTbl.FindControl(openingTime.DayOfWeekNum + "_open"); closeTimeTxt = (TextBox)openingTimesTbl.FindControl(openingTime.DayOfWeekNum + "_close"); closedChk = (CheckBox)openingTimesTbl.FindControl(openingTime.DayOfWeekNum + "_Chk"); validDates = true; if (OpeningTime.CheckTimeValid(openTimeTxt.Text) == true) { openTime = Convert.ToDateTime(openTimeTxt.Text); openTimeChk = openTime; } else if (openTimeTxt.Text == "") { openTimeChk = null; } else { validDates = false; inputErrorLbl.Text = inputErrorLbl.Text + "Invalid opening date on " + openingTime.DayOfWeek + "<br />"; } if (OpeningTime.CheckTimeValid(closeTimeTxt.Text) == true) { closeTime = Convert.ToDateTime(closeTimeTxt.Text); closeTimeChk = closeTime; } else if (closeTimeTxt.Text == "") { closeTimeChk = null; } else { validDates = false; inputErrorLbl.Text = inputErrorLbl.Text + "Invalid closing date on " + openingTime.DayOfWeek + "<br />"; } if (openTimeChk > closeTimeChk) { validDates = false; inputErrorLbl.Text = inputErrorLbl.Text + "End time must be after start time for " + openingTime.DayOfWeek + "<br />"; } if (validDates == true) { //Check if date has been updated and set this day to closed if (closeTimeChk != null && openTimeChk != null) { OpeningTime.UpdateOpeningTimes(openingTime.LocationID, openingTime.DayOfWeekNum, openTime, closeTime, false); closedChk.Checked = false; timeSavedLbl.Text = timeSavedLbl.Text + "Save successful for " + openingTime.DayOfWeek + "<br />"; } //If only closed date has been entered return error else if (openTimeChk != null) { inputErrorLbl.Text = inputErrorLbl.Text + "Only opening date entered on " + openingTime.DayOfWeek + "<br />"; } //If only open date has been entered return error else if (closeTimeChk != null) { inputErrorLbl.Text = inputErrorLbl.Text + "Only closing date entered on " + openingTime.DayOfWeek + "<br />"; } else { OpeningTime.UpdateOpeningTimes(openingTime.LocationID, openingTime.DayOfWeekNum, openTime, closeTime, true); timeSavedLbl.Text = timeSavedLbl.Text + "Save successful for " + openingTime.DayOfWeek + "<br />"; } } } } catch (Exception ex) { generalErrorLbl.Text = "An error has occured saying: " + ex.Message + " Please contact your system administrator."; } }
/// <summary> /// Adds a new holiday opening time record to the selected location /// </summary> protected void HolidayTimeAddBtn_Click(object sender, EventArgs e) { try { TextBox holidayStartTxt; TextBox altOpeningTimeTxt; TextBox altClosingTimeTxt; CheckBox closedChk; DateTime?openTimeChk = null, closeTimeChk = null; DateTime openTime = DateTime.Now, closeTime = DateTime.Now; DateTime holidayStartDate; bool validDates = true; holidayStartTxt = (TextBox)holidayOpeningTimesTbl.FindControl("HolidayStartAdd"); altOpeningTimeTxt = (TextBox)holidayOpeningTimesTbl.FindControl("altOpenTimeAdd"); altClosingTimeTxt = (TextBox)holidayOpeningTimesTbl.FindControl("altCloseTimeAdd"); closedChk = (CheckBox)holidayOpeningTimesTbl.FindControl("ClosedAdd"); //Checks the start date is in a dd/mm/yyyy format validDates = DateTime.TryParseExact(holidayStartTxt.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out holidayStartDate); if (closedChk.Checked == false) { if (OpeningTime.CheckTimeValid(altOpeningTimeTxt.Text) == true) { openTime = Convert.ToDateTime(altOpeningTimeTxt.Text); openTimeChk = openTime; } else if (altOpeningTimeTxt.Text == "") { openTimeChk = null; } else { validDates = false; Session["InputFailed"] = Session["InputFailed"] + "Invalid opening date entered <br />"; } if (OpeningTime.CheckTimeValid(altClosingTimeTxt.Text) == true) { closeTime = Convert.ToDateTime(altClosingTimeTxt.Text); closeTimeChk = closeTime; } else if (altClosingTimeTxt.Text == "") { closeTimeChk = null; } else { validDates = false; Session["InputFailed"] = Session["InputFailed"] + "Invalid closing date entered <br />"; } if (openTimeChk > closeTimeChk) { validDates = false; Session["InputFailed"] = Session["InputFailed"] + "End time must be after start time <br />"; } } if (validDates == true) { Session["HolidayStatus"] = "Holiday time saved"; Session["InputFailed"] = ""; if (closeTimeChk != null && openTimeChk != null) { OpeningTime.InsertHolidayOpeningTimes(Convert.ToInt32(Request.QueryString["LocationID"]), holidayStartDate, openTimeChk, closeTimeChk, closedChk.Checked); } //No times entered so closed selected else { OpeningTime.InsertHolidayOpeningTimes(Convert.ToInt32(Request.QueryString["LocationID"]), holidayStartDate, openTimeChk, closeTimeChk, true); } } else { //Session["InputFailed"] = "Time or date entered was not in the correct format. Please try again."; Session["HolidayStatus"] = ""; } //Reload dates to include new holiday that has just been added RefreshPage("LocationID=" + Request.QueryString["LocationID"]); } catch (Exception ex) { Session["GeneralError"] = "An error has occured saying: " + ex.Message + " Please contact your system administrator."; } }