using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using EnVisage.Code; namespace EnVisage.Models { public class PeopleResourceModel : IBaseModel { public class CapacityValues { public DateTime WeekEnding { get; set; } public decimal Quantity { get; set; } public decimal Cost { get; set; } public Guid ExpCatId { get; set; } } private Dictionary weeklyCapacity = null; #region Properties public Guid Id { get; set; } [Required] [StringLength(250, MinimumLength = 1)] [Display(Name = "First Name")] public string FirstName { get; set; } [Required] [StringLength(250, MinimumLength = 1)] [Display(Name = "Last Name")] public string LastName { get; set; } [Display(Name = "Employee Status")] public bool IsActiveEmployee { get; set; } [Display(Name = "On Deactivation")] public bool ReassignOnDeactivation { get; set; } [Required] [Display(Name = "Start Date")] [DataType(System.ComponentModel.DataAnnotations.DataType.Date)] public DateTime StartDate { get; set; } [Required] [Display(Name = "End Date")] [DataType(System.ComponentModel.DataAnnotations.DataType.Date)] public DateTime EndDate { get; set; } [Display(Name = "Team")] public Guid? TeamId { get; set; } public Team Team { get; set; } [Display(Name = "Expenditure Category")] public Guid ExpenditureCategoryId { get; set; } [Display(Name = "Resource Capacity")] public Dictionary WeeklyCapacities { get { if (weeklyCapacity == null) weeklyCapacity = GetResourceWeeklyCapacity(null); return weeklyCapacity; } } public bool ChangeCapacity { get; set; } public bool PermanentResource { get; set; } public ExpenditureCategory ExpenditureCategory { get; set; } public List Vacations { get; set; } public List Trainings { get; set; } public List SubstituteResources { get; set; } [Display(Name = "Create another")] public bool AddMore { get; set; } //[Display(Name = "Resources")] //[UIHint("Resources")] //public List Resources { get; set; } //[Display(Name = "Training Name")] //public string TrainingName { get; set; } //[Required] //[Display(Name = "Start Date")] //[DataType(System.ComponentModel.DataAnnotations.DataType.Date)] //public DateTime TrainingStartDate { get; set; } //[Required] //[Display(Name = "End Date")] //[DataType(System.ComponentModel.DataAnnotations.DataType.Date)] //public DateTime TrainingEndDate { get; set; } //[Display(Name = "Duration")] //public int TrainingDuration { get; set; } //[Display(Name = "Training Type")] //public Guid TrainingTypeID { get; set; } //[Display(Name = "Cost")] //public decimal TrainingCost { get; set; } //[Display(Name = "% Time Allocated")] //public decimal TrainingTimeAllocated { get; set; } public TrainingModel Training { get; set; } #endregion #region Constructor public PeopleResourceModel() { IsActiveEmployee = true; StartDate = DateTime.Today; EndDate = DateTime.Today.AddYears(1); Training = new TrainingModel(){ TrainingStartDate = DateTime.Today, TrainingEndDate = DateTime.Today.AddYears(1), Resources = new List() }; } #endregion #region Methods /// /// Casts a obect to the object of type . /// /// A object. /// A object filled with data from db. public static explicit operator PeopleResourceModel(PeopleResource obj) { if (obj == null) return null; var model = new PeopleResourceModel() { Id = obj.Id, ExpenditureCategoryId = obj.ExpenditureCategoryId, ExpenditureCategory = obj.ExpenditureCategory, TeamId = obj.TeamId, Team = obj.Team, FirstName = obj.FirstName, LastName = obj.LastName, IsActiveEmployee = obj.IsActiveEmployee, StartDate = obj.StartDate, EndDate = obj.EndDate }; model.TrimStringProperties(); return model; } /// /// Copies data from model to DAL object. /// /// A target DAL object. public void CopyTo(PeopleResource dbObj) { if (dbObj == null) throw new ArgumentNullException(); dbObj.FirstName = FirstName; dbObj.LastName = LastName; dbObj.IsActiveEmployee = IsActiveEmployee; dbObj.TeamId = TeamId; dbObj.ExpenditureCategoryId = ExpenditureCategoryId; dbObj.StartDate = StartDate; dbObj.EndDate = EndDate; } /// /// Returns the dictionary containing resource capacity distribution over weeks /// /// Global rates. Can be null or empty - in this case method will retrieve the data itself. /// Dictionary format: key is expenditure category id; dictionary item is list of rates (can be unordered) /// public Dictionary GetResourceWeeklyCapacity(Dictionary> globalRates) { Dictionary dict = new Dictionary(); decimal? weeklyCapVal = null; var context = new EnVisageEntities(); if (ExpenditureCategory == null) ExpenditureCategory = context.ExpenditureCategory.FirstOrDefault(x => x.Id == ExpenditureCategoryId); // get week capacity for resource's Expenditure Category if (ExpenditureCategory != null && ExpenditureCategory.UOM != null) weeklyCapVal = ExpenditureCategory.UOM.UOMValue; // get weekending dates between resource Start and End dates var weekendings = context.FiscalCalendars .Where(x => x.Type == (int)EnVisage.Models.FiscalCalendarModel.FiscalYearType.Week && x.StartDate >= StartDate && x.EndDate <= EndDate && x.NonWorking ==0 && x.AdjustingPeriod == false).Select(x => x.EndDate); //Get global rates if they are not supplied if (globalRates == null || globalRates.Count == 0) { globalRates = new Dictionary>(); globalRates.Add(ExpenditureCategory.Id, context.Rates.Where(r => r.ExpenditureCategoryId == ExpenditureCategory.Id && r.Type == (short)EnVisage.Models.RateModel.RateType.Global).OrderBy(r => r.StartDate).ThenBy(r => r.EndDate).ToList()); } List rates = new List(); if (globalRates.ContainsKey(ExpenditureCategory.Id)) rates = globalRates[ExpenditureCategory.Id]; //we need to sort rates list by startdate asc and enddate asc to make sure we can go with single loop List orderedRates = rates.OrderBy(r => r.StartDate).ThenBy(r => r.EndDate).ToList(); foreach (var week in weekendings) { if (!dict.ContainsKey(week)) { decimal rate = 0.0M; for (int i = 0; i < orderedRates.Count; i++) { Rate r = orderedRates[i]; if (week >= r.StartDate && week <= r.EndDate) { rate = r.Rate1; break; } } decimal cost = weeklyCapVal.HasValue ? weeklyCapVal.Value * rate : 0; dict.Add(week, new CapacityValues() { Quantity = weeklyCapVal.HasValue ? weeklyCapVal.Value : 0, Cost = cost, WeekEnding = week, ExpCatId = ExpenditureCategoryId }); } } return dict; } #endregion } public class ResourceLeavingInfoModel { public Guid PeopleResourceId { get; set; } public int HoursOff { get; set; } public DateTime WeekEndingDate { get; set; } } }