using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using EnVisage.Code; namespace EnVisage.Models { public class VacationModel : IBaseModel, IValidatableObject { public Guid Id { get; set; } public Guid PeopleResourceId { get; set; } [Required] [Display(Name = "Start Date")] [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)] public DateTime? StartDate { get; set; } [Required] [Display(Name = "End Date")] [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)] public DateTime? EndDate { get; set; } [Required] [Display(Name = "Hours Off")] [Range(0, 100)] [UIHint("Slider")] public short HoursOff { get; set; } public decimal UOMValue { get; set; } public int? Duration { get { return StartDate.HasValue && EndDate.HasValue ? (EndDate.Value.Subtract(StartDate.Value).Days + 1) : (int?)null; } } public List PeopleResourceVacations { get; set; } /// /// Gets or sets a collection of weekend days during vacation. /// /// /// Usually it is all Saturdays and Sundays, but can be overriden. /// [Display(Name = "Weekends")] [UIHint("Weekends")] public List Weekends { get; set; } /// /// Gets or sets a day of the week when the week ends, according to system settings. Integer representation of enum, 0 - Sunday. /// public short WeekendingDay { get; set; } public VacationModel() { PeopleResourceVacations = new List(); Weekends = new List(); } public void CopyTo(Vacation obj) { if (obj == null) throw new ArgumentNullException(); obj.StartDate = StartDate.Value; obj.EndDate = EndDate.Value; obj.PeopleResourceId = PeopleResourceId; obj.HoursOff = HoursOff / 100.0M; obj.PeopleResourceVacations = PeopleResourceVacations.Select(t => { var o = new PeopleResourceVacation(); t.CopyTo(o); return o; }).ToList(); obj.Weekends = string.Join(",", Weekends.Select(t=>t.ToShortDateString())); } public static explicit operator VacationModel(Vacation obj) { if (obj == null) return null; var model = new VacationModel { Id = obj.Id, PeopleResourceId = obj.PeopleResourceId, StartDate = obj.StartDate, EndDate = obj.EndDate, HoursOff = Convert.ToInt16(obj.HoursOff * 100), Weekends = obj.Weekends.Split(new []{','}, StringSplitOptions.RemoveEmptyEntries).Select(c => { DateTime dt; return DateTime.TryParse(c, out dt) ? dt : DateTime.MinValue; }).Where(t=>t != DateTime.MinValue).ToList() }; var str = string.Empty; foreach (var dateTime in model.Weekends) { if (!string.IsNullOrEmpty(str)) str += ","; str += "'" + dateTime.ToShortDateString() + "'"; } model.TrimStringProperties(); return model; } public IEnumerable Validate(ValidationContext validationContext) { if (StartDate.Value > EndDate.Value) yield return new ValidationResult("End Date should be greater than Start Date", new[] { "StartDate", "EndDate" }); } } public class PeopleResourceVacationModel : IBaseModel { public Guid Id { get; set; } public Guid PeopleResourceId { get; set; } public DateTime WeekEndingDate { get; set; } public int HoursOff { get; set; } public DateTime LastUpdate { get; set; } public void CopyTo(PeopleResourceVacation obj) { if (obj == null) throw new ArgumentNullException(); obj.HoursOff = HoursOff; obj.LastUpdate = LastUpdate; obj.PeopleResourceId = PeopleResourceId; obj.WeekEndingDate = WeekEndingDate; } } }