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

121 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using EnVisage.Code;
namespace EnVisage.Models
{
public class VacationModel : IBaseModel<Vacation>, IValidatableObject
{
public Guid Id { get; set; }
public Guid PeopleResourceId { get; set; }
public Guid? ParentViewId { 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<PeopleResourceVacationModel> PeopleResourceVacations { get; set; }
/// <summary>
/// Gets or sets a collection of weekend days during vacation.
/// </summary>
/// <remarks>
/// Usually it is all Saturdays and Sundays, but can be overriden.
/// </remarks>
[Display(Name = "Weekends")]
[UIHint("Weekends")]
public List<DateTime> Weekends { get; set; }
/// <summary>
/// Gets or sets a day of the week when the week ends, according to system settings. Integer representation of <see cref="DayOfWeek"/> enum, 0 - Sunday.
/// </summary>
public short WeekendingDay { get; set; }
public VacationModel()
{
PeopleResourceVacations = new List<PeopleResourceVacationModel>();
Weekends = new List<DateTime>();
}
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<ValidationResult> 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<PeopleResourceVacation>
{
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;
}
}
}