EnVisageOnline/Main-RMO/Source/EnVisage/Models/HolidayExModel.cs

375 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web;
using EnVisage.Code;
using EnVisage.Code.BLL;
namespace EnVisage.Models
{
public class HolidayModel : IBaseModel<Holiday>
{
#region Enums
/// <summary>
/// Status of the Calendar Settings record
/// </summary>
public enum HolidayStatus
{
/// <summary>
/// NoValue = 0.
/// </summary>
[DisplayValue("No Value")]
NoValue = 0,
/// <summary>
/// Inactive = 0.
/// </summary>
[DisplayValue("Inactive")]
Inactive = 1,
/// <summary>
/// Active = 1.
/// </summary>
[DisplayValue("Active")]
Active = 2,
/// <summary>
/// Inactive = 2.
/// </summary>
[DisplayValue("Future")]
Future = 3,
}
public enum HolidayOccurrence
{
[DisplayValue("Each")]
SameDayEveryYear = 0,
[DisplayValue("First")]
FirstDayOfWeek = 1,
[DisplayValue("Second")]
SecondDayOfWeek = 2,
[DisplayValue("Third")]
ThirdDayOfWeek = 3,
[DisplayValue("Fourth")]
FourthDayOfWeek = 4,
[DisplayValue("Last")]
LastDayOfWeek = 5,
[DisplayValue("Varies every year")]
VariesEveryYear = 6,
[DisplayValue("Only once")]
OnlyOnce = 7,
}
#endregion
#region Constructors
public HolidayModel()
{
Id = Guid.Empty;
HolidayId = Guid.Empty;
Status = HolidayStatus.NoValue;
CreatedAt = DateTime.UtcNow;
}
#endregion
#region Properties
public Guid Id { get; set; }
public Guid HolidayId { get; set; }
/// <summary>
/// Gets or sets a name of the holiday.
/// </summary>
[Required]
[DataType(DataType.Text)]
[StringLength(50)]
[Display(Name = "Name")]
public string Name { get; set; }
/// <summary>
/// Gets or sets an occurrence of the holiday each year.
/// </summary>
[Required]
[Display(Name = "Occurrence")]
public HolidayOccurrence? OccurrenceType { get; set; }
/// <summary>
/// Gets or sets a week day of holiday.
/// </summary>
[Display(Name = "Occurrence day of week")]
public DayOfWeek? OccurrenceWeekDay { get; set; }
/// <summary>
/// Gets or sets a day of holiday.
/// </summary>
[Display(Name = "Occurrence day of month")]
[Range(1, 31)]
public short? OccurrenceMonthDay { get; set; }
/// <summary>
/// Gets or sets a month of holiday.
/// </summary>
[Display(Name = "of")]
[Range(1, 12)]
public short? OccurrenceMonth { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a holiday is a working day or not.
/// </summary>
[Display(Name = "Working Day")]
public bool WorkingDay { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a holiday results in non-working week or not.
/// </summary>
[Display(Name = "Non-Work Week")]
public bool NonWorkingWeek { get; set; }
[Required]
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)]
[Display(Name = "Effective Date of Change")]
public DateTime? EffectiveChangeDate { get; set; }
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)]
[Display(Name = "Valid To")]
public DateTime? ValidTo { get; set; }
public DateTime CreatedAt { get; set; }
[Display(Name = "Status")]
public HolidayStatus Status { get; set; }
public bool ShowWeekDayDdl
{
get
{
return OccurrenceType.HasValue && OccurrenceType.Value == HolidayOccurrence.FirstDayOfWeek &&
OccurrenceType.Value == HolidayOccurrence.SecondDayOfWeek &&
OccurrenceType.Value == HolidayOccurrence.ThirdDayOfWeek &&
OccurrenceType.Value == HolidayOccurrence.FourthDayOfWeek &&
OccurrenceType.Value == HolidayOccurrence.LastDayOfWeek &&
OccurrenceType.Value == HolidayOccurrence.VariesEveryYear &&
OccurrenceType.Value == HolidayOccurrence.OnlyOnce;
}
}
#endregion
#region Methods
/// <summary>
/// Determines whether the specified object is valid.
/// </summary>
/// <param name="validationContext">The validation context.</param>
/// <returns>A collection that holds failed-validation information.</returns>
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (OccurrenceType != null)
switch (OccurrenceType)
{
case HolidayOccurrence.SameDayEveryYear:
case HolidayOccurrence.OnlyOnce:
case HolidayOccurrence.VariesEveryYear:
switch (OccurrenceMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (OccurrenceMonthDay > 31)
yield return new ValidationResult(string.Format(Constants.ERROR_RANGE_MAX_VALUE_TEMPLATE, "Occurrence day of month", 31), new[] { "OccurrenceMonthDay" });
break;
case 2:
if (OccurrenceMonthDay > 28)
yield return new ValidationResult(string.Format(Constants.ERROR_RANGE_MAX_VALUE_TEMPLATE, "Occurrence day of month", 28), new[] { "OccurrenceMonthDay" });
break;
default:
if (OccurrenceMonthDay > 30)
yield return new ValidationResult(string.Format(Constants.ERROR_RANGE_MAX_VALUE_TEMPLATE, "Occurrence day of month", 30), new[] { "OccurrenceMonthDay" });
break;
}
yield break;
default:
break;
}
}
/// <summary>
/// Casts a <see cref="Holiday"/> obect to the object of type <see cref="HolidayModel"/>.
/// </summary>
/// <param name="obj">A <see cref="Holiday"/> object.</param>
/// <returns>A <see cref="HolidayModel"/> object filled with data from db.</returns>
public static explicit operator HolidayModel(Holiday obj)
{
if (obj == null)
return null;
var model = new HolidayModel
{
Id = obj.Id,
HolidayId = obj.HolidayId,
Name = obj.Name,
NonWorkingWeek = obj.NonWorkingWeek,
OccurrenceType = (HolidayOccurrence)obj.OccurrenceType,
OccurrenceMonth = obj.OccurrenceMonth,
OccurrenceMonthDay = obj.OccurrenceMonthDay,
OccurrenceWeekDay = (DayOfWeek)obj.OccurrenceWeekDay,
WorkingDay = obj.WorkingDay,
EffectiveChangeDate = obj.EffectiveChangeDate,
CreatedAt = obj.CreatedAt
};
model.TrimStringProperties();
return model;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="holiday">A target DAL object.</param>
public void CopyTo(Holiday holiday)
{
if (holiday == null)
throw new ArgumentNullException("holiday");
if (!EffectiveChangeDate.HasValue)
throw new Exception("Effective Date of Change must be set");
holiday.Id = Id;
holiday.HolidayId = HolidayId;
holiday.Name = Name;
holiday.OccurrenceMonth = OccurrenceMonth ?? 1;
holiday.OccurrenceMonthDay = OccurrenceMonthDay ?? 1;
holiday.OccurrenceType = (short)(OccurrenceType ?? HolidayOccurrence.SameDayEveryYear);
holiday.OccurrenceWeekDay = (short)(OccurrenceWeekDay ?? DayOfWeek.Monday);
holiday.WorkingDay = WorkingDay;
holiday.NonWorkingWeek = NonWorkingWeek;
holiday.EffectiveChangeDate = EffectiveChangeDate.Value;
holiday.CreatedAt = CreatedAt;
}
public static DateTime GetHolidayDate(HolidayOccurrence occurrenceType, short month, short day, short weekDay)
{
DateTime date = DateTime.Today;
switch (occurrenceType)
{
case HolidayOccurrence.OnlyOnce:
case HolidayOccurrence.VariesEveryYear:
case HolidayOccurrence.SameDayEveryYear:
return new DateTime(DateTime.Today.Year, month, day);
case HolidayOccurrence.FirstDayOfWeek:
date = Utils.GetNextDateByWeekDate (new DateTime(DateTime.Today.Year, month, 1), weekDay, 1);
break;
case HolidayOccurrence.SecondDayOfWeek:
date = Utils.GetNextDateByWeekDate(new DateTime(DateTime.Today.Year, month, 1), weekDay, 2);
break;
case HolidayOccurrence.ThirdDayOfWeek:
date = Utils.GetNextDateByWeekDate(new DateTime(DateTime.Today.Year, month, 1), weekDay, 3);
break;
case HolidayOccurrence.FourthDayOfWeek:
date = Utils.GetNextDateByWeekDate(new DateTime(DateTime.Today.Year, month, 1), weekDay, 4);
break;
case HolidayOccurrence.LastDayOfWeek:
date = Utils.GetLastDateByWeekDate(new DateTime(DateTime.Today.Year, month, 1), weekDay);
break;
}
return date;
}
public static string GetOccurenceText(HolidayModel item)
{
if (item == null)
return String.Empty;
var sb = new StringBuilder();
sb.Append((item.OccurrenceType.Value).ToDisplayValue() + " ");
switch (item.OccurrenceType.Value)
{
case HolidayModel.HolidayOccurrence.OnlyOnce:
case HolidayModel.HolidayOccurrence.SameDayEveryYear:
case HolidayModel.HolidayOccurrence.VariesEveryYear:
switch (item.OccurrenceMonthDay % 10)
{
case 2:
sb.Append("2nd ");
break;
case 3:
sb.Append("3rd ");
break;
default:
sb.Append(item.OccurrenceMonthDay.Value + "th ");
break;
}
sb.Append("of " +
new DateTime(DateTime.Today.Year, item.OccurrenceMonth.Value, 1).ToString("MMMM") + " every year");
break;
default:
sb.Append(" " + (item.OccurrenceWeekDay.Value).ToString() + " of " +
new DateTime(DateTime.Today.Year, item.OccurrenceMonth.Value, 1).ToString("MMMM") + " every year");
break;
}
string result = sb.ToString();
return result;
}
#endregion
}
public class HolidayDisplayModel
{
#region Properties
public Guid Id { get; set; }
public Guid HolidayId { get; set; }
/// <summary>
/// Gets or sets a name of the holiday.
/// </summary>
[DataType(DataType.Text)]
[Display(Name = "Name")]
public string Name { get; set; }
public DateTime Date { get; set; }
[Display(Name = "Date")]
public string DateText { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a holiday is a working day or not.
/// </summary>
[Display(Name = "Working Day")]
public bool WorkingDay { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a holiday results in non-working week or not.
/// </summary>
[Display(Name = "Non-Work Week")]
public bool NonWorkingWeek { get; set; }
public DateTime EffectiveChangeDate { get; set; }
[Display(Name = "Effective Date of Change")]
public string EffectiveChangeDateAsText { get; set; }
public DateTime? ValidTo { get; set; }
[Display(Name = "Valid To")]
public string ValidToAsText { get; set; }
#endregion
}
public class HolidayListModel
{
public HolidayListModel()
{
Items = new List<HolidayModel>();
}
public List<HolidayModel> Items { get; set; }
}
}